I am translating some Matlab code into Python and I having some problems regarding matrix multiplication accuracy. Assuming we have following data:
A: 6x6 matrix B: 5x5 matrix C: 2x2 matrix D: 5x5 matrix E: 6x5 matrix
In Matlab, my operation looks as follows:
R1 = A * (-( B*C(1,1) + D*C(2,1) ) * E.').'
Previous set of operations produces a 6x5 matrix (R1 matrix).
In my Python code I have the same matrices and the operation looks as follows:
R2 = np.matmul(A, np.matmul(-(np.multiply(B, C[0,0]) + np.multiply(D, C[1,0])), E.transpose()).transpose())
However, the same operation on same data produces different results in terms of
norm-2(R1-R2) = 7.4506e-09
I am not able to understand why the results are different. Does anyone know what the reason could be?
For the sake of clarity, I attach the data and the scripts here. The instructions are:
- Run the Run_python.py script. This will perform the operations and will generate a python_results.mat file.
- Run the Run_matlab script. This will perform the operations and will compare both results in terms of its norm-2.
Finally, my Python version is 3.6.3, with numpy (1.14.3) and scipy (1.1.0).