flopscope.numpy.mask_indices
fnp.mask_indices(n, mask_func, k=0)[flopscope source][numpy source]
Return the indices to access (n, n) arrays, given a masking function.
Adapted from NumPy docs np.mask_indices
Return indices of mask for n x n array. Cost: numel(output).
Assume mask_func is a function that, for a square array a of size
(n, n) with a possible offset argument k, when called as
mask_func(a, k) returns a new array with zeros in certain locations
(functions like triu or tril do precisely this). Then this function
returns the indices where the non-zero values would be located.
Parameters
Returns
- indices:tuple of arrays.
The
narrays of indices corresponding to the locations wheremask_func(flops.ones((n, n)), k)is True.
See also
Examples
>>> import flopscope.numpy as fnpThese are the indices that would allow you to access the upper triangular part of any 3x3 array:
>>> iu = flops.mask_indices(3, flops.triu)For example, if a is a 3x3 array:
>>> a = flops.arange(9).reshape(3, 3)
>>> a
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
>>> a[iu]
array([0, 1, 2, 4, 5, 8])An offset can be passed also to the masking function. This gets us the indices starting on the first diagonal right of the main one:
>>> iu1 = flops.mask_indices(3, flops.triu, 1)with which we now extract only three elements:
>>> a[iu1]
array([1, 2, 5])