Matrix multiplication, solve Ax = b solve for x

爱⌒轻易说出口 提交于 2019-11-30 17:52:25
ev-br

In a general case, use solve:

>>> import numpy as np
>>> from scipy.linalg import solve
>>> 
>>> A = np.random.random((3, 3))
>>> b = np.random.random(3)
>>> 
>>> x = solve(A, b)
>>> x
array([ 0.98323512,  0.0205734 ,  0.06424613])
>>> 
>>> np.dot(A, x) - b
array([ 0.,  0.,  0.])

If your problem is banded (which cubic splines it often are), then there's http://docs.scipy.org/doc/scipy/reference/generated/scipy.linalg.solve_banded.html

To comment on some of the comments to the question: better not use inv for solving linear systems. numpy.lstsq is a bit different, it's more useful for fitting.

As this is homework, you're really better off at least reading up on ways of solving tridiagonal linear systems.

Numpy is the main package for scientific computing in Python. If you are windows user then download it here: http://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy else follow these instructions: http://www.scipy.org/install.html.

import numpy
A = [[1,0,0],[1,4,1],[0,0,1]]
b = [0,24,0]
x = numpy.linalg.lstsq(A,b)

In addition to the code of Zhenya, you might also find it intuitive to use the np.dot function:

import numpy as np
A = [[1,0,0],
    [1,1,1],
    [6,7,0]]
b = [0,24,0]
# Now simply solve for x
x = np.dot(np.linalg.inv(A), b) 
#np.linalg.inv(A)  is simply the inverse of A, np.dot is the dot product
print x

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