8000 Einsum double contraction in particular order causes ValueError · Issue #5147 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content
Einsum double contraction in particular order causes ValueError #5147
Closed
@alexalemi

Description

@alexalemi

If I try to do

a = np.random.randn(3,3,3,3)
ans1 = np.einsum('iijj',a)

I get a ValueError:

>>> ValueError: dimensions in operand 0 for collapsing index 'j' don't match (0 != 3)

However, these alternatives work:

ans2 = np.einsum('ii', np.einsum('ijkk', a))
ans3 = np.einsum('ijij', np.einsum('ijkl->ikjl',a))
ans2 == ans3
>>> True

And these agree with the naive version up to the last decimal place

ans4 = 0.0
for i in xrange(3):
    for j in xrange(3):
        ans4 += a[i,i,j,j]
np.allclose(ans2, ans4) and np.allclose(ans3, ans4)
>>> True

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0