symnum.array module#

Symbolic array classes.

class symnum.array.SymbolicArray(object_, shape=None, dtype=None)[source]#

Bases: object

Symbolic n-dimensional array with NumPy-like interface.

Specifically implements NumPy style operator overloading, broadcasting and indexing semantics.

Parameters:
  • object – Object to construct symbolic array from. Either a nested sequence of (symbolic) scalars, an existing array like object or a single (symbolic) scalar.

  • shape (Optional[tuple[int, ...]]) – Tuple defining shape of array (sizes of each dimension / axis). If object_ is array-like or scalar-like and shape is set to None (the default) the object_.shape attribute will be used if array-like or a () shape used if scalar-like.

  • dtype (DTypeLike) – Object defining datatype to use for array when converting to NumPy arrays. If set to None (default) then an appropriate datatype will be inferred from the properties of the symbolic expressions in the array.

  • object_ (Union[ScalarLike, ArrayLike, tuple, list])

property T: SymbolicArray#

SymNum implementation of numpy.ndarray.T.

diff(*variables)[source]#

Compute derivative of array with respect to one or more arguments.

Wrapper of sympy.NDimArray.diff().

Parameters:

*variables (Expr | SymbolicArray) – One or more variables to differentiate with respect to.

Returns:

Array of derivatives.

Return type:

SymbolicArray

property dtype: dtype[Any] | None | type[Any] | _SupportsDType[dtype[Any]] | str | tuple[Any, int] | tuple[Any, SupportsIndex | Sequence[SupportsIndex]] | list[Any] | _DTypeDict | tuple[Any, Any]#

SymNum implementation of numpy.ndarray.dtype.

property flat: Iterator[sympy.Expr]#

SymNum implementation of numpy.ndarray.flat.

flatten()[source]#

SymNum implementation of numpy.ndarray.flatten.

Return type:

SymbolicArray

property free_symbols: set[Symbol]#

Set of all free symbols in symbolic expressions defined in array.

property imag: Expr | Boolean | bool | int | float | complex | number | NDimArray | MatrixBase | ndarray[Any, dtype[_ScalarType_co]] | SymbolicArray#

SymNum implementation of numpy.ndarray.imag.

property ndim: int#

SymNum implementation of numpy.ndarray.ndim.

prod(axis=None)[source]#

SymNum implementation of numpy.ndarray.prod.

Parameters:

axis (Optional[Union[tuple, list, int]])

Return type:

Union[SymbolicArray, sympy.Expr]

property real: Expr | Boolean | bool | int | float | complex | number | NDimArray | MatrixBase | ndarray[Any, dtype[_ScalarType_co]] | SymbolicArray#

SymNum implementation of numpy.ndarray.real.

reshape(shape)[source]#

SymNum implementation of numpy.ndarray.reshape.

Parameters:

shape (int | tuple[int, ...] | Tuple)

Return type:

SymbolicArray

property shape: tuple[int, ...]#

SymNum implementation of numpy.ndarray.shape.

simplify(**kwargs)[source]#

Simplify symbolic expressions in array.

Parameters:

**kwargs – Any keyword arguments to sympy.NDimArray.simplify().

Returns:

Array with simplified symbolic expressions.

Return type:

SymbolicArray

property size: int#

SymNum implementation of numpy.ndarray.size.

subs(*args)[source]#

Substitute variables in an expression.

Wrapper of sympy.NDimArray.subs().

Parameters:

*args – Either two arguments (old, new) corresponding to the old and new variables to subsitute for respectively, or an iterable of (old, new) pairs, with replacements processed in order given, or a dictionary mapping from old to new variables.

Returns:

Array with subsititutions applies.

Return type:

SymbolicArray

sum(axis=None)[source]#

SymNum implementation of numpy.ndarray.sum.

Parameters:

axis (Optional[Union[tuple, list, int]])

Return type:

Union[SymbolicArray, sympy.Expr]

tolist()[source]#

SymNum implementation of numpy.ndarray.tolist.

Return type:

list | Expr

transpose(axes=None)[source]#

SymNum implementation of numpy.ndarray.transpose.

Parameters:

axes (Optional[tuple[int, ...]])

Return type:

SymbolicArray

symnum.array.as_symbolic_array(array)[source]#

Convert an array to a SymbolicArray instance if not already an instance.

Parameters:

array (NDimArray | MatrixBase | ndarray[Any, dtype[_ScalarType_co]] | SymbolicArray) – Array to return as SymbolicArray instance.

Returns:

Original array if argument is already an SymbolicArray instance and SymbolicArray`SymbolicArray with equivalent shape and entries to array argument if not.

Return type:

SymbolicArray

symnum.array.binary_broadcasting_func(func, name=None, doc=None)[source]#

Wrap binary function to give broadcasting semantics.

Parameters:
  • func (Callable[[ScalarLike, ScalarLike], ScalarLike]) – Binary argument function to wrap.

  • name (Optional[str]) – Name to assign to wrapped function’s __name__ attribute.

  • doc (Optional[str]) – Docstring to assign to wrapped function’s __doc__ attribute.

Returns:

Wrapped function which broadcasts on both arguments.

Return type:

Callable[[ArrayLike, ArrayLike], ArrayLike]

symnum.array.infer_array_dtype(array)[source]#

Infer safe dtype for array.

Parameters:

array (SymbolicArray) – Array to infer dtype for.

Returns:

NumPy dtype which can represent array elements.

Return type:

dtype[Any] | None | type[Any] | _SupportsDType[dtype[Any]] | str | tuple[Any, int] | tuple[Any, SupportsIndex | Sequence[SupportsIndex]] | list[Any] | _DTypeDict | tuple[Any, Any]

symnum.array.infer_scalar_dtype(scalar)[source]#

Infer safe dtype for array.

Parameters:
Returns:

NumPy dtype which can represent array elements.

Return type:

dtype[Any] | None | type[Any] | _SupportsDType[dtype[Any]] | str | tuple[Any, int] | tuple[Any, SupportsIndex | Sequence[SupportsIndex]] | list[Any] | _DTypeDict | tuple[Any, Any]

symnum.array.is_array(obj)[source]#

Check if object is a valid SymNum, SymPy or NumPy array type.

Parameters:

obj – Object to check.

Returns:

Whether object is valid array type.

Return type:

bool

symnum.array.is_scalar(obj)[source]#

Check if object is a symbolic or numeric scalar type.

Parameters:

obj – Object to check.

Returns:

Whether object is valid scalar type.

Return type:

bool

symnum.array.is_sympy_array(obj)[source]#

Check if object is a valid SymPy array type.

Parameters:

obj – Object to check.

Returns:

Whether object is valid SymPy array type.

Return type:

bool

symnum.array.is_valid_shape(obj)[source]#

Check if object is a valid array shape type.

Parameters:

obj – Object to check.

Returns:

Whether object is valid array shape type.

Return type:

bool

symnum.array.named_array(name, shape, dtype=None)[source]#

Create a symbolic array with common name prefix to elements.

Parameters:
  • name (str) – Name prefix to use for symbolic array elements.

  • shape (ShapeLike) – Dimensions of array.

  • dtype (Optional[DTypeLike]) – NumPy dtype to use for array.

Returns:

Symbolic array with elements name[index_list] for index_list iterating over strings of valid comma-separated indices, for example calling with name=”a” and shape=(2, 2) would give a symbolic array of shape (2, 2) with elements a[0, 0], a[0, 1], a[1, 0] and a[1, 1].

Return type:

SymbolicArray

symnum.array.unary_elementwise_func(func, name=None, doc=None)[source]#

Wrap unary function to give elementwise semantics.

Parameters:
  • func (Callable[[ScalarLike], ScalarLike]) – Binary argument function to wrap.

  • name (Optional[str]) – Name to assign to wrapped function’s __name__ attribute.

  • doc (Optional[str]) – Docstring to assign to wrapped function’s __doc__ attribute.

Returns:

Wrapped function which acts elementwise on argument.

Return type:

Callable[[ArrayLike], ArrayLike]