diff --git a/numpy/einstein/README.md b/numpy/einstein/README.md index ef47b28..61b8cbc 100644 --- a/numpy/einstein/README.md +++ b/numpy/einstein/README.md @@ -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)