Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Einsum Expression works in numpy but not TensorOperations #195

Closed
ejmeitz opened this issue Oct 25, 2024 · 2 comments
Closed

Einsum Expression works in numpy but not TensorOperations #195

ejmeitz opened this issue Oct 25, 2024 · 2 comments

Comments

@ejmeitz
Copy link

ejmeitz commented Oct 25, 2024

I have the following contraction that I am trying to implement in TensorOperations, but without luck.

@tensor K[n,m] := P[i,j,α,β] * e[n,α] * e[m,β] * E[n,i] * E[m,j]

I believe the issue is because the n and m indices have three copies. I thought this was fine since they are the free indices. The error I get is "non-matching indices between left and right hand side"

Using the numpy einsum macro things work fine. Is this a limitation of TensorOperations or do I need to somehow re-frame my contraction? Thanks!

import numpy as np
N, L, A, B = 5, 4, 3, 3

P = np.random.rand(L, L, A, B)
e = np.random.rand(N, A)
E = np.random.rand(N, L)

K = np.einsum('ijab,na,mb,ni,mj->nm', P, e, e, E, E)
@Jutho
Copy link
Owner

Jutho commented Oct 25, 2024

Yes, TensorOperations.jl is strict in only admitting open indices that appear once on the right hand side, or thus, in general, every index should only appear twice (either once left and once right, or two times right for a contraction): that is really the original Einstein summation convention. All other combinations with indices appearing more than two times, while sometimes useful, do not map directly onto matrix multiplication (e.g. more like batched matrix multiplication) and are, from a more abstract perspective, operations that are very much specific to a chosen basis.

Note that there is also OMEinsum.jl or Tullio.jl, which do support these more general flavors of einsum expressions.

@ejmeitz
Copy link
Author

ejmeitz commented Oct 25, 2024

Makes sense, thanks!

@Jutho Jutho closed this as completed Oct 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants