Update README.md

Signed-off-by: David Rotermund <54365609+davrot@users.noreply.github.com>
This commit is contained in:
David Rotermund 2024-01-02 17:10:30 +01:00 committed by GitHub
parent f44f21ce1b
commit 2285e996aa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -10,6 +10,77 @@
Questions to [David Rotermund](mailto:davrot@uni-bremen.de)
## Examples
$$\sum_i a_{i,i}$$
```python
import numpy as np
a = np.arange(0, 25).reshape(5, 5)
print(a)
print(a.shape) # -> (5, 5)
print(np.einsum("ii", a)) # -> 60
```
Output:
```python
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]
[20 21 22 23 24]]
```
$$b(i) = a_{i,i}$$
```python
import numpy as np
a = np.arange(0, 25).reshape(5, 5)
print(np.einsum("ii->i", a)) # -> [ 0 6 12 18 24]
```
$$b(i) = \sum_j a_{i,j}$$
```python
import numpy as np
a = np.arange(0, 25).reshape(5, 5)
print(np.einsum("ij->i", a)) # -> [ 10 35 60 85 110]
print(np.einsum("...j->...", a)) # -> [ 10 35 60 85 110]
```
$$b(j,i) = a_{i,j}$$
```python
import numpy as np
a = np.arange(0, 25).reshape(5, 5)
print(a)
print()
print(np.einsum("ij->ji", a))
```
Output:
```python
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]
[20 21 22 23 24]]
[[ 0 5 10 15 20]
[ 1 6 11 16 21]
[ 2 7 12 17 22]
[ 3 8 13 18 23]
[ 4 9 14 19 24]]
```
## [numpy.einsum](https://numpy.org/doc/stable/reference/generated/numpy.einsum.html)