npplus.pcwise module¶
A decorator to neatly write piecewise functions.
The decorator is an aid to writing functions of one variable x which have different algorithms in different domains of x:
@pcwise
def fun(x):
def funlo(x):
return 1. - x
def funmid(x):
return numpy.sin(x)
def funhi(x):
return x**2 - 1.
return funlo, xa, funmid, xb, funhi
Defines fun(x)
to be funlo(x)
for x<xa
, funmid(x)
for
xa<=x<xb
, and funhi(x)
for xb<=x
. Any number of domains
is allowed.
- pcwise(f)[source]¶
Decorator to simplify creating
f(x)
with algorithm dependent on x.Use the pcwise decorator like this:
@pcwise def f(x): '''Return a function of a single real variable x.''' def f0(x): ...algorithm when x < x1 def f1(x): ...algorithm when x1 <= x < x2 def f2(x): ...algorithm when x2 <= x < x3 <and so on> return (f0, x1, f1, x2, f2, ...)
Any of the fN in the return value may be a string to raise a ValueError with that string for any points in that range.