Array Routines#

dewloosh.math.array.ascont(array: ndarray) ndarray#

Returns the input as contiguous array (ndim >= 1). It is basically a shortcut to np.ascontiguousarray.

dewloosh.math.array.atleast1d(a: Union[float, ndarray, list]) ndarray#

Returns an array that is at least 1 dimensional.

dewloosh.math.array.atleast2d(a: ndarray, **kwargs) ndarray#

Returns an array that is at least 2 dimensional.

dewloosh.math.array.atleast3d(a: ndarray, **kwargs) ndarray#

Returns an array that is at least 3 dimensional.

dewloosh.math.array.atleast4d(a: ndarray, **kwargs) ndarray#

Returns an array that is at least 4 dimensional.

dewloosh.math.array.atleastnd(a: ndarray, n=2, front=True, back=False) ndarray#

Returns an array that is at least ‘n’ dimensional. The required shape is obtained by inserting new axes either before or after the existing data. This behaviour can be controlled using the parameters ‘front’ and ‘back’. If front is True and back is False, new axes are crated before the first existing data index, and the opposite happens in every other case.

dewloosh.math.array.bool_to_float(a: ndarray, true=1.0, false=0.0) ndarray#

Transforms a boolean array to a float array using the specified values for True and False.

dewloosh.math.array.choice(choices, size, probs=None) ndarray#

Returns a numpy array, whose elements are selected from ‘choices’ under probabilities provided with ‘probs’ (optionally).

Example

>>> N, p = 10, 0.2
>>> choice([False, True], (N, N), [p, 1-p])
dewloosh.math.array.clip1d(a: ndarray, a_min: float, a_max: float) ndarray#

Clips the values outside the interval [a_min, a_max] to either a_min or a_max.

dewloosh.math.array.flatten2d(a: ndarray, order: str = 'C') ndarray#

Returns a flattened view of a.

dewloosh.math.array.is1dfloatarray(a: ndarray) bool#

Returns True if a is a 1d float array.

dewloosh.math.array.is1dintarray(a: ndarray) bool#

Returns True if a is a 1d integer array.

dewloosh.math.array.isboolarray(a: ndarray) bool#

Returns True if a is a boolean array.

dewloosh.math.array.isfloatarray(a: ndarray) bool#

Returns True if a is a float array.

dewloosh.math.array.isintarray(a: ndarray) bool#

Returns True if a is a integer array.

dewloosh.math.array.isintegerarray(a: ndarray) bool#

Returns True if a is an integer array.

dewloosh.math.array.isposdef(A: ndarray, tol=0)#

Returns True if A is positive definite.

Example

>>> from dewloosh.math.array import random_posdef_matrix, isposdef
>>> A = random_posdef_matrix(3, 0.1)
>>> isposdef(A)
True
>>> A[0, 0] = 0
>>> isposdef(A)
False
dewloosh.math.array.ispossemidef(A: ndarray)#

Returns True if A is positive semidefinite.

Example

>>> from dewloosh.math.array import random_pos_semidef_matrix, ispossemidef
>>> A = random_pos_semidef_matrix(3)
>>> ispossemidef(A)
True
dewloosh.math.array.issymmetric(a: ndarray, tol: float = 1e-08) bool#

Returns True if a is symmetric with a given tolerance prescribed by tol.

dewloosh.math.array.matrixform(f: ndarray) ndarray#

Returns an array that is at least 2 dimensional.

dewloosh.math.array.minmax(a: ndarray) Tuple[float]#

Returns the minimum and maximum values of an array.

dewloosh.math.array.random_pos_semidef_matrix(N) ndarray#

Returns a random positive semidefinite matrix of shape (N, N).

Example

>>> from dewloosh.math.array import random_pos_semidef_matrix
>>> random_pos_semidef_matrix(3)
...
dewloosh.math.array.random_posdef_matrix(N, alpha=1e-12) ndarray#

Returns a random positive definite matrix of shape (N, N).

All eigenvalues of this matrix are >= alpha.

Example

>>> from dewloosh.math.array import random_posdef_matrix
>>> random_posdef_matrix(3, 0.1)
...
dewloosh.math.array.repeat(a: ndarray, N=1) ndarray#

Repeats an array N-times.

Parameters
  • a (ndarray) – Input array.

  • N (int, Optional.) – Number of repetitions. Default is 1.

Returns

Output array with shape

Return type

ndarray

Example

For example, to generate basis vectors for 10 vectors embedded in the same coordinate frame, we need to stack up 10 identical identity matrices. This can be done the quickest by:

>>> from dewloosh.math.array import repeat
>>> axes = repeat(np.eye(3), 10)