NumpyTensorSpace.element

NumpyTensorSpace.element(self, inp=None, data_ptr=None, order=None)[source]

Create a new element.

Parameters
inparray-like, optional

Input used to initialize the new element.

If inp is None, an empty element is created with no guarantee of its state (memory allocation only). The new element will use order as storage order if provided, otherwise default_order.

Otherwise, a copy is avoided whenever possible. This requires correct shape and dtype, and if order is provided, also contiguousness in that ordering. If any of these conditions is not met, a copy is made.

data_ptrint, optional

Pointer to the start memory address of a contiguous Numpy array or an equivalent raw container with the same total number of bytes. For this option, order must be either 'C' or 'F'. The option is also mutually exclusive with inp.

order{None, ‘C’, ‘F’}, optional

Storage order of the returned element. For 'C' and 'F', contiguous memory in the respective ordering is enforced. The default None enforces no contiguousness.

Returns
elementNumpyTensor

The new element, created from inp or from scratch.

Examples

Without arguments, an uninitialized element is created. With an array-like input, the element can be initialized:

>>> space = odl.rn(3)
>>> empty = space.element()
>>> empty.shape
(3,)
>>> empty.space
rn(3)
>>> x = space.element([1, 2, 3])
>>> x
rn(3).element([ 1.,  2.,  3.])

If the input already is a numpy.ndarray of correct dtype, it will merely be wrapped, i.e., both array and space element access the same memory, such that mutations will affect both:

>>> arr = np.array([1, 2, 3], dtype=float)
>>> elem = odl.rn(3).element(arr)
>>> elem[0] = 0
>>> elem
rn(3).element([ 0.,  2.,  3.])
>>> arr
array([ 0.,  2.,  3.])

Elements can also be constructed from a data pointer, resulting again in shared memory:

>>> int_space = odl.tensor_space((2, 3), dtype=int)
>>> arr = np.array([[1, 2, 3],
...                 [4, 5, 6]], dtype=int, order='F')
>>> ptr = arr.ctypes.data
>>> y = int_space.element(data_ptr=ptr, order='F')
>>> y
tensor_space((2, 3), dtype=int).element(
    [[1, 2, 3],
     [4, 5, 6]]
)
>>> y[0, 1] = -1
>>> arr
array([[ 1, -1,  3],
       [ 4,  5,  6]])