fixed errors

This commit is contained in:
dino.celebic 2025-10-24 16:47:31 +02:00
commit dea72060c2
3 changed files with 62 additions and 56 deletions

View file

@ -13,7 +13,7 @@
#include <stdexcept>
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(); i++){
deviation += pow(x - a.at(i),2);
}
deviation = sqrt(deviation/a.size());
deviation = sqrt(deviation/static_cast<double>(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<int> 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<loops; i++){
sum = formula(n);
sum = static_cast<long int>(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<double> f3 = M2.Mult(w);
for (int k=1; k<NLOOPS; ++k){
for (size_t k=1; k<NLOOPS; ++k){
f3 = M2.Mult(w);
}
double t2 = toc();
@ -208,8 +208,8 @@ void task_g() {
// Time MultT
tic();
vector<double> f4 = M2.MultT(w);
for (int k=1; k<NLOOPS; ++k){
f4 = M2.Mult(w);
for (size_t k=1; k<NLOOPS; ++k){
f4 = M2.MultT(w);
}
double t3 = toc();
@ -220,7 +220,7 @@ void task_g() {
printf("Time for MultT : %f seconds, %f per loop.\n", t3, t3/NLOOPS);
// Check if resulting vectors are equal
for (int i = 0; i < n; i++)
for (size_t i = 0; i < n; i++)
{
double err = f3[i] - f4[i];
if(abs(err) > 1e-4)
@ -233,14 +233,14 @@ void task_g() {
// Time initialization
tic();
vector<double> x(n,0);
for (int i=0; i<n; i++){x[i] = sigmoid( (10.0*i)/(n-1) - 5 );}
for (int i=0; i<n; i++){x[i] = sigmoid( (10.0*static_cast<double>(i))/(n-1) - 5 );}
DenseMatrix2 M3(x,x);
double t4 = toc();
// Time Mult
tic();
vector<double> f5 = M3.Mult(w);
for (int k=1; k<NLOOPS; ++k){
for (size_t k=1; k<NLOOPS; ++k){
f5 = M3.Mult(w);
}
double t5 = toc();
@ -248,8 +248,8 @@ void task_g() {
// Time MultT
tic();
vector<double> f6 = M3.MultT(w);
for (int k=1; k<NLOOPS; ++k){
f6 = M3.Mult(w);
for (size_t k=1; k<NLOOPS; ++k){
f6 = M3.MultT(w);
}
double t6 = toc();
@ -260,7 +260,7 @@ void task_g() {
printf("Time for MultT : %f seconds, %f per loop.\n", t6, t6/NLOOPS);
// Check if resulting vectors are equal
for (int i = 0; i < n; i++)
for (size_t i = 0; i < n; i++)
{
double err = f5[i] - f6[i];
if(abs(err) > 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;
};
}

View file

@ -18,26 +18,26 @@ using namespace std;
tuple<double, double, double> 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<double, double, double> means(const vector<double>& 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; ++i){
for (size_t i = 0; i<n; ++i){
sum += v[i];
prod *= v[i];
invsum += 1.0f/v[i];
logsum += log(v[i]);
invsum += 1.0/v[i];
}
double arith = sum / n;
double geo = pow(prod, 1.0f/n);
double harm = n / invsum;
double arith = sum / static_cast<double>(n);
double geo = exp(1.0/static_cast<double>(n) * logsum);
double harm = static_cast<double>(n) / invsum;
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
if( fout.is_open() )
{
for (unsigned int k=0; k<v.size(); ++k)
for (size_t k=0; k<v.size(); ++k)
{
fout << v.at(k) << endl;
}
@ -100,10 +100,10 @@ void write_vector_to_file(const string& file_name, const vector<double>& 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<n+1; i++){
for (long int i=1; i<n+1; i++){
if (i % 3 == 0 || i % 5 == 0){
sum += i;
}
@ -113,13 +113,13 @@ double sum_of_spec(int n)
double formula(int n)
{
double div_by_3 = floor( n / 3.0f );
double div_by_5 = floor( n / 5.0f );
double div_by_15 = floor( n / 15.0f );
double div_by_3 = floor( n / 3.0 );
double div_by_5 = floor( n / 5.0 );
double div_by_15 = floor( n / 15.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.0f));
double S_15 = 15.0 * (div_by_15*((div_by_15+1)/2.0f));
double S_3 = 3.0 * (div_by_3*((div_by_3+1)/2.0));
double S_5 = 5.0 * (div_by_5*((div_by_5+1)/2.0));
double S_15 = 15.0 * (div_by_15*((div_by_15+1)/2.0));
double sum = S_3 + S_5 - S_15;
@ -215,15 +215,15 @@ double DenseMatrix::sigmoid(double x)
return 1.0 / (1.0 + exp(-x));
}
DenseMatrix::DenseMatrix(int n, int m) : rows(n), cols(m), matrix(n*m)
DenseMatrix::DenseMatrix(size_t n, size_t m) : rows(n), cols(m), matrix(n*m)
{
int nm = max(n, m);
size_t nm = max(n, m);
for (size_t i = 0; i < rows; i++)
{
for (size_t j = 0; j < cols; j++)
{
double x_i = 10.0*i/(nm-1) - 5.0;
double x_j = 10.0*j/(nm-1) - 5.0;
double x_i = 10.0*static_cast<double>(i)/(nm-1) - 5.0;
double x_j = 10.0*static_cast<double>(j)/(nm-1) - 5.0;
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 {
int count(0);
size_t count(0);
cout.precision(5);
for (double val : matrix) {
printf("%.6f ", val);
@ -308,4 +308,13 @@ vector<double> DenseMatrix2::MultT(const vector<double>& vec) const
}
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,10 +56,10 @@ void fill_vector(istream& istr, vector<double>& 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<double> matrix;
public:
DenseMatrix(int n, int m); // Constructor
DenseMatrix(size_t n, size_t m); // Constructor
vector<double> Mult(const vector<double>& vec) const;
vector<double> MultT(const vector<double>& vec) const;
void print() const;
@ -109,4 +109,4 @@ public:
DenseMatrix2(vector<double> const u, vector<double> const v); // Constructor
vector<double> Mult(const vector<double>& vec) const;
vector<double> MultT(const vector<double>& vec) const;
};
};