question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Einsum double contraction in particular order causes ValueError

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
mfdgrootcommented, Mar 23, 2018

Could there be a regression of this bug?

a=np.arange(4).reshape(1,1,2,1,2)
np.einsum('bbcdc->d,a)

returns ValueError: dimensions in operand 0 for collapsing index 'd' don't match (1 != 2).

Summing b and c separately works as do the following examples:

a=np.arange(4).reshape(1,1,2,2,1)
np.einsum('bbccd->d,a)

a=np.arange(8).reshape(1,1,2,2,2)
np.eisum('bbcdc->d,a)
0reactions
charriscommented, Mar 24, 2018

@mfdgroot That would be good.

Read more comments on GitHub >

github_iconTop Results From Across the Web

python - Understanding NumPy's einsum - Stack Overflow
Imagine that we have two multi-dimensional arrays, A and B . Now let's suppose we want to... multiply A with B in a...
Read more >
NumPy 1.9.1 Release Notes
The second order behaviour is available via the edge_order keyword ... gh-5147: Einsum double contraction in particular order causes ValueError.
Read more >
numpy/test_einsum.py at main - GitHub
assert_raises(TypeError, np.einsum, "", 0, out='test',. optimize=do_opt). # order parameter must be a valid order. assert_raises(ValueError, np.einsum, ...
Read more >
Source code for opt_einsum.contract
Contains the primary optimization and contraction routines. ... output. dtype : str The dtype of the given contraction, see np.einsum. order : str...
Read more >
A basic introduction to NumPy's einsum - ajcr
sum this new array along particular axes, and/or; transpose the axes of the array in a particular order. Then there's a good chance...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found