lundi 16 novembre 2020

Passing callable in lamda capture

While reading SICP's section about "Formulating Abstractions with Higher-OrderFunctions" I was curious to implement a simple, root solving procedure using Newtons' Method in C++. However my implementation is faulty but I cannot figure out why. I think that I miss something about callable passing and lamda captures (I try to pass std::function in lamda). Bellow I have the sample code which runs a square root problem.

#include <iostream>
#include <functional>
#include <cmath>

static constexpr double DELTA = 0.0000001;
static constexpr int MAX_IT = 50000;
using my_function = std::function<double(double)>;

double derivative(const my_function& f, const double x) {
    return (f(x + DELTA) - f(x)) / DELTA;
}

bool isConverge(const double p1, const double p2) {
    return std::abs(p1 - p2) < DELTA;
}

double fixedPoint(const my_function& g, double x = 1) {

    int itCnt = 0;
    std::function<double(double, double)> loop;
    loop = [&g, &itCnt, &loop](double x0, double x1) -> double {
        if (isConverge(x0, x1) || (++itCnt > MAX_IT)) return x1;
        return loop(x1, g(x1));
    };

    return loop(x, g(x));
}

my_function newtonsMethod(const my_function& f) {
    return [&f](double x) -> double {return x - f(x) / derivative(f, x); };
}

double findRoot(const my_function& f) {
    return fixedPoint(newtonsMethod(f));
}

double sqRoot(double x) { 
    return findRoot([x](int y) {return y * y - x; });
}

int main() {
    const double num = 133;
    std::cout << "Square root " << num << ": " << sqRoot(num) << '\n';
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire