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
vals (
numpy.ndarray
) – Sorted eigenvalues.vecs (
numpy.ndarray
) – Sorted eigenvectors.
- pyrolite.util.math.augmented_covariance_matrix(M, C)[source]
Constructs an augmented covariance matrix from means M and covariance matrix C.
- Parameters
M (
numpy.ndarray
) – Array of means.C (
numpy.ndarray
) – Covariance matrix.- Returns
Augmented covariance matrix A.
- Return type
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
x (
numpy.ndarray
) – 1D array of x values.y (
numpy.ndarray
) – ND array of y values.
- 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
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
- pyrolite.util.math.linspc_(_min, _max, step=0.0, bins=20)[source]
Linear spaced array, with optional step for grid margins.
- Parameters
- Returns
Linearly-spaced array.
- Return type
- pyrolite.util.math.logspc_(_min, _max, step=1.0, bins=20)[source]
Log spaced array, with optional step for grid margins.
- Parameters
- Returns
Log-spaced array.
- Return type
- 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
- pyrolite.util.math.is_numeric(obj)[source]
Check for numerical behaviour.
- Parameters
obj – Object to check.
- Return type
- 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
- 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 reformatunc (
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 returnnp.nan
in place of values which are within precision equal to zero.- Returns
Reformatted number.
- Return type
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
- 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 considernp.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
- 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 forscipy.linalg.helmert()
.- Return type
- 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
- 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