Compare commits
3 commits
d261de0bda
...
3f48f5381a
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3f48f5381a | ||
|
|
dea72060c2 | ||
|
|
875d4bd72e |
9 changed files with 61 additions and 61 deletions
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
|
@ -13,7 +13,7 @@
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
void task_a() {
|
static 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 @@ void task_a() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void task_b() {
|
static 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 @@ void task_b() {
|
||||||
printf("Harmonic: %f\n", z);
|
printf("Harmonic: %f\n", z);
|
||||||
|
|
||||||
// deviation
|
// deviation
|
||||||
double deviation;
|
double deviation(0.0);
|
||||||
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/a.size());
|
deviation = sqrt(deviation/static_cast<double>(a.size()));
|
||||||
printf("Deviation: %f\n", deviation);
|
printf("Deviation: %f\n", deviation);
|
||||||
|
|
||||||
// write results to file
|
// 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");
|
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);
|
||||||
double sum = 0;
|
long int sum = 0;
|
||||||
double loops = 1000;
|
double loops = 1000;
|
||||||
|
|
||||||
// Timing first function
|
// Timing first function
|
||||||
|
|
@ -86,24 +86,24 @@ void task_c() {
|
||||||
sum = sum_of_spec(n);
|
sum = sum_of_spec(n);
|
||||||
}
|
}
|
||||||
double sec1 = toc();
|
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
|
// Timing second function
|
||||||
tic();
|
tic();
|
||||||
for (int i=0; i<loops; i++){
|
for (int i=0; i<loops; i++){
|
||||||
sum = formula(n);
|
sum = static_cast<long int>(formula(n));
|
||||||
}
|
}
|
||||||
double sec2 = toc();
|
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("\n\n-------------- Task D --------------\n\n");
|
||||||
printf("See folder D.\n");
|
printf("See folder D.\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
void task_e() {
|
static 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 @@ void task_e() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void task_f() {
|
static 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 @@ void task_f() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void task_g() {
|
static 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 @@ void task_g() {
|
||||||
// #######################################################
|
// #######################################################
|
||||||
|
|
||||||
int const NLOOPS = 100;
|
int const NLOOPS = 100;
|
||||||
int const n = 1000;
|
int const n = 3000;
|
||||||
|
|
||||||
// Time initialization
|
// Time initialization
|
||||||
tic();
|
tic();
|
||||||
|
|
@ -200,7 +200,7 @@ void task_g() {
|
||||||
// Time Mult
|
// Time Mult
|
||||||
tic();
|
tic();
|
||||||
vector<double> f3 = M2.Mult(w);
|
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);
|
f3 = M2.Mult(w);
|
||||||
}
|
}
|
||||||
double t2 = toc();
|
double t2 = toc();
|
||||||
|
|
@ -208,8 +208,8 @@ void task_g() {
|
||||||
// Time MultT
|
// Time MultT
|
||||||
tic();
|
tic();
|
||||||
vector<double> f4 = M2.MultT(w);
|
vector<double> f4 = M2.MultT(w);
|
||||||
for (int k=1; k<NLOOPS; ++k){
|
for (size_t k=1; k<NLOOPS; ++k){
|
||||||
f4 = M2.Mult(w);
|
f4 = M2.MultT(w);
|
||||||
}
|
}
|
||||||
double t3 = toc();
|
double t3 = toc();
|
||||||
|
|
||||||
|
|
@ -220,7 +220,7 @@ 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 (int i = 0; i < n; i++)
|
for (size_t 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 @@ 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*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);
|
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 (int k=1; k<NLOOPS; ++k){
|
for (size_t k=1; k<NLOOPS; ++k){
|
||||||
f5 = M3.Mult(w);
|
f5 = M3.Mult(w);
|
||||||
}
|
}
|
||||||
double t5 = toc();
|
double t5 = toc();
|
||||||
|
|
@ -248,8 +248,8 @@ void task_g() {
|
||||||
// Time MultT
|
// Time MultT
|
||||||
tic();
|
tic();
|
||||||
vector<double> f6 = M3.MultT(w);
|
vector<double> f6 = M3.MultT(w);
|
||||||
for (int k=1; k<NLOOPS; ++k){
|
for (size_t k=1; k<NLOOPS; ++k){
|
||||||
f6 = M3.Mult(w);
|
f6 = M3.MultT(w);
|
||||||
}
|
}
|
||||||
double t6 = toc();
|
double t6 = toc();
|
||||||
|
|
||||||
|
|
@ -260,7 +260,7 @@ 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 (int i = 0; i < n; i++)
|
for (size_t 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,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();
|
task_g();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
};
|
}
|
||||||
|
|
|
||||||
Binary file not shown.
|
|
@ -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.0f/3);
|
double geo = pow((a*b*c), 1.0/3);
|
||||||
double harm = 3 / ((1.0f/a) + (1.0f/b) + (1.0f/c));
|
double harm = 3 / ((1.0/a) + (1.0/b) + (1.0/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){
|
||||||
int n = v.size();
|
size_t n = v.size();
|
||||||
double sum = 0;
|
double sum = 0;
|
||||||
double prod = 1;
|
double logsum = 0;
|
||||||
double invsum = 0;
|
double invsum = 0;
|
||||||
|
|
||||||
for (int i = 0; i<n; ++i){
|
for (size_t i = 0; i<n; ++i){
|
||||||
sum += v[i];
|
sum += v[i];
|
||||||
prod *= v[i];
|
logsum += log(v[i]);
|
||||||
invsum += 1.0f/v[i];
|
invsum += 1.0/v[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
double arith = sum / n;
|
double arith = sum / static_cast<double>(n);
|
||||||
double geo = pow(prod, 1.0f/n);
|
double geo = exp(1.0/static_cast<double>(n) * logsum);
|
||||||
double harm = n / invsum;
|
double harm = static_cast<double>(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 (unsigned int k=0; k<v.size(); ++k)
|
for (size_t 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 --------------
|
||||||
|
|
||||||
double sum_of_spec(int n)
|
long int sum_of_spec(int n)
|
||||||
{
|
{
|
||||||
long int sum = 0;
|
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){
|
if (i % 3 == 0 || i % 5 == 0){
|
||||||
sum += i;
|
sum += i;
|
||||||
}
|
}
|
||||||
|
|
@ -113,13 +113,13 @@ double sum_of_spec(int n)
|
||||||
|
|
||||||
double formula(int n)
|
double formula(int n)
|
||||||
{
|
{
|
||||||
double div_by_3 = floor( n / 3.0f );
|
double div_by_3 = floor( n / 3.0 );
|
||||||
double div_by_5 = floor( n / 5.0f );
|
double div_by_5 = floor( n / 5.0 );
|
||||||
double div_by_15 = floor( n / 15.0f );
|
double div_by_15 = floor( n / 15.0 );
|
||||||
|
|
||||||
double S_3 = 3.0 * (div_by_3*((div_by_3+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.0f));
|
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.0f));
|
double S_15 = 15.0 * (div_by_15*((div_by_15+1)/2.0));
|
||||||
|
|
||||||
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(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 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*i/(nm-1) - 5.0;
|
double x_i = 10.0*static_cast<double>(i)/(nm-1) - 5.0;
|
||||||
double x_j = 10.0*j/(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);
|
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 {
|
||||||
int count(0);
|
size_t count(0);
|
||||||
cout.precision(5);
|
cout.precision(5);
|
||||||
for (double val : matrix) {
|
for (double val : matrix) {
|
||||||
printf("%.6f ", val);
|
printf("%.6f ", val);
|
||||||
|
|
@ -309,3 +309,12 @@ 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();
|
||||||
|
|
|
||||||
|
|
@ -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.
|
||||||
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.
|
// 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(int n, int m); // Constructor
|
DenseMatrix(size_t n, size_t 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.
|
|
@ -1,6 +0,0 @@
|
||||||
1
|
|
||||||
1000
|
|
||||||
498.184
|
|
||||||
inf
|
|
||||||
95.6857
|
|
||||||
287.905
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue