diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..65bbf4a --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,24 @@ +#include "GeneralAlgorithm.hpp" +#include +#include + +double f(double x) { + return 100. * std::exp(-10.*x); +} + +double a_sol(double x) { + return 1. - (1. - std::exp(-10)) * x - std::exp(-10*x); +} + +int main() { + arma::mat A = arma::eye(3,3); + + GeneralAlgorithm ga(3, &A, f, a_sol, 0., 1.); + + ga.solve(); + std::cout << "Time: " << ga.time(5) << std::endl; + ga.error(); + + return 0; + +} diff --git a/src/simpleFile.cpp b/src/simpleFile.cpp index d055846..344e7a6 100644 --- a/src/simpleFile.cpp +++ b/src/simpleFile.cpp @@ -1,4 +1,12 @@ #include +#include +#include +#include +#include +#include +#include + +#define TIMING_ITERATIONS 5 arma::vec* general_algorithm( arma::vec* sub_diag, @@ -50,26 +58,105 @@ arma::vec* special_algorithm( return g_vec; } -arma::vec* error( +void error( + std::string filename, + arma::vec* x_vec, arma::vec* v_vec, arma::vec* a_vec ) { + std::ofstream ofile; + ofile.open(filename); -} - -double time() { - -} - -int main() { - int n = 10; - arma::vec x = arma::vec(n); - arma::vec y = arma::vec(n); - double step_size = 1.0 / n; - - for (int i = 0; i < n; i++) { - x(i) = (i+1) * step_size; - y(i) = 1.0 - (1.0 - exp(-10))*x(i) - exp(-10*x(i)); + if (!ofile.is_open()) { + exit(1); } + + for (int i=0; i < a_vec->n_elem; i++) { + double sub = (*a_vec)(i) - (*v_vec)(i); + ofile << std::setprecision(8) << std::scientific << (*x_vec)(i) + << std::setprecision(8) << std::scientific << std::log10(std::abs(sub)) + << std::setprecision(8) << std::scientific << std::log10(std::abs(sub/(*a_vec)(i))) + << std::endl; + } + + ofile.close(); + +} + +double f(double x) { + return 100*std::exp(-10*x); +} + +void build_array( + int n_steps, + arma::vec* sub_diag, + arma::vec* main_diag, + arma::vec* sup_diag, + arma::vec* g_vec +) +{ + sub_diag->resize(n_steps-2); + main_diag->resize(n_steps-1); + sup_diag->resize(n_steps-2); + + sub_diag->fill(-1); + main_diag->fill(2); + sup_diag->fill(-1); + + g_vec->resize(n_steps-1); + + double step_size = 1./ (double) n_steps; + for (int i=0; i < n_steps-1; i++) { + (*g_vec)(i) = f((i+1)*step_size); + } + +} + +void timing() { + arma::vec sub_diag, main_diag, sup_diag, g_vec; + int n_steps; + + std::ofstream ofile; + ofile.open("timing.txt"); + + // Timing + for (int i=1; i <= 8; i++) { + n_steps = std::pow(10, i); + clock_t g_1, g_2, s_1, s_2; + double g_res = 0, s_res = 0; + + for (int j=0; j < TIMING_ITERATIONS; j++) { + build_array(n_steps, &sub_diag, &main_diag, &sup_diag, &g_vec); + + g_1 = clock(); + + general_algorithm(&sub_diag, &main_diag, &sup_diag, &g_vec); + + g_2 = clock(); + + g_res += (double) (g_2 - g_1) / CLOCKS_PER_SEC; + build_array(n_steps, &sub_diag, &main_diag, &sup_diag, &g_vec); + + s_1 = clock(); + + special_algorithm(-1., 2., -1., &g_vec); + + s_2 = clock(); + + s_res += (double) (s_2 - s_1) / CLOCKS_PER_SEC; + + } + ofile + << n_steps << "," + << g_res / (double) TIMING_ITERATIONS << "," + << s_res / (double) TIMING_ITERATIONS << std::endl; + } + + ofile.close(); +} + +int main() +{ + timing(); }