3
* Solution of simultaneous linear equations AX = B
4
* by Gaussian elimination with partial pivoting
10
* double A[n*n], B[n], X[n];
15
* ercode = simq( A, B, X, n, flag, IPS );
21
* B, X, IPS are vectors of length n.
22
* A is an n x n matrix (i.e., a vector of length n*n),
23
* stored row-wise: that is, A(i,j) = A[ij],
24
* where ij = i*n + j, which is the transpose of the normal
25
* column-wise storage.
27
* The contents of matrix A are destroyed.
29
* Set flag=0 to solve.
30
* Set flag=-1 to do a new back substitution for different B vector
31
* using the same A matrix previously reduced when flag=0.
33
* The routine returns nonzero on error; messages are printed.
38
* Depends on the conditioning (range of eigenvalues) of matrix A.
43
* Computer Solution of Linear Algebraic Systems,
44
* by George E. Forsythe and Cleve B. Moler; Prentice-Hall, 1967.
53
int simq(double [], double [], double [], int, int, int [] );
56
#define fabs(x) ((x) < 0 ? -(x) : (x))
58
int simq( A, B, X, n, flag, IPS )
63
int i, j, ij, ip, ipj, ipk, ipn;
65
int k, kp, kp1, kpk, kpn;
67
double em, q, rownrm, big, size, pivot, sum;
73
/* Initialize IPS and X */
89
puts("SIMQ ROWNRM=0");
96
/* Gaussian elimination with partial pivoting */
98
for( k=0; k<nm1; k++ )
106
size = fabs( A[ipk] ) * X[ip];
116
puts( "SIMQ BIG=0" );
122
IPS[k] = IPS[idxpiv];
129
for( i=kp1; i<n; i++ )
137
for( j=kp1; j<n; j++ )
140
A[ipj] = A[ipj] + em * A[nkp + j];
144
kpn = n * IPS[n-1] + n - 1; /* last element of IPS[n] th row */
147
puts( "SIMQ A[kpn]=0");
152
/* back substitution */
164
sum += A[ipj] * X[j];
170
ipn = n * IPS[n-1] + n - 1;
171
X[n-1] = X[n-1]/A[ipn];
173
for( iback=1; iback<n; iback++ )
175
/* i goes (n-1),...,1 */
180
for( j=i+1; j<n; j++ )
181
sum += A[nip+j] * X[j];
182
X[i] = (X[i] - sum)/A[nip+i];