矩阵乘法,求解Ax = b求解x [英] Matrix multiplication, solve Ax = b solve for x
问题描述
因此,我得到了一项家庭作业,需要解决三次样条曲线的系数.现在,我清楚地了解了如何在纸上以及MatLab上进行数学运算,我想用Python解决问题.给定一个方程Ax = b,我知道A和b的值,我希望能够用Python求解x,而我很难找到一个好的资源来做这样的事情.
So I was given a homework assignment that requires solving the coefficients of cubic splines. Now I clearly understand how to do the math on paper as well as with MatLab, I want to solve the problem with Python. Given an equation Ax = b where I know the values of A and b, I want to be able to solve for x with Python and I am having trouble finding a good resource to do such a thing.
例如.
A = |1 0 0|
|1 4 1|
|0 0 1|
x = Unknown 3x1 matrix
b = |0 |
|24|
|0 |
解决x
推荐答案
在一般情况下,请使用solve
:
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.])
如果您的问题是有条带的(通常是三次样条),则有 http://docs.scipy.org/doc/scipy/reference/genic/scipy.linalg.solve_banded.html
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
要对问题的某些评论进行评论:更好的 not 可以使用inv
求解线性系统. numpy.lstsq
有点不同,它对拟合更有用.
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.
这篇关于矩阵乘法,求解Ax = b求解x的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!