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

ENH: @ operator differs from the convention for tensor dot #26438

Closed
WenyinWei opened this issue May 15, 2024 · 3 comments
Closed

ENH: @ operator differs from the convention for tensor dot #26438

WenyinWei opened this issue May 15, 2024 · 3 comments
Labels
33 - Question Question about NumPy usage or development 57 - Close? Issues which may be closable unless discussion continued

Comments

@WenyinWei
Copy link

Describe the issue:

Hi, the current @ operator has np.matmul function underlying, which means if $A$ or $B$ is multi-dimensional, C = A@B is equivalent to $C_{ijm} = \sum_{k} A_{ijk} B_{ikm}$. However, for multi-dimensional array, is would be more convenient and intuitive if it can somewhat imitate the behavior of tensor dots like using C = A@@B to calculate $C_{ij} = \sum_{m,n} A_{imn} B_{nmj}$. Is it possible to overload functions for such @@@... operators?

Reproduce the code example:

import numpy as np
testA, testB = np.random.rand(5,3,4), np.random.rand(5,4,3)
np.allclose( testA @ testB, np.einsum("ijk,ikm->ijm", testA, testB  )  )

Error message:

No response

Python and NumPy Versions:

1.26.4
3.11.9 (tags/v3.11.9:de54cf5, Apr 2 2024, 10:12:12) [MSC v.1938 64 bit (AMD64)]

Runtime Environment:

No response

Context for the issue:

No response

@WenyinWei WenyinWei changed the title BUG: @ operator differs from the convention for tensor dot ENH: @ operator differs from the convention for tensor dot May 15, 2024
@ngoldbaum
Copy link
Member

See numpy.vecdot. Of course that doesn't help someone wanting to use @ like you'd like, but changing it would also almost certainly break user code so there would need to be a deprecation period, a period where using @ in this way is a hard error, and then finally @ is an alias for vecdot. Is the change worth the disruption?

@seberg
Copy link
Member

seberg commented May 15, 2024

@ was proposed in the PEP to be useful for stacks of matrices, I don't think we should touch it and I would suspect that the stack of matrices use case is more common anyway.

Changing/adding operators in Python will not be easy, matmul was one of the few things that happened.

@seberg seberg added 33 - Question Question about NumPy usage or development 57 - Close? Issues which may be closable unless discussion continued and removed 00 - Bug labels May 15, 2024
@ngoldbaum
Copy link
Member

Also see np.einsum, which lets you write the contraction you want explicitly using Einstein notation. Closing since there's nothing really numpy can do to add more operators to Python and the operation the reporter was looking for is possible with a different spelling.

@ngoldbaum ngoldbaum closed this as not planned Won't fix, can't repro, duplicate, stale May 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
33 - Question Question about NumPy usage or development 57 - Close? Issues which may be closable unless discussion continued
Projects
None yet
Development

No branches or pull requests

3 participants