Undocumented
Variable | array_function_dispatch |
Undocumented |
Variable | c_ |
Undocumented |
Variable | index_exp |
Undocumented |
Variable | mgrid |
Undocumented |
Variable | ogrid |
Undocumented |
Variable | r_ |
Undocumented |
Variable | s_ |
Undocumented |
Class | AxisConcatenator |
Translates slice objects to concatenation along an axis. |
Class | CClass |
Translates slice objects to concatenation along the second axis. |
Class | IndexExpression |
A nicer way to build up index tuples for arrays. |
Class | MGridClass |
nd_grid instance which returns a dense multi-dimensional "meshgrid". |
Class | nd_grid |
Construct a multi-dimensional "meshgrid". |
Class | ndenumerate |
Multidimensional index iterator. |
Class | ndindex |
An N-dimensional iterator object to index arrays. |
Class | OGridClass |
nd_grid instance which returns an open multi-dimensional "meshgrid". |
Class | RClass |
Translates slice objects to concatenation along the first axis. |
Function | _diag_indices_from |
Undocumented |
Function | _fill_diagonal_dispatcher |
Undocumented |
Function | _ix__dispatcher |
Undocumented |
Function | diag_indices |
Return the indices to access the main diagonal of an array. |
Function | diag_indices_from |
Return the indices to access the main diagonal of an n-dimensional array. |
Function | fill_diagonal |
Fill the main diagonal of the given array of any dimensionality. |
Function | ix_ |
Construct an open mesh from multiple sequences. |
Return the indices to access the main diagonal of an array.
This returns a tuple of indices that can be used to access the main
diagonal of an array a
with a.ndim >= 2 dimensions and shape
(n, n, ..., n). For a.ndim = 2 this is the usual diagonal, for
a.ndim > 2 this is the set of indices to access a[i, i, ..., i]
for i = [0..n-1].
diag_indices_from
Create a set of indices to access the diagonal of a (4, 4) array:
>>> di = np.diag_indices(4) >>> di (array([0, 1, 2, 3]), array([0, 1, 2, 3])) >>> a = np.arange(16).reshape(4, 4) >>> a array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11], [12, 13, 14, 15]]) >>> a[di] = 100 >>> a array([[100, 1, 2, 3], [ 4, 100, 6, 7], [ 8, 9, 100, 11], [ 12, 13, 14, 100]])
Now, we create indices to manipulate a 3-D array:
>>> d3 = np.diag_indices(2, 3) >>> d3 (array([0, 1]), array([0, 1]), array([0, 1]))
And use it to set the diagonal of an array of zeros to 1:
>>> a = np.zeros((2, 2, 2), dtype=int) >>> a[d3] = 1 >>> a array([[[1, 0], [0, 0]], [[0, 0], [0, 1]]])
Return the indices to access the main diagonal of an n-dimensional array.
See diag_indices
for full details.
arr : array, at least 2-D
diag_indices
Fill the main diagonal of the given array of any dimensionality.
For an array a
with a.ndim >= 2, the diagonal is the list of
locations with indices a[i, ..., i] all identical. This function
modifies the input array in-place, it does not return a value.
val
is scalar, the value is
written along the diagonal. If array-like, the flattened val
is
written along the diagonal, repeating if necessary to fill all
diagonal entries.diag_indices, diag_indices_from
This functionality can be obtained via diag_indices
, but internally
this version uses a much faster implementation that never constructs the
indices and uses simple slicing.
>>> a = np.zeros((3, 3), int) >>> np.fill_diagonal(a, 5) >>> a array([[5, 0, 0], [0, 5, 0], [0, 0, 5]])
The same function can operate on a 4-D array:
>>> a = np.zeros((3, 3, 3, 3), int) >>> np.fill_diagonal(a, 4)
We only show a few blocks for clarity:
>>> a[0, 0] array([[4, 0, 0], [0, 0, 0], [0, 0, 0]]) >>> a[1, 1] array([[0, 0, 0], [0, 4, 0], [0, 0, 0]]) >>> a[2, 2] array([[0, 0, 0], [0, 0, 0], [0, 0, 4]])
The wrap option affects only tall matrices:
>>> # tall matrices no wrap >>> a = np.zeros((5, 3), int) >>> np.fill_diagonal(a, 4) >>> a array([[4, 0, 0], [0, 4, 0], [0, 0, 4], [0, 0, 0], [0, 0, 0]])
>>> # tall matrices wrap >>> a = np.zeros((5, 3), int) >>> np.fill_diagonal(a, 4, wrap=True) >>> a array([[4, 0, 0], [0, 4, 0], [0, 0, 4], [0, 0, 0], [4, 0, 0]])
>>> # wide matrices >>> a = np.zeros((3, 5), int) >>> np.fill_diagonal(a, 4, wrap=True) >>> a array([[4, 0, 0, 0, 0], [0, 4, 0, 0, 0], [0, 0, 4, 0, 0]])
The anti-diagonal can be filled by reversing the order of elements
using either numpy.flipud
or numpy.fliplr
.
>>> a = np.zeros((3, 3), int); >>> np.fill_diagonal(np.fliplr(a), [1,2,3]) # Horizontal flip >>> a array([[0, 0, 1], [0, 2, 0], [3, 0, 0]]) >>> np.fill_diagonal(np.flipud(a), [1,2,3]) # Vertical flip >>> a array([[0, 0, 3], [0, 2, 0], [1, 0, 0]])
Note that the order in which the diagonal is filled varies depending on the flip function.
Construct an open mesh from multiple sequences.
This function takes N 1-D sequences and returns N outputs with N dimensions each, such that the shape is 1 in all but one dimension and the dimension with the non-unit shape value cycles through all N dimensions.
Using ix_
one can quickly construct index arrays that will index
the cross product. a[np.ix_([1,3],[2,5])] returns the array
[[a[1,2] a[1,5]], [a[3,2] a[3,5]]].
ogrid, mgrid, meshgrid
>>> a = np.arange(10).reshape(2, 5) >>> a array([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]) >>> ixgrid = np.ix_([0, 1], [2, 4]) >>> ixgrid (array([[0], [1]]), array([[2, 4]])) >>> ixgrid[0].shape, ixgrid[1].shape ((2, 1), (1, 2)) >>> a[ixgrid] array([[2, 4], [7, 9]])
>>> ixgrid = np.ix_([True, True], [2, 4]) >>> a[ixgrid] array([[2, 4], [7, 9]]) >>> ixgrid = np.ix_([True, True], [False, False, True, False, True]) >>> a[ixgrid] array([[2, 4], [7, 9]])