5.7 Solving systems of linear equations

Using maxima, you can easily solve linear equations:

sage: var('a,b,c')
(a, b, c)
sage: eqn = [a+b*c==1, b-a*c==0, a+b==5]
sage: s = solve(eqn, a,b,c); s
[[a == (25*sqrt(79)*I + 25)/(6*sqrt(79)*I - 34), 
  b == (5*sqrt(79)*I + 5)/(sqrt(79)*I + 11), 
  c == (sqrt(79)*I + 1)/10], 
 [a == (25*sqrt(79)*I - 25)/(6*sqrt(79)*I + 34), 
  b == (5*sqrt(79)*I - 5)/(sqrt(79)*I - 11), 
  c == (1 - sqrt(79)*I)/10]]

You can even nicely typeset the solution in LaTeX:

sage.: print latex(s)
...
To have the above appear onscreen via xdvi, type view(s).

You can also solve linear equations symbolically using the solve_linear command:

sage: var('x,y,z,a')
(x, y, z, a)
sage: eqns = [x + z == y, 2*a*x - y == 2*a^2, y - 2*z == 2]    
sage: solve(eqns, x, y, z)
[[x == a + 1, y == 2*a, z == a - 1]]

Here is a numerical Numpy example:

sage: from numpy import arange, eye, linalg      
sage: A = eye(10)       ##   the 10x10 identity matrix
sage: b = arange(1,11)
sage: x = linalg.solve(A,b)

Another way to solve a system numerically is to use Sage's octave interface:

sage: M33 = MatrixSpace(QQ,3,3)
sage: A   = M33([1,2,3,4,5,6,7,8,0])
sage: V3  = VectorSpace(QQ,3)
sage: b   = V3([1,2,3])
sage: octave.solve_linear_system(A,b)    # requires optional octave
[-0.33333299999999999, 0.66666700000000001, -3.5236600000000002e-18]

See About this document... for information on suggesting changes.