Proving Properties of Matrices (Associativity) using np.einsum

Viewed 43

I'm experimenting with np.einsum and I was wondering if there's a way to prove Associativity just using np.einsum.

Here's the data:

A = np.array([[1, 1, 1],
              [2, 2, 2],
              [5, 5, 5]])

B = np.array([[0, 1, 0],
              [1, 1, 0],
              [1, 1, 1]])

C = np.array([[ 6,  4,  2],
              [-2,  0,  2],
              [ 3,  2,  1]])

Here's what I have tried:

D = np.einsum('il, lj', A, B).dot(C)

E = A.dot(np.einsum('il, lj', B, C))

(D == E).all() # True

Is it possible (and best?) to compute D and E respectively in one np.einsum operation? A lot of the examples I can find use two matrices, but I know it's possible to multiply multiple matrices together using np.einsum from this post np.einsum performance of 4 matrix multiplications, I just can't seem to figure out how that post relates to what I want to achieve. Thanks:)

0 Answers
Related