TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: THOMAS J. PEDERSEN
from: DAN TRACZYNSKI
date: 1998-04-25 01:54:00
subject: Re: Solving linear equations

The bird circled around, dropping this on Thomas J. Pedersen...
 TJP> I need a program to solve this set of equations:
 TJP> a11*x1 + a12*x2 + a13*x3 +  - - - - - - -+ a1n*xn       =   b1
 TJP> a21*x1 + a22*x2 + a23*x3 +  - - - - - - -+ a3n*xn       =   b2
 TJP> a31*x1 + a32*x2 + a33*x3 +  - - - - - - -+ a3n*xn       =   b3
 TJP> - - - - - - - - -
 TJP> - - - - - - - - -
 TJP> - - - - - - - - --
 TJP> am1*x1 + am2*x2 + am3*x3 + - - - - - -+ amn*xn      =   bn
 TJP> A(matrice)  X  x(vector) = b(vector)
 TJP> There are 3 possible solutions:
 TJP> (The letters are vectors in n-dim space)
 TJP> 1)  x = k                                               (one
 TJP> solution) 2)  x = k + t*a1 + s*a2 + r*a3              (many
 TJP> solutions) 3)   x =                                               
 TJP> (no solution) 
Take the linear combinations you setup above, put them into a matrix, and
reduce it to row-echelon form.  Use two-dimensional arrays to form the
matrix.  Let's use the following matrix as an example for the algorithm.
[  0  0  0  3 | 0  ]
[  3  3  0  0 | 0  ]
[  0  0  0  0 | 4  ]
[  1  2  4  0 | 0  ]
Look at the first column.  Take the row with the lowest value (in the above
example, row 4) and then subtract that particular value from each column with
a non-zero value in the first column until only the one row has a non-zero
value at column 1.  Then swap that lone row with a pivot in the first column
with the first row.  The formula for subtracting row 4 from row 2 would be
r2 - (r4 * (r2c1 / r4c1)) where r2c1 = 3, r4c1 = 1, and r2 and r4 are rows 2
and 4.  Then proceed to column 2 and rows 2 through m.  Here's some
pseudocode to row-reduce a matrix M.
const cols = 5;  // # of columns
      rows = 3;  // # of rows
Matrix M;     // Matrix is the 2-dimensional array type,
              //  ie. M[0][3] is row 0, col 3
       Least; // Row with the lowest value
for (int x = 0; x < cols; x++)
{
  // Find the row with the lowest value at column x and assign it to Least
  for (int y = x; y < rows; y++)
  {
    // Here you'll need to overload = and - for vector subtraction
    // and assignment
    M[y] = M[y] - ((M[y][x] / M[Least][x]) * M[Least]);
    // We're subtracting row y from a number times row Least so that
    // row y = row Least * some number at column x.  That way row y at
    // the current column being processed equals zero and after this
    // loop there is only one pivot element in column x.
  };
  // Swap row 0 with row Least, ie. M[0]  M[Least].
};
After the previous code has run, this is what the above matrix would look
like.
[  1  2  4  0 | 0  ]
[  0 -4 -12 0 | 0  ]
[  0  0  0  3 | 0  ]
[  0  0  0  0 | 4  ]
The matrix is now in row-echelon form and you can see from row 4 that
0x1 + 0x2 + 0x3 + 0x4 = 4 so you know there are no solutions.  Otherwise
just go through the matrix and find the solutions.
Assuming this isn't a programming homework assignment, the following text
comes with a disk which includes software to do what you want.  Try your
local university bookstore.
Fraleigh, John B. and Beauregard, Raymond A.  "Linear Algebra" 3rd Edition,
Addison-Wesley Publishing, 1995.
Cya!
Dan
dant@sfu.ca
http://www.sfu.ca/~dant/
... Advice is what we ask for when we already know.
--- Blue Wave v2.12
---------------
* Origin: The Rusty MailBox - Vancouver, B.C. Canada (1:153/757)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.