https://github.com/pseewald/fortran-einsum-example
A generic implementation of tensor einsum in Fortran.
https://github.com/pseewald/fortran-einsum-example
einsum fortran generic-programming tensor
Last synced: 3 months ago
JSON representation
A generic implementation of tensor einsum in Fortran.
- Host: GitHub
- URL: https://github.com/pseewald/fortran-einsum-example
- Owner: pseewald
- Created: 2020-07-01T14:01:19.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2021-01-29T20:11:11.000Z (over 4 years ago)
- Last Synced: 2025-01-15T10:55:27.736Z (5 months ago)
- Topics: einsum, fortran, generic-programming, tensor
- Language: Fortran
- Homepage:
- Size: 92.8 KB
- Stars: 26
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# fortran-einsum-example
This project goes back to a talk I gave at the [FortranCon conference 2020](https://tcevents.chem.uzh.ch/event/12/). It demonstrates how to develop generic APIs in Fortran by making use of polymorphism and generic programming techniques. This specific example implements a tensor contraction / Einstein summation API. To learn more have a look at the [presentation slides](presentation.pdf).
## Examples
sum(k) A(ijk) B(kj) = C(i)
```Fortran
class(tensor), allocatable :: a, b, creal, dimension(:,:,:), allocatable :: data_a
real, dimension(:,:), allocatable :: data_b! ... allocate and assign data_a, data_b ...
a = tensor(data_a)
b = tensor(data_b)c = tensor_einsum( &
a, [1,2,3], b, [3,2], [1])
```
sum(ij) A(ijkl) B(jim) = C(mkl)
```Fortran
class(tensor), allocatable :: a, b, cinteger, dimension(:,:,:,:), allocatable :: data_a
integer, dimension(:,:,:), allocatable :: data_b! ... allocate and assign data_a, data_b ...
a = tensor(data_a)
b = tensor(data_b)c = tensor_einsum( &
a, [1,2,3,4], b, [2,1,5], [5,3,4])
```A working example that can be compiled and run is [included](tensor_example.f90).
## Dependencies
* [Fypp](https://github.com/aradi/fypp) preprocessor