8000 Merge pull request #13316 from AnderUstarroz/added-antidiagonal-examples · pentschev/numpy@37da972 · GitHub
[go: up one dir, main page]

Skip to content

Commit 37da972

Browse files
authored
Merge pull request numpy#13316 from AnderUstarroz/added-antidiagonal-examples
DOC: Added anti-diagonal examples to np.diagonal and np.fill_diagonal
2 parents f5749f9 + e44713c commit 37da972

File tree

2 files changed

+40
-8
lines changed

2 files changed

+40
-8
lines changed

numpy/core/fromnumeric.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1532,23 +1532,38 @@ def diagonal(a, offset=0, axis1=0, axis2=1):
15321532
[2, 3]],
15331533
[[4, 5],
15341534
[6, 7]]])
1535-
>>> a.diagonal(0, # Main diagonals of two arrays created by skipping
1536-
... 0, # across the outer(left)-most axis last and
1537-
... 1) # the "middle" (row) axis first.
1535+
>>> a.diagonal(0, # Main diagonals of two arrays created by skipping
1536+
... 0, # across the outer(left)-most axis last and
1537+
... 1) # the "middle" (row) axis first.
15381538
array([[0, 6],
15391539
[1, 7]])
15401540
15411541
The sub-arrays whose main diagonals we just obtained; note that each
15421542
corresponds to fixing the right-most (column) axis, and that the
15431543
diagonals are "packed" in rows.
15441544
1545-
>>> a[:,:,0] # main diagonal is [0 6]
1545+
>>> a[:,:,0] # main diagonal is [0 6]
15461546
array([[0, 2],
15471547
[4, 6]])
1548-
>>> a[:,:,1] # main diagonal is [1 7]
1548+
>>> a[:,:,1] # main diagonal is [1 7]
15491549
array([[1, 3],
15501550
[5, 7]])
15511551
1552+
The anti-diagonal can be obtained by reversing the order of elements
1553+
using either `numpy.flipud` or `numpy.fliplr`.
1554+
1555+
>>> a = np.arange(9).reshape(3, 3)
1556+
>>> a
1557+
array([[0, 1, 2],
1558+
[3, 4, 5],
1559+
[6, 7, 8]])
1560+
>>> np.fliplr(a).diagonal() # Horizontal flip
1561+
array([2, 4, 6])
1562+
>>> np.flipud(a).diagonal() # Vertical flip
1563+
array([6, 4, 2])
1564+
1565+
Note that the order in which the diagonal is retrieved varies depending
1566+
on the flip function.
15521567
"""
15531568
if isinstance(a, np.matrix):
15541569
# Make diagonal of matrix 1-D to preserve backward compatibility.

numpy/lib/index_tricks.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,7 @@ def fill_diagonal(a, val, wrap=False):
813813
The wrap option affects only tall matrices:
814814
815815
>>> # tall matrices no wrap
816-
>>> a = np.zeros((5, 3),int)
816+
>>> a = np.zeros((5, 3), int)
817817
>>> np.fill_diagonal(a, 4)
818818
>>> a
819819
array([[4, 0, 0],
@@ -823,7 +823,7 @@ def fill_diagonal(a, val, wrap=False):
823823
[0, 0, 0]])
824824
825825
>>> # tall matrices wrap
826-
>>> a = np.zeros((5, 3),int)
826+
>>> a = np.zeros((5, 3), int)
827827
>>> np.fill_diagonal(a, 4, wrap=True)
828828
>>> a
829829
array([[4, 0, 0],
@@ -833,13 +833,30 @@ def fill_diagonal(a, val, wrap=False):
833833
[4, 0, 0]])
834834
835835
>>> # wide matrices
836-
>>> a = np.zeros((3, 5),int)
836+
>>> a = np.zeros((3, 5), int)
837837
>>> np.fill_diagonal(a, 4, wrap=True)
838838
>>> a
839839
array([[4, 0, 0, 0, 0],
840840
[0, 4, 0, 0, 0],
841841
[0, 0, 4, 0, 0]])
842842
843+
The anti-diagonal can be filled by reversing the order of elements
844+
using either `numpy.flipud` or `numpy.fliplr`.
845+
846+
>>> a = np.zeros((3, 3), int);
847+
>>> np.fill_diagonal(np.fliplr(a), [1,2,3]) # Horizontal flip
848+
>>> a
849+
array([[0, 0, 1],
850+
[0, 2, 0],
851+
[3, 0, 0]])
852+
>>> np.fill_diagonal(np.flipud(a), [1,2,3]) # Vertical flip
853+
>>> a
854+
array([[0, 0, 3],
855+
[0, 2, 0],
856+
[1, 0, 0]])
857+
858+
Note that the order in which the diagonal is filled varies depending
859+
on the flip function.
843860
"""
844861
if a.ndim < 2:
845862
raise ValueError("array must be at least 2-d")

0 commit comments

Comments
 (0)
0