Update README.md

Signed-off-by: David Rotermund <54365609+davrot@users.noreply.github.com>
This commit is contained in:
David Rotermund 2023-12-15 11:06:31 +01:00 committed by GitHub
parent 5ef7f2bc22
commit 76897aec23
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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)