Commit 21e906d2 authored by Mario Bajer's avatar Mario Bajer
Browse files

added changes based on feedback from haas.c

made +- const
removed 2nd ()overload
fixed binary error for multiplication
parent fa51fdaa
...@@ -36,6 +36,7 @@ void Matrix::print(){ ...@@ -36,6 +36,7 @@ void Matrix::print(){
int Matrix::generateMatrix(int dimension){ int Matrix::generateMatrix(int dimension){
dim = dimension; dim = dimension;
delete [] data;
data = new int[dim * dim]; data = new int[dim * dim];
srand(time(NULL)); srand(time(NULL));
for (int i = 0; i < dim * dim; i++) for (int i = 0; i < dim * dim; i++)
...@@ -59,29 +60,18 @@ int Matrix::generateMatrix(int dimension){ ...@@ -59,29 +60,18 @@ int Matrix::generateMatrix(int dimension){
ifstream in1(configloc); ifstream in1(configloc);
in1 >> jconfig; in1 >> jconfig;
// in.close();
this->ID = jconfig["counter"]; //rand() % 100; this->ID = jconfig["counter"];
// cout << "<p> this->ID=" << this->ID << endl;
int temp_ID = jconfig["counter"]; int temp_ID = jconfig["counter"];
temp_ID++; temp_ID++;
// cout << "<p> temp_ID=" << temp_ID << endl;
jconfig["counter"] = temp_ID; jconfig["counter"] = temp_ID;
ofstream writecounter; ofstream writecounter;
writecounter.open(configloc); writecounter.open(configloc);
writecounter << jconfig << endl; writecounter << jconfig << endl;
writecounter.close(); writecounter.close();
// cout << "<p> counter in json=" << jconfig["counter"] << endl;
// cout << "<p>" << "post ID setzen" << "</p>" << endl;
string folderpath = "Json/"; string folderpath = "Json/";
// string testpath = folderpath + to_string(this->ID);
// while(existsm(testpath)){
// cout << "<p>" << "File mit ID: " << this->ID << " existiert bereits!" << "</p>";
// this->ID = rand() % 100;
// testpath = folderpath + to_string(this->ID);
// }
folderpath += to_string(this->ID) + ".json"; folderpath += to_string(this->ID) + ".json";
json j; json j;
...@@ -104,7 +94,6 @@ Matrix::Matrix(int dim) { ...@@ -104,7 +94,6 @@ Matrix::Matrix(int dim) {
while (dim2 < dim) while (dim2 < dim)
dim2 *= 2; dim2 *= 2;
this->dim = dim2; this->dim = dim2;
// this->ID = 101;
this->data = new int[dim * dim]; this->data = new int[dim * dim];
...@@ -113,21 +102,20 @@ Matrix::Matrix(int dim) { ...@@ -113,21 +102,20 @@ Matrix::Matrix(int dim) {
} }
Matrix Matrix::operator+(Matrix b) { Matrix Matrix::operator+(const Matrix& b) const{
Matrix c(b.dim); Matrix c(b.dim);
for (int i = 0; i < b.dim; i++) for (int i = 0; i < b.dim; i++)
for (int j = 0; j < b.dim; j++) for (int j = 0; j < b.dim; j++)
c(i,j) = (*this)(i,j) + b(i,j); c(i,j) = ((*this)(i,j) + b(i,j)) % 2;
return c; return c;
} }
Matrix Matrix::operator-(Matrix b) { Matrix Matrix::operator-(const Matrix& b) const{
Matrix c(b.dim); Matrix c(b.dim);
// this->dim = b.dim;
for (int i = 0; i < this->dim; i++) for (int i = 0; i < this->dim; i++)
for (int j = 0; j < this->dim; j++) for (int j = 0; j < this->dim; j++)
c(i,j) = (*this)(i,j) - b(i,j); c(i,j) = ((*this)(i,j) - b(i,j)) % 2;
return c; return c;
} }
\ No newline at end of file
...@@ -24,25 +24,25 @@ using json = nlohmann::json; ...@@ -24,25 +24,25 @@ using json = nlohmann::json;
*/ */
class Matrix{ class Matrix{
public: public:
Matrix(int dim); explicit Matrix(int dim);
int ID; int ID{};
int dim; int dim;
int *data; int *data;
int generateMatrix(int dimension); int generateMatrix(int dimension);
void print(); void print();
inline int& operator()(unsigned row, unsigned col) { inline int& operator()(unsigned row, unsigned col) const {
return data[dim*row + col]; return data[dim*row + col];
} }
inline int operator()(unsigned row, unsigned col) const { // inline int operator()(unsigned row, unsigned col) const {
return data[dim*row + col]; // return data[dim*row + col];
} // }
Matrix operator+(Matrix b); Matrix operator+(const Matrix& b) const ;
Matrix operator-(Matrix b); Matrix operator-(const Matrix& b) const ;
private: private:
......
...@@ -14,7 +14,7 @@ Matrix mult_std(Matrix a, Matrix b) { ...@@ -14,7 +14,7 @@ Matrix mult_std(Matrix a, Matrix b) {
for (int i = 0; i < a.dim; i++) for (int i = 0; i < a.dim; i++)
for (int k = 0; k < a.dim; k++) for (int k = 0; k < a.dim; k++)
for (int j = 0; j < a.dim; j++) for (int j = 0; j < a.dim; j++)
c(i,j) = (c(i,j) + a(i,k) * b(k,j)) % 2; c(i,j) = (c(i,j) + (a(i,k) * b(k,j))) % 2;
return c; return c;
} }
...@@ -26,7 +26,7 @@ Matrix get_part(int pi, int pj, Matrix m) { ...@@ -26,7 +26,7 @@ Matrix get_part(int pi, int pj, Matrix m) {
for (int i = 0; i < p.dim; i++) for (int i = 0; i < p.dim; i++)
for (int j = 0; j < p.dim; j++) for (int j = 0; j < p.dim; j++)
p(i,j) = m(i + pi,j + pj); p(i,j) = m(i + pi,j + pj) % 2;
return p; return p;
} }
...@@ -37,7 +37,7 @@ void set_part(int pi, int pj, Matrix* m, Matrix p) { ...@@ -37,7 +37,7 @@ void set_part(int pi, int pj, Matrix* m, Matrix p) {
for (int i = 0; i < p.dim; i++) for (int i = 0; i < p.dim; i++)
for (int j = 0; j < p.dim; j++) for (int j = 0; j < p.dim; j++)
(*m)(i + pi,j + pj) = p(i,j); (*m)(i + pi,j + pj) = p(i,j) % 2;
} }
Matrix mult_strassen(Matrix a, Matrix b) { Matrix mult_strassen(Matrix a, Matrix b) {
...@@ -74,7 +74,7 @@ Matrix mult_strassen(Matrix a, Matrix b) { ...@@ -74,7 +74,7 @@ Matrix mult_strassen(Matrix a, Matrix b) {
Matrix run(Matrix (*f)(Matrix, Matrix), Matrix a, Matrix b) { Matrix run(Matrix (*f)(Matrix, Matrix), Matrix a, Matrix b) {
Matrix c(a.dim); Matrix c(a.dim);
if(a.dim != b.dim) { if(a.dim != b.dim) {
cout << "Dimensions of Matrices do not match!" << endl; cout << "ERROR: Dimensions of Matrices do not match!" << endl;
return c; return c;
} }
c = f(a, b); c = f(a, b);
...@@ -86,10 +86,8 @@ int main(){ ...@@ -86,10 +86,8 @@ int main(){
htmlHeader("multi.cgi"); htmlHeader("multi.cgi");
cout << "<body>\n"; cout << "<body>\n";
// cout << "<br>" << endl;
cout << "<div class=\"container\">" << endl; cout << "<div class=\"container\">" << endl;
string matrixs1 = ""; string matrixs1 = "";
string matrixs2 = ""; string matrixs2 = "";
string matrixs3 = ""; string matrixs3 = "";
...@@ -97,7 +95,7 @@ int main(){ ...@@ -97,7 +95,7 @@ int main(){
form_iterator fi1 = formData.getElement("multi1"); form_iterator fi1 = formData.getElement("multi1");
if( !fi1->isEmpty() && fi1 != (*formData).end()) { if( !fi1->isEmpty() && fi1 != (*formData).end()) {
matrixs1 = "Json/" + **fi1 + ".json"; matrixs1 = "Json/" + **fi1 + ".json";
cout << matrixs1 << endl; cout << "Multiplying " << matrixs1 << " with " << endl;
} }
else { else {
cout << "Error on Matrix 1!" << endl; cout << "Error on Matrix 1!" << endl;
...@@ -106,7 +104,7 @@ int main(){ ...@@ -106,7 +104,7 @@ int main(){
form_iterator fi2 = formData.getElement("multi2"); form_iterator fi2 = formData.getElement("multi2");
if( !fi2->isEmpty() && fi2 != (*formData).end()) { if( !fi2->isEmpty() && fi2 != (*formData).end()) {
matrixs2 = "Json/" + **fi2 + ".json"; matrixs2 = "Json/" + **fi2 + ".json";
cout << matrixs2 << endl; cout << matrixs2 << " and storing result in " << endl;
} }
else { else {
cout << "Error on Matrix 2!" << endl; cout << "Error on Matrix 2!" << endl;
...@@ -122,45 +120,27 @@ int main(){ ...@@ -122,45 +120,27 @@ int main(){
cout << "Error on Matrix 3 (result)!" << endl; cout << "Error on Matrix 3 (result)!" << endl;
} }
string debug = "debug 0";
cout << debug << endl;
json json1; json json1;
json json2; json json2;
json json3; json json3;
ifstream in1(matrixs1); ifstream in1(matrixs1);
ifstream in2(matrixs2); ifstream in2(matrixs2);
// ifstream in3(matrixs3);
in1 >> json1; in1 >> json1;
in2 >> json2; 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); Matrix matrix1(2);
vector<int> v1 = json1["matrix"]; const vector<int>& v1 = json1["matrix"];
matrix1.ID = json1["id"]; matrix1.ID = json1["id"];
matrix1.dim = json1["dimension"]; matrix1.dim = json1["dimension"];
matrix1.data = &v1[0]; matrix1.data = const_cast<int *>(&v1[0]);
cout << to_string(matrix1.ID) << endl;
// matrix.print();
Matrix matrix2(2); Matrix matrix2(2);
vector<int> v2 = json1["matrix"]; const vector<int>& v2 = json1["matrix"];
matrix2.ID = json1["id"]; matrix2.ID = json2["id"];
matrix2.dim = json1["dimension"]; matrix2.dim = json2["dimension"];
matrix2.data = &v1[0]; matrix2.data = const_cast<int *>(&v2[0]);
cout << to_string(matrix2.ID) << endl;
cout << "debug 2" << endl;
Matrix result(matrix1.dim); Matrix result(matrix1.dim);
cout << "dim for result" << to_string(result.dim) << endl;
cout << "debug 3" << endl;
result = run(mult_strassen, matrix1, matrix2); result = run(mult_strassen, matrix1, matrix2);
cout << "debug 4";
// json3["id"] = result.ID;
// json3["dimension"] = result.dim;
int size = result.dim * result.dim; int size = result.dim * result.dim;
vector<int> v(result.data, result.data + size); vector<int> v(result.data, result.data + size);
json3["id"] = targetID; json3["id"] = targetID;
...@@ -171,27 +151,6 @@ int main(){ ...@@ -171,27 +151,6 @@ int main(){
o << json3 << endl; o << json3 << endl;
o.close(); o.close();
// 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();
// result.generateMatrix(result.dim);
cout << "Resulting Matrix saved at ID: " + to_string(result.ID) << endl;
cout << "</div>" << endl; cout << "</div>" << endl;
cout << "<a href=\"http://localhost:80/index.html\">Back to Index</a>"; cout << "<a href=\"http://localhost:80/index.html\">Back to Index</a>";
cout << "</body>\n"; cout << "</body>\n";
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment