Re: [Tutor] Help with a Conversion
On 01/05/2017 01:10 PM, Peter Otten wrote: S. P. Molnar wrote: I have just started attempting programming in Python and am using Spyder with Python 3.5.2 on a Linux platform. (I first started programing in Fortran II using punched paper tape. Yes, am a rather elderly . . .). I have bumbled through, what I foolishly thought was a simple problem, a short program to change frequency to wavelength for a plot of ultraviolet spectra. I have attached a pdf of the program. During my attempt at programming I have printed results at various stages. Printing wavelength = [row[0] for row in data] gives me 25000 as the first frequency in the wavelength list (the corresponding wavelength is 400). 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. What I want to do is divide 1e7 by each element of the freqquency list. How di I do this? Since you are using numpy anyway I'd put the frequencies into a numpy.array as soon as possible: import numpy frequencies = numpy.array([25000, 1250, 400]) Because of numpy's "broadcasting" you can mix skalars and vectors as you already tried -- and with the right formula, lamda = c / nu, you get the correct result: speed_of_light = 1e7 wavelengths = speed_of_light / frequencies wavelengths array([ 400., 8000., 25000.]) The equivalent list comprehension in plain Python looks like this: frequencies = [25000, 1250, 400] wavelengths = [speed_of_light/freq for freq in frequencies] wavelengths [400.0, 8000.0, 25000.0] Please keep in mind that many, many hyears ago I learned the ole arithmetic That hasn't changed and is honoured by numpy; you were probably confused by the new tool ;) and an not trying to start a flame war. Thanks in advance for the assistance tha I am sure will be most helpful. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor Somehow I KNEW THIS WAS THE LIWST TO Ask. Thanks very much. -- Stephen P. Molnar, Ph.D.Life is a fuzzy set www.Molecular-Modeling.net Stochastic and multivariate (614)312-7528 (c) Skype: smolnar1 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help with a Conversion
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.0333]) (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 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help with a Conversion
S. P. Molnar wrote: > I have just started attempting programming in Python and am using Spyder > with Python 3.5.2 on a Linux platform. (I first started programing in > Fortran II using punched paper tape. Yes, am a rather elderly . . .). > > I have bumbled through, what I foolishly thought was a simple problem, a > short program to change frequency to wavelength for a plot of > ultraviolet spectra. I have attached a pdf of the program. > > During my attempt at programming I have printed results at various > stages. Printing wavelength = [row[0] for row in data] gives me 25000 > as the first frequency in the wavelength list (the corresponding > wavelength is 400). > > 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. > > What I want to do is divide 1e7 by each element of the freqquency list. > > How di I do this? Since you are using numpy anyway I'd put the frequencies into a numpy.array as soon as possible: >>> import numpy >>> frequencies = numpy.array([25000, 1250, 400]) Because of numpy's "broadcasting" you can mix skalars and vectors as you already tried -- and with the right formula, lamda = c / nu, you get the correct result: >>> speed_of_light = 1e7 >>> wavelengths = speed_of_light / frequencies >>> wavelengths array([ 400., 8000., 25000.]) The equivalent list comprehension in plain Python looks like this: >>> frequencies = [25000, 1250, 400] >>> wavelengths = [speed_of_light/freq for freq in frequencies] >>> wavelengths [400.0, 8000.0, 25000.0] > Please keep in mind that many, many hyears ago I learned the ole > arithmetic That hasn't changed and is honoured by numpy; you were probably confused by the new tool ;) > and an not trying to start a flame war. > Thanks in advance for the assistance tha I am sure will be most helpful. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help with a Conversion
On 01/05/2017 11:41 AM, Alan Gauld via Tutor wrote: On 05/01/17 13:29, S. P. Molnar wrote: Fortran II using punched paper tape. Yes, am a rather elderly . . .). You are not the only one, there are at least 2 more of us on this list that started in that era... short program to change frequency to wavelength for a plot of ultraviolet spectra. I have attached a pdf of the program. This is a text list so attachments usually get stripped off. Please post the code in the body of the email using plain text formatting. To change the frequency to wave length I did the following: p=1/1e7 p = 1e-7 wave_length = p*np.array(frequency) I don't really use numpy so don't know what that line does. But assuming it applies the multiplication to each array element I'd probably use: wave_lengths = [p*f for f in frequencies] or possibly wave_lengths = map(lambda f: p*f, frequencies) where frequencies was a tuple/list of frequency values. However... (The relationship between wavelength and frequency is: wavelength = 1.0e7/frequency, where 1e7 is the speed of light) That formula doesn't look like the one you use above if my guess is correct. That would look like: wave_length = [1e7/f for f in frequencies] ie positive exponent and division instead of multiplication Apparently what I have managed to do is divide each element of the frequency list by 1/1e7. What I want to do is divide 1e7 by each element of the freqquency list. How di I do this? Rearrange the equation and use division instead of multiplication I think that by calculating 1/p you have made things much more complicated - unless there is some subtle arithmetic magic going on that I'm missing? Many thanks for the reply and your suggestions. As it happens, I stumbled on the solution through trial and error. The correct line is: wave_length = 1e7/np.array(frequency). All is now well. -- Stephen P. Molnar, Ph.D.Life is a fuzzy set www.Molecular-Modeling.net Stochastic and multivariate (614)312-7528 (c) Skype: smolnar1 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help with a Conversion
On 05/01/17 13:29, S. P. Molnar wrote: > Fortran II using punched paper tape. Yes, am a rather elderly . . .). You are not the only one, there are at least 2 more of us on this list that started in that era... > short program to change frequency to wavelength for a plot of > ultraviolet spectra. I have attached a pdf of the program. This is a text list so attachments usually get stripped off. Please post the code in the body of the email using plain text formatting. > To change the frequency to wave length I did the following: > > p=1/1e7 p = 1e-7 > wave_length = p*np.array(frequency) I don't really use numpy so don't know what that line does. But assuming it applies the multiplication to each array element I'd probably use: wave_lengths = [p*f for f in frequencies] or possibly wave_lengths = map(lambda f: p*f, frequencies) where frequencies was a tuple/list of frequency values. However... > (The relationship between wavelength and frequency is: wavelength = > 1.0e7/frequency, where 1e7 is the speed of light) That formula doesn't look like the one you use above if my guess is correct. That would look like: wave_length = [1e7/f for f in frequencies] ie positive exponent and division instead of multiplication > Apparently what I have managed to do is divide each element of the frequency > list by 1/1e7. > > What I want to do is divide 1e7 by each element of the freqquency list. > How di I do this? Rearrange the equation and use division instead of multiplication I think that by calculating 1/p you have made things much more complicated - unless there is some subtle arithmetic magic going on that I'm missing? -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor