Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.76 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define dbg(x) cerr<<#x": "<<x<<"\n"
  4. #define dbg_p(x) cerr<<#x": "<<x.first<<","<<x.second<<"\n"
  5. #define dbg_v(x, n) do{cerr<<#x"[]: ";for(int _=0;_<n;++_)cerr<<x[_]<<" ";cerr<<'\n';}while(0)
  6. #define dbg_ok cerr<<"OK!\n"
  7.  
  8. #define DMAX 1
  9. #define NMAX 1
  10. #define MMAX 1
  11.  
  12. using namespace std;
  13.  
  14. int n, k, x;
  15. string s;
  16.  
  17.  
  18. double getPrecision() {
  19. double u = 0.1;
  20. while(1.0 - u != 1.0) u /= 10.0;
  21. return u * 10.0;
  22. }
  23.  
  24. double getMulPrecision() {
  25. double u = 0.1;
  26. while(1.0 * (1.0 + u) != 1.0) u /= 10.0;
  27. return u * 10.0;
  28. }
  29.  
  30. class matrix {
  31. public:
  32. int n;
  33. vector <vector <double> > v;
  34.  
  35. matrix(int _n) {
  36. n = _n;
  37. for(int i = 0; i < n; i++)
  38. v.push_back(*(new vector<double>(n, 0.0)));
  39. }
  40.  
  41. matrix(string nume_fisier) {
  42. ifstream fin(nume_fisier);
  43. fin >> n;
  44.  
  45. for(int i = 0; i < n; i++)
  46. v.push_back(vector<double>(n, 0.0));
  47.  
  48. for(int i = 0; i < n ; i++)
  49. for(int j = 0; j < n ;j++)
  50. fin >> v[i][j];
  51. }
  52.  
  53. matrix& operator+(matrix & m) {
  54. // if(n != m.n) return *nullptr;
  55. matrix *ans = new matrix(n);
  56. for(int i = 0; i < n; i++)
  57. for(int j = 0; j < n; j++)
  58. ans->v[i][j] = m.v[i][j] + v[i][j];
  59. return *ans;
  60. }
  61.  
  62. matrix& operator-(matrix & m) {
  63. // if(n != m.n) return *nullptr;
  64. matrix *ans = new matrix(n);
  65. for(int i = 0; i < n; i++)
  66. for(int j = 0; j < n; j++)
  67. ans->v[i][j] = v[i][j] - m.v[i][j];
  68. return *ans;
  69. }
  70.  
  71. matrix& operator*(matrix & m) {
  72. matrix *ans = new matrix(n);
  73. for(int i = 0; i < n; i++)
  74. for(int j = 0; j < n; j++)
  75. for(int k = 0; k < n; k++)
  76. ans->v[i][j] += v[i][k] * m.v[k][j];
  77. return *ans;
  78. }
  79.  
  80. matrix& operator=(matrix &m) {
  81. v.clear();
  82. n = m.n;
  83. for(int i = 0; i < n; i++)
  84. v.push_back(*(new vector<double>(n, 0.0)));
  85.  
  86. for(int i = 0; i < m.n; i++)
  87. for(int j = 0; j < m.n; j++)
  88. v[i][j] = m.v[i][j];
  89. return *this;
  90. }
  91.  
  92. matrix& get(int x, int y) {
  93. matrix * ans = new matrix(n/2);
  94. for(int i = 0; i < n/2; i++)
  95. for(int j = 0; j < n/2; j++)
  96. ans->v[i][j] = v[i + x*(n/2)][j + y*(n/2)];
  97. return *ans;
  98. }
  99.  
  100. };
  101.  
  102.  
  103. ostream& operator<<(ostream& out, matrix & m)
  104. {
  105. for(auto l : m.v) {
  106. for(auto e : l) out << e << ' ';
  107. cout << '\n';
  108. }
  109. return out;
  110. }
  111.  
  112. matrix& build(matrix & c11, matrix & c12, matrix & c21, matrix & c22) {
  113. matrix * ans = new matrix(2 * c11.n);
  114. int n = ans->n;
  115. for(int i = 0; i < n/2; i++)
  116. for(int j = 0; j < n/2; j++)
  117. ans->v[i][j] = c11.v[i][j];
  118.  
  119. for(int i = 0; i < n/2; i++)
  120. for(int j = n/2; j < n; j++)
  121. ans->v[i][j] = c12.v[i][j - n/2];
  122.  
  123. for(int i = n/2; i < n; i++)
  124. for(int j = 0; j < n/2; j++)
  125. ans->v[i][j] = c21.v[i - n/2][j];
  126.  
  127. for(int i = n/2; i < n; i++)
  128. for(int j = n/2; j < n; j++)
  129. ans->v[i][j] = c22.v[i - n/2][j - n/2];
  130. dbg(c11);
  131. return *ans;
  132. }
  133.  
  134. matrix& multiply_Strassen(matrix & a, matrix & b, int n_min) {
  135. // generalizare pt orice N - BOUNS de Implementare!!! , padding cu 0, pana la 2 ^ 3 :)
  136.  
  137. if(a.n <= n_min) return a * b;
  138.  
  139. matrix a11 = a.get(0, 0);
  140. matrix a12 = a.get(0, 1);
  141. matrix a21 = a.get(1, 0);
  142. matrix a22 = a.get(1, 1);
  143.  
  144. matrix b11 = b.get(0, 0);
  145. matrix b12 = b.get(0, 1);
  146. matrix b21 = b.get(1, 0);
  147. matrix b22 = b.get(1, 1);
  148.  
  149. matrix p1 = multiply_Strassen(a11 + a22, b11 + b22, n_min);
  150. matrix p2 = multiply_Strassen(a21 + a22, b11, n_min);
  151. matrix p3 = multiply_Strassen(a11, b12 - b22, n_min);
  152. matrix p4 = multiply_Strassen(a22, b21 - b11, n_min);
  153. matrix p5 = multiply_Strassen(a11 + a12, b22, n_min);
  154. matrix p6 = multiply_Strassen(a21 - a11, b11 + b12, n_min);
  155. matrix p7 = multiply_Strassen(a12 - a22, b21 + b22, n_min);
  156.  
  157. matrix c11 = p1 + p4 - p5 + p7;
  158. matrix c12 = p3 + p5;
  159. matrix c21 = p2 + p4;
  160. matrix c22 = p1 + p3 - p2 + p6;
  161. return build(c11, c12, c21, c22);
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement