Advertisement
Guest User

Untitled

a guest
Mar 4th, 2015
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. //
  2. // main.cpp
  3. // Matrix
  4. //
  5. // Created by Anton Wetret on 02/03/15.
  6. // Copyright (c) 2015 mipt. All rights reserved.
  7. //
  8.  
  9. #include <iostream>
  10. #include <vector>
  11.  
  12. using namespace std;
  13.  
  14.  
  15. template <typename T>
  16. class Matrix {
  17. public:
  18. Matrix() {
  19.  
  20. }
  21. Matrix(int n, int m): _n(n), _m(m) {
  22.  
  23. }
  24. const int GetRows() {
  25. return _n;
  26. }
  27. const int GetCol() {
  28. return _m;
  29. }
  30. const T Trace() {
  31. T tr;
  32. for (int i = 0; i < _n; ++i) {
  33. tr += _data[i][i];
  34. }
  35. return tr;
  36. }
  37. const T Det() {
  38. vector<vector<double> > data = _data;
  39. int s;
  40. bool f;
  41. for (int i = 0; i < _m; ++i) {
  42. f = true;
  43. for (int j = i; j < _n-1; ++j) {
  44. if (data[i][i] == 0) {
  45. f = false;
  46. int k = i + 1;
  47. while (k < _n && data[k][i] == 0) { ++k; }
  48. if (k < _n) {
  49. s++;
  50. swap(data[i], data[k]);
  51. f = true;
  52. }
  53. }
  54. if (f) {
  55. T p = data[j+1][i]/data[i][i];
  56. for (int k = i; k < _m; ++k) {
  57. data[j+1][k] -= p * data[i][k];
  58. }
  59. }
  60. }
  61. }
  62. T det = 1;
  63. for (int i = 0; i < _m; ++i) {
  64. det *= data[i][i];
  65. }
  66. return det;
  67. }
  68. void Push(const vector<T> &temp) {
  69. _data.push_back(temp);
  70. }
  71. ///////////-----
  72. vector<T>* operator[](int i) {
  73. return &(_data[i]);
  74. }
  75. ///////-----
  76. Matrix& operator=(const Matrix& other) {
  77. if (this == &other) return *this;
  78. else {
  79. for (int i = 0; i < _n; ++i) {
  80. for (int j = 0; j < _m; ++j) {
  81. _data[i][j] = (*other[i])[j];
  82. }
  83. }
  84. return *this;
  85. }
  86. }
  87. Matrix& operator+=(const Matrix& other) {
  88. for (int i = 0; i < _n; ++i) {
  89. for (int j = 0; j < _m; ++j) {
  90. _data[i][j] += (*other[i])[j];
  91. }
  92. }
  93. return *this;
  94. }
  95. private:
  96. int _n; // Число строк
  97. int _m; // Число столбцов
  98. vector<vector<T> > _data; // Содержание матрицы
  99. };
  100.  
  101. template <typename T>
  102. istream& operator>>(istream& stream, Matrix<T>& mat) {
  103. T x;
  104. for (int i = 0; i < mat.GetRows(); ++i) {
  105. vector<T> temp;
  106. for (int j = 0; j < mat.GetCol(); ++j) {
  107. cin >> x;
  108. temp.push_back(x);
  109. }
  110. mat.Push(temp);
  111. }
  112. return stream;
  113. }
  114.  
  115. template <typename T>
  116. ostream& operator<<(ostream& stream, const Matrix<T>& mat) {
  117. for (int i = 0; i < mat.GetRows(); ++i) {
  118. for (int j = 0; j < mat.GetCol(); ++j) {
  119. stream << (*mat[i])[j] << ' ';
  120. }
  121. stream << endl;
  122. }
  123. stream << endl;
  124. return stream;
  125. }
  126.  
  127. template <typename T>
  128. Matrix<T> operator+(const Matrix<T>& first, const Matrix<T>& second) {
  129. Matrix<T> result(first);
  130. result += second;
  131. return result;
  132. }
  133.  
  134.  
  135. int main() {
  136. Matrix<double> m(2,2);
  137. Matrix<double> n(2,2);
  138. cin >> m;
  139. cin >> n;
  140. Matrix<double> c(2,2);
  141. c = m + n;
  142.  
  143. // cout << m+n;
  144. // cin >> n;
  145. // cout << (m + n);
  146. // c = m + n;
  147. // cout << m.Trace();
  148. return 0;
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement