Re: [Numpy-discussion] element-wise array segmental function operation?

2015-03-23 Thread Julian Taylor
On 23.03.2015 07:46, oyster wrote:
> Hi, all
> I want to know wether there is a terse way to apply a function to
> every array element, where the function behaves according to the
> element value.
> for example
> [code]
> def fun(v):
> if 0<=v<60:
> return f1(v)#where f1 is a function
> elif 60<=v<70:
> return f2(v)
> elif 70<=v<80:
> return f3(v)
> ...and so on...
> [/code]
> 
> for 'a=numpy.array([20,50,75])', I hope to get numpy.array([f1(20),
> f1(50), f3(75)])
> 

piecewise should be what you are looking for:
http://docs.scipy.org/doc/numpy/reference/generated/numpy.piecewise.html


___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] element-wise array segmental function operation?

2015-03-22 Thread Nick Papior Andersen
You can do:

vfun = np.vectorize(fun)
vfun([20,50,75])

that should work, note the abundant options available for denoting the
vectorized arrays in "vectorize".
Otherwise you could do nested where calls.


2015-03-23 7:46 GMT+01:00 oyster :

> Hi, all
> I want to know wether there is a terse way to apply a function to
> every array element, where the function behaves according to the
> element value.
> for example
> [code]
> def fun(v):
> if 0<=v<60:
> return f1(v)#where f1 is a function
> elif 60<=v<70:
> return f2(v)
> elif 70<=v<80:
> return f3(v)
> ...and so on...
> [/code]
>
> for 'a=numpy.array([20,50,75])', I hope to get numpy.array([f1(20),
> f1(50), f3(75)])
>
> thanks in advance
>
> Lee
> ___
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>



-- 
Kind regards Nick
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


[Numpy-discussion] element-wise array segmental function operation?

2015-03-22 Thread oyster
Hi, all
I want to know wether there is a terse way to apply a function to
every array element, where the function behaves according to the
element value.
for example
[code]
def fun(v):
if 0<=v<60:
return f1(v)#where f1 is a function
elif 60<=v<70:
return f2(v)
elif 70<=v<80:
return f3(v)
...and so on...
[/code]

for 'a=numpy.array([20,50,75])', I hope to get numpy.array([f1(20),
f1(50), f3(75)])

thanks in advance

Lee
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion