Compare commits

..

No commits in common. "3f48f5381aae01eaeec826bfdf2c129fdb3d3b76" and "d261de0bda764ded02f90bab2da93a6d38b6cdc0" have entirely different histories.

9 changed files with 61 additions and 61 deletions

BIN
ex1/ABCEFG/skalar_stl/.swo Normal file

Binary file not shown.

BIN
ex1/ABCEFG/skalar_stl/.swp Normal file

Binary file not shown.

BIN
ex1/ABCEFG/skalar_stl/main.GCC_ Executable file

Binary file not shown.

View file

@ -13,7 +13,7 @@
#include <stdexcept> #include <stdexcept>
using namespace std; using namespace std;
static void task_a() { void task_a() {
printf("\n\n-------------- Task A --------------\n\n"); printf("\n\n-------------- Task A --------------\n\n");
auto [a,b,c] = means0(1,4,16); auto [a,b,c] = means0(1,4,16);
@ -30,7 +30,7 @@ static void task_a() {
} }
static void task_b() { void task_b() {
printf("\n\n-------------- Task B --------------\n\n"); printf("\n\n-------------- Task B --------------\n\n");
// Read vector // Read vector
@ -57,11 +57,11 @@ static void task_b() {
printf("Harmonic: %f\n", z); printf("Harmonic: %f\n", z);
// deviation // deviation
double deviation(0.0); double deviation;
for (long unsigned int i=0; i<a.size(); i++){ for (long unsigned int i=0; i<a.size(); i++){
deviation += pow(x - a.at(i),2); deviation += pow(x - a.at(i),2);
} }
deviation = sqrt(deviation/static_cast<double>(a.size())); deviation = sqrt(deviation/a.size());
printf("Deviation: %f\n", deviation); printf("Deviation: %f\n", deviation);
// write results to file // write results to file
@ -70,14 +70,14 @@ static void task_b() {
} }
static void task_c() { void task_c() {
printf("\n\n-------------- Task C --------------\n\n"); printf("\n\n-------------- Task C --------------\n\n");
vector<int> n_values = {15, 1001, 1432987}; vector<int> n_values = {15, 1001, 1432987};
for (int n : n_values) { for (int n : n_values) {
printf("n = %d\n", n); printf("n = %d\n", n);
long int sum = 0; double sum = 0;
double loops = 1000; double loops = 1000;
// Timing first function // Timing first function
@ -86,24 +86,24 @@ static void task_c() {
sum = sum_of_spec(n); sum = sum_of_spec(n);
} }
double sec1 = toc(); double sec1 = toc();
printf("For-loop funtion: result = %ld | time = %f milliseconds\n", sum, sec1*1000); printf("For-loop funtion: result = %.f | time = %f milliseconds\n", sum, sec1*1000);
// Timing second function // Timing second function
tic(); tic();
for (int i=0; i<loops; i++){ for (int i=0; i<loops; i++){
sum = static_cast<long int>(formula(n)); sum = formula(n);
} }
double sec2 = toc(); double sec2 = toc();
printf("Formula funtion: result = %ld | time = %f milliseconds\n", sum, sec2*1000); printf("Formula funtion: result = %.f | time = %f milliseconds\n", sum, sec2*1000);
} }
} }
static void task_d() { void task_d() {
printf("\n\n-------------- Task D --------------\n\n"); printf("\n\n-------------- Task D --------------\n\n");
printf("See folder D.\n"); printf("See folder D.\n");
} }
static void task_e() { void task_e() {
printf("\n\n-------------- Task E --------------\n\n"); printf("\n\n-------------- Task E --------------\n\n");
for (int n : {100, 1000, 10000}) { for (int n : {100, 1000, 10000}) {
@ -130,7 +130,7 @@ static void task_e() {
} }
} }
static void task_f() { void task_f() {
printf("\n\n-------------- Task F --------------\n\n"); printf("\n\n-------------- Task F --------------\n\n");
// single_goldbach(k) // single_goldbach(k)
@ -161,7 +161,7 @@ static void task_f() {
} }
static void task_g() { void task_g() {
printf("\n\n-------------- Task G --------------\n\n"); printf("\n\n-------------- Task G --------------\n\n");
DenseMatrix const M(5,3); DenseMatrix const M(5,3);
@ -188,7 +188,7 @@ static void task_g() {
// ####################################################### // #######################################################
int const NLOOPS = 100; int const NLOOPS = 100;
int const n = 3000; int const n = 1000;
// Time initialization // Time initialization
tic(); tic();
@ -200,7 +200,7 @@ static void task_g() {
// Time Mult // Time Mult
tic(); tic();
vector<double> f3 = M2.Mult(w); vector<double> f3 = M2.Mult(w);
for (size_t k=1; k<NLOOPS; ++k){ for (int k=1; k<NLOOPS; ++k){
f3 = M2.Mult(w); f3 = M2.Mult(w);
} }
double t2 = toc(); double t2 = toc();
@ -208,8 +208,8 @@ static void task_g() {
// Time MultT // Time MultT
tic(); tic();
vector<double> f4 = M2.MultT(w); vector<double> f4 = M2.MultT(w);
for (size_t k=1; k<NLOOPS; ++k){ for (int k=1; k<NLOOPS; ++k){
f4 = M2.MultT(w); f4 = M2.Mult(w);
} }
double t3 = toc(); double t3 = toc();
@ -220,7 +220,7 @@ static void task_g() {
printf("Time for MultT : %f seconds, %f per loop.\n", t3, t3/NLOOPS); printf("Time for MultT : %f seconds, %f per loop.\n", t3, t3/NLOOPS);
// Check if resulting vectors are equal // Check if resulting vectors are equal
for (size_t i = 0; i < n; i++) for (int i = 0; i < n; i++)
{ {
double err = f3[i] - f4[i]; double err = f3[i] - f4[i];
if(abs(err) > 1e-4) if(abs(err) > 1e-4)
@ -233,14 +233,14 @@ static void task_g() {
// Time initialization // Time initialization
tic(); tic();
vector<double> x(n,0); vector<double> x(n,0);
for (int i=0; i<n; i++){x[i] = sigmoid( (10.0*static_cast<double>(i))/(n-1) - 5 );} for (int i=0; i<n; i++){x[i] = sigmoid( (10.0*i)/(n-1) - 5 );}
DenseMatrix2 M3(x,x); DenseMatrix2 M3(x,x);
double t4 = toc(); double t4 = toc();
// Time Mult // Time Mult
tic(); tic();
vector<double> f5 = M3.Mult(w); vector<double> f5 = M3.Mult(w);
for (size_t k=1; k<NLOOPS; ++k){ for (int k=1; k<NLOOPS; ++k){
f5 = M3.Mult(w); f5 = M3.Mult(w);
} }
double t5 = toc(); double t5 = toc();
@ -248,8 +248,8 @@ static void task_g() {
// Time MultT // Time MultT
tic(); tic();
vector<double> f6 = M3.MultT(w); vector<double> f6 = M3.MultT(w);
for (size_t k=1; k<NLOOPS; ++k){ for (int k=1; k<NLOOPS; ++k){
f6 = M3.MultT(w); f6 = M3.Mult(w);
} }
double t6 = toc(); double t6 = toc();
@ -260,7 +260,7 @@ static void task_g() {
printf("Time for MultT : %f seconds, %f per loop.\n", t6, t6/NLOOPS); printf("Time for MultT : %f seconds, %f per loop.\n", t6, t6/NLOOPS);
// Check if resulting vectors are equal // Check if resulting vectors are equal
for (size_t i = 0; i < n; i++) for (int i = 0; i < n; i++)
{ {
double err = f5[i] - f6[i]; double err = f5[i] - f6[i];
if(abs(err) > 1e-4) if(abs(err) > 1e-4)
@ -269,6 +269,9 @@ static void task_g() {
} }
} }
printf("\nNOTE: difference in runtime noticable with n=10.000 (~30 seconds)\n");
} }
@ -283,4 +286,4 @@ int main(){
task_g(); task_g();
return 0; return 0;
} };

Binary file not shown.

View file

@ -18,26 +18,26 @@ using namespace std;
tuple<double, double, double> means0(double a, double b, double c){ tuple<double, double, double> means0(double a, double b, double c){
double arith = (a+b+c) / 3; double arith = (a+b+c) / 3;
double geo = pow((a*b*c), 1.0/3); double geo = pow((a*b*c), 1.0f/3);
double harm = 3 / ((1.0/a) + (1.0/b) + (1.0/c)); double harm = 3 / ((1.0f/a) + (1.0f/b) + (1.0f/c));
return make_tuple(arith, geo, harm); return make_tuple(arith, geo, harm);
} }
tuple<double, double, double> means(const vector<double>& v){ tuple<double, double, double> means(const vector<double>& v){
size_t n = v.size(); int n = v.size();
double sum = 0; double sum = 0;
double logsum = 0; double prod = 1;
double invsum = 0; double invsum = 0;
for (size_t i = 0; i<n; ++i){ for (int i = 0; i<n; ++i){
sum += v[i]; sum += v[i];
logsum += log(v[i]); prod *= v[i];
invsum += 1.0/v[i]; invsum += 1.0f/v[i];
} }
double arith = sum / static_cast<double>(n); double arith = sum / n;
double geo = exp(1.0/static_cast<double>(n) * logsum); double geo = pow(prod, 1.0f/n);
double harm = static_cast<double>(n) / invsum; double harm = n / invsum;
return make_tuple(arith, geo, harm); return make_tuple(arith, geo, harm);
} }
@ -84,7 +84,7 @@ void write_vector_to_file(const string& file_name, const vector<double>& v)
ofstream fout(file_name); // Oeffne das File im ASCII-Modus ofstream fout(file_name); // Oeffne das File im ASCII-Modus
if( fout.is_open() ) if( fout.is_open() )
{ {
for (size_t k=0; k<v.size(); ++k) for (unsigned int k=0; k<v.size(); ++k)
{ {
fout << v.at(k) << endl; fout << v.at(k) << endl;
} }
@ -100,10 +100,10 @@ void write_vector_to_file(const string& file_name, const vector<double>& v)
// -------------- Task C -------------- // -------------- Task C --------------
long int sum_of_spec(int n) double sum_of_spec(int n)
{ {
long int sum = 0; long int sum = 0;
for (long int i=1; i<n+1; i++){ for (int i=1; i<n+1; i++){
if (i % 3 == 0 || i % 5 == 0){ if (i % 3 == 0 || i % 5 == 0){
sum += i; sum += i;
} }
@ -113,13 +113,13 @@ long int sum_of_spec(int n)
double formula(int n) double formula(int n)
{ {
double div_by_3 = floor( n / 3.0 ); double div_by_3 = floor( n / 3.0f );
double div_by_5 = floor( n / 5.0 ); double div_by_5 = floor( n / 5.0f );
double div_by_15 = floor( n / 15.0 ); double div_by_15 = floor( n / 15.0f );
double S_3 = 3.0 * (div_by_3*((div_by_3+1)/2.0)); double S_3 = 3.0 * (div_by_3*((div_by_3+1)/2.0f));
double S_5 = 5.0 * (div_by_5*((div_by_5+1)/2.0)); double S_5 = 5.0 * (div_by_5*((div_by_5+1)/2.0f));
double S_15 = 15.0 * (div_by_15*((div_by_15+1)/2.0)); double S_15 = 15.0 * (div_by_15*((div_by_15+1)/2.0f));
double sum = S_3 + S_5 - S_15; double sum = S_3 + S_5 - S_15;
@ -215,15 +215,15 @@ double DenseMatrix::sigmoid(double x)
return 1.0 / (1.0 + exp(-x)); return 1.0 / (1.0 + exp(-x));
} }
DenseMatrix::DenseMatrix(size_t n, size_t m) : rows(n), cols(m), matrix(n*m) DenseMatrix::DenseMatrix(int n, int m) : rows(n), cols(m), matrix(n*m)
{ {
size_t nm = max(n, m); int nm = max(n, m);
for (size_t i = 0; i < rows; i++) for (size_t i = 0; i < rows; i++)
{ {
for (size_t j = 0; j < cols; j++) for (size_t j = 0; j < cols; j++)
{ {
double x_i = 10.0*static_cast<double>(i)/(nm-1) - 5.0; double x_i = 10.0*i/(nm-1) - 5.0;
double x_j = 10.0*static_cast<double>(j)/(nm-1) - 5.0; double x_j = 10.0*j/(nm-1) - 5.0;
matrix[i*cols + j] = sigmoid(x_i) * sigmoid(x_j); matrix[i*cols + j] = sigmoid(x_i) * sigmoid(x_j);
} }
} }
@ -260,7 +260,7 @@ vector<double> DenseMatrix::MultT(const vector<double>& vec) const
} }
void DenseMatrix::print() const { void DenseMatrix::print() const {
size_t count(0); int count(0);
cout.precision(5); cout.precision(5);
for (double val : matrix) { for (double val : matrix) {
printf("%.6f ", val); printf("%.6f ", val);
@ -309,12 +309,3 @@ vector<double> DenseMatrix2::MultT(const vector<double>& vec) const
return result; return result;
} }
void task_a();
void task_b();
void task_c();
void task_d();
void task_e();
void task_f();
void task_g();

View file

@ -56,7 +56,7 @@ void fill_vector(istream& istr, vector<double>& v);
// -------------- Task C -------------- // -------------- Task C --------------
// Sums up all positive integers less or equal n which are multiples of 3 or of 5 (including or!) by brute force. // Sums up all positive integers less or equal n which are multiples of 3 or of 5 (including or!) by brute force.
long int sum_of_spec(int n); double 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. // 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(int n);
@ -93,7 +93,7 @@ private:
size_t cols; size_t cols;
vector<double> matrix; vector<double> matrix;
public: public:
DenseMatrix(size_t n, size_t m); // Constructor DenseMatrix(int n, int m); // Constructor
vector<double> Mult(const vector<double>& vec) const; vector<double> Mult(const vector<double>& vec) const;
vector<double> MultT(const vector<double>& vec) const; vector<double> MultT(const vector<double>& vec) const;
void print() const; void print() const;

Binary file not shown.

View file

@ -0,0 +1,6 @@
1
1000
498.184
inf
95.6857
287.905