fork download
  1. // gauss.cpp
  2. // Liczy wyznacznik macierzy wymiernych metodÄ… Gaussa.
  3.  
  4. #include <cassert>
  5. #include <cmath>
  6. #include <iostream>
  7. #include <iomanip>
  8.  
  9. using namespace std;
  10.  
  11. class Rational {
  12. int p, q; /* p/q, q > 0 */
  13. public:
  14. Rational(int p, int q = 1) {
  15. assert(q != 0);
  16. if (q < 0) {
  17. p = -p;
  18. q = -q;
  19. }
  20. int g = gcd(abs(p), q);
  21.  
  22. this->p = p/g;
  23. this->q = q/g;
  24. }
  25. friend Rational operator+(const Rational &a, const Rational &b) {
  26. return Rational(a.p*b.q + b.p*a.q, a.q*b.q);
  27. }
  28. friend Rational operator-(const Rational &a, const Rational &b) {
  29. return Rational(a.p*b.q - b.p*a.q, a.q*b.q);
  30. }
  31. friend Rational operator*(const Rational &a, const Rational &b) {
  32. return Rational(a.p*b.p, a.q*b.q);
  33. }
  34. friend Rational operator/(const Rational &a, const Rational &b) {
  35. return Rational(a.p*b.q, a.q*b.p);
  36. }
  37. friend ostream & operator<<(ostream &out, const Rational &a) {
  38. out << a.p;
  39. if (a.q != 1) {
  40. out << "/" << a.q;
  41. }
  42. return out;
  43. }
  44. friend bool operator==(const Rational &a, const Rational &b) {
  45. return a.p == b.p && a.q == b.q;
  46. }
  47. friend bool operator!=(const Rational &a, const Rational &b) {
  48. return a.p != b.p || a.q != b.q;
  49. }
  50.  
  51. Rational operator-() const { return Rational(-p, q); }
  52. private:
  53. int gcd(int a, int b) const {
  54. int r;
  55. while (b != 0) {
  56. r = a % b;
  57. a = b;
  58. b = r;
  59. }
  60. return a;
  61. }
  62. };
  63.  
  64. void printMatrix(Rational **A, int n) {
  65. for (int i = 0; i < n; i++) {
  66. for (int j = 0; j < n; j++) {
  67. cout << A[i][j] << "\t";
  68. }
  69. cout << "\n";
  70. }
  71. cout << endl;
  72. }
  73.  
  74. inline void swap(Rational &a, Rational &b) {
  75. Rational c = a;
  76. a = b;
  77. b = c;
  78. }
  79.  
  80. Rational determinant(Rational **A, int n) {
  81. cout << "Eliminacja Gaussa dla macierzy:\n";
  82. printMatrix(A,n);
  83.  
  84. int sign = 1;
  85. for (int k = 0; k < n; k++) {
  86. if (A[k][k] == 0) {
  87. for (int s = k+1; s < n; s++) {
  88. if (A[s][k] != 0) {
  89. cout << "w" << s+1 << " <=> w" << k+1 << "\n";
  90. for (int j = k; j < n; j++) {
  91. swap(A[k][j], A[s][j]);
  92. }
  93. sign = -sign;
  94. break;
  95. }
  96. }
  97. if (A[k][k] == 0) {
  98. cout << "Macierz jest osobliwa." << endl;
  99. return 0;
  100. }
  101. printMatrix(A, n);
  102. }
  103.  
  104. bool hasChanged = false;
  105. for (int i = k+1; i < n; i++) {
  106. if (A[i][k] != 0) {
  107. Rational m = A[i][k] / A[k][k];
  108. cout << "w" << i+1 << " := w" << i+1
  109. << " - (" << m << ")*w" << k+1 << "\n";
  110. for (int j = k; j < n; j++) {
  111. A[i][j] = A[i][j] - m * A[k][j];
  112. }
  113. hasChanged = true;
  114. }
  115. }
  116. if (hasChanged) {
  117. cout << endl;
  118. printMatrix(A, n);
  119. }
  120. }
  121. cout << "Eliminacja zakonczona sukcesem!" << endl;
  122.  
  123. Rational det = 1;
  124. for (int k = 0; k < n; k++) {
  125. det = det * A[k][k];
  126. }
  127.  
  128. return sign*det;
  129. }
  130.  
  131.  
  132. int main() {
  133. Rational A[4][4] = {
  134. { 0, 0, 0, 4 },
  135. { 0, 2, 0, 0 },
  136. { 0, 0, 3, 0 },
  137. { 1, 0, 0, 0 },
  138. };
  139. Rational *As[4] = { A[0], A[1], A[2], A[3] };
  140.  
  141. cout << "detA = " << determinant(As, 4) << "\n\n" << endl;
  142.  
  143.  
  144.  
  145. Rational B[7][7] = {
  146. { 2, -1, 0, 0, 0, 0, 0 },
  147. { -1, 2, -1, 0, 0, 0, 0 },
  148. { 0, -1, 2, -1, 0, 0, 0 },
  149. { 0, 0, -1, 2, -1, 0, 0 },
  150. { 0, 0, 0, -1, 2, -1, 0 },
  151. { 0, 0, 0, 0, -1, 2, -1 },
  152. { 0, 0, 0, 0, 0, -1, 2 },
  153. };
  154. Rational *Bs[7] = { B[0], B[1], B[2], B[3], B[4], B[5], B[6] };
  155.  
  156. cout << "detB = " << determinant(Bs, 7) << endl;
  157.  
  158.  
  159. Rational C[7][7] = {
  160. { 2, -1, 0, 0, 0, 0, -1 },
  161. { -1, 2, -1, 0, 0, 0, 0 },
  162. { 0, -1, 2, -1, 0, 0, 0 },
  163. { 0, 0, -1, 2, -1, 0, 0 },
  164. { 0, 0, 0, -1, 2, -1, 0 },
  165. { 0, 0, 0, 0, -1, 2, -1 },
  166. { -1, 0, 0, 0, 0, -1, 2 },
  167. };
  168. Rational *Cs[7] = { C[0], C[1], C[2], C[3], C[4], C[5], C[6] };
  169.  
  170. cout << "detC = " << determinant(Cs, 7) << endl;
  171.  
  172.  
  173.  
  174. Rational D[7][7] = {
  175. { 1, 2, 3, 4, 5, 6, 7 },
  176. { 2, 3, 4, 5, 6, 7, 6 },
  177. { 3, 4, 5, 6, 7, 6, 5 },
  178. { 4, 5, 6, 7, 6, 5, 4 },
  179. { 5, 6, 7, 6, 5, 4, 3 },
  180. { 6, 7, 6, 5, 4, 3, 2 },
  181. { 7, 6, 5, 4, 3, 2, 1 },
  182. };
  183. Rational *Ds[7] = { D[0], D[1], D[2], D[3], D[4], D[5], D[6] };
  184.  
  185. cout << "detD = " << determinant(Ds, 7) << endl;
  186.  
  187. return 0;
  188. }
  189.  
Success #stdin #stdout 0s 2904KB
stdin
Standard input is empty
stdout
Eliminacja Gaussa dla macierzy:
0	0	0	4	
0	2	0	0	
0	0	3	0	
1	0	0	0	

w4  <=>  w1
1	0	0	0	
0	2	0	0	
0	0	3	0	
0	0	0	4	

Eliminacja zakonczona sukcesem!
detA = -24


Eliminacja Gaussa dla macierzy:
2	-1	0	0	0	0	0	
-1	2	-1	0	0	0	0	
0	-1	2	-1	0	0	0	
0	0	-1	2	-1	0	0	
0	0	0	-1	2	-1	0	
0	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0	0	0	0	0	0	0	

Macierz jest osobliwa.
detC = 0
Eliminacja Gaussa dla macierzy:
1	2	3	4	5	6	7	
2	3	4	5	6	7	6	
3	4	5	6	7	6	5	
4	5	6	7	6	5	4	
5	6	7	6	5	4	3	
6	7	6	5	4	3	2	
7	6	5	4	3	2	1	

w2  :=  w2 - (2)*w1
w3  :=  w3 - (3)*w1
w4  :=  w4 - (4)*w1
w5  :=  w5 - (5)*w1
w6  :=  w6 - (6)*w1
w7  :=  w7 - (7)*w1

1	2	3	4	5	6	7	
0	-1	-2	-3	-4	-5	-8	
0	-2	-4	-6	-8	-12	-16	
0	-3	-6	-9	-14	-19	-24	
0	-4	-8	-14	-20	-26	-32	
0	-5	-12	-19	-26	-33	-40	
0	-8	-16	-24	-32	-40	-48	

w3  :=  w3 - (2)*w2
w4  :=  w4 - (3)*w2
w5  :=  w5 - (4)*w2
w6  :=  w6 - (5)*w2
w7  :=  w7 - (8)*w2

1	2	3	4	5	6	7	
0	-1	-2	-3	-4	-5	-8	
0	0	0	0	0	-2	0	
0	0	0	0	-2	-4	0	
0	0	0	-2	-4	-6	0	
0	0	-2	-4	-6	-8	0	
0	0	0	0	0	0	16	

w6  <=>  w3
1	2	3	4	5	6	7	
0	-1	-2	-3	-4	-5	-8	
0	0	-2	-4	-6	-8	0	
0	0	0	0	-2	-4	0	
0	0	0	-2	-4	-6	0	
0	0	0	0	0	-2	0	
0	0	0	0	0	0	16	

w5  <=>  w4
1	2	3	4	5	6	7	
0	-1	-2	-3	-4	-5	-8	
0	0	-2	-4	-6	-8	0	
0	0	0	-2	-4	-6	0	
0	0	0	0	-2	-4	0	
0	0	0	0	0	-2	0	
0	0	0	0	0	0	16	

Eliminacja zakonczona sukcesem!
detD = -256