writable_array

odl.core.util.utility.writable_array(obj, must_be_contiguous: bool = False)[source]

Context manager that casts obj to a backend-specific array and saves changes made on that array back into obj.

Parameters

objarray-like

Object that should be made available as writable array. It must be valid as input to numpy.asarray and needs to support the syntax obj[:] = arr.

must_be_contiguousbool

Whether the writable array should guarantee standard C order.

Examples

Usage with ODL vectors:

>>> space = odl.uniform_discr(0, 1, 3)
>>> x = space.element([1, 2, 3])
>>> with writable_array(x) as arr:
...     arr += [1, 1, 1]
>>> x
uniform_discr(0.0, 1.0, 3).element([ 2.,  3.,  4.])

Note that the changes are in general only saved upon exiting the context manager. Before, the input object may remain unchanged.