def einsum(string, *args):
    index_groups = map(list, string.split(','))
assert len(index_groups) == len(args)
tensor_indices_tuples = zip(index_groups, args)
return reduce(einsum_for_two, tensor_indices_tuples)[1]

def einsum_for_two(tensor_indices1, tensor_indices2):
    string1, tensor1 = tensor_indices1
string2, tensor2 = tensor_indices2
sum_over_indices = set(string1).intersection(set(string2))
new_string = string1 + string2
axes = [], []
for i in sum_over_indices:
    new_string.remove(i)
new_string.remove(i)
axes[0].append(string1.index(i))
axes[1].append(string2.index(i))
return new_string, np.tensordot(tensor1, tensor2, axes)
Как мне заставить numpy.einsum хорошо играть с sympy?