Python and Linear Algebra: Systems of Linear Equations
Posted on September 10, 2009 by oubiwann

I've recently found a need to brush up on my linear algebra (due to some software projects I'm working on). It's been about 10 years since I last did quantum mechanics, where we only used a small subset of liner algebra, and 12 years since an actual linear algebra class. I've used it a bit here and there, but most of it is forgotten.
I thought it might be nice to write up some posts as I relearn some of this material and explore areas of NumPy that I either haven't used before or haven't touched in a while. I anticipate that my approach will be essentially code samples based on problems or examples from various linear algebra texts I own. I want to keep it simple: here's Python, and here's how you solve this linear algebra problem with it.
Solving a System of Linear Equations
Given these equations, find a solution, if one exists:
x - 2y + z = 0
2y - 8z = 8
-4x
- 5y +9z = -9
Keep in mind the geometry of these equations: we havethree equations with three dimensions. Each equation describes a plane. Byseeking a solution that these three all have in common, we are trying to findthe geometric entity defined by all three planes intersecting: a point.
Defining the coefficient and constant matrix separtely, we have:
>>> from numpy import matrix
>>> a = matrix([
... [1, -2,1],
... [0, 2, -8],
... [-4, 5, 9],
... ])
>>> b =matrix([
... [0],
... [8],
... [-9],
... ])
Feel free to enter this in whichever way is most comfortable for you :-) I'mjust typing it like this for clear presentation and to maintain a visualconnection to the format often used in text books.
Note that we canwrite the constants matrix "horizontally" and then transpose it:
b= matrix([[0, 8, -9]]).getT()
Now we're going to solve it :-)
>>> from numpy.linalg import solve
>>> solve(a, b)
matrix([[29.],
[ 16.],
[ 3.]])
To be clear, the solution forthis system of equations is the point located at the coordinates x=29, y=16,and z=3.
What happens if we've got a system of equations whosesolution is inconsistent? Let's give it a try :-)
>>> a = matrix([
... [1, -3, 1],
... [2, -1, -2],
... [1, 2, -3],
...])
>>> b = matrix([[1, 2, -1]]).transpose()
>>> solve(a, b)
Traceback (most recent call last):
File "", line 1, in
File "/usr/lib/python2.5/site-packages/numpy/linalg/linalg.py",line 235, in solve
raise LinAlgError, 'Singular matrix'
numpy.linalg.linalg.LinAlgError: Singular matrix
No solution forthat one!
Network Analysis
-a - b + 0 + 0 + 0 + 20 =0
0 + 0 - c + d + 0 - 20 = 0
0 + b + c + 0 + 0 - 20 = 0
a +0 + 0 + 0 - e + 10 = 0
0 + 0 + 0 - d + e + 10 = 0
a =matrix([
[-1, -1, 0, 0, 0],
[0, 0, -1, 1, 0],
[0, 1, 1, 0,0],
[1, 0, 0, 0, -1],
[0, 0, 0, -1, 1],
])
b =matrix([[-20, 20, 20, -10, -10]]).transpose()