multi.cpp 5.19 KB
Newer Older
1
2
3
4
5
6
/**
* @file multi.cpp
* @date 2020-05-14
* @version 1.0
*/

7
8
#include "Matrix.hpp"

9
// Value to cut the matrices
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
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 << "<body>\n";
//    cout << "<br>" << endl;
    cout << "<div class=\"container\">" << endl;


    string matrixs1 = "";
    string matrixs2 = "";
Mario Bajer's avatar
Mario Bajer committed
95
    string matrixs3 = "";
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
    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's avatar
Mario Bajer committed
114
115
116
117
118
119
120
121
122
    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;
    }

123
124
125
126
127
    string debug = "debug 0";
    cout << debug << endl;

    json json1;
    json json2;
Mario Bajer's avatar
Mario Bajer committed
128
    json json3;
129
130
    ifstream in1(matrixs1);
    ifstream in2(matrixs2);
Mario Bajer's avatar
Mario Bajer committed
131
//    ifstream in3(matrixs3);
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
    in1 >> json1;
    in2 >> json2;
    cout << "debug 1" << endl;
//    cout << "<p style=\"word-wrap: break-word\">" << json1 << "</p>" << endl;
//    cout << "<a href=\"http://localhost:80/index.html\">Back to Index</a>";
//    cout << "<p style=\"word-wrap: break-word\">" << json1 << "</p>" << endl;
    Matrix matrix1(2);
    vector<int> 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<int> 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's avatar
Mario Bajer committed
160
161
//    json3["id"] = result.ID;
//    json3["dimension"] = result.dim;
Mario Bajer's avatar
Mario Bajer committed
162
163
164
    int size = result.dim * result.dim;
    vector<int> v(result.data, result.data + size);
    json3["matrix"] = v;
Mario Bajer's avatar
Mario Bajer committed
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187

//    json json3;

//    srand(time(NULL));
//
//    result.ID = rand() % 100;
//    string folderpath = "Json/";
//    string testpath = folderpath + to_string(result.ID);
//    while(existsm(testpath)){
//        cout << "<p>" << "File mit ID: " << result.ID << " existiert bereits!" << "</p>";
//        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<int> v(result.data, result.data + size);
//    json3["matrix"] = v;
//    ofstream o;
//    o.open(folderpath);
//    o << json3 << endl;
//    o.close();
Mario Bajer's avatar
Mario Bajer committed
188
189

//    result.generateMatrix(result.dim);
190
191
192
    cout << "Resulting Matrix saved at ID: " + to_string(result.ID) << endl;

    cout << "</div>" << endl;
Mario Bajer's avatar
Mario Bajer committed
193
    cout << "<a href=\"http://localhost:80/index.html\">Back to Index</a>";
194
195
196
197
    cout << "</body>\n";
    cout << "</html>\n";
    return 0;
}