#include "integral.h"

double rectangle(func_ptr f, double a, double b) {
    return (b - a) * f((a + b) / 2);
}

double trapezoidal(func_ptr f, double a, double b) {
    return .5 * (b - a) * (f(a) + f(b));
}

double simpson(func_ptr f, double a, double b) {
    //return (2 * rectangle(f, a, b) + trapezoidal(f, a, b)) / 3;
    return (b - a) / 6 * (f(a) + 4 * f((a + b) / 2) + f(b));
}

double gauss(func_ptr f, double a, double b) {
    double half_dx = .5 * (b - a),
           x1 = a + half_dx * (1 - 1 / sqrt(3)),
           x2 = a + half_dx * (1 + 1 / sqrt(3));

    return half_dx * (f(x1) + f(x2));
}

double integral(func_ptr f, method_ptr method, double a, double b, int steps) {
    int i;
    double surface = 0, dx = (b - a) / steps;

    for( i = 0; i < steps; i++)
        surface += method(f, a + i * dx, a + (i + 1) * dx);

    return surface;
}
