I'm trying to solve the linear equation AX=B where A,X,B are Matrices.
I've tried using the np.linalg.solve function of numpy but the result seems to be wrong.
Example:
Matrix A
[9 1 8]
[3 2 5]
[1 6 5]
Matrix B
[7 0 5]
[7 8 4]
[5 6 7]
So to solve X, i've used:
X = np.linalg.solve(A,B)
The result is:
X
[ 1.17521368 -0.17948718 0.40598291]
[ 0.20512821 -0.30769231 0.74358974]
[-0.56410256 -0.15384615 1.20512821]
But if i try to verify the result by multiplying A by X, the result is anything but B:
B
[ 5.40598291 -2.02564103 8.86752137]
[ 7.61111111 -4.33333333 13.61111111]
[ 3.15811966 -3.82051282 14.92735043]
If i use this:
np.matmul(B, np.linalg.inv(A))
Instead of the solve function, i get the same results.
Is there something i am missing here?
EDIT 1:
I've printed
np.allclose(np.dot(A, X), B)
And is returning False
EDIT 2
Here is the code i'm using:
B = np.array([7,0,5,7,8,4,5,6,7]).reshape(3,3)
A = np.array([9,1,8,3,2,5,1,6,5]).reshape(3,3)
X = np.linalg.solve(A,B)
print(x)
#[[-1.70967742 -4.48387097 0.08064516]
# [-1.35483871 -2.74193548 0.79032258]
# [ 2.96774194 5.38709677 0.43548387]]
My apologies if this is a very basic question, i appreciate any help.
Thanks.
See Question&Answers more detail:
os