Upload files to "ex1G_dense_matrices_access"
This commit is contained in:
parent
bdf01bd761
commit
394e1be746
4 changed files with 228 additions and 0 deletions
93
ex1G_dense_matrices_access/main.cpp
Normal file
93
ex1G_dense_matrices_access/main.cpp
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
#include "../utils/timing.h"
|
||||
#include "DenseMatrix.h"
|
||||
#include "ProductMatrix.h"
|
||||
#include <algorithm>
|
||||
|
||||
int main()
|
||||
{
|
||||
// b) ------------------------------------------------------------------------------------------------------
|
||||
DenseMatrix const M(5,3);
|
||||
vector<double> const u{{1, 2, 3}};
|
||||
vector<double> f1 = M.Mult(u);
|
||||
vector<double> const v{{-1, 2, -3, 4, -5}};
|
||||
vector<double> 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<double> x(n, 1.0);
|
||||
|
||||
size_t n_loops = 100;
|
||||
vector<double> y_1;
|
||||
vector<double> 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<double> 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<double> 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;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue