[Numpy-discussion] function name as parameter
I'm trying to write an implementation of the amoeba function from numerical recipes and need to be able to pass a function name and parameter list to be called from within the amoeba function. Simply passing the name as a string doesn't work since python doesn't know it is a function and throws a typeerror. Is there something similar to IDL's 'call_function' routine in python/numpy or a pythonic/numpy means of passing function names? -- Thomas K. Gamble Research Technologist, System/Network Administrator Chemical Diagnostics and Engineering (C-CDE) Los Alamos National Laboratory MS-E543,p:505-665-4323 f:505-665-4267 There cannot be a crisis next week. My schedule is already full. Henry Kissinger ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] function name as parameter
On Wed, 2010-10-20 at 09:46 -0400, Zachary Pincus wrote: I'm trying to write an implementation of the amoeba function from numerical recipes and need to be able to pass a function name and parameter list to be called from within the amoeba function. Simply passing the name as a string doesn't work since python doesn't know it is a function and throws a typeerror. Is there something similar to IDL's 'call_function' routine in python/numpy or a pythonic/numpy means of passing function names? Just pass the function itself! For example: def foo(): print 6 def call_function_repeatedly(func, count): for i in range(count): func() call_function_repeatedly(foo, 2) # calls foo twice bar = foo bar() # still calls foo... we've just assigned the function to a different name This works fine. Too obvious to see, I guess. In python, functions (and classes, and everything else) are first- class objects and can be assigned to variables, passed around, etc, etc, just as anything else. However, note that scipy.optimize.fmin implements the Nelder-Mead simplex algorithm, which is (I think) the same as the amoeba optimizer. Also you might be interested in the openopt package, which implements more optimizers a bit more consistently than scipy.optimize. I try this and compare performance. I had looked for an 'amoeba' function, but couldn't find anything by that name. I should have broadened my search a bit. ;-) Zach ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion -- Thomas K. Gamble Research Technologist, System/Network Administrator Chemical Diagnostics and Engineering (C-CDE) Los Alamos National Laboratory MS-E543,p:505-665-4323 f:505-665-4267 There cannot be a crisis next week. My schedule is already full. Henry Kissinger ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] function name as parameter
On Wed, 2010-10-20 at 13:18 -0400, Alan G Isaac wrote: On 10/20/2010 9:42 AM, Thomas Kirk Gamble wrote: I'm trying to write an implementation of the amoeba function from numerical recipes and need to be able to pass a function name and parameter list to be called from within the amoeba function. 1. Have you checked whether this might already be in OpenOpt? 2. Here is a GAUSS version, that might (?) be easier to follow. http://www1.american.edu/academic.depts/cas/econ/gaussres/optimize/honore.src Yes, Zachary already pointed out a version in the scipy.optimize module. Thanks anyway for the reference. I tend to prefer evaluating multiple options rather than just taking the first one that seems to work. Alan Isaac ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion -- Thomas K. Gamble Research Technologist, System/Network Administrator Chemical Diagnostics and Engineering (C-CDE) Los Alamos National Laboratory MS-E543,p:505-665-4323 f:505-665-4267 There cannot be a crisis next week. My schedule is already full. Henry Kissinger ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
[Numpy-discussion] convert FORTRAN exponential format text to float
I need to convert numbers read from a text file to floating point. The numbers are in the format 1.538D-06 (written by a FORTRAN application) and have varying amounts of whitespace between them from line to line. The function fromstring() deals with the whitespace just fine but 'dtype=float' doesn't correctly convert the data. It sees every thing up to the 'D' and ignores the rest(I assume expecting an 'e' to indicate the exponential). I was able to get around this using re.sub() to change the 'D' to 'e' in the string before using fromstring(), but I was wondering if python has any way to directly read this data as float? Google didn't find an answer. ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] convert FORTRAN exponential format text to float
On Wed, 2010-09-29 at 16:36 +0200, David Froger wrote: Did you try loadtxt? I try to output something in the format 1.538D-06 with Fortran in order to test reading it with loadtxt, but I always get 1.538E-06. Where does the 'D' come from? No, I didn't, but I will. Preserving the 'D' isn't important, only proper conversion of the string to float. The data was created by an atmosphere modeling program called AMOEBA that was written in FORTRAN. ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion