GaussJordan.cc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Gauss-Jordan elimination with full pivoting.
  2. //
  3. // Uses:
  4. // (1) solving systems of linear equations (AX=B)
  5. // (2) inverting matrices (AX=I)
  6. // (3) computing determinants of square matrices
  7. //
  8. // Running time: O(n^3)
  9. //
  10. // INPUT: a[][] = an nxn matrix
  11. // b[][] = an nxm matrix
  12. //
  13. // OUTPUT: X = an nxm matrix (stored in b[][])
  14. // A^{-1} = an nxn matrix (stored in a[][])
  15. // returns determinant of a[][]
  16. #include <iostream>
  17. #include <vector>
  18. #include <cmath>
  19. using namespace std;
  20. const double EPS = 1e-10;
  21. typedef vector<int> VI;
  22. typedef double T;
  23. typedef vector<T> VT;
  24. typedef vector<VT> VVT;
  25. T GaussJordan(VVT &a, VVT &b) {
  26. const int n = a.size();
  27. const int m = b[0].size();
  28. VI irow(n), icol(n), ipiv(n);
  29. T det = 1;
  30. for (int i = 0; i < n; i++) {
  31. int pj = -1, pk = -1;
  32. for (int j = 0; j < n; j++) if (!ipiv[j])
  33. for (int k = 0; k < n; k++) if (!ipiv[k])
  34. if (pj == -1 || fabs(a[j][k]) > fabs(a[pj][pk])) { pj = j; pk = k; }
  35. if (fabs(a[pj][pk]) < EPS) { cerr << "Matrix is singular." << endl; exit(0); }
  36. ipiv[pk]++;
  37. swap(a[pj], a[pk]);
  38. swap(b[pj], b[pk]);
  39. if (pj != pk) det *= -1;
  40. irow[i] = pj;
  41. icol[i] = pk;
  42. T c = 1.0 / a[pk][pk];
  43. det *= a[pk][pk];
  44. a[pk][pk] = 1.0;
  45. for (int p = 0; p < n; p++) a[pk][p] *= c;
  46. for (int p = 0; p < m; p++) b[pk][p] *= c;
  47. for (int p = 0; p < n; p++) if (p != pk) {
  48. c = a[p][pk];
  49. a[p][pk] = 0;
  50. for (int q = 0; q < n; q++) a[p][q] -= a[pk][q] * c;
  51. for (int q = 0; q < m; q++) b[p][q] -= b[pk][q] * c;
  52. }
  53. }
  54. for (int p = n-1; p >= 0; p--) if (irow[p] != icol[p]) {
  55. for (int k = 0; k < n; k++) swap(a[k][irow[p]], a[k][icol[p]]);
  56. }
  57. return det;
  58. }
  59. int main() {
  60. const int n = 4;
  61. const int m = 2;
  62. double A[n][n] = { {1,2,3,4},{1,0,1,0},{5,3,2,4},{6,1,4,6} };
  63. double B[n][m] = { {1,2},{4,3},{5,6},{8,7} };
  64. VVT a(n), b(n);
  65. for (int i = 0; i < n; i++) {
  66. a[i] = VT(A[i], A[i] + n);
  67. b[i] = VT(B[i], B[i] + m);
  68. }
  69. double det = GaussJordan(a, b);
  70. // expected: 60
  71. cout << "Determinant: " << det << endl;
  72. // expected: -0.233333 0.166667 0.133333 0.0666667
  73. // 0.166667 0.166667 0.333333 -0.333333
  74. // 0.233333 0.833333 -0.133333 -0.0666667
  75. // 0.05 -0.75 -0.1 0.2
  76. cout << "Inverse: " << endl;
  77. for (int i = 0; i < n; i++) {
  78. for (int j = 0; j < n; j++)
  79. cout << a[i][j] << ' ';
  80. cout << endl;
  81. }
  82. // expected: 1.63333 1.3
  83. // -0.166667 0.5
  84. // 2.36667 1.7
  85. // -1.85 -1.35
  86. cout << "Solution: " << endl;
  87. for (int i = 0; i < n; i++) {
  88. for (int j = 0; j < m; j++)
  89. cout << b[i][j] << ' ';
  90. cout << endl;
  91. }
  92. }