Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define dbg(x) cerr<<#x": "<<x<<"\n"
- #define dbg_p(x) cerr<<#x": "<<x.first<<","<<x.second<<"\n"
- #define dbg_v(x, n) do{cerr<<#x"[]: ";for(int _=0;_<n;++_)cerr<<x[_]<<" ";cerr<<'\n';}while(0)
- #define dbg_ok cerr<<"OK!\n"
- #define DMAX 1
- #define NMAX 1
- #define MMAX 1
- using namespace std;
- int n, k, x;
- string s;
- double getPrecision() {
- double u = 0.1;
- while(1.0 - u != 1.0) u /= 10.0;
- return u * 10.0;
- }
- double getMulPrecision() {
- double u = 0.1;
- while(1.0 * (1.0 + u) != 1.0) u /= 10.0;
- return u * 10.0;
- }
- class matrix {
- public:
- int n;
- vector <vector <double> > v;
- matrix(int _n) {
- n = _n;
- for(int i = 0; i < n; i++)
- v.push_back(*(new vector<double>(n, 0.0)));
- }
- matrix(string nume_fisier) {
- ifstream fin(nume_fisier);
- fin >> n;
- for(int i = 0; i < n; i++)
- v.push_back(vector<double>(n, 0.0));
- for(int i = 0; i < n ; i++)
- for(int j = 0; j < n ;j++)
- fin >> v[i][j];
- }
- matrix& operator+(matrix & m) {
- // if(n != m.n) return *nullptr;
- matrix *ans = new matrix(n);
- for(int i = 0; i < n; i++)
- for(int j = 0; j < n; j++)
- ans->v[i][j] = m.v[i][j] + v[i][j];
- return *ans;
- }
- matrix& operator-(matrix & m) {
- // if(n != m.n) return *nullptr;
- matrix *ans = new matrix(n);
- for(int i = 0; i < n; i++)
- for(int j = 0; j < n; j++)
- ans->v[i][j] = v[i][j] - m.v[i][j];
- return *ans;
- }
- matrix& operator*(matrix & m) {
- matrix *ans = new matrix(n);
- for(int i = 0; i < n; i++)
- for(int j = 0; j < n; j++)
- for(int k = 0; k < n; k++)
- ans->v[i][j] += v[i][k] * m.v[k][j];
- return *ans;
- }
- matrix& operator=(matrix &m) {
- v.clear();
- n = m.n;
- for(int i = 0; i < n; i++)
- v.push_back(*(new vector<double>(n, 0.0)));
- for(int i = 0; i < m.n; i++)
- for(int j = 0; j < m.n; j++)
- v[i][j] = m.v[i][j];
- return *this;
- }
- matrix& get(int x, int y) {
- matrix * ans = new matrix(n/2);
- for(int i = 0; i < n/2; i++)
- for(int j = 0; j < n/2; j++)
- ans->v[i][j] = v[i + x*(n/2)][j + y*(n/2)];
- return *ans;
- }
- };
- ostream& operator<<(ostream& out, matrix & m)
- {
- for(auto l : m.v) {
- for(auto e : l) out << e << ' ';
- cout << '\n';
- }
- return out;
- }
- matrix& build(matrix & c11, matrix & c12, matrix & c21, matrix & c22) {
- matrix * ans = new matrix(2 * c11.n);
- int n = ans->n;
- for(int i = 0; i < n/2; i++)
- for(int j = 0; j < n/2; j++)
- ans->v[i][j] = c11.v[i][j];
- for(int i = 0; i < n/2; i++)
- for(int j = n/2; j < n; j++)
- ans->v[i][j] = c12.v[i][j - n/2];
- for(int i = n/2; i < n; i++)
- for(int j = 0; j < n/2; j++)
- ans->v[i][j] = c21.v[i - n/2][j];
- for(int i = n/2; i < n; i++)
- for(int j = n/2; j < n; j++)
- ans->v[i][j] = c22.v[i - n/2][j - n/2];
- dbg(c11);
- return *ans;
- }
- matrix& multiply_Strassen(matrix & a, matrix & b, int n_min) {
- // generalizare pt orice N - BOUNS de Implementare!!! , padding cu 0, pana la 2 ^ 3 :)
- if(a.n <= n_min) return a * b;
- matrix a11 = a.get(0, 0);
- matrix a12 = a.get(0, 1);
- matrix a21 = a.get(1, 0);
- matrix a22 = a.get(1, 1);
- matrix b11 = b.get(0, 0);
- matrix b12 = b.get(0, 1);
- matrix b21 = b.get(1, 0);
- matrix b22 = b.get(1, 1);
- matrix p1 = multiply_Strassen(a11 + a22, b11 + b22, n_min);
- matrix p2 = multiply_Strassen(a21 + a22, b11, n_min);
- matrix p3 = multiply_Strassen(a11, b12 - b22, n_min);
- matrix p4 = multiply_Strassen(a22, b21 - b11, n_min);
- matrix p5 = multiply_Strassen(a11 + a12, b22, n_min);
- matrix p6 = multiply_Strassen(a21 - a11, b11 + b12, n_min);
- matrix p7 = multiply_Strassen(a12 - a22, b21 + b22, n_min);
- matrix c11 = p1 + p4 - p5 + p7;
- matrix c12 = p3 + p5;
- matrix c21 = p2 + p4;
- matrix c22 = p1 + p3 - p2 + p6;
- return build(c11, c12, c21, c22);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement