multi.cpp 3.82 KB
 Mario Bajer committed May 08, 2020 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 ``````#include "Matrix.hpp" int cut = 64; Matrix mult_std(Matrix a, Matrix b) { Matrix c(a.dim); for (int i = 0; i < a.dim; i++) for (int k = 0; k < a.dim; k++) for (int j = 0; j < a.dim; j++) c(i,j) += a(i,k) * b(k,j); return c; } Matrix get_part(int pi, int pj, Matrix m) { Matrix p(m.dim / 2); pi = pi * p.dim; pj = pj * p.dim; for (int i = 0; i < p.dim; i++) for (int j = 0; j < p.dim; j++) p(i,j) = m(i + pi,j + pj); return p; } void set_part(int pi, int pj, Matrix* m, Matrix p) { pi = pi * p.dim; pj = pj * p.dim; for (int i = 0; i < p.dim; i++) for (int j = 0; j < p.dim; j++) (*m)(i + pi,j + pj) = p(i,j); } Matrix mult_strassen(Matrix a, Matrix b) { if (a.dim <= cut) return mult_std(a, b); Matrix a11 = get_part(0, 0, a); Matrix a12 = get_part(0, 1, a); Matrix a21 = get_part(1, 0, a); Matrix a22 = get_part(1, 1, a); Matrix b11 = get_part(0, 0, b); Matrix b12 = get_part(0, 1, b); Matrix b21 = get_part(1, 0, b); Matrix b22 = get_part(1, 1, b); Matrix m1 = mult_strassen(a11 + a22, b11 + b22); Matrix m2 = mult_strassen(a21 + a22, b11); Matrix m3 = mult_strassen(a11, b12 - b22); Matrix m4 = mult_strassen(a22, b21 - b11); Matrix m5 = mult_strassen(a11 + a12, b22); Matrix m6 = mult_strassen(a21 - a11, b11 + b12); Matrix m7 = mult_strassen(a12 - a22, b21 + b22); Matrix c(a.dim); set_part(0, 0, &c, m1 + m4 - m5 + m7); set_part(0, 1, &c, m3 + m5); set_part(1, 0, &c, m2 + m4); set_part(1, 1, &c, m1 - m2 + m3 + m6); return c; } Matrix run(Matrix (*f)(Matrix, Matrix), Matrix a, Matrix b) { Matrix c(a.dim); if(a.dim != b.dim) { cout << "Dimensions of Matrices do not match!" << endl; return c; } c = f(a, b); return c; } int main(){ Cgicc formData; htmlHeader("multi.cgi"); cout << "\n"; // cout << "