NumpyTensor.__setitem__

NumpyTensor.__setitem__(self, indices, values)[source]

Implement self[indices] = values.

Parameters
indicesindex expression

Integer, slice or sequence of these, defining the positions of the data array which should be written to.

valuesscalar, array-like or NumpyTensor

The value(s) that are to be assigned.

If index is an integer, value must be a scalar.

If index is a slice or a sequence of slices, value must be broadcastable to the shape of the slice.

Examples

For 1d spaces, entries can be set with scalars or sequences of correct shape:

>>> space = odl.rn(3)
>>> x = space.element([1, 2, 3])
>>> x[0] = -1
>>> x[1:] = (0, 1)
>>> x
rn(3).element([-1.,  0.,  1.])

It is also possible to use tensors of other spaces for casting and assignment:

>>> space = odl.rn((2, 3))
>>> x = space.element([[1, 2, 3],
...                    [4, 5, 6]])
>>> x[0, 1] = -1
>>> x
rn((2, 3)).element(
    [[ 1., -1.,  3.],
     [ 4.,  5.,  6.]]
)
>>> short_space = odl.tensor_space((2, 2), dtype='short')
>>> y = short_space.element([[-1, 2],
...                          [0, 0]])
>>> x[:, :2] = y
>>> x
rn((2, 3)).element(
    [[-1.,  2.,  3.],
     [ 0.,  0.,  6.]]
)

The Numpy assignment and broadcasting rules apply:

>>> x[:] = np.array([[0, 0, 0],
...                  [1, 1, 1]])
>>> x
rn((2, 3)).element(
    [[ 0.,  0.,  0.],
     [ 1.,  1.,  1.]]
)
>>> x[:, 1:] = [7, 8]
>>> x
rn((2, 3)).element(
    [[ 0.,  7.,  8.],
     [ 1.,  7.,  8.]]
)
>>> x[:, ::2] = -2.
>>> x
rn((2, 3)).element(
    [[-2.,  7., -2.],
     [-2.,  7., -2.]]
)