Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
443 views
in Technique[技术] by (71.8m points)

numpy - Solve the linear equations system AX = B in Python, np.linalg.solve not working

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

My results with your arrays look right:

In [582]: A=np.array([9,1,8,3,2,5,1,6,5]).reshape(3,3)                                                
In [583]: B=np.array([7,0,5,7,8,4,5,6,7]).reshape(3,3)                                                
In [584]: x=np.linalg.solve(A,B)                                                                      
In [585]: x                                                                                           
Out[585]: 
array([[-1.70967742, -4.48387097,  0.08064516],
       [-1.35483871, -2.74193548,  0.79032258],
       [ 2.96774194,  5.38709677,  0.43548387]])
In [586]: A@x                                                                                         
Out[586]: 
array([[7., 0., 5.],
       [7., 8., 4.],
       [5., 6., 7.]])

The other approach: AX=B => X=1/A B:

In [591]: np.linalg.inv(A)@B                                                                          
Out[591]: 
array([[-1.70967742, -4.48387097,  0.08064516],
       [-1.35483871, -2.74193548,  0.79032258],
       [ 2.96774194,  5.38709677,  0.43548387]])

And formally testing for equality:

In [602]: np.allclose([email protected](A, B), B)                                                       
Out[602]: True

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...