how to make a complex number from a tuple of two real variables?
Ah, thanks
Alternative: (+ a (* b +1i))
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)))
Yes.
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.
this looks better. Thanks
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.
Thanks, that’s neat!
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)))
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?
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))))))
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…
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