假设我有一个np.einsum
执行一些计算的设备,然后将其直接泵入另一个np.einsum
执行其他操作。一般来说,我可以将这两个einsum
s 组合成一个 seinsum
吗?
我的具体用例是我正在做一个转置,一个矩阵乘法,然后是另一个矩阵乘法来计算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)
)