Suppose that I have a np.einsum
that performs some calculation, and then pump that directly into yet another np.einsum
to do some other thing. Can I, in general, compose those two einsum
s into a single einsum
?
My specific use case is that I am doing a transpose, a matrix multiplication, and then another matrix multiplication to compute b a^T a
:
import numpy as np
from numpy import array
a = array([[1, 2],
[3, 4]])
b = array([[1, 2],
[3, 4],
[5, 6]])
matrix_multiply_by_transpose = 'ij,kj->ik'
matrix_multiply = 'ij,jk->ik'
test_answer = np.einsum(matrix_multiply,
np.einsum(matrix_multiply_by_transpose,
b, a
),
a
)
assert np.array_equal(test_answer,
np.einsum(an_answer_to_this_question, b, a, a))
#or, the ultimate most awesomest answer ever, if such a thing even exists
assert np.array_equal(test_answer,
np.einsum(the_bestest_answer(matrix_multiply_by_transpose, matrix_multiply),
b, a, a)
)