DiscretizedSpace.element¶
-
DiscretizedSpace.
element
(self, inp=None, order=None, \*\*kwargs)[source]¶ Create an element from
inp
or from scratch.- Parameters
- inpoptional
Input used to initialize the new element. The following options are available:
None
: an empty element is created with no guarantee of its state (memory allocation only). The new element will useorder
as storage order if provided, otherwisedefault_order
.array-like: an element wrapping a
tensor
is created, where a copy is avoided whenever possible. This usually requires correctshape
,dtype
andimpl
if applicable, and iforder
is provided, also contiguousness in that ordering. See theelement
method oftspace
for more information.If any of these conditions is not met, a copy is made.
callable: a new element is created by sampling the function using
point_collocation
.
- order{None, ‘C’, ‘F’}, optional
Storage order of the returned element. For
'C'
and'F'
, contiguous memory in the respective ordering is enforced. The defaultNone
enforces no contiguousness.- kwargs :
Additional arguments passed on to
point_collocation
when called oninp
, in the formpoint_collocation(inp, points, **kwargs)
. This can be used e.g. for functions with parameters.
- Returns
- element
DiscretizedSpaceElement
The discretized element, calculated as
point_collocation(inp)
ortspace.element(inp)
, tried in this order.
- element
Examples
Elements can be created from array-like objects that represent an already discretized function:
>>> space = odl.uniform_discr(-1, 1, 4) >>> space.element([1, 2, 3, 4]) uniform_discr(-1.0, 1.0, 4).element([ 1., 2., 3., 4.]) >>> vector = odl.rn(4).element([0, 1, 2, 3]) >>> space.element(vector) uniform_discr(-1.0, 1.0, 4).element([ 0., 1., 2., 3.])
On the other hand, non-discretized objects like Python functions can be discretized “on the fly”:
>>> space.element(lambda x: x * 2) uniform_discr(-1.0, 1.0, 4).element([-1.5, -0.5, 0.5, 1.5])
This works also with parameterized functions, however only through keyword arguments (not positional arguments with defaults):
>>> def f(x, c=0.0): ... return np.maximum(x, c) ... >>> space = odl.uniform_discr(-1, 1, 4) >>> space.element(f, c=0.5) uniform_discr(-1.0, 1.0, 4).element([ 0.5 , 0.5 , 0.5 , 0.75])