[Tutor] Help with a Conversion
Steven D'Aprano
steve at pearwood.info
Thu Jan 5 18:18:29 EST 2017
On Thu, Jan 05, 2017 at 08:29:33AM -0500, S. P. Molnar wrote:
[...]
> To change the frequency to wave length I did the following:
>
>
> p=1/1e7
> wave_length = p*np.array(frequency)
>
> (The relationship between wavelength and frequency is: wavelength =
> 1.0e7/frequency, where 1e7 is the speed of light)
>
>
> Apparently whhat I have managed to do is divide each element of the
> frequency list by 1/1e7.
Indeed :-)
This is a matter of arithmetic:
Let p = 1/x
then p*f = (1/x)*f = f/x
So you have divided each frequency by x, namely 1e7.
What you want is:
x/f
which divides x (1e7) by the frequency.
The interactive interpreter is very good for exploring simple questions
like this. If you need help starting the interactive interpreter, please
ask, although I haven't used Spyder for many years and I'm not familiar
with it. But in the regular Python interpreter, I can do this:
py> import numpy as np
py> data = np.array([1, 2, 3])
py> data
array([1, 2, 3])
py> factor = 1/10.0
py> factor*data
array([ 0.1, 0.2, 0.3])
py> factor/data
array([ 0.1 , 0.05 , 0.03333333])
(lines starting with "py>" is the code I have typed).
So to get the result you want, you should be able to do this:
wave_length = 1e7/np.array(frequency)
--
Steve
More information about the Tutor
mailing list