arifshaikh.astro
2021-11-3 10:29:16

how to make a complex number from a tuple of two real variables?



arifshaikh.astro
2021-11-3 10:55:29

Ah, thanks


soegaard2
2021-11-3 10:57:39

Alternative: (+ a (* b +1i))


arifshaikh.astro
2021-11-3 11:07:59

Would the following be a good away to get the amplitudes (define (get-amp df) (let ([real (vector->list (df-select df "strain/real"))] [imag (vector->list (df-select df "strain/imag"))]) (map (compose magnitude make-rectangular) real imag)))


soegaard2
2021-11-3 11:08:59

Yes.


soegaard2
2021-11-3 11:10:22

An alternative: (for/list ([r (df-select df "strain/real")] [i (df-select df "strain/imag")]) (magnitude (make-rectangular r i))) This has the advantage of never converting the vectors to lists.


arifshaikh.astro
2021-11-3 11:11:14

this looks better. Thanks


alexharsanyi
2021-11-3 11:25:27

You can also use ‘in-data-frame’ instead of ‘df-select’ and select both the real and imaginary values in one go. This avoids allocating an intermediate vector by df-select which will be discarded once the loop completes.


arifshaikh.astro
2021-11-3 12:15:17

Thanks, that’s neat!


alexharsanyi
2021-11-3 12:54:26

Also, your original code was very inefficient and would be slow for large data sets. This is because:

• it uses df-select to allocate a vector in which to hold the result, copying the values from the series in it • it uses vector->list to allocate a list, and copy the contents of the vector in it, the original vector is than discarded • does this a second time for “strain/imag” series • process the list using map and allocate a list of magnitude values, the previous two lists are than discarded. Using for/list and in-data-frame allows allocating only the final list of elements which holds the result: (for/list ([(r i) (in-data-frame df "strain/real" "strain/imag")]) (magnitude (make-rectangular r i)))


arifshaikh.astro
2021-11-3 14:39:14

Thanks, this is very helpful. A related question, using the above I get the list of magnitudes which I want to plot vs "times" which is a series in the data-frame . What would be the most efficient way to do that?


arifshaikh.astro
2021-11-3 14:44:48

I am trying something like this (plot (lines (for/list ([[t r i] (in-data-frame df "times" "strain/real" "strain/imag")]) (vector t (magnitude (make-rectangular r i))))))


alexharsanyi
2021-11-3 22:20:18

That’s how I would do it. However, if you find that you have to reuse the magnitudes, or the complex numbers themselves you can add them as derived series to the data frame:

(df-add-derived! df "strain" '("strain/real" "strain/imag") (lambda (v) (match-define (list r i) v) (make-rectangular r i))) (df-add-derived! df "magnitude '("strain") (lambda (v) (match-define (list strain) v) (magnitude strain))) (plot (lines (df-select* df "times" "magnitude"))) … but this is only worth doing if you will reuse the “strain” and “magnitude” series for other things as well…


ben.knoble
2021-11-3 22:26:14

I’m not convinced I would know how to do that efficiently or correctly with pandas, so it’s cool to see that look so sensible in df