vectorize

class odl.util.vectorization.vectorize(*args, **kwargs)[source]

Bases: OptionalArgDecorator

Decorator class for function vectorization.

This vectorizer expects a function with exactly one positional argument (input) and optional keyword arguments. The decorated function has an optional out parameter for in-place evaluation.

Examples

Use the decorator witout arguments:

>>> @vectorize
... def f(x):
...     return x[0] + x[1] if x[0] < x[1] else x[0] - x[1]
>>>
>>> f([0, 1])  # np.vectorize'd functions always return an array
array(1)
>>> f([[0, -2], [1, 4]])  # corresponds to points [0, 1], [-2, 4]
array([1, 2])

The function may have kwargs:

>>> @vectorize
... def f(x, param=1.0):
...     return x[0] + x[1] if x[0] < param else x[0] - x[1]
>>>
>>> f([[0, -2], [1, 4]])
array([1, 2])
>>> f([[0, -2], [1, 4]], param=-1.0)
array([-1,  2])

You can pass arguments to the vectorizer, too:

>>> @vectorize(otypes=['float32'])
... def f(x):
...     return x[0] + x[1] if x[0] < x[1] else x[0] - x[1]
>>> f([[0, -2], [1, 4]])
array([ 1.,  2.], dtype=float32)

Methods

__call__(func)

Return self(func).

__init__(*args, **kwargs)