Implementing Matrix Multiplication Algorithms Discovered By DeepMind’s AlphaTensor

Last week there was a lot of excitement when DeepMind’s AlphaTensor discovered some new algorithms for doing matrix multiplication. Implementing these algorithms in Python should have been a trivial task but I could not find any source code. After a great deal of effort I was able to write some code to demonstrate at least one of the new algorithms.

If you download the PDF from the Nature article you will find the algorithm for multiplying 4 × 4 matrices in modular arithmetic (Z2) with 47 multiplications on page 12 and the algorithm for multiplying 4 × 5 by 5 × 5 matrices in standard arithmetic with 76 multiplications on page 13.

I was unable to implement the algorithm for multiplying 4×4 matrices because they used modular arithmetic. Now there is nothing complicated about modular arithmetic but I just could not figure out how it applied to the equations they provided. Fortunately the other algorithm used standard arithmetic so there was nothing difficult about that. It was tedious to copy their equations because they provided them in an image file. I had to use OCR to convert the image of the equations to text. Since there are 76 multiplications for the 4 × 5 by 5 × 5 matrices it was a lot of work.

The 76 multiplications are:

H1 = a3,2 * (-b2,1 - b2,5 - b3,1)
H2 = (a2,2 + a2,5 - a3,5) * (-b2,5 - b5,1)
H3 = (-a3,1 - a4,1 + a4,2) * (-b1,1 + b2,5)
H4 = (a1,2 + a1,4 + a3,4) * (-b2,5 - b4,1)
H5 = (a1,5 + a2,2 + a2,5) * (-b2,4 + b5,1)
H6 = (-a2,2 - a2,5 - a4,5) * (b2,3 + b5,1)
H7 = (-a1,1 + a4,1 - a4,2) * (b1,1 + b2,4) 
H8 = (a3,2 - a3,3 - a4,3) * (-b2,3 + b3,1)
H9 = (-a1,2 - a1,4 + a4,4) * (b2,3 + b4,1)
H10 = (a2,2 + a2,5) * b5,1 
H11 = (-a2,1 - a4,1 + a4,2) * (-b1,1 + b2,2)
H12 = (a4,1 - a4,2) * b1,1 
H13 = (a1,2 + a1,4 + a2,4) * (b2,2 + b4,1)
H14 = (a1,3 - a3,2 + a3,3) * (b2,4 + b3,1) 
H15 = (-a1,2 - a1,4) * b4,1 
H16 = (-a3,2 + a3,3) * b3,1
H17 = (a1,2 + a1,4 - a2,1 + a2,2 - a2,3 + a2,4 - a3,2 + a3,3 - a4,1 + a4,2) * b2,2
H18 = a2,1 * (b1,1 + b1,2 + b5,2)
H19 = -a2,3 * (b3,1 + b3,2 + b5,2)
H20 = (-a1,5 + a2,1 + a2,3 - a2,5) * (-b1,1 - b1,2 + b1,4 - b5,2)
H21 = (a2,1 + a2,3 - a2,5) * b5,2
H22 = (a1,3 - a1,4 - a2,4) * (b1,1 + b1,2 - b1,4 - b3,1 - b3,2 + b3,4 + b4,4)
H23 = a1,3 * (-b3,1 + b3,4 + b4,4)
H24 = a1,5 * (-b4,4 - b5,1 + b5,4)
H25 = -a1,1 * (b1,1 - b1,4)
H26 = (-a1,3 + a1,4 + a1,5) * b4,4
H27 = (a1,3 - a3,1 + a3,3) * (b1,1 - b1,4 + b1,5 + b3,5)
H28 = -a3,4 * (-b3,5 - b4,1 - b4,5)
H29 = a3,1 * (b1,1 + b1,5 + b3,5)
H30 = (a3,1 - a3,3 + a3,4) * b3,5
H31 = (-a1,4 - a1,5 - a3,4) * (-b4,4 - b5,1 + b5,4 - b5,5)
H32 = (a2,1 + a4,1 + a4,4) * (b1,3 - b4,1 - b4,2 - b4,3)
H33 = a4,3 * (-b3,1 - b3,3)
H34 = a4,4 * (-b1,3 + b4,1 + b4,3)
H35 = -a4,5 * (b1,3 + b5,1 + b5,3)
H36 = (a2,3 - a2,5 - a4,5) * (b3,1 + b3,2 + b3,3 + b5,2)
H37 = (-a4,1 - a4,4 + a4,5) * b1,3
H38 = (-a2,3 - a3,1 + a3,3 - a3,4) * (b3,5 + b4,1 + b4,2 + b4,5)
H39 = (-a3,1 - a4,1 - a4,4 + a4,5) * (b1,3 + b5,1 + b5,3 + b5,5)
H40 = (-a1,3 + a1,4 + a1,5 - a4,4) * (-b3,1 - b3,3 + b3,4 + b4,4)
H41 = (-a1,1 + a4,1 - a4,5) * (b1,3 + b3,1 + b3,3 - b3,4 + b5,1 + b5,3 - b5,4)
H42 = (-a2,1 + a2,5 - a3,5) * (-b1,1 - b1,2 - b1,5 + b4,1 + b4,2 + b4,5 - b5,2)
H43 = a2,4 * (b4,1 + b4,2)
H44 = (a2,3 + a3,2 - a3,3) * (b2,2 - b3,1)
H45 = (-a3,3 + a3,4 - a4,3) * (b3,5 + b4,1 + b4,3 + b4,5 + b5,1 + b5,3 + b5,5)
H46 = -a3,5 * (-b5,1 -b5,5)
H47 = (a2,1 - a2,5 - a3,1 + a3,5) * (b1,1 + b1,2 + b1,5 - b4,1 - b4,2 - b4,5)
H48 = (-a2,3 + a3,3) * (b2,2 + b3,2 + b3,5 + b4,1 + b4,2 + b4,5)
H49 = (-a1,1 - a1,3 + a1,4 + a1,5 - a2,1 - a2,3 + a2,4 + a2,5) * (-b1,1 - b1,2 + b1,4)
H50 = (-a1,4 - a2,4) * (b2,2 - b3,1 - b3,2 + b3,4 - b4,2 + b4,4)
H51 = a2,2 * (b2,1 + b2,2 - b5,1)
H52 = a4,2 * (b1,1 + b2,1 + b2,3)
H53 = -a1,2 * (-b2,1 + b2,4 + b4,1)
H54 = (a1,2 + a1,4 - a2,2 - a2,5 - a3,2 + a3,3 - a4,2 + a4,3 - a4,4 - a4,5) * b2,3
H55 = (a1,4 - a4,4) * (-b2,3 + b3,1 + b3,3 - b3,4 + b4,3 - b4,4)
H56 = (a1,1 - a1,5 - a4,1 + a4,5) * (b3,1 + b3,3 - b3,4 + b5,1 + b5,3 - b5,4)
H57 = (-a3,1 - a4,1) * (-b1,3 - b1,5 - b2,5 - b5,1 - b5,3 - b5,5)
H58 = (-a1,4 - a1,5 - a3,4 - a3,5) * (-b5,1 + b5,4 - b5,5)
H59 = (-a3,3 + a3,4 - a4,3 + a4,4) * (b4,1 + b4,3 + b4,5 + b5,1 + b5,3 + b5,5)
H60 = (a2,5 + a4,5) * (b2,3 - b3,1 - b3,2 - b3,3 - b5,2 - b5,3)
H61 = (a1,4 + a3,4) * (b1,1 - b1,4 + b1,5 - b2,5 - b4,4 + b4,5 - b5,1 + b5,4 - b5,5)
H62 = (a2,1 + a4,1) * (b1,2 + b1,3 + b2,2 - b4,1 - b4,2 - b4,3)
H63 = (-a3,3 - a4,3) * (-b2,3 - b3,3 - b3,5 - b4,1 - b4,3 - b4,5)
H64 = (a1,1 - a1,3 - a1,4 + a3,1 - a3,3 - a3,4) * (b1,1 - b1,4 + b1,5)
H65 = (-a1,1 + a4,1) * (-b1,3 + b1,4 + b2,4 - b5,1 - b5,3 + b5,4)
H66 = (a1,1 - a1,2 + a1,3 - a1,5 - a2,2 - a2,5 - a3,2 + a3,3 - a4,1 + a4,2) * b2,4
H67 = (a2,5 - a3,5) * (b1,1 + b1,2 + b1,5 - b2,5 - b4,1 - b4,2 - b4,5 + b5,2 + b5,5)
H68 = (a1,1 + a1,3 - a1,4 - a1,5 - a4,1 - a4,3 + a4,4 + a4,5) * (-b3,1 - b3,3 + b3,4)
H69 = (-a1,3 + a1,4 - a2,3 + a2,4) * (-b2,4 - b3,1 - b3,2 + b3,4 - b5,2 + b5,4)
H70 = (a2,3 - a2,5 + a4,3 - a4,5) * (-b3,1 - b3,2 - b3,3)
H71 = (-a3,1 + a3,3 - a3,4 + a3,5 - a4,1 + a4,3 - a4,4 + a4,5) * (-b5,1 - b5,3 - b5,5)
H72 = (-a2,1 - a2,4 - a4,1 - a4,4) * (b4,1 + b4,2 + b4,3)
H73 = (a1,3 - a1,4 - a1,5 + a2,3 - a2,4 - a2,5) * (b1,1 + b1,2 - b1,4 + b2,4 + b5,2 - b5,4)
H74 = (a2,1 - a2,3 + a2,4 - a3,1 + a3,3 - a3,4) * (b4,1 + b4,2 + b4,5)
H75 = - (a1,2 + a1,4 - a2,2 - a2,5 - a3,1 + a3,2 + a3,4 + a3,5 - a4,1 + a4,2) * b2,5
H76 = (a1,3 + a3,3) * (-b1,1 + b1,4 - b1,5 + b2,4 + b3,4 - b3,5)

These calculations are used to get the numbers for the 4×5 matrix that is the result of the multiplication:

C1,1 = -H10 + H12 + H14 - H15 - H16 + H53 + H5 - H66 - H7
C2,1 = H10 + H11 - H12 + H13 + H15 + H16 - H17 - H44 + H51
C3,1 = H10 - H12 + H15 + H16 - H1 + H2 + H3 - H4 + H75
C4,1 = -H10 + H12 - H15 - H16 + H52 + H54 - H6 - H8 + H9
C1,2 = H13 + H15 + H20 + H21 - H22 + H23 + H25 - H43 + H49 + H50
C2,2 = -H11 + H12 - H13 - H15 - H16 + H17 + H18 - H19 - H21 + H43 + H44
C3,2 = -H16 - H19 - H21 - H28 - H29 - H38 + H42 + H44 - H47 + H48
C4,2 = H11 - H12 - H18 + H21 - H32 + H33 - H34 - H36 + H62 - H70 
C1,3 = H15 + H23 + H24 + H34 - H37 + H40 - H41 + H55 - H56 - H9
C2,3 = -H10 + H19 + H32 + H35 + H36 + H37 - H43 - H60 - H6 - H72
C3,3 = -H16 - H28 + H33 + H37 - H39 + H45 - H46 + H63 - H71 - H8 
C4,3 = H10 + H15 + H16 - H33 + H34 - H35 - H37 - H54 + H6 + H8 - H9
C1,4 = -H10 + H12 + H14 - H16 + H23 + H24 + H25 + H26 + H5 - H66 - H7 
C2,4 = H10 + H18 - H19 + H20 - H22 - H24 - H26 - H5 - H69 + H73 
C3,4 = -H14 + H16 - H23 - H26 + H27 + H29 + H31 + H46 - H58 + H76
C4,4 = H12 + H25 + H26 - H33 - H35 - H40 + H41 + H65 - H68 - H7 
C1,5 = H15 + H24 + H25 + H27 - H28 + H30 + H31 - H4 + H61 + H64 
C2,5 = -H10 - H18 - H2 - H30 - H38 + H42 - H43 + H46 + H67 + H74
C3,5 = -H10 + H12 - H15 + H28 + H29 - H2 - H30 - H3 + H46 + H4 - H75
C4,5 = -H12 - H29 + H30 - H34 + H35 + H39 + H3 - H45 + H57 + H59

The following Python code is an implementation of this algorithm using 76 multiplications to get 20 numbers for the matrix:

import numpy as np

# 4x5 matrix
A = [[3, -1, 7, 3, 9],
      [-2, 2, -2, 7, 5],
      [-5, 9, 3, 3, 5],
      [-2, 6, 6, 3, 7]]
    
 # 5x5 matrix 
B = [[1, 1, 0, 4, 0],
      [0, 0, 0, 1, 2],
      [1, 2, 1, 1, 3],
      [4, 0, 2, 0, 3],
      [0, 4, 0,1, 0]]

print(np.matrix(A))
print('----------')
print("The Rank of Matrix A: ", np.linalg.matrix_rank(A))
print('----------')
print(np.matrix(B))
print('----------')
print("The Rank of Matrix B: ", np.linalg.matrix_rank(B))
print('----------')
#print(np.dot(A,B)) 


def AlphaTensor4x5by5x5(A, B):
    H1 = np.array(A) [2,1] * (-np.array(B) [1,0] - np.array(B) [1,4] - np.array(B) [2,0])
    H2 = (np.array(A) [1,1] + np.array(A) [1,4] - np.array(A) [2,4]) * (-np.array(B) [1,4] - np.array(B) [4,0])
    H3 = (-np.array(A) [2,0] - np.array(A) [3,0] + np.array(A) [3,1]) * (-np.array(B) [0,0] + np.array(B) [1,4])
    H4 = (np.array(A) [0,1] + np.array(A) [0,3] + np.array(A) [2,3]) * (-np.array(B) [1,4] - np.array(B) [3,0])
    H5 = (np.array(A) [0,4] + np.array(A) [1,1] + np.array(A) [1,4]) * (-np.array(B) [1,3] + np.array(B) [4,0])
    H6 = (-np.array(A) [1,1] - np.array(A) [1,4] - np.array(A) [3,4]) * (np.array(B) [1,2] + np.array(B) [4,0])
    H7 = (-np.array(A) [0,0] + np.array(A) [3,0] - np.array(A) [3,1]) * (np.array(B) [0,0] + np.array(B) [1,3])
    H8 = (np.array(A) [2,1] - np.array(A) [2,2] - np.array(A) [3,2]) * (-np.array(B) [1,2] + np.array(B) [2,0])
    H9 = (-np.array(A) [0,1] - np.array(A) [0,3] + np.array(A) [3,3]) * (np.array(B) [1,2] + np.array(B) [3,0])
    H10 = (np.array(A) [1,1] + np.array(A) [1,4]) * np.array(B) [4,0]
    H11 = (-np.array(A) [1,0] - np.array(A) [3,0] + np.array(A) [3,1]) * (-np.array(B) [0,0] + np.array(B) [1,1])
    H12 = (np.array(A) [3,0] - np.array(A) [3,1]) * np.array(B) [0,0]
    H13 = (np.array(A) [0,1] + np.array(A) [0,3] + np.array(A) [1,3]) * (np.array(B) [1,1] + np.array(B) [3,0])
    H14 = (np.array(A) [0,2] - np.array(A) [2,1] + np.array(A) [2,2]) * (np.array(B) [1,3] + np.array(B) [2,0])
    H15 = (-np.array(A) [0,1] - np.array(A) [0,3]) * np.array(B) [3,0]
    H16 = (-np.array(A) [2,1] + np.array(A) [2,2]) * np.array(B) [2,0]
    H17 = (np.array(A) [0,1] + np.array(A) [0,3] - np.array(A) [1,0] + np.array(A) [1,1] - np.array(A) [1,2] + np.array(A) [1,3] - np.array(A) [2,1] + np.array(A) [2,2] - np.array(A) [3,0] + np.array(A) [3,1]) * np.array(B) [1,1]
    H18 = np.array(A) [1,0] * (np.array(B) [0,0] + np.array(B) [0,1] + np.array(B) [4,1])
    H19 = -np.array(A) [1,2] * (np.array(B) [2,0] + np.array(B) [2,1] + np.array(B) [4,1])
    H20 = (-np.array(A) [0,4] + np.array(A) [1,0] + np.array(A) [1,2] - np.array(A) [1,4]) * (-np.array(B) [0,0] - np.array(B) [0,1] + np.array(B) [0,3] - np.array(B) [4,1])
    H21 = (np.array(A) [1,0] + np.array(A) [1,2] - np.array(A) [1,4]) * np.array(B) [4,1]
    H22 = (np.array(A) [0,2] - np.array(A) [0,3] - np.array(A) [1,3]) * (np.array(B) [0,0] + np.array(B) [0,1] - np.array(B) [0,3] - np.array(B) [2,0] - np.array(B) [2,1] + np.array(B) [2,3] + np.array(B) [3,3])
    H23 = np.array(A) [0,2] * (-np.array(B) [2,0] + np.array(B) [2,3] + np.array(B) [3,3])
    H24 = np.array(A) [0,4] * (-np.array(B) [3,3] - np.array(B) [4,0] + np.array(B) [4,3])
    H25 = -np.array(A) [0,0] * (np.array(B) [0,0] - np.array(B) [0,3])
    H26 = (-np.array(A) [0,2] + np.array(A) [0,3] + np.array(A) [0,4]) * np.array(B) [3,3]
    H27 = (np.array(A) [0,2] - np.array(A) [2,0] + np.array(A) [2,2]) * (np.array(B) [0,0] - np.array(B) [0,3] + np.array(B) [0,4] + np.array(B) [2,4])
    H28 = -np.array(A) [2,3] * (-np.array(B) [2,4] - np.array(B) [3,0] - np.array(B) [3,4])
    H29 = np.array(A) [2,0] * (np.array(B) [0,0] + np.array(B) [0,4] + np.array(B) [2,4])
    H30 = (np.array(A) [2,0] - np.array(A) [2,2] + np.array(A) [2,3]) * np.array(B) [2,4]
    H31 = (-np.array(A) [0,3] - np.array(A) [0,4] - np.array(A) [2,3]) * (-np.array(B) [3,3] - np.array(B) [4,0] + np.array(B) [4,3] - np.array(B) [4,4])
    H32 = (np.array(A) [1,0] + np.array(A) [3,0] + np.array(A) [3,3]) * (np.array(B) [0,2] - np.array(B) [3,0] - np.array(B) [3,1] - np.array(B) [3,2])
    H33 = np.array(A) [3,2] * (-np.array(B) [2,0] - np.array(B) [2,2])
    H34 = np.array(A) [3,3] * (-np.array(B) [0,2] + np.array(B) [3,0] + np.array(B) [3,2])
    H35 = -np.array(A) [3,4] * (np.array(B) [0,2] + np.array(B) [4,0] + np.array(B) [4,2])
    H36 = (np.array(A) [1,2] - np.array(A) [1,4] - np.array(A) [3,4]) * (np.array(B) [2,0] + np.array(B) [2,1] + np.array(B) [2,2] + np.array(B) [4,1])
    H37 = (-np.array(A) [3,0] - np.array(A) [3,3] + np.array(A) [3,4]) * np.array(B) [0,2]
    H38 = (-np.array(A) [1,2] - np.array(A) [2,0] + np.array(A) [2,2] - np.array(A) [2,3]) * (np.array(B) [2,4] + np.array(B) [3,0] + np.array(B) [3,1] + np.array(B) [3,4])
    H39 = (-np.array(A) [2,0] - np.array(A) [3,0] - np.array(A) [3,3] + np.array(A) [3,4]) * (np.array(B) [0,2] + np.array(B) [4,0] + np.array(B) [4,2] + np.array(B) [4,4])
    H40 = (-np.array(A) [0,2] + np.array(A) [0,3] + np.array(A) [0,4] - np.array(A) [3,3]) * (-np.array(B) [2,0] - np.array(B) [2,2] + np.array(B) [2,3] + np.array(B) [3,3])
    H41 = (-np.array(A) [0,0] + np.array(A) [3,0] - np.array(A) [3,4]) * (np.array(B) [0,2] + np.array(B) [2,0] + np.array(B) [2,2] - np.array(B) [2,3] + np.array(B) [4,0] + np.array(B) [4,2] - np.array(B) [4,3])
    H42 = (-np.array(A) [1,0] + np.array(A) [1,4] - np.array(A) [2,4]) * (-np.array(B) [0,0] - np.array(B) [0,1] - np.array(B) [0,4] + np.array(B) [3,0] + np.array(B) [3,1] + np.array(B) [3,4] - np.array(B) [4,1])
    H43 = np.array(A) [1,3] * (np.array(B) [3,0] + np.array(B) [3,1])
    H44 = (np.array(A) [1,2] + np.array(A) [2,1] - np.array(A) [2,2]) * (np.array(B) [1,1] - np.array(B) [2,0])
    H45 = (-np.array(A) [2,2] + np.array(A) [2,3] - np.array(A) [3,2]) * (np.array(B) [2,4] + np.array(B) [3,0] + np.array(B) [3,2]  + np.array(B) [3,4] + np.array(B) [4,0] + np.array(B) [4,2] + np.array(B) [4,4])
    H46 = -np.array(A) [2,4] * (-np.array(B) [4,0] -np.array(B) [4,4])
    H47 = (np.array(A) [1,0] - np.array(A) [1,4] - np.array(A) [2,0] + np.array(A) [2,4]) * (np.array(B) [0,0] + np.array(B) [0,1] + np.array(B) [0,4] - np.array(B) [3,0] - np.array(B) [3,1] - np.array(B) [3,4])
    H48 = (-np.array(A) [1,2] + np.array(A) [2,2]) * (np.array(B) [1,1] + np.array(B) [2,1] + np.array(B) [2,4] + np.array(B) [3,0] + np.array(B) [3,1] + np.array(B) [3,4])
    H49 = (-np.array(A) [0,0] - np.array(A) [0,2]  + np.array(A) [0,3] + np.array(A) [0,4] - np.array(A) [1,0] - np.array(A) [1,2] + np.array(A) [1,3] + np.array(A) [1,4]) * (-np.array(B) [0,0] - np.array(B) [0,1] + np.array(B) [0,3])
    H50 = (-np.array(A) [0,3] - np.array(A) [1,3]) * (np.array(B) [1,1] - np.array(B) [2,0] - np.array(B) [2,1] + np.array(B) [2,3] - np.array(B) [3,1] + np.array(B) [3,3])
    H51 = np.array(A) [1,1] * (np.array(B) [1,0] + np.array(B) [1,1] - np.array(B) [4,0])
    H52 = np.array(A) [3,1] * (np.array(B) [0,0] + np.array(B) [1,0] + np.array(B) [1,2])
    H53 = -np.array(A) [0,1] * (-np.array(B) [1,0] + np.array(B) [1,3] + np.array(B) [3,0])
    H54 = (np.array(A) [0,1] + np.array(A) [0,3] - np.array(A) [1,1] - np.array(A) [1,4] - np.array(A) [2,1] + np.array(A) [2,2] - np.array(A) [3,1] + np.array(A) [3,2] - np.array(A) [3,3] - np.array(A) [3,4]) * np.array(B) [1,2]
    H55 = (np.array(A) [0,3] - np.array(A) [3,3]) * (-np.array(B) [1,2] + np.array(B) [2,0] + np.array(B) [2,2] - np.array(B) [2,3]  + np.array(B) [3,2] - np.array(B) [3,3])
    H56 = (np.array(A) [0,0] - np.array(A) [0,4] - np.array(A) [3,0] + np.array(A) [3,4]) * (np.array(B) [2,0] + np.array(B) [2,2] - np.array(B) [2,3] + np.array(B) [4,0] + np.array(B) [4,2] - np.array(B) [4,3])
    H57 = (-np.array(A) [2,0] - np.array(A) [3,0]) * (-np.array(B) [0,2] - np.array(B) [0,4] - np.array(B) [1,4] - np.array(B) [4,0] - np.array(B) [4,2] - np.array(B) [4,4])
    H58 = (-np.array(A) [0,3] - np.array(A) [0,4] - np.array(A) [2,3] - np.array(A) [2,4]) * (-np.array(B) [4,0] + np.array(B) [4,3] - np.array(B) [4,4])
    H59 = (-np.array(A) [2,2] + np.array(A) [2,3] - np.array(A) [3,2] + np.array(A) [3,3]) * (np.array(B) [3,0] + np.array(B) [3,2] + np.array(B) [3,4] + np.array(B) [4,0] + np.array(B) [4,2] + np.array(B) [4,4])
    H60 = (np.array(A) [1,4] + np.array(A) [3,4]) * (np.array(B) [1,2] - np.array(B) [2,0] - np.array(B) [2,1] - np.array(B) [2,2] - np.array(B) [4,1] - np.array(B) [4,2])
    H61 = (np.array(A) [0,3] + np.array(A) [2,3]) * (np.array(B) [0,0] - np.array(B) [0,3] + np.array(B) [0,4] - np.array(B) [1,4] - np.array(B) [3,3] + np.array(B) [3,4] - np.array(B) [4,0] + np.array(B) [4,3] - np.array(B) [4,4])
    H62 = (np.array(A) [1,0] + np.array(A) [3,0]) * (np.array(B) [0,1] + np.array(B) [0,2] + np.array(B) [1,1] - np.array(B) [3,0] - np.array(B) [3,1] - np.array(B) [3,2])
    H63 = (-np.array(A) [2,2] - np.array(A) [3,2]) * (-np.array(B) [1,2] - np.array(B) [2,2] - np.array(B) [2,4] - np.array(B) [3,0] - np.array(B) [3,2] - np.array(B) [3,4])
    H64 = (np.array(A) [0,0] - np.array(A) [0,2] - np.array(A) [0,3] + np.array(A) [2,0] - np.array(A) [2,2] - np.array(A) [2,3]) * (np.array(B) [0,0] - np.array(B) [0,3] + np.array(B) [0,4])
    H65 = (-np.array(A) [0,0] + np.array(A) [3,0]) * (-np.array(B) [0,2] + np.array(B) [0,3] + np.array(B) [1,3] - np.array(B) [4,0] - np.array(B) [4,2] + np.array(B) [4,3])
    H66 = (np.array(A) [0,0] - np.array(A) [0,1] + np.array(A) [0,2] - np.array(A) [0,4] - np.array(A) [1,1] - np.array(A) [1,4] - np.array(A) [2,1] + np.array(A) [2,2] - np.array(A) [3,0] + np.array(A) [3,1]) * np.array(B) [1,3]
    H67 = (np.array(A) [1,4] - np.array(A) [2,4]) * (np.array(B) [0,0] + np.array(B) [0,1] + np.array(B) [0,4] - np.array(B) [1,4] - np.array(B) [3,0] - np.array(B) [3,1] - np.array(B) [3,4] + np.array(B) [4,1] + np.array(B) [4,4])
    H68 = (np.array(A) [0,0] + np.array(A) [0,2] - np.array(A) [0,3] - np.array(A) [0,4] - np.array(A) [3,0] - np.array(A) [3,2] + np.array(A) [3,3] + np.array(A) [3,4]) * (-np.array(B) [2,0] - np.array(B) [2,2] + np.array(B) [2,3])
    H69 = (-np.array(A) [0,2] + np.array(A) [0,3] - np.array(A) [1,2] + np.array(A) [1,3]) * (-np.array(B) [1,3] - np.array(B) [2,0] - np.array(B) [2,1] + np.array(B) [2,3] - np.array(B) [4,1] + np.array(B) [4,3])
    H70 = (np.array(A) [1,2] - np.array(A) [1,4] + np.array(A) [3,2] - np.array(A) [3,4]) * (-np.array(B) [2,0] - np.array(B) [2,1] - np.array(B) [2,2])
    H71 = (-np.array(A) [2,0] + np.array(A) [2,2] - np.array(A) [2,3] + np.array(A) [2,4] - np.array(A) [3,0] + np.array(A) [3,2] - np.array(A) [3,3] + np.array(A) [3,4]) * (-np.array(B) [4,0] - np.array(B) [4,2] - np.array(B) [4,4])
    H72 = (-np.array(A) [1,0] - np.array(A) [1,3] - np.array(A) [3,0] - np.array(A) [3,3]) * (np.array(B) [3,0] + np.array(B) [3,1] + np.array(B) [3,2])
    H73 = (np.array(A) [0,2] - np.array(A) [0,3] - np.array(A) [0,4] + np.array(A) [1,2] - np.array(A) [1,3] - np.array(A) [1,4]) * (np.array(B) [0,0] + np.array(B) [0,1] - np.array(B) [0,3] + np.array(B) [1,3] + np.array(B) [4,1] - np.array(B) [4,3])
    H74 = (np.array(A) [1,0] - np.array(A) [1,2] + np.array(A) [1,3] - np.array(A) [2,0] + np.array(A) [2,2] - np.array(A) [2,3]) * (np.array(B) [3,0] + np.array(B) [3,1] + np.array(B) [3,4])
    H75 = - (np.array(A) [0,1] + np.array(A) [0,3] - np.array(A) [1,1] - np.array(A) [1,4] - np.array(A) [2,0] + np.array(A) [2,1] + np.array(A) [2,3] + np.array(A) [2,4] - np.array(A) [3,0] + np.array(A) [3,1]) * np.array(B) [1,4]
    H76 = (np.array(A) [0,2] + np.array(A) [2,2]) * (-np.array(B) [0,0] + np.array(B) [0,3] - np.array(B) [0,4] + np.array(B) [1,3] + np.array(B) [2,3] - np.array(B) [2,4])
    
        
    C11 = -H10 + H12 + H14 - H15 - H16 + H53 + H5 - H66 - H7
    C21 = H10 + H11 - H12 + H13 + H15 + H16 - H17 - H44 + H51
    C31 = H10 - H12 + H15 + H16 - H1 + H2 + H3 - H4 + H75
    C41 = -H10 + H12 - H15 - H16 + H52 + H54 - H6 - H8 + H9
    C12 = H13 + H15 + H20 + H21 - H22 + H23 + H25 - H43 + H49 + H50
    C22 = -H11 + H12 - H13 - H15 - H16 + H17 + H18 - H19 - H21 + H43 + H44
    C32 = -H16 - H19 - H21 - H28 - H29 - H38 + H42 + H44 - H47 + H48
    C42 = H11 - H12 - H18 + H21 - H32 + H33 - H34 - H36 + H62 - H70 
    C13 = H15 + H23 + H24 + H34 - H37 + H40 - H41 + H55 - H56 - H9
    C23 = -H10 + H19 + H32 + H35 + H36 + H37 - H43 - H60 - H6 - H72
    C33 = -H16 - H28 + H33 + H37 - H39 + H45 - H46 + H63 - H71 - H8 
    C43 = H10 + H15 + H16 - H33 + H34 - H35 - H37 - H54 + H6 + H8 - H9
    C14 = -H10 + H12 + H14 - H16 + H23 + H24 + H25 + H26 + H5 - H66 - H7 
    C24 = H10 + H18 - H19 + H20 - H22 - H24 - H26 - H5 - H69 + H73 
    C34 = -H14 + H16 - H23 - H26 + H27 + H29 + H31 + H46 - H58 + H76
    C44 = H12 + H25 + H26 - H33 - H35 - H40 + H41 + H65 - H68 - H7 
    C15 = H15 + H24 + H25 + H27 - H28 + H30 + H31 - H4 + H61 + H64 
    C25 = -H10 - H18 - H2 - H30 - H38 + H42 - H43 + H46 + H67 + H74
    C35 = -H10 + H12 - H15 + H28 + H29 - H2 - H30 - H3 + H46 + H4 - H75
    C45 = -H12 - H29 + H30 - H34 + H35 + H39 + H3 - H45 + H57 + H59
    
    # Create matrix C
    C = [[C11, C12, C13, C14, C15], 
         [C21, C22, C23, C24, C25],
         [C31, C32, C33, C34, C35], 
         [C41, C42, C43, C44, C45]]
    
    return C    

C = AlphaTensor4x5by5x5(A, B)   
print(np.matrix(C))
print('----------')
print("The Rank of Matrix C: ", np.linalg.matrix_rank(C))

When the above code is executed, it produces the following result:

[[ 3 -1  7  3  9]
 [-2  2 -2  7  5]
 [-5  9  3  3  5]
 [-2  6  6  3  7]]
----------
The Rank of Matrix A:  4
----------
[[1 1 0 4 0]
 [0 0 0 1 2]
 [1 2 1 1 3]
 [4 0 2 0 3]
 [0 4 0 1 0]]
----------
The Rank of Matrix B:  5
----------
[[22 53 13 27 28]
 [24 14 12 -3 19]
 [10 21  9 -3 36]
 [16 38 12 11 39]]
----------
The Rank of Matrix C:  4
This entry was posted in Artificial Intelligence, Programming, Python, Technology and tagged , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit exceeded. Please complete the captcha once again.