multi.cpp 5.11 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 ``````#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 << "
" << endl; cout << "
" << endl; string matrixs1 = ""; string matrixs2 = ""; `````` Mario Bajer committed May 11, 2020 88 `````` string matrixs3 = ""; `````` Mario Bajer committed May 08, 2020 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 `````` form_iterator fi1 = formData.getElement("multi1"); if( !fi1->isEmpty() && fi1 != (*formData).end()) { matrixs1 = **fi1; cout << matrixs1 << endl; } else { cout << "Error on Matrix 1!" << endl; } form_iterator fi2 = formData.getElement("multi2"); if( !fi2->isEmpty() && fi2 != (*formData).end()) { matrixs2 = **fi2; cout << matrixs2 << endl; } else { cout << "Error on Matrix 2!" << endl; } `````` Mario Bajer committed May 11, 2020 107 108 109 110 111 112 113 114 115 `````` form_iterator fi3 = formData.getElement("multi3"); if( !fi3->isEmpty() && fi3 != (*formData).end()) { matrixs3 = **fi3; cout << matrixs3 << endl; } else { cout << "Error on Matrix 3 (result)!" << endl; } `````` Mario Bajer committed May 08, 2020 116 117 118 119 120 `````` string debug = "debug 0"; cout << debug << endl; json json1; json json2; `````` Mario Bajer committed May 11, 2020 121 `````` json json3; `````` Mario Bajer committed May 08, 2020 122 123 `````` ifstream in1(matrixs1); ifstream in2(matrixs2); `````` Mario Bajer committed May 11, 2020 124 ``````// ifstream in3(matrixs3); `````` Mario Bajer committed May 08, 2020 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 `````` in1 >> json1; in2 >> json2; cout << "debug 1" << endl; // cout << "

" << json1 << "

" << endl; // cout << "Back to Index"; // cout << "

" << json1 << "

" << endl; Matrix matrix1(2); vector v1 = json1["matrix"]; matrix1.ID = json1["id"]; matrix1.dim = json1["dimension"]; matrix1.data = &v1[0]; cout << to_string(matrix1.ID) << endl; // matrix.print(); Matrix matrix2(2); vector v2 = json1["matrix"]; matrix2.ID = json1["id"]; matrix2.dim = json1["dimension"]; matrix2.data = &v1[0]; cout << to_string(matrix2.ID) << endl; cout << "debug 2" << endl; Matrix result(matrix1.dim); cout << "dim for result" << to_string(result.dim) << endl; cout << "debug 3" << endl; result = run(mult_strassen, matrix1, matrix2); cout << "debug 4"; `````` Mario Bajer committed May 11, 2020 153 154 ``````// json3["id"] = result.ID; // json3["dimension"] = result.dim; `````` Mario Bajer committed May 08, 2020 155 156 157 `````` int size = result.dim * result.dim; vector v(result.data, result.data + size); json3["matrix"] = v; `````` Mario Bajer committed May 11, 2020 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 `````` // json json3; // srand(time(NULL)); // // result.ID = rand() % 100; // string folderpath = "Json/"; // string testpath = folderpath + to_string(result.ID); // while(existsm(testpath)){ // cout << "

" << "File mit ID: " << result.ID << " existiert bereits!" << "

"; // result.ID = rand() % 100; // testpath = folderpath + to_string(result.ID); // } // folderpath += to_string(result.ID) + ".json"; // json3["id"] = result.ID; // json3["dimension"] = result.dim; // int size = result.dim * result.dim; // vector v(result.data, result.data + size); // json3["matrix"] = v; // ofstream o; // o.open(folderpath); // o << json3 << endl; // o.close(); `````` Mario Bajer committed May 08, 2020 181 182 `````` // result.generateMatrix(result.dim); `````` Mario Bajer committed May 08, 2020 183 184 185 `````` cout << "Resulting Matrix saved at ID: " + to_string(result.ID) << endl; cout << "
" << endl; `````` Mario Bajer committed May 08, 2020 186 `````` cout << "Back to Index"; `````` Mario Bajer committed May 08, 2020 187 188 189 190 `````` cout << "\n"; cout << "\n"; return 0; }``````