To receive notifications about scheduled maintenance, please subscribe to the mailing-list gitlab-operations@sympa.ethz.ch. You can subscribe to the mailing-list at https://sympa.ethz.ch

Commit 756f5e7b authored by Erick Turricelli's avatar Erick Turricelli
Browse files

Add const in ex02 and functors in ex04

parent 96eea3d9
......@@ -3,7 +3,7 @@
#include <cassert>
#define _USE_MATH_DEFINES
double integration(double a, double b, int N, double (*my_func)(double x)) {
double integration(const double a, const double b, const int N, const double (*my_func)(double x)) {
assert(N>0);
assert(b > a);
double delta = (b-a)/N;
......
double integration(double a, double b, int N, double (*my_func)(double x));
double integration(const double a, const double b, const int N, const double (*my_func)(double x));
// Pre-conditions
/*
......
......@@ -3,10 +3,14 @@
#include "integration.hpp"
#define _USE_MATH_DEFINES
double my_sin(double x) {
const double my_sin(double x) {
return sin(x);
}
const double test_func(double x) {
return x*(1-x);
}
int main() {
std::cout << "Integral of sin(x) in [0,pi] is " << integration(0.0, M_PI, 100, my_sin) << "\n";
......@@ -16,5 +20,8 @@ int main() {
std::cout << i << " " << integration(0.0, M_PI, i, my_sin) << "\n";
}
std::cout << "Test simpson rule \n";
std::cout << "Integral of x*(1-x) in [0, 1] is " << integration(0.0, 1.0, 55, test_func) << "\n";
return 0;
}
\ No newline at end of file
# require minimum version of CMake
cmake_minimum_required(VERSION 2.8)
# set name of project
project(simpson_object)
# add executable to the project using the specified source files
add_executable(simpson_object main.cpp function_objects.hpp)
\ No newline at end of file
#include <iostream>
#include <cmath>
#include <cassert>
#define _USE_MATH_DEFINES
// Pre-conditions
/*
- N must be a positive integer
- b >= a
- a and b are templated, so I put the implementation of the function in this header
- we do not need cpp file, the compiler needs to know in the declaration what type to use
Post-conditions
- The result is the integral of the specified function in the interval [a,b]
*/
class Func_Obj {
private:
double lambda;
public:
Func_Obj(double lambda_value) : lambda(lambda_value) { }
// This operator overloading enables calling
// operator function () on objects of increment
double operator () (double x) const {
return exp(-lambda*x);
}
// you need friend! Otherwise the class std::ostream can't access Func_obj members
friend std::ostream &operator<<( std::ostream &out, Func_Obj &F_O) {
out << "( Lambda: " << F_O.lambda << " )\n";
return out;
}
};
template <typename T>
double integration(const T a, const T b, const int N, Func_Obj my_obj) {
assert(N>0);
assert(b > a);
double delta = (b-a)/N;
double result = my_obj(a);
for (int i = 1; i< N; i++) {
if (i%2==1) {
result += 4*my_obj(a+i*delta);
}
else {
result += 2*my_obj(a+i*delta);
}
}
result += my_obj(b);
return result*delta/3.0;
}
// template <typename T>
/*
double integration(const double a, const double b, const int N, Func_Obj my_obj(double lambda_value)) {
assert(N>0);
assert(b > a);
double delta = (b-a)/N;
double result = my_obj(a);
for (int i = 1; i< N; i++) {
if (i%2==1) {
result += 4*my_obj(a+i*delta);
}
else {
result += 2*my_obj(a+i*delta);
}
}
result += my_obj(b);
return result*delta/3.0;
}
*/
#include <iostream>
#include <cmath>
#include "function_objects.hpp"
#define _USE_MATH_DEFINES
int main() {
double lambda = 1;
Func_Obj object(lambda);
std::cout << object << "\n";
std::cout << "The integral in [0,1] of exp(-lambda*x) is " << integration<double>(0.0, 1.0, 128, object) << "\n";
return 0;
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment