dask 2021.10.0

ParametersRaisesReturnsBackRef
diagonal(a, offset=0, axis1=0, axis2=1)

This docstring was copied from numpy.diagonal.

Some inconsistencies with the Dask version may exist.

If a is 2-D, returns the diagonal of a with the given offset, i.e., the collection of elements of the form a[i, i+offset] . If a has more than two dimensions, then the axes specified by :None:None:`axis1` and :None:None:`axis2` are used to determine the 2-D sub-array whose diagonal is returned. The shape of the resulting array can be determined by removing :None:None:`axis1` and :None:None:`axis2` and appending an index to the right equal to the size of the resulting diagonals.

In versions of NumPy prior to 1.7, this function always returned a new, independent array containing a copy of the values in the diagonal.

In NumPy 1.7 and 1.8, it continues to return a copy of the diagonal, but depending on this fact is deprecated. Writing to the resulting array continues to work as it used to, but a FutureWarning is issued.

Starting in NumPy 1.9 it returns a read-only view on the original array. Attempting to write to the resulting array will produce an error.

In some future release, it will return a read/write view and writing to the returned array will alter your original array. The returned array will have the same type as the input array.

If you don't write to the array returned by this function, then you can just ignore all of the above.

If you depend on the current behavior, then we suggest copying the returned array explicitly, i.e., use np.diagonal(a).copy() instead of just np.diagonal(a) . This will work with both past and future versions of NumPy.

Parameters

a : array_like

Array from which the diagonals are taken.

offset : int, optional

Offset of the diagonal from the main diagonal. Can be positive or negative. Defaults to main diagonal (0).

axis1 : int, optional

Axis to be used as the first axis of the 2-D sub-arrays from which the diagonals should be taken. Defaults to first axis (0).

axis2 : int, optional

Axis to be used as the second axis of the 2-D sub-arrays from which the diagonals should be taken. Defaults to second axis (1).

Raises

ValueError

If the dimension of a is less than 2.

Returns

array_of_diagonals : ndarray

If a is 2-D, then a 1-D array containing the diagonal and of the same type as a is returned unless a is a :None:None:`matrix`, in which case a 1-D array rather than a (2-D) :None:None:`matrix` is returned in order to maintain backward compatibility.

If a.ndim > 2 , then the dimensions specified by :None:None:`axis1` and :None:None:`axis2` are removed, and a new axis inserted at the end corresponding to the diagonal.

Return specified diagonals.

See Also

diag

MATLAB work-a-like for 1-D and 2-D arrays.

diagflat

Create diagonal arrays.

trace

Sum along diagonals.

Examples

This example is valid syntax, but we were not able to check execution
>>> a = np.arange(4).reshape(2,2)  # doctest: +SKIP
... a # doctest: +SKIP array([[0, 1], [2, 3]])
This example is valid syntax, but we were not able to check execution
>>> a.diagonal()  # doctest: +SKIP
array([0, 3])
This example is valid syntax, but we were not able to check execution
>>> a.diagonal(1)  # doctest: +SKIP
array([1])

A 3-D example:

This example is valid syntax, but we were not able to check execution
>>> a = np.arange(8).reshape(2,2,2); a  # doctest: +SKIP
array([[[0, 1],
        [2, 3]],
       [[4, 5],
        [6, 7]]])
This example is valid syntax, but we were not able to check execution
>>> a.diagonal(0,  # Main diagonals of two arrays created by skipping  # doctest: +SKIP
...  0, # across the outer(left)-most axis last and
...  1) # the "middle" (row) axis first. array([[0, 6], [1, 7]])

The sub-arrays whose main diagonals we just obtained; note that each corresponds to fixing the right-most (column) axis, and that the diagonals are "packed" in rows.

This example is valid syntax, but we were not able to check execution
>>> a[:,:,0]  # main diagonal is [0 6]  # doctest: +SKIP
array([[0, 2],
       [4, 6]])
This example is valid syntax, but we were not able to check execution
>>> a[:,:,1]  # main diagonal is [1 7]  # doctest: +SKIP
array([[1, 3],
       [5, 7]])

The anti-diagonal can be obtained by reversing the order of elements using either numpy.flipud or numpy.fliplr .

This example is valid syntax, but we were not able to check execution
>>> a = np.arange(9).reshape(3, 3)  # doctest: +SKIP
... a # doctest: +SKIP array([[0, 1, 2], [3, 4, 5], [6, 7, 8]])
This example is valid syntax, but we were not able to check execution
>>> np.fliplr(a).diagonal()  # Horizontal flip  # doctest: +SKIP
array([2, 4, 6])
This example is valid syntax, but we were not able to check execution
>>> np.flipud(a).diagonal()  # Vertical flip  # doctest: +SKIP
array([6, 4, 2])

Note that the order in which the diagonal is retrieved varies depending on the flip function.

See :

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

dask.array.reductions.trace dask.array.routines.select dask.array.creation.diag dask.array.routines.compress

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


File: /dask/array/creation.py#616
type: <class 'function'>
Commit: