diff --git a/ex1G_dense_matrices_access/DenseMatrix.h b/ex1G_dense_matrices_access/DenseMatrix.h new file mode 100644 index 0000000..edf5160 --- /dev/null +++ b/ex1G_dense_matrices_access/DenseMatrix.h @@ -0,0 +1,71 @@ +#pragma once +#include "sigmoid.h" +#include +#include +using namespace std; + +class DenseMatrix +{ + private: + vector M; + size_t n; + size_t m; + + + public: + vector Mult(const vector &x) const + { + vector y(n,0); + for(size_t i = 0; i < n; ++i) // iterate row + { + for(size_t j = 0; j < m; ++j) // iterate column + { + y[i] += M[i*m + j]*x[j]; + } + } + return y; + } + + vector MultT(const vector &y) const + { + vector x(m,0); + for(size_t j = 0; j < m; ++j) // iterate column + { + for(size_t i = 0; i < n; ++i) // iterate row + { + x[j] += M[i*m + j]*y[i]; + } + } + return x; + } + + void Print() const + { + for(size_t i = 0; i < n; ++i) // iterate row + { + for(size_t j = 0; j < m; ++j) // iterate column + { + cout << M[i*m + j] << " "; + } + cout << endl; + } + cout << endl; + } + + + DenseMatrix(size_t n, size_t m) + { + this->n = n; + this->m = m; + M = vector(n*m); + size_t nm = max(n,m); + + for(size_t i = 0; i < n; ++i) // iterate row + { + for(size_t j = 0; j < m; ++j) // iterate column + { + M[i*m + j] = sigmoid(x_entry(i,nm))*sigmoid(x_entry(j,nm)); + } + } + } +}; \ No newline at end of file diff --git a/ex1G_dense_matrices_access/ProductMatrix.h b/ex1G_dense_matrices_access/ProductMatrix.h new file mode 100644 index 0000000..8f40764 --- /dev/null +++ b/ex1G_dense_matrices_access/ProductMatrix.h @@ -0,0 +1,52 @@ +#pragma once +#include +#include +#include +using namespace std; + +class ProductMatrix +{ + private: + vector u; + vector v; + size_t n; + size_t m; + + public: + vector Mult(const vector &x) const + { + vector y(n,0); + for(int i = 0; i < n; ++i) + { + for(int j = 0; j < m; ++j) + { + y[i] += v[j]*x[j]; + } + y[i] *= u[i]; + } + return y; + } + + vector MultT(const vector &y) const + { + vector x(m,0); + for(int j = 0; j < m; ++j) + { + for(int i = 0; i < n; ++i) + { + x[j] += y[i]*u[i]; + } + x[j] *= v[j]; + } + return x; + } + + ProductMatrix(const vector &u, const vector &v) + { + n = u.size(); + m = v.size(); + this->u = u; + this->v = v; + + } +}; \ No newline at end of file diff --git a/ex1G_dense_matrices_access/main.cpp b/ex1G_dense_matrices_access/main.cpp new file mode 100644 index 0000000..fede0cc --- /dev/null +++ b/ex1G_dense_matrices_access/main.cpp @@ -0,0 +1,93 @@ +#include "../utils/timing.h" +#include "DenseMatrix.h" +#include "ProductMatrix.h" +#include + +int main() +{ + // b) ------------------------------------------------------------------------------------------------------ + DenseMatrix const M(5,3); + vector const u{{1, 2, 3}}; + vector f1 = M.Mult(u); + vector const v{{-1, 2, -3, 4, -5}}; + vector f2 = M.MultT(v); + + M.Print(); + + for(size_t i = 0; i < f1.size(); ++i) + cout << f1[i] << endl; + cout << endl; + + + for(size_t j = 0; j < f2.size(); ++j) + cout << f2[j] << " "; + cout << endl << "-------------------------------------------------" << endl; + + // c) ------------------------------------------------------------------------------------------------------ + size_t n = pow(10,3); + DenseMatrix const M_1(n,n); + vector x(n, 1.0); + + size_t n_loops = 100; + vector y_1; + vector y_2; + + double time_1 = 0; + double time_2 = 0; + + tic(); + for(int l = 0; l < n_loops; ++l) + y_1 = M_1.Mult(x); + time_1 += toc(); + + tic(); + for(int l = 0; l < n_loops; ++l) + y_2 = M_1.MultT(x); + time_2 += toc(); + + vector error_vec(n,0); + for(int i = 0; i < n; ++i) + error_vec[i] = abs(y_1[i] - y_2[i]); + double sup_error = *max_element(error_vec.begin(), error_vec.end()); + + + cout << "n = " << n << endl; + cout << "Average duration for Mult: " << time_1/n_loops << endl; + cout << "Average duration for MultT: " << time_2/n_loops << endl; + cout << "sup-error: " << sup_error << endl; + cout << "-------------------------------------------------" << endl; + + // d) ------------------------------------------------------------------------------------------------------ + vector u_M(n,0); + for(int i = 0; i < n; ++i) + u_M[i] = sigmoid(x_entry(i, n)); + + ProductMatrix const M_2(u_M, u_M); + + time_1 = 0; + time_2 = 0; + + tic(); + for(int l = 0; l < n_loops; ++l) + y_1 = M_2.Mult(x); + time_1 += toc(); + + tic(); + for(int l = 0; l < n_loops; ++l) + y_2 = M_2.MultT(x); + time_2 += toc(); + + for(int i = 0; i < n; ++i) + error_vec[i] = abs(y_1[i] - y_2[i]); + sup_error = *max_element(error_vec.begin(), error_vec.end()); + + + cout << "n = " << n << endl; + cout << "Average duration for Mult: " << time_1/n_loops << endl; + cout << "Average duration for MultT: " << time_2/n_loops << endl; + cout << "sup-error: " << sup_error << endl; + cout << "-------------------------------------------------" << endl; + + + return 0; +} diff --git a/ex1G_dense_matrices_access/sigmoid.h b/ex1G_dense_matrices_access/sigmoid.h new file mode 100644 index 0000000..9fd581f --- /dev/null +++ b/ex1G_dense_matrices_access/sigmoid.h @@ -0,0 +1,12 @@ +#pragma once +#include + +double sigmoid(double x) +{ + return 1./(1. + exp(-x)); +} + +double x_entry(size_t k, size_t nm) +{ + return (10.*k)/(nm - 1) - 5.; +} \ No newline at end of file