Fixed #5147 collapsingbug in einsum

At tests is also included that fails without the fix.
This test is erroring in Py3K, because `xrange` is undefined there, change these two to `range` and it should be OK.