From 76897aec23d6a6259352fa33fbf18f2cd5704df2 Mon Sep 17 00:00:00 2001 From: David Rotermund <54365609+davrot@users.noreply.github.com> Date: Fri, 15 Dec 2023 11:06:31 +0100 Subject: [PATCH] Update README.md Signed-off-by: David Rotermund <54365609+davrot@users.noreply.github.com> --- numpy/ndarray/README.md | 51 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/numpy/ndarray/README.md b/numpy/ndarray/README.md index a1b4e16..bd1430b 100644 --- a/numpy/ndarray/README.md +++ b/numpy/ndarray/README.md @@ -776,6 +776,57 @@ print(np.round(2.5)) # -> 2.0 print(np.round(2.5 + 1e-15)) # -> 3.0 ``` +## [numpy.ndarray.trace](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.trace.html) + +```python +ndarray.trace(offset=0, axis1=0, axis2=1, dtype=None, out=None) +``` + +> Return the sum along diagonals of the array. + +```python +import numpy as np + +A = np.eye(3) + +print(A) +print(A.trace()) # -> 3.0 +``` + +```python +[[1. 0. 0.] + [0. 1. 0.] + [0. 0. 1.]] +``` + +## [numpy.ndarray.diagonal](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.diagonal.html) + +```python +ndarray.diagonal(offset=0, axis1=0, axis2=1) +``` + +> Return specified diagonals. In NumPy 1.9 the returned array is a read-only view instead of a copy as in previous NumPy versions. In a future version the read-only restriction will be removed. + +```python +import numpy as np + +rng = np.random.default_rng() +A = rng.random((3, 3)) +print(A) +print(A.diagonal()) # -> [0.7434178 0.11672896] +print(A.diagonal(offset=1)) # -> [0.7434178 0.11672896] +print(A.diagonal(offset=2)) # -> [0.84915636] +print(A.diagonal(offset=-1)) # -> [0.10826248 0.50223328] +print(A.diagonal(offset=-2)) # -> [0.43068892] +``` + +Output + +```python +[[0.82574583 0.7434178 0.84915636] + [0.10826248 0.39898052 0.11672896] + [0.43068892 0.50223328 0.63444263]] +``` ## [Array methods](https://numpy.org/doc/stable/reference/arrays.ndarray.html#array-methods)