minv.c 1020 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /* minv.c
  2. *
  3. * Matrix inversion
  4. *
  5. *
  6. *
  7. * SYNOPSIS:
  8. *
  9. * int n, errcod;
  10. * double A[n*n], X[n*n];
  11. * double B[n];
  12. * int IPS[n];
  13. * int minv();
  14. *
  15. * errcod = minv( A, X, n, B, IPS );
  16. *
  17. *
  18. *
  19. * DESCRIPTION:
  20. *
  21. * Finds the inverse of the n by n matrix A. The result goes
  22. * to X. B and IPS are scratch pad arrays of length n.
  23. * The contents of matrix A are destroyed.
  24. *
  25. * The routine returns nonzero on error; error messages are printed
  26. * by subroutine simq().
  27. *
  28. */
  29. minv( A, X, n, B, IPS )
  30. double A[], X[];
  31. int n;
  32. double B[];
  33. int IPS[];
  34. {
  35. double *pX;
  36. int i, j, k;
  37. for( i=1; i<n; i++ )
  38. B[i] = 0.0;
  39. B[0] = 1.0;
  40. /* Reduce the matrix and solve for first right hand side vector */
  41. pX = X;
  42. k = simq( A, B, pX, n, 1, IPS );
  43. if( k )
  44. return(-1);
  45. /* Solve for the remaining right hand side vectors */
  46. for( i=1; i<n; i++ )
  47. {
  48. B[i-1] = 0.0;
  49. B[i] = 1.0;
  50. pX += n;
  51. k = simq( A, B, pX, n, -1, IPS );
  52. if( k )
  53. return(-1);
  54. }
  55. /* Transpose the array of solution vectors */
  56. mtransp( n, X, X );
  57. return(0);
  58. }