diff --git a/ex1/ABCEFG/skalar_stl/main.cpp b/ex1/ABCEFG/skalar_stl/main.cpp index d2bd2b0..ba34d8b 100644 --- a/ex1/ABCEFG/skalar_stl/main.cpp +++ b/ex1/ABCEFG/skalar_stl/main.cpp @@ -13,7 +13,7 @@ #include using namespace std; -void task_a() { +static void task_a() { printf("\n\n-------------- Task A --------------\n\n"); auto [a,b,c] = means0(1,4,16); @@ -30,7 +30,7 @@ void task_a() { } -void task_b() { +static void task_b() { printf("\n\n-------------- Task B --------------\n\n"); // Read vector @@ -57,11 +57,11 @@ void task_b() { printf("Harmonic: %f\n", z); // deviation - double deviation; + double deviation(0.0); for (long unsigned int i=0; i(a.size())); printf("Deviation: %f\n", deviation); // write results to file @@ -70,14 +70,14 @@ void task_b() { } -void task_c() { +static void task_c() { printf("\n\n-------------- Task C --------------\n\n"); vector n_values = {15, 1001, 1432987}; for (int n : n_values) { printf("n = %d\n", n); - double sum = 0; + long int sum = 0; double loops = 1000; // Timing first function @@ -86,24 +86,24 @@ void task_c() { sum = sum_of_spec(n); } double sec1 = toc(); - printf("For-loop funtion: result = %.f | time = %f milliseconds\n", sum, sec1*1000); + printf("For-loop funtion: result = %ld | time = %f milliseconds\n", sum, sec1*1000); // Timing second function tic(); for (int i=0; i(formula(n)); } double sec2 = toc(); - printf("Formula funtion: result = %.f | time = %f milliseconds\n", sum, sec2*1000); + printf("Formula funtion: result = %ld | time = %f milliseconds\n", sum, sec2*1000); } } -void task_d() { +static void task_d() { printf("\n\n-------------- Task D --------------\n\n"); printf("See folder D.\n"); } -void task_e() { +static void task_e() { printf("\n\n-------------- Task E --------------\n\n"); for (int n : {100, 1000, 10000}) { @@ -130,7 +130,7 @@ void task_e() { } } -void task_f() { +static void task_f() { printf("\n\n-------------- Task F --------------\n\n"); // single_goldbach(k) @@ -161,7 +161,7 @@ void task_f() { } -void task_g() { +static void task_g() { printf("\n\n-------------- Task G --------------\n\n"); DenseMatrix const M(5,3); @@ -188,7 +188,7 @@ void task_g() { // ####################################################### int const NLOOPS = 100; - int const n = 1000; + int const n = 3000; // Time initialization tic(); @@ -200,7 +200,7 @@ void task_g() { // Time Mult tic(); vector f3 = M2.Mult(w); - for (int k=1; k f4 = M2.MultT(w); - for (int k=1; k 1e-4) @@ -233,14 +233,14 @@ void task_g() { // Time initialization tic(); vector x(n,0); - for (int i=0; i(i))/(n-1) - 5 );} DenseMatrix2 M3(x,x); double t4 = toc(); // Time Mult tic(); vector f5 = M3.Mult(w); - for (int k=1; k f6 = M3.MultT(w); - for (int k=1; k 1e-4) @@ -269,9 +269,6 @@ void task_g() { } } - printf("\nNOTE: difference in runtime noticable with n=10.000 (~30 seconds)\n"); - - } @@ -286,4 +283,4 @@ int main(){ task_g(); return 0; -}; \ No newline at end of file +} diff --git a/ex1/ABCEFG/skalar_stl/mylib.cpp b/ex1/ABCEFG/skalar_stl/mylib.cpp index fbea237..9348cec 100644 --- a/ex1/ABCEFG/skalar_stl/mylib.cpp +++ b/ex1/ABCEFG/skalar_stl/mylib.cpp @@ -18,26 +18,26 @@ using namespace std; tuple means0(double a, double b, double c){ double arith = (a+b+c) / 3; - double geo = pow((a*b*c), 1.0f/3); - double harm = 3 / ((1.0f/a) + (1.0f/b) + (1.0f/c)); + double geo = pow((a*b*c), 1.0/3); + double harm = 3 / ((1.0/a) + (1.0/b) + (1.0/c)); return make_tuple(arith, geo, harm); } tuple means(const vector& v){ - int n = v.size(); + size_t n = v.size(); double sum = 0; - double prod = 1; + double logsum = 0; double invsum = 0; - for (int i = 0; i(n); + double geo = exp(1.0/static_cast(n) * logsum); + double harm = static_cast(n) / invsum; return make_tuple(arith, geo, harm); } @@ -84,7 +84,7 @@ void write_vector_to_file(const string& file_name, const vector& v) ofstream fout(file_name); // Oeffne das File im ASCII-Modus if( fout.is_open() ) { - for (unsigned int k=0; k& v) // -------------- Task C -------------- -double sum_of_spec(int n) +long int sum_of_spec(int n) { long int sum = 0; - for (int i=1; i(i)/(nm-1) - 5.0; + double x_j = 10.0*static_cast(j)/(nm-1) - 5.0; matrix[i*cols + j] = sigmoid(x_i) * sigmoid(x_j); } } @@ -260,7 +260,7 @@ vector DenseMatrix::MultT(const vector& vec) const } void DenseMatrix::print() const { - int count(0); + size_t count(0); cout.precision(5); for (double val : matrix) { printf("%.6f ", val); @@ -308,4 +308,13 @@ vector DenseMatrix2::MultT(const vector& vec) const } return result; -} \ No newline at end of file +} + + +void task_a(); +void task_b(); +void task_c(); +void task_d(); +void task_e(); +void task_f(); +void task_g(); diff --git a/ex1/ABCEFG/skalar_stl/mylib.h b/ex1/ABCEFG/skalar_stl/mylib.h index 554a1f5..7540de4 100644 --- a/ex1/ABCEFG/skalar_stl/mylib.h +++ b/ex1/ABCEFG/skalar_stl/mylib.h @@ -56,10 +56,10 @@ void fill_vector(istream& istr, vector& v); // -------------- Task C -------------- // Sums up all positive integers less or equal n which are multiples of 3 or of 5 (including or!) by brute force. -double sum_of_spec(int n); +long int sum_of_spec(int n); // Sums up all positive integers less or equal n which are multiples of 3 or of 5 (including or!) by inclusion-exclusion principle. -double formula(int n); +double formula(long int n); // -------------- Task E -------------- @@ -93,7 +93,7 @@ private: size_t cols; vector matrix; public: - DenseMatrix(int n, int m); // Constructor + DenseMatrix(size_t n, size_t m); // Constructor vector Mult(const vector& vec) const; vector MultT(const vector& vec) const; void print() const; @@ -109,4 +109,4 @@ public: DenseMatrix2(vector const u, vector const v); // Constructor vector Mult(const vector& vec) const; vector MultT(const vector& vec) const; -}; \ No newline at end of file +};