First function object f receives unsigned integer and returns value. It calculates n-th value of the sequence to converge. func returns the same type as f returns after the value is converged. We call the type as value_type hereafter.
Second function object tol recieves two value_type and returns bool that represents the sequence is converged. The two arguments corresponds to the value before and current term.
Third unsigned integer value represents maximum number of iteration. After this iteration, it returns regardless of the result of convergence check.
example
The code below calculates π using the Leibniz formula that states
1−31+51−71+91−⋯=4π.
// function that generates the quadraple of n-th term of leibniz formula.constauto leibniz = [](std::uintmax_t n) noexcept -> double {return (n %2==0) ?4.0/ (2* n +1) :-4.0/ (2* n +1);};// function that judges convergence with some tolerance.constauto tolerance = [](double x,double y) noexcept -> bool {return std::abs(x - y) <1e-12|| std::abs(x / y -1.0) <1e-8;};// maximum number of iteration.std::uintmax_t iteration =10000;// calculating sum of {leibniz(n)} for 0...iteration.constauto sum = ksk::aitken_sum(leibniz, tolerance, iteration);
With an array that contains a sequence
kasok works with arrays that contains the sequence to converge.
The first two iterators point begin() and end(), respectively. After calculation, first points the term used at the last iteration.
The last function object tol recieves two value_type and returns bool that represents the sequence is converged. The two arguments corresponds to the value before and current term.
example
// pre-calculated table.const std::vector<double> table = {1.0,1.0/1.0,1.0/ (1.0*2.0),1.0/ (1.0*2.0*3.0),1.0/ (1.0*2.0*3.0*4.0),1.0/ (1.0*2.0*3.0*4.0*5.0),1.0/ (1.0*2.0*3.0*4.0*5.0*6.0),1.0/ (1.0*2.0*3.0*4.0*5.0*6.0*7.0),1.0/ (1.0*2.0*3.0*4.0*5.0*6.0*7.0*8.0),1.0/ (1.0*2.0*3.0*4.0*5.0*6.0*7.0*8.0*9.0),1.0/ (1.0*2.0*3.0*4.0*5.0*6.0*7.0*8.0*9.0*10.0)};// toleranceconstauto tol = [](double x,double y) noexcept -> bool {return std::abs(x - y) <1e-8|| std::abs(x / y -1.0) <1e-6;};// iterator that points the first elementauto iter =table.begin();// caluclate sum using lookup tableconstauto sum = ksk::aitken_sum(iter,table.end(), tol);
The code below calculates e using the infinite series