/* *----------------------------------------------------------------------------- * file: matsolve.c * desc: solve linear equations * by: ko shu pui, patrick * date: 24 nov 91 v0.1 * revi: 14 may 92 v0.2 * ref: * [1] Mary L.Boas, "Mathematical Methods in the Physical Sciene," * John Wiley & Sons, 2nd Ed., 1983. Chap 3. * * [2] Kendall E.Atkinson, "An Introduction to Numberical Analysis," * John Wiley & Sons, 1978. * *----------------------------------------------------------------------------- */ #include #include #ifdef __TURBOC__ #include #else #include #endif #include "matrix.h" /* *----------------------------------------------------------------------------- * funct: mat_lu * desct: in-place LU decomposition with partial pivoting * given: !! A = square matrix (n x n) !ATTENTION! see commen * P = permutation vector (n x 1) * retrn: number of permutation performed * -1 means suspected singular matrix * comen: A will be overwritten to be a LU-composite matrix * * note: the LU decomposed may NOT be equal to the LU of * the orignal matrix a. But equal to the LU of the * rows interchanged matrix. *----------------------------------------------------------------------------- */ int mat_lu( A, P ) MATRIX A; MATRIX P; { int i, j, k, n; int maxi, tmp; double c, c1; int p; n = MatCol(A); for (p=0,i=0; i c) { c = c1; maxi = i; } } /* * row exchange, update permutation vector */ if (k != maxi) { p++; tmp = P[k][0]; P[k][0] = P[maxi][0]; P[maxi][0] = tmp; } /* * suspected singular matrix */ if ( A[(int)P[k][0]][k] == 0.0 ) return (-1); for (i=k+1; i=0; k--) { sum = 0.0; for (j=k+1; j