NumPy

Provides a collection of functions useful in manipulating NumPy arrays.

e13tools.numpy.diff(array1, array2=None, axis=0, flatten=True)[source]

Calculates the pair-wise differences between inputs array1 and array2 over the given axis.

Parameters:

array1 (array_like) – One of the inputs used to calculate the pair-wise differences.

Other Parameters:
 
  • array2 (array_like or None. Default: None) – The other input used to calculate the pair-wise differences. If None, array2 is equal to array1. If not None, the length of all axes except axis must be equal for both arrays.
  • axis (int. Default: 0) – Over which axis to calculate the pair-wise differences. Default is over the first axis. A negative value counts from the last to the first axis.
  • flatten (bool. Default: True) – If array2 is None, whether or not to calculate all pair-wise differences. If True, a flattened array containing all above-diagonal pair-wise differences is returned. This is useful if only off-diagonal terms are required and the sign is not important. If False, an array with all pair-wise differences is returned.
Returns:

diff_array (ndarray object) – Depending on the input parameters, an array with n dimensions containing the pair-wise differences between array1 and array2 over the given axis.

Examples

Using two matrices returns the pair-wise differences in row-vectors:

>>> mat1 = np.array([[1, 2, 3], [4, 5, 6]])
>>> mat2 = np.array([[4, 5, 6], [7, 8, 9]])
>>> diff(mat1, mat2)
array([[[-3., -3., -3.],
        [-6., -6., -6.]],
<BLANKLINE>
       [[ 0.,  0.,  0.],
        [-3., -3., -3.]]])

Setting axis to 1 returns the pair-wise differences in column-vectors:

>>> mat1 = np.array([[1, 2, 3], [4, 5, 6]])
>>> mat2 = np.array([[4, 5, 6], [7, 8, 9]])
>>> diff(mat1, mat2, axis=1)
array([[[-3., -3.],
        [-4., -4.],
        [-5., -5.]],
<BLANKLINE>
       [[-2., -2.],
        [-3., -3.],
        [-4., -4.]],
<BLANKLINE>
       [[-1., -1.],
        [-2., -2.],
        [-3., -3.]]])

Only using a single matrix returns the pair-wise differences in row-vectors in that matrix (either flattened or not):

>>> mat = np.array([[1, 2, 3], [4, 5, 6]])
>>> diff(mat, flatten=True)
array([[-3., -3., -3.]])
>>> diff(mat, flatten=False)
array([[[ 0.,  0.,  0.],
        [-3., -3., -3.]],
<BLANKLINE>
       [[ 3.,  3.,  3.],
        [ 0.,  0.,  0.]]])

Using a matrix and a vector returns the pair-wise differences in row-vectors:

>>> mat = np.array([[1, 2, 3], [4, 5, 6]])
>>> vec = np.array([7, 8, 9])
>>> diff(mat, vec)
array([[-6, -6, -6],
       [-3, -3, -3]])

Using two vectors returns the pair-wise differences in scalars:

>>> vec1 = np.array([1, 2, 3])
>>> vec2 = np.array([4, 5, 6])
>>> diff(vec1, vec2)
array([[-3., -4., -5.],
       [-2., -3., -4.],
       [-1., -2., -3.]])
e13tools.numpy.intersect(array1, array2, axis=0, assume_unique=False)[source]

Finds the intersection between given arrays array1 and array2 over provided axis and returns the unique elements that are both in array1 and array2.

This is an nD-version of NumPy’s intersect1d() function.

Parameters:
  • array1 (array_like) – Input array.
  • array2 (array_like) – Comparison array with same shape as array1 except in given axis.
Other Parameters:
 
  • axis (int or None. Default: 0) – Axis over which elements must be checked in both arrays. A negative value counts from the last to the first axis. If None, both arrays are flattened first (this is the functionality of intersect1d()).
  • assume_unique (bool. Default: False) – Whether to assume that the elements in both arrays are unique, which can speed up the calculation.
Returns:

intersect_array (ndarray object) – Array containing the unique elements found both in array1 and array2 over given axis.

Example

>>> array1 = np.array([[1, 2], [1, 3], [2, 1]])
>>> array2 = np.array([[1, 2], [1, 3]])
>>> intersect(array1, array2)
array([[1, 2], [1, 3]])
e13tools.numpy.isin(array1, array2, axis=0, assume_unique=False, invert=False)[source]

Checks over the provided axis which elements of given array1 are also in given array2 and returns it.

This is an nD-version of NumPy’s isin() function.

Parameters:
  • array1 (array_like) – Input array.
  • array2 (array_like) – Comparison array with same shape as array1 except in given axis.
Other Parameters:
 
  • axis (int or None. Default: 0) – Axis over which elements must be checked in both arrays. A negative value counts from the last to the first axis. If None, both arrays are compared element-wise (this is the functionality of isin()).
  • assume_unique (bool. Default: False) – Whether to assume that the elements in both arrays are unique, which can speed up the calculation.
  • invert (bool. Default: False) – Whether to invert the returned boolean values. If True, the values in bool_array are as if calculating array1 not in array2.
Returns:

bool_array (ndarray object of bool) – Bool array containing the elements found in array1 that are in array2 over given axis.

Example

>>> array1 = np.array([[1, 2], [1, 3], [2, 1]])
>>> array2 = np.array([[1, 2], [1, 3]])
>>> isin(array1, array2)
array([True, True, False])
e13tools.numpy.rot90(array, axes=(0, 1), rot_axis='center', n_rot=1)[source]

Rotates the given array by 90 degrees around the point rot_axis in the given axes. This function is different from NumPy’s rot90() function in that every column (2nd axis) defines a different dimension instead of every individual axis.

Parameters:

array (2D array_like) – Array with shape [n_pts, n_dim] with n_pts the number of points and n_dim the number of dimensions. Requires: n_dim > 1.

Other Parameters:
 
  • axes (1D array_like with 2 ints. Default: (0, 1)) – Array containing the axes defining the rotation plane. Rotation is from the first axis towards the second. Can be omitted if rot_axis has length n_dim.
  • rot_axis (1D array_like of length 2/n_dim or ‘center’. Default: ‘center’) – If ‘center’, the rotation axis is chosen in the center of the minimum and maximum values found in the given axes. If 1D array of length 2, the rotation axis is chosen around the given values in the given axes. If 1D array of length n_dim, the rotation axis is chosen around the first two non-zero values.
  • n_rot (int. Default: 1) – Number of times to rotate array by 90 degrees.
Returns:

array_rot (2D ndarray object) – Array with shape [n_pts, n_dim] that has been rotated by 90 degrees n_rot times.

Examples

Using an array with just two dimensions:

>>> array = np.array([[0.75, 0], [0.25, 1], [1, 0.75], [0, 0.25]])
>>> rot90(array)
array([[ 1.  ,  0.75],
       [ 0.  ,  0.25],
       [ 0.25,  1.  ],
       [ 0.75,  0.  ]])

Using the same array, but rotating it around a different point:

>>> array = np.array([[0.75, 0], [0.25, 1], [1, 0.75], [0, 0.25]])
>>> rot90(array, rot_axis=[0.2, 0.7])
array([[ 0.9 ,  1.25],
       [-0.1 ,  0.75],
       [ 0.15,  1.5 ],
       [ 0.65,  0.5 ]])
e13tools.numpy.setdiff(array1, array2, axis=0, assume_unique=False)[source]

Finds the set difference between given arrays array1 and array2 over provided axis and returns the unique elements in array1 that are not in array2.

This is an nD-version of NumPy’s setdiff1d() function.

Parameters:
  • array1 (array_like) – Input array.
  • array2 (array_like) – Comparison array with same shape as array1 except in given axis.
Other Parameters:
 
  • axis (int or None. Default: 0) – Axis over which elements must be checked in both arrays. A negative value counts from the last to the first axis. If None, both arrays are flattened first (this is the functionality of setdiff1d()).
  • assume_unique (bool. Default: False) – Whether to assume that the elements in both arrays are unique, which can speed up the calculation.
Returns:

diff_array (ndarray object) – Array containing the unique elements found in array1 but not in array2 over given axis.

Example

>>> array1 = np.array([[1, 2], [1, 3], [2, 1]])
>>> array2 = np.array([[1, 2], [1, 3]])
>>> setdiff(array1, array2)
array([[2, 1]])
e13tools.numpy.setxor(array1, array2, axis=0, assume_unique=False)[source]

Finds the set exclusive-or between given arrays array1 and array2 over provided axis and returns the unique elements that are in either array1 or array2 (but not both).

This is an nD-version of NumPy’s setxor1d() function.

Parameters:
  • array1 (array_like) – Input array.
  • array2 (array_like) – Comparison array with same shape as array1 except in given axis.
Other Parameters:
 
  • axis (int or None. Default: 0) – Axis over which elements must be checked in both arrays. A negative value counts from the last to the first axis. If None, both arrays are flattened first (this is the functionality of setxor1d()).
  • assume_unique (bool. Default: False) – Whether to assume that the elements in both arrays are unique, which can speed up the calculation.
Returns:

xor_array (ndarray object) – Array containing the unique elements found in either array1 or array2 (but not both) over given axis.

Example

>>> array1 = np.array([[1, 2], [1, 3], [2, 1]])
>>> array2 = np.array([[1, 2], [1, 3], [3, 1]])
>>> setxor(array1, array2)
array([[2, 1], [3, 1]])
e13tools.numpy.sort2D(array, axis=-1, order=None)[source]

Sorts a 2D array over a given axis in the specified order. This function is different from NumPy’s sort() function in that it sorts over a given axis rather than along it, and the order can be given as integers rather than field strings.

Parameters:

array (2D array_like) – Input array that requires sorting.

Other Parameters:
 
  • axis (int. Default: -1) – Axis over which to sort the elements. Default is to sort all elements over the last axis. A negative value counts from the last to the first axis.
  • order (int, 1D array_like of int or None. Default: None) – The order in which the vectors in the given axis need to be sorted. Negative values count from the last to the first vector. If None, all vectors in the given axis are sorted individually.
Returns:

array_sort (2D ndarray object) – Input array with its axis sorted in the specified order.

Examples

Sorting the column elements of a given 2D array with no order specified:

>>> array = np.array([[0, 5, 1], [7, 4, 9], [3, 13, 6], [0, 1, 8]])
>>> array
array([[ 0,  5,  1],
       [ 7,  4,  9],
       [ 3, 13,  6],
       [ 0,  1,  8]])
>>> sort2D(array)
array([[ 0,  1,  1],
       [ 0,  4,  6],
       [ 3,  5,  8],
       [ 7, 13,  9]])

Sorting the same array in only the first column:

>>> sort2D(array, order=0)
array([[ 0,  5,  1],
       [ 0,  1,  8],
       [ 3, 13,  6],
       [ 7,  4,  9]])

Sorting all three columns in order:

>>> sort2D(array, order=(0, 1, 2))
array([[ 0,  1,  8],
       [ 0,  5,  1],
       [ 3, 13,  6],
       [ 7,  4,  9]])

Sorting all three columns in a different order:

>>> sort2D(array, order=(0, 2, 1))
array([[ 0,  5,  1],
       [ 0,  1,  8],
       [ 3, 13,  6],
       [ 7,  4,  9]])
e13tools.numpy.transposeC(array, axes=None)[source]

Returns the (conjugate) transpose of the input array.

Parameters:array (array_like) – Input array that needs to be transposed.
Other Parameters:
 axes (1D array_like of int or None. Default: None) – If None, reverse the dimensions. Else, permute the axes according to the values given.
Returns:array_t (ndarray object) – Input array with its axes transposed.

Examples

Using an array with only real values returns its transposed variant:

>>> array = np.array([[1, 2.5], [3.5, 5]])
>>> array
array([[ 1. ,  2.5],
       [ 3.5,  5. ]])
>>> transposeC(array)
array([[ 1. ,  3.5],
       [ 2.5,  5. ]])

And using an array containing complex values returns its conjugate transposed:

>>> array = np.array([[1, -2+4j], [7.5j, 0]])
>>> array
array([[ 1.+0.j , -2.+4.j ],
       [ 0.+7.5j,  0.+0.j ]])
>>> transposeC(array)
array([[ 1.-0.j ,  0.-7.5j],
       [-2.-4.j ,  0.-0.j ]])
e13tools.numpy.union(array1, array2, axis=0)[source]

Finds the union between given arrays array1 and array2 over provided axis and returns the unique elements in array1 and array2.

This is an nD-version of NumPy’s union1d() function.

Parameters:
  • array1 (array_like) – Input array.
  • array2 (array_like) – Comparison array with same shape as array1 except in given axis.
Other Parameters:
 

axis (int or None. Default: 0) – Axis over which elements must be checked in both arrays. A negative value counts from the last to the first axis. If None, both arrays are flattened first (this is the functionality of union1d()).

Returns:

union_array (ndarray object) – Sorted array containing the unique elements found in array1 and array2 over given axis.

Example

>>> array1 = np.array([[1, 2], [1, 3], [3, 1]])
>>> array2 = np.array([[1, 2], [1, 3], [2, 1]])
>>> union(array1, array2)
array([[1, 2], [1, 3], [2, 1], [3, 1]])