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 andshape
is set toNone
(the default) theobject_.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.
- 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:
- 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:
- 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:
- 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:
- Return type:
- 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:
- 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:
- sum(axis=None)[source]#
SymNum implementation of
numpy.ndarray.sum
.- Parameters:
- Return type:
Union[SymbolicArray, sympy.Expr]
- tolist()[source]#
SymNum implementation of
numpy.ndarray.tolist
.
- transpose(axes=None)[source]#
SymNum implementation of
numpy.ndarray.transpose
.- Parameters:
- Return type:
- 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 andSymbolicArray`SymbolicArray
with equivalent shape and entries to array argument if not.- Return type:
- symnum.array.binary_broadcasting_func(func, name=None, doc=None)[source]#
Wrap binary function to give broadcasting semantics.
- Parameters:
- 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.
- 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:
- 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:
- 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:
- 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:
- 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: