[Numpy-discussion] element wise help

josef.pktd@gmai... josef.pktd@gmai...
Thu May 7 15:22:05 CDT 2009


On Thu, May 7, 2009 at 3:39 PM,  <josef.pktd@gmail.com> wrote:
> On Thu, May 7, 2009 at 3:10 PM, Chris Colbert <sccolbert@gmail.com> wrote:
>> the user of the program inputs the transform in a text field. So I have no
>> way of know the function apriori.
>>
>> that doesn't mean I still couldn't throw the exec and eval commands into
>> another function just to clean things up.
>>
>> Chris
>
> No, I think this is ok then, it is similar to what sympy.lambdify
> does. Now you call exec only twice, which might have been the main
> slowdown in the loop version.
>
> In this case, users need to write vectorized transform functions that
> handle the full n x t array.
>
> Josef

this would be an alternative, which might also work better in a loop,
requires numpy.* in local scope

>>> transform = 'sqrt(x)'
>>> exec('def fun(x): return ' + transform)
>>> from numpy import *
>>> fun(5)
2.2360679774997898
>>> fun(np.arange(5))
array([ 0.        ,  1.        ,  1.41421356,  1.73205081,  2.        ])
>>>
>>> fun
<function fun at 0x081BD0B0>

Josef


More information about the Numpy-discussion mailing list