pyrolite.util.math

pyrolite.util.math.eigsorted(cov)[source]

Returns arrays of eigenvalues and eigenvectors sorted by magnitude.

Parameters

cov (numpy.ndarray) – Covariance matrix to extract eigenvalues and eigenvectors from.

Returns

pyrolite.util.math.augmented_covariance_matrix(M, C)[source]

Constructs an augmented covariance matrix from means M and covariance matrix C.

Parameters
Returns

Augmented covariance matrix A.

Return type

numpy.ndarray

Notes

Augmented covariance matrix constructed from mean of shape (D, ) and covariance matrix of shape (D, D) as follows:

\[\begin{split}\begin{array}{c|c} -1 & M.T \\ \hline M & C \end{array}\end{split}\]
pyrolite.util.math.interpolate_line(x, y, n=0, logy=False)[source]

Add intermediate evenly spaced points interpolated between given x-y coordinates, assuming the x points are the same.

Parameters
pyrolite.util.math.grid_from_ranges(X, bins=100, **kwargs)[source]

Create a meshgrid based on the ranges along columns of array X.

Parameters
  • X (numpy.ndarray) – Array of shape (samples, dimensions) to create a meshgrid from.

  • bins (int | tuple) – Shape of the meshgrid. If an integer, provides a square mesh. If a tuple, values for each column are required.

Return type

numpy.ndarray

Notes

Can pass keyword arg indexing = {‘xy’, ‘ij’}

pyrolite.util.math.flattengrid(grid)[source]

Convert a collection of arrays to a concatenated array of flattened components. Useful for passing meshgrid values to a function which accepts argumnets of shape (samples, dimensions).

Parameters

grid (list) – Collection of arrays (e.g. a meshgrid) to flatten and concatenate.

Return type

numpy.ndarray

pyrolite.util.math.linspc_(_min, _max, step=0.0, bins=20)[source]

Linear spaced array, with optional step for grid margins.

Parameters
  • _min (float) – Minimum value for spaced range.

  • _max (float) – Maximum value for spaced range.

  • step (float, 0.0) – Step for expanding at grid edges. Default of 0.0 results in no expansion.

  • bins (int) – Number of bins to divide the range (adds one by default).

Returns

Linearly-spaced array.

Return type

numpy.ndarray

pyrolite.util.math.logspc_(_min, _max, step=1.0, bins=20)[source]

Log spaced array, with optional step for grid margins.

Parameters
  • _min (float) – Minimum value for spaced range.

  • _max (float) – Maximum value for spaced range.

  • step (float, 1.0) – Step for expanding at grid edges. Default of 1.0 results in no expansion.

  • bins (int) – Number of bins to divide the range (adds one by default).

Returns

Log-spaced array.

Return type

numpy.ndarray

pyrolite.util.math.logrng_(v, exp=0.0)[source]

Range of a sample, where values <0 are excluded.

Parameters
  • v (list; list-like) – Array of values to obtain a range from.

  • exp (float, (0, 1)) – Fractional expansion of the range.

Returns

Min, max tuple.

Return type

tuple

pyrolite.util.math.linrng_(v, exp=0.0)[source]

Range of a sample, where values <0 are included.

Parameters
  • v (list; list-like) – Array of values to obtain a range from.

  • exp (float, (0, 1)) – Fractional expansion of the range.

Returns

Min, max tuple.

Return type

tuple

pyrolite.util.math.isclose(a, b)[source]

Implementation of np.isclose with equal nan.

Parameters

a,b (float | numpy.ndarray) – Numbers or arrays to compare.

Return type

bool

pyrolite.util.math.is_numeric(obj)[source]

Check for numerical behaviour.

Parameters

obj – Object to check.

Return type

bool

pyrolite.util.math.significant_figures(n, unc=None, max_sf=20, rtol=1e-20)[source]

Iterative method to determine the number of significant digits for a given float, optionally providing an uncertainty.

Parameters
  • n (float) – Number from which to ascertain the significance level.

  • unc (float, None) – Uncertainty, which if provided is used to derive the number of significant digits.

  • max_sf (int) – An upper limit to the number of significant digits suggested.

  • rtol (float) – Relative tolerance to determine similarity of numbers, used in calculations.

Returns

Number of significant digits.

Return type

int

pyrolite.util.math.signify_digit(n, unc=None, leeway=0, low_filter=True)[source]

Reformats numbers to contain only significant_digits. Uncertainty can be provided to digits with relevant precision.

Parameters
  • n (float) – Number to reformat

  • unc (float, None) – Absolute uncertainty on the number, optional.

  • leeway (int, 0) – Manual override for significant figures. Positive values will force extra significant figures; negative values will remove significant figures.

  • low_filter (bool, True) – Whether to return np.nan in place of values which are within precision equal to zero.

Returns

Reformatted number.

Return type

float

Notes

  • Will not pad 0s at the end or before floats.

pyrolite.util.math.most_precise(arr)[source]

Get the most precise element from an array.

Parameters

arr (numpy.ndarray) – Array to obtain the most precise element/subarray from.

Returns

Returns the most precise array element (for ndim=1), or most precise subarray (for ndim > 1).

Return type

float | numpy.ndarray

pyrolite.util.math.equal_within_significance(arr, equal_nan=False, rtol=1e-15)[source]

Test whether elements of an array are equal within the precision of the least precise.

Parameters
  • arr (numpy.ndarray) – Array to test.

  • equal_nan (bool, False) – Whether to consider np.nan elements equal to one another.

  • rtol (float) – Relative tolerance for comparison.

Return type

bool | numpy.ndarray`(:class:`bool)

pyrolite.util.math.helmert_basis(D: int, full=False, **kwargs)[source]

Generate a set of orthogonal basis vectors in the form of a helmert matrix.

Parameters

D (int) – Dimension of compositional vectors.

Returns

(D-1, D) helmert matrix corresponding to default orthogonal basis.

Return type

numpy.ndarray

pyrolite.util.math.symbolic_helmert_basis(D, full=False)[source]

Get a symbolic representation of a Helmert Matrix.

Parameters
  • D (int) – Order of the matrix. Equivalent to dimensionality for compositional data analysis.

  • full (bool) – Whether to return the full matrix, or alternatively exclude the first row. Analogous to the option for scipy.linalg.helmert().

Return type

sympy.matrices.dense.DenseMatrix

pyrolite.util.math.on_finite(X, f)[source]

Calls a function on an array ignoring np.nan and +/- np.inf. Note that the shape of the output may be different to that of the input.

Parameters
  • X (numpy.ndarray) – Array on which to perform the function.

  • f (Callable) – Function to call on the array.

Return type

numpy.ndarray

pyrolite.util.math.nancov(X)[source]

Generates a covariance matrix excluding nan-components.

Parameters

X (numpy.ndarray) – Input array for which to derive a covariance matrix.

Return type

numpy.ndarray

pyrolite.util.math.solve_ratios(*eqs, evaluate=True)[source]

Solve a ternary system (top-left-right) given two constraints on two ratios, which together describe intersecting lines/a point.