arrays#
This module contains common numerical routines that operate on NumPy arrays.
- minterpy.utils.arrays.lp_norm(arr, p, axis=None, keepdims=False)[source]#
Robust lp-norm function.
Works essentially like
numpy.linalg.norm
, but is numerically stable for big arguments.- Parameters:
arr (np.ndarray) – Input array.
axis ({None, int, 2-tuple of int}, optional) – If axis is an integer, it specifies the axis of x along which to compute the vector norms. If axis is a 2-tuple, it specifies the axes that hold 2-D matrices, and the matrix norms of these matrices are computed. If axis is None then either a vector norm (when x is 1-D) or a matrix norm (when x is 2-D) is returned. The default is
None
.keepdims (bool, optional) – If this is set to True, the axes which are normed over are left in the result as dimensions with size one. With this option the result will broadcast correctly against the original
arr
.
- minterpy.utils.arrays.cartesian_product(*arrays)[source]#
Build the cartesian product of any number of 1D arrays.
- Parameters:
arrays (list) – List of 1D array_like.
- Returns:
Array of all combinations of elements of the input arrays (a cartesian product).
- Return type:
np.ndarray
Examples
>>> x = np.array([1,2,3]) >>> y = np.array([4,5]) >>> cartesian_product(x,y) array([[1, 4], [1, 5], [2, 4], [2, 5], [3, 4], [3, 5]])
>>> s= np.array([0,1]) >>> cartesian_product(s,s,s,s) array([[0, 0, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0], [0, 0, 1, 1], [0, 1, 0, 0], [0, 1, 0, 1], [0, 1, 1, 0], [0, 1, 1, 1], [1, 0, 0, 0], [1, 0, 0, 1], [1, 0, 1, 0], [1, 0, 1, 1], [1, 1, 0, 0], [1, 1, 0, 1], [1, 1, 1, 0], [1, 1, 1, 1]])
- minterpy.utils.arrays.lp_sum(arr, p)[source]#
Sum of powers, i.e. lp-norm to the lp-degree.
- Parameters:
arr (np.ndarray) – 2D-array to be lp-summed
p (Real) – Power for each element in the lp-sum
- Returns:
lp-sum over the last axis of the input array powered by the given power
- Return type:
np.ndarray
Notes
equivalent to
`lp_norm(arr,p,axis=-1)**p`
but more stable then the implementation usingnp.linalg.norm
(see numpy #5697 for more informations)
- minterpy.utils.arrays.make_coeffs_2d(coefficients)[source]#
Make coefficients array 2d.
- Parameters:
coefficients (np.ndarray with coefficients)
- Return type:
Returns a 2d array in the case of both single and multiple polynomials
Notes
This function is similar to np.atleast_2d, but adds the extra dimension differently.
- minterpy.utils.arrays.expand_dim(xx, target_dim, new_values=None)[source]#
Expand the dimension of a given 2D array filled with given values.
- Parameters:
xx (
numpy.ndarray
) – Input array (exponents array or interpolating grid array) which will be expanded; it must be a two-dimensional array.target_dim (int) – The target dimension up to which the array will be expanded. The value must be larger than or equal to the dimension of the current array.
new_values (
numpy.ndarray
, optional) – The new values for the expanded dimensions; the values will be tiled to fill in the expanded dimensions.
- Returns:
Exponents or grid array with expanded dimension (i.e., additional columns).
- Return type:
- Raises:
ValueError – If the number of dimension of the input array is not equal to 2; if the target number of columns is less than the number of columns of the input array; or if the number of values for the new columns is inconsistent (not equal the number of rows of the input array).
Notes
The term dimension here refers to dimensionality of exponents or interpolating grid; in other words, it refers to the number of columns of such arrays.
Examples
>>> array = np.array([[0, 0], [1, 0], [0, 1]]) # 2 columns / "dimensions" >>> expand_dim(array, 4) # expand to 4 columns / "dimensions" array([[0, 0, 0, 0], [1, 0, 0, 0], [0, 1, 0, 0]]) >>> expand_dim(array, 4, np.array([3, 2])) # expand with tiled values array([[0, 0, 3, 2], [1, 0, 3, 2], [0, 1, 3, 2]])
- minterpy.utils.arrays.is_unique(xx)[source]#
Return
True
if the input array has unique values.- Parameters:
xx (
numpy.ndarray
) – The one-dimensional array to be checked.- Returns:
True
if the values in the array are unique.- Return type:
Examples
>>> is_unique(np.array([0, 1, 2, 3, 4, 5])) True >>> is_unique(np.array([0, 1, 2, 3, 4, 5, 5, 6])) False >>> is_unique(np.array([[0, 1, 2, 3, 4]])) # two-dimensional, squeeze-able True >>> is_unique(np.array([0.0])) True >>> is_unique(np.array([[1, 0], [1, 0]])) Traceback (most recent call last): ... ValueError: The input array must be one-dimensional