Reputation: 203
I obtain different results from matmul in Fortran and Python. How to get the right one in Fortran. B is probably not understood as a 3x3 matrix. How to solve it, please?
k = 1 m = 3
In Fortran, I have:
A(k:m,k): 0.88807383397711526 0.32505758367186816 0.32505758367186816
B(k:m,k:m): 1.0000000000000000 1.0000000000000000 1.0000000000000000 -1.0000000000000000 0.0000000000000000 1.0000000000000000 1.0000000000000000 0.0000000000000000 1.0000000000000000
matmul gives
1.5381890013208515 -0.56301625030524716 1.2131314176489834
In Python, I have:
A[k,k:m] [ 0.888074 0.325058 0.325058]
B[k:m,k:m] [[ 1.000000 1.000000 1.000000]
[-1.000000 0.000000 1.000000]
[ 1.000000 0.000000 1.000000]]
matmul gives
[ 0.888074 0.888074 1.538189]
Upvotes: 1
Views: 290
Reputation: 812
It depends on the order of the multiplication. Numpy considers 1D arrays without treating it as a row or column vector.
import numpy as np
A = np.array([0.88807383397711526, 0.32505758367186816, 0.32505758367186816])
B = np.array([[1, 1, 1], [-1, 0, 1], [1, 0, 1]], dtype=float)
If you do A@B
, which is the same as np.matmul(A,B)
, assuming A
is actually a "1D row array", you get
>>> A@B
array([0.88807383, 0.88807383, 1.538189 ])
But if you do the opposite, i.e. B@A
which is the same as np.matmul(B,A)
, assuming A
is actually a "1D column array" you get
>>> B@A
array([ 1.538189 , -0.56301625, 1.21313142])
Upvotes: 3