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

痴心易碎 提交于 2021-01-29 11:29:42

问题


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.


回答1:


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(A@np.linalg.solve(A, B), B)                                                       
Out[602]: True



回答2:


The result of X is correct. To verify if your solution is correct, as per the official docs you can use allclose() which should return True if two arrays (AX and B)are equal element-wise, within a tolerance.

import numpy as np

A = np.array([[9, 1, 8], [3, 2, 5], [1, 6, 5]])  
B = np.array([[7, 0, 5], [7, 8, 4], [5, 6, 7]])  

X = np.linalg.solve(A,B)

np.allclose(np.dot(A, X), B)
# True

In your case, it indeed returns True.



来源:https://stackoverflow.com/questions/56333472/solve-the-linear-equations-system-ax-b-in-python-np-linalg-solve-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!