|
From: Gerard V. <ger...@gr...> - 2005-09-23 08:54:58
|
On Thu, 22 Sep 2005 17:44:59 +0200
Andrea Riciputi <ari...@pi...> wrote:
> Hi all,
> this is probably an already discussed problem, but I've not been able
> to find a solution even after googling a lot.
>
> I've a piecewise defined function:
>
> /
> | f1(x) if x <= a
> f(x) = |
> | f2(x) if x > a
> \
>
> where f1 and f2 are not defined outside the above range. How can I
> define such a function in Python in order to apply (map) it to an
> array ranging from values smaller to values bigger than a?
>
This does maybe what you want:
from scipy import *
def f(x):
"""Approximative implementation of:
-1/(x-pi) for x < pi
1/(x-pi) for x > pi
"""
result = zeros(len(x), x.typecode())
i = argmin(abs(x-pi))
# you may have to tweak i here, because it may be off by 1
result[:i+1] = -1/(x[:i+1]-pi)
result[i+1:] = 1/(x[i+1:]-pi)
return result
x = arange(0, 10, 1, Float)
print f(x)
print abs(1/(x-pi))
Gerard
|