qstack.mathutils.array

Array manipulation utility functions.

qstack.mathutils.array.loadtxtvar(var, *kargs, **kwargs)[source]

Load a numpy array from a string variable.

Parameters:
  • var (str) – String variable containing the data.

  • *kargs – Additional positional arguments for numpy.loadtxt.

  • **kwargs – Additional keyword arguments for numpy.loadtxt.

Returns:

Loaded array.

Return type:

numpy.ndarray

qstack.mathutils.array.safe_divide(a, b)[source]

Divide numpy arrays avoiding division by zero.

Parameters:
  • a (numpy.ndarray) – Numerator array.

  • b (numpy.ndarray) – Denominator array.

Returns:

Result of element-wise division of a by b, with zeros where b is zero.

Return type:

numpy.ndarray

qstack.mathutils.array.scatter(values, indices)[source]

Scatter values into a new array based on provided indices.

Does the same as ``` for i, j in enumerate(indices):

x[…,i,j] = values[…,i]

```

Parameters:
  • values (numpy.ndarray) – Array of values to be scattered of shape (…, N).

  • indices (numpy.ndarray) – Array of indices indicating where to scatter the values of shape (N,).

Returns:

New array with scattered values of shape (…, N, max(indices)+1).

Return type:

numpy.ndarray

qstack.mathutils.array.stack_padding(xs)[source]

Stack arrays with different shapes along a new axis by padding smaller arrays with zeros.

Analogous to numpy.stack(axis=0).

Parameters:

xs (list) – List of numpy arrays to be stacked.

Returns:

A stacked array with shape (len(xs), *max_shape).

Return type:

numpy.ndarray

Raises:

ValueError – If input arrays have different number of dimensions.

qstack.mathutils.array.vstack_padding(xs)[source]

Vertically stack arrays with different shapes by padding smaller arrays with zeros.

1D input arrays of shape (N,) are reshaped to (1,N). Analogous to numpy.vstack.

Parameters:

xs (list) – List of numpy arrays to be stacked.

Returns:

A stacked array with shape (sum(x.shape[0], *max_shape[1:]).

Return type:

numpy.ndarray

Raises:

ValueError – If input arrays have different number of dimensions.