nutopy.tools¶
- tensorize(*dfuns, tvars=(), full=False)[source]¶
- Parameters
dfuns (callables) – Functions implementing order 1, 2 derivatives, etc.
) (tvars=() – Indices (>= 1) of vars wrt. which tensorize. All vars for empty tuple (default).
full=False (bool) – If True, each dfuns[i] must return values up to order i+1, not just at i+1
- Returns
Decorator to tensorize a function
Example
>>> def dg(x, dx, y, dy, p): ... dz = 2*x*dx+p*dy ... return dz
>>> def d2g(x, dx, d2x, y, dy, d2y, p): ... d2z = 2*d2x*dx ... return d2z
>>> @tensorize(dg, d2g, tvars=(1, 2)) ... def g(x, y, p): ... z = x**2+p*y ... return z
>>> g(1, 2, 3) 7 >>> g((1, 1), 2, 3) (7, 2.0) >>> g((1, 1), (2, 2, 2), 3) (7, 8, 0.0) >>> g((1, 1, 1), 2, 3) (7, 2.0, 2)
>>> def df(x, dx, y, dy, p): ... z = x**2+p*y ... dz = 2*x*dx+p*dy ... return z, dz
>>> def d2f(x, dx, d2x, y, dy, d2y, p): ... z = x**2+p*y ... dz = 2*x*dx+p*dy ... d2z = 2*d2x*dx ... return z, dz, d2z
>>> @tensorize(df, d2f, tvars=(1, 2), full=True) ... def f(x, y, p): ... z = x**2+p*y ... return z
- vectorize(vvars=(), next=False)[source]¶
- Parameters
) (vvars=() – Indices (>= 1) of vars wrt. which vectorize. All vars for empty tuple (default).
next=False (bool) – If True, the function to vectorize must have a keyword argument next=False; when called with next=True, this function must return an additional result that will serve as the argument for the next call
- Returns
Decorator to vectorize a function.
Note
The vectorized function has one additional keyword arguments: dispatch=True. If True, when the original function returns a tuple, its vectorization returns a tuple of lists. (Set dispatch=False to have a list of tuples instead.) None values are used to fill the holes (in any).
Example
>>> @vectorize(vvars=(1,)) ... @vectorize(vvars=(2, 3)) ... def f(x, y, z): ... return x+y, y+z
>>> f(1, 2, 3) (3, 5) >>> f(1, [ 2, 2 ], [ 3, 3 ]) ([3, 3], [5, 5]) >>> f([ 1, 1, 1 ], 2, 3) ([3, 3, 3], [5, 5, 5]) >>> f([ 1, 1, 1 ], 2, 3, dispatch=False) [(3, 5), (3, 5), (3, 5)] >>> f([ 1, 1, 1 ], [ 2, 2], [ 3, 3 ]) ([[3, 3], [3, 3], [3, 3]], [[5, 5], [5, 5], [5, 5]])
- exception WrongOrder(d)[source]¶
Bases:
nutopy.tools.Error
- Parameters
d (int) – Order causing error
- Returns
An exception raised by order or tensorize
- exception BadIndices(x)[source]¶
Bases:
nutopy.tools.Error
- Parameters
x (object) – Object causing error
- Returns
An exception raised by tensorize or vectorize
- exception NotVectorizable(x)[source]¶
Bases:
nutopy.tools.Error
- Parameters
x (object) – Object causing error
- Returns
An exception raised by vectorize
- exception WrongLength(x)[source]¶
Bases:
nutopy.tools.Error
- Parameters
x (object) – Object causing error
- Returns
An exception raised by vectorize