simq.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /* simq.c
  2. *
  3. * Solution of simultaneous linear equations AX = B
  4. * by Gaussian elimination with partial pivoting
  5. *
  6. *
  7. *
  8. * SYNOPSIS:
  9. *
  10. * double A[n*n], B[n], X[n];
  11. * int n, flag;
  12. * int IPS[];
  13. * int simq();
  14. *
  15. * ercode = simq( A, B, X, n, flag, IPS );
  16. *
  17. *
  18. *
  19. * DESCRIPTION:
  20. *
  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.
  26. *
  27. * The contents of matrix A are destroyed.
  28. *
  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.
  32. *
  33. * The routine returns nonzero on error; messages are printed.
  34. *
  35. *
  36. * ACCURACY:
  37. *
  38. * Depends on the conditioning (range of eigenvalues) of matrix A.
  39. *
  40. *
  41. * REFERENCE:
  42. *
  43. * Computer Solution of Linear Algebraic Systems,
  44. * by George E. Forsythe and Cleve B. Moler; Prentice-Hall, 1967.
  45. *
  46. */
  47. /* simq 2 */
  48. #include <stdio.h>
  49. #define fabs(x) ((x) < 0 ? -(x) : (x))
  50. int simq( A, B, X, n, flag, IPS )
  51. double A[], B[], X[];
  52. int n, flag;
  53. int IPS[];
  54. {
  55. int i, j, ij, ip, ipj, ipk, ipn;
  56. int idxpiv, iback;
  57. int k, kp, kp1, kpk, kpn;
  58. int nip, nkp, nm1;
  59. double em, q, rownrm, big, size, pivot, sum;
  60. nm1 = n-1;
  61. if( flag < 0 )
  62. goto solve;
  63. /* Initialize IPS and X */
  64. ij=0;
  65. for( i=0; i<n; i++ )
  66. {
  67. IPS[i] = i;
  68. rownrm = 0.0;
  69. for( j=0; j<n; j++ )
  70. {
  71. q = fabs( A[ij] );
  72. if( rownrm < q )
  73. rownrm = q;
  74. ++ij;
  75. }
  76. if( rownrm == 0.0 )
  77. {
  78. printf("SIMQ ROWNRM=0");
  79. return(1);
  80. }
  81. X[i] = 1.0/rownrm;
  82. }
  83. /* simq 3 */
  84. /* Gaussian elimination with partial pivoting */
  85. for( k=0; k<nm1; k++ )
  86. {
  87. big= 0.0;
  88. idxpiv = 0;
  89. for( i=k; i<n; i++ )
  90. {
  91. ip = IPS[i];
  92. ipk = n*ip + k;
  93. size = fabs( A[ipk] ) * X[ip];
  94. if( size > big )
  95. {
  96. big = size;
  97. idxpiv = i;
  98. }
  99. }
  100. if( big == 0.0 )
  101. {
  102. printf( "SIMQ BIG=0" );
  103. return(2);
  104. }
  105. if( idxpiv != k )
  106. {
  107. j = IPS[k];
  108. IPS[k] = IPS[idxpiv];
  109. IPS[idxpiv] = j;
  110. }
  111. kp = IPS[k];
  112. kpk = n*kp + k;
  113. pivot = A[kpk];
  114. kp1 = k+1;
  115. for( i=kp1; i<n; i++ )
  116. {
  117. ip = IPS[i];
  118. ipk = n*ip + k;
  119. em = -A[ipk]/pivot;
  120. A[ipk] = -em;
  121. nip = n*ip;
  122. nkp = n*kp;
  123. for( j=kp1; j<n; j++ )
  124. {
  125. ipj = nip + j;
  126. A[ipj] = A[ipj] + em * A[nkp + j];
  127. }
  128. }
  129. }
  130. kpn = n * IPS[n-1] + n - 1; /* last element of IPS[n] th row */
  131. if( A[kpn] == 0.0 )
  132. {
  133. printf( "SIMQ A[kpn]=0");
  134. return(3);
  135. }
  136. /* simq 4 */
  137. /* back substitution */
  138. solve:
  139. ip = IPS[0];
  140. X[0] = B[ip];
  141. for( i=1; i<n; i++ )
  142. {
  143. ip = IPS[i];
  144. ipj = n * ip;
  145. sum = 0.0;
  146. for( j=0; j<i; j++ )
  147. {
  148. sum += A[ipj] * X[j];
  149. ++ipj;
  150. }
  151. X[i] = B[ip] - sum;
  152. }
  153. ipn = n * IPS[n-1] + n - 1;
  154. X[n-1] = X[n-1]/A[ipn];
  155. for( iback=1; iback<n; iback++ )
  156. {
  157. /* i goes (n-1),...,1 */
  158. i = nm1 - iback;
  159. ip = IPS[i];
  160. nip = n*ip;
  161. sum = 0.0;
  162. for( j=i+1; j<n; j++ )
  163. sum += A[nip+j] * X[j];
  164. X[i] = (X[i] - sum)/A[nip+i];
  165. }
  166. return(0);
  167. }