Wednesday, September 14, 2011

python to solve linear system

Solving good ol' Ax=b:

>>> import numpy as np
>>> A = np.matrix('[3 -1 1; -1 1 -1; 1 -1 3]')
>>> A
matrix([[ 3, -1,  1],
        [-1,  1, -1],
        [ 1, -1,  3]])
>>> b = np.matrix('[1;1;1]')
>>> from scipy import linalg
>>> x = linalg.solve(A,b)
>>> x
array([[ 1.],
       [ 3.],
       [ 1.]])

See docs:
numpy.linalg.solve
routines.linalg
numpy solve examples (prob better than above!)

No comments:

Post a Comment