diff --git a/exercice_3/.gitignore b/exercice_3/.gitignore new file mode 100644 index 0000000..7d30f78 --- /dev/null +++ b/exercice_3/.gitignore @@ -0,0 +1,35 @@ +# Prerequisites +*.d + +# Compiled Object files +*.slo +*.lo +*.o +*.obj + +# Precompiled Headers +*.gch +*.pch + +# Compiled Dynamic libraries +*.so +*.dylib +*.dll + +# Fortran module files +*.mod +*.smod + +# Compiled Static libraries +*.lai +*.la +*.a +*.lib + +# Executables +*.exe +*.out +*.app + +# build directory +build/ diff --git a/exercice_3/CMakeLists.txt b/exercice_3/CMakeLists.txt new file mode 100644 index 0000000..0199b12 --- /dev/null +++ b/exercice_3/CMakeLists.txt @@ -0,0 +1,4 @@ +cmake_minimum_required(VERSION 2.6) + +add_subdirectory(src) + diff --git a/exercice_3/README.md b/exercice_3/README.md new file mode 100644 index 0000000..5d61179 --- /dev/null +++ b/exercice_3/README.md @@ -0,0 +1,25 @@ +Readme exercice 3: code created by Marti Bosch (marti.bosch@epfl.ch) and Marc Schwaerzel (marc.schwaerzel@epfl.ch) +# Exercise 1 +# Exercise 2 +# Exercise 3 +# Exercise 4 +# Exercise 5 + +# Run the code +To run the code follow the instructions: + +1) create a new directory in the exercice_3 directory and go into that folder: + + $ mkdir build + $ cd build + +2) compile the code: + + $ cmake ../ + $ make + +3) run the code with following arguments: -..: + + $ ./src/main ... + + diff --git a/exercice_3/src/CMakeLists.txt b/exercice_3/src/CMakeLists.txt new file mode 100644 index 0000000..5722a90 --- /dev/null +++ b/exercice_3/src/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 2.8) +project(exercice_2) + +add_executable(main main.cc series.hh series.cc compute_arithmetic.hh compute_arithmetic.cc compute_pi.hh compute_pi.cc dumper_series.hh dumper_series.cc print_series.hh print_series.cc write_series.hh write_series.cc) + + diff --git a/exercice_3/src/compute_arithmetic.cc b/exercice_3/src/compute_arithmetic.cc new file mode 100644 index 0000000..8e62239 --- /dev/null +++ b/exercice_3/src/compute_arithmetic.cc @@ -0,0 +1,15 @@ +#include "compute_arithmetic.hh" +/* -------------------------------------------------------------------------- */ + +ComputeArithmetic::~ComputeArithmetic() = default; + +double ComputeArithmetic::compute(unsigned int N) { + this-> computeTerm(N); + return current_value; +} + +double ComputeArithmetic::computeTerm(unsigned int k){ + return k; +} + + diff --git a/exercice_3/src/compute_arithmetic.hh b/exercice_3/src/compute_arithmetic.hh new file mode 100644 index 0000000..9b8c75f --- /dev/null +++ b/exercice_3/src/compute_arithmetic.hh @@ -0,0 +1,18 @@ +#ifndef COMPUTE_ARITHMETIC_HH +#define COMPUTE_ARITHMETIC_HH + +#include "series.hh" + +class ComputeArithmetic : public Series { + +public: + // ComputeArithmetic(); + virtual ~ComputeArithmetic(); + +public: + double compute(unsigned int N) override; +double computeTerm(unsigned int N) override; +}; + +#endif + diff --git a/exercice_3/src/compute_pi.cc b/exercice_3/src/compute_pi.cc new file mode 100644 index 0000000..6fea01e --- /dev/null +++ b/exercice_3/src/compute_pi.cc @@ -0,0 +1,19 @@ +#define _USE_MATH_DEFINES +#include "compute_pi.hh" +#include + +ComputePi::~ComputePi() = default; + +double ComputePi::compute(unsigned int N) { + this-> computeTerm(N); + return sqrt(6. * current_value); +} + +double ComputePi::computeTerm(unsigned int k){ + return 1./(1.*k*k); +} + +double ComputePi::getAnalyticPrediction() { return M_PI; } + + + diff --git a/exercice_3/src/compute_pi.hh b/exercice_3/src/compute_pi.hh new file mode 100644 index 0000000..a46604d --- /dev/null +++ b/exercice_3/src/compute_pi.hh @@ -0,0 +1,16 @@ +#ifndef COMPUTE_PI_HH +#define COMPUTE_PI_HH + +#include "series.hh" + +class ComputePi : public Series { +public: + // ComputePi(); + virtual ~ComputePi(); + +public: + double compute(unsigned int N) override; + double getAnalyticPrediction() override; + double computeTerm(unsigned int k) override; +}; +#endif diff --git a/exercice_3/src/dumper_series.cc b/exercice_3/src/dumper_series.cc new file mode 100644 index 0000000..687ce69 --- /dev/null +++ b/exercice_3/src/dumper_series.cc @@ -0,0 +1,9 @@ +#include "dumper_series.hh" + +DumperSeries::DumperSeries(Series &series) : series(series) {} + +void DumperSeries::setPrecision(unsigned int precision) { + this->precision = precision; +} + +DumperSeries::~DumperSeries() = default; diff --git a/exercice_3/src/dumper_series.hh b/exercice_3/src/dumper_series.hh new file mode 100644 index 0000000..1c7d270 --- /dev/null +++ b/exercice_3/src/dumper_series.hh @@ -0,0 +1,26 @@ +#ifndef DUMPER_SERIES_HH +#define DUMPER_SERIES_HH + +#include "series.hh" +#include + +class DumperSeries { +public: + DumperSeries(Series &series); + virtual ~DumperSeries(); + +public: + virtual void dump(std::ostream &os) = 0; + virtual void setPrecision(unsigned int precision); + +protected: + Series &series; + unsigned int precision; +}; + +inline std::ostream &operator<<(std::ostream &stream, DumperSeries &_this) { + _this.dump(stream); + return stream; +} + +#endif diff --git a/exercice_3/src/main.cc b/exercice_3/src/main.cc new file mode 100644 index 0000000..171ee9f --- /dev/null +++ b/exercice_3/src/main.cc @@ -0,0 +1,63 @@ +/* created by Marti Bosch and Marc Schwaerzel*/ +/* -------------------------------------------------------------------------- */ +#include +#include + +#include +#include +#include + +#include "compute_arithmetic.hh" +#include "compute_pi.hh" +#include "print_series.hh" +#include "series.hh" +#include "write_series.hh" + +using namespace std; +/* -------------------------------------------------------------------------- */ + +int main(int argc, char **argv) { + + if (argc != 5) { + cout << "Arguments: frequency, maxiter, series_type (arithmetic, pi), " + "dump_type (print, write)" + << endl; + return EXIT_FAILURE; + } + + std::stringstream sstr; + for (int i = 1; i < argc; ++i) + sstr << argv[i] << " "; + + int frequency; + int maxiter; + string series_type; + string dump_type; + + sstr >> frequency; + sstr >> maxiter; + sstr >> series_type; + sstr >> dump_type; + + Series *series; + + if (series_type == "arithmetic") + series = new ComputeArithmetic(); + else if (series_type == "pi") + series = new ComputePi(); + else { + std::cout << "input error" << std::endl; + return EXIT_FAILURE; + } + + std::ofstream of("output.txt"); + if (dump_type == "print") { + PrintSeries printseries(*series, maxiter, frequency); + printseries.dump(); + } else if (dump_type == "write") { + WriteSeries writeseries(*series, maxiter, frequency); + writeseries.dump(of); + } + + delete series; +} diff --git a/exercice_3/src/plot_series.py b/exercice_3/src/plot_series.py new file mode 100644 index 0000000..8de58c1 --- /dev/null +++ b/exercice_3/src/plot_series.py @@ -0,0 +1,31 @@ +import sys + +import matplotlib.pyplot as plt + + +if __name__ == '__main__': + fpath = sys.argv[1] + if fpath[-4:] == ".csv": + sep = ',' + elif fpath[-4] == ".psv": + sep = '|' + else: + sep = ' ' + + + numerical = [] + analytic = [] + + with open(fpath, 'r') as f: + for line in f: + num, an = line.split(sep) + numerical.append(float(num)) + analytic.append(float(an)) + + fig, ax = plt.subplots() + ax.plot(numerical, marker='o', label='Numerical') + ax.plot(analytic, label='Analytical') + ax.set_xlabel(r'$k$') + ax.set_ylabel(r'Series') + ax.legend() + plt.show() diff --git a/exercice_3/src/print_series.cc b/exercice_3/src/print_series.cc new file mode 100644 index 0000000..259cd9e --- /dev/null +++ b/exercice_3/src/print_series.cc @@ -0,0 +1,21 @@ +#include "print_series.hh" +#include +#include +#include + +PrintSeries::PrintSeries(Series &series, unsigned int maxiter, + unsigned int frequency) + : DumperSeries(series), maxiter(maxiter), frequency(frequency) {} + +void PrintSeries::dump(std::ostream &os) { + double analytic_prediction = this->series.getAnalyticPrediction(); + if (~std::isnan(analytic_prediction)) { + for (int i = 1; i <= this->maxiter; i += this->frequency) { + double value = this->series.compute(i); + os << std::setprecision(this->precision) << value << " / " + << analytic_prediction << std::endl; + } + } +} + +PrintSeries::~PrintSeries() = default; diff --git a/exercice_3/src/print_series.hh b/exercice_3/src/print_series.hh new file mode 100644 index 0000000..0388607 --- /dev/null +++ b/exercice_3/src/print_series.hh @@ -0,0 +1,19 @@ +#ifndef PRINT_SERIES_HH +#define PRINT_SERIES_HH + +#include "dumper_series.hh" + +class PrintSeries : public DumperSeries { +public: + PrintSeries(Series &series, unsigned int maxiter, unsigned int frequency); + virtual ~PrintSeries(); + +public: + void dump(std::ostream &os = std::cout); + +protected: + unsigned int maxiter; + unsigned int frequency; +}; + +#endif diff --git a/exercice_3/src/series.cc b/exercice_3/src/series.cc new file mode 100644 index 0000000..23e9352 --- /dev/null +++ b/exercice_3/src/series.cc @@ -0,0 +1,30 @@ +#include "series.hh" +#include + +Series::~Series() = default; + +double Series::getAnalyticPrediction() { return std::nan(""); } +double Series::compute(unsigned int N) { + if (this-> current_index <= N){ + N -= this-> current_index; + } + else { + this->current_index = 0; + this->current_value = 0; + } + for (int i = 0; i < N; ++i){ + this->addTerm(); + } + return this->current_value; + +} + +void Series::addTerm(){ + + this->current_index += 1; + this->current_value += this-> computeTerm(this-> current_index); + +} + + + diff --git a/exercice_3/src/series.hh b/exercice_3/src/series.hh new file mode 100644 index 0000000..74cde83 --- /dev/null +++ b/exercice_3/src/series.hh @@ -0,0 +1,34 @@ +#ifndef SERIES_HH +#define SERIES_HH + +/*class Series { +public: + virtual ~Series(); + +public: + virtual double compute(unsigned int N) = 0; + virtual double getAnalyticPrediction(); +}; + +#endif*/ +class Series { + public: + virtual ~Series(); //destructor + + protected: + unsigned int current_index; + double current_value; + + public: + virtual double compute(unsigned int N) = 0; + virtual double getAnalyticPrediction(); + public: + + virtual double computeTerm(unsigned int k) = 0; + void addTerm(); + + + +}; + +#endif diff --git a/exercice_3/src/write_series.cc b/exercice_3/src/write_series.cc new file mode 100644 index 0000000..15005c4 --- /dev/null +++ b/exercice_3/src/write_series.cc @@ -0,0 +1,33 @@ +#include "write_series.hh" +#include +#include + +WriteSeries::WriteSeries(Series &series, unsigned int maxiter, + unsigned int frequency) + : DumperSeries(series), maxiter(maxiter), frequency(frequency), + separator(" ") {} + +void WriteSeries::dump(std::ostream &os) { + std::string separator = this->separator; + // std::string filepath("series"); + // if (separator == ",") + // filepath += ".csv"; + // else if (separator == "|") + // filepath += ".psv"; + // else + // filepath += ".txt"; + // std::ofstream f(filepath); + double analytic_prediction = this->series.getAnalyticPrediction(); + if (~std::isnan(analytic_prediction)) { + for (int i = 1; i <= this->maxiter; i += this->frequency) { + double value = this->series.compute(i); + os << value << separator << analytic_prediction << std::endl; + } + } +} + +void WriteSeries::setSeparator(std::string separator) { + this->separator = separator; +} + +WriteSeries::~WriteSeries() = default; diff --git a/exercice_3/src/write_series.hh b/exercice_3/src/write_series.hh new file mode 100644 index 0000000..ce1f5f0 --- /dev/null +++ b/exercice_3/src/write_series.hh @@ -0,0 +1,22 @@ +#ifndef WRITE_SERIES_HH +#define WRITE_SERIES_HH + +#include "dumper_series.hh" +#include + +class WriteSeries : public DumperSeries { +public: + WriteSeries(Series &series, unsigned int maxiter, unsigned int frequency); + virtual ~WriteSeries(); + +public: + void dump(std::ostream &os); + void setSeparator(std::string separator); + +protected: + unsigned int maxiter; + unsigned int frequency; + std::string separator; +}; + +#endif