E.V.E
v2023.02.15
 
Loading...
Searching...
No Matches

◆ lentz_b

auto eve::lentz_b = functor<lentz_b_t>
inlineconstexpr

Defined in header

#include <eve/module/math.hpp>

Callable Signatures

namespace eve
{
template< typename Gen, eve::floating_value T> auto lentz_b(Gen g, const T& tol, size_t & max_terms) noexcept;
}
constexpr auto lentz_b
Implement the lentz scheme to evaluate continued fractions.
Definition lentz_b.hpp:79
EVE Main Namespace.
Definition abi.hpp:18

Parameters

  • g : generator function.
  • tol : tolerance value. If negative the effective tolerance will be abs(tol)*eveeps(as(< u_t>) where u_t is the underlying floating type associated to the return type of the invocable g.
  • max_terms : no more than max_terms calls to the generator will be made.

The generator type should be an invocable which supports the following operations:

  • The call to g() returns a floating value or a pair (kumi::tuple) of such. Each time this operator is called then the next pair of a and b values has to be returned, or, if result_type is not a pair type, then the next b value has to be returned and all the a values are assumed to be equal to one.
  • In all the continued fraction evaluation functions the effective tol parameter is the relative precision desired in the result, The evaluation of the fraction will continue until the last term evaluated leaves the relative error in the result less than tolerance or the max_terms iteration is reached.

Return value

The value of the continued fraction is returned. \(\displaystyle b_0+\frac{a_1}{b_1+\frac{a_2}{b_2+\frac{a_3}{b_3+\cdots\vphantom{\frac{1}{1}} }}}\)

Note that the the first a value (a0) generated is not used here.

Note
the implementation is largely inspired by the boost/math/fraction one, with less requirements on the invocable. Peculiarly lambda functions can be used.

Example

// revision 1
#include <eve/module/math.hpp>
#include <iostream>
template <class T>
struct const_fraction
{
auto operator()(){ return T{1.0, 2.0, 3.0, 4.0}; }
};
template <typename T>
struct tan_fraction
{
T a, b;
tan_fraction(T v) : a(-v * v), b(-1) {}
auto operator()()
{
b += T(2);
return kumi::tuple{a, b};
}
};
template <class T>
T mytan(T a)
{
tan_fraction<T> fract(a);
return a/eve::lentz_b(fract, eve::eps(eve::as<eve::underlying_type_t<T>>()), 100);
}
int main()
{
const_fraction<w_t> func;
eve::wide zz{1.0, 2.0, 3.0, 4.0};
std::cout << "ref constant fracs " << (zz+eve::sqrt(eve::sqr(zz)+4))/2 << std::endl;
auto gr = eve::lentz_b(func,eve::eps(eve::as<double>()), 100);
std::cout << " constant fracs " << gr << std::endl;
std::cout << "frac tan(" << z << ") is: " << mytan(z) << std::endl;
std::cout << "ref tan(" << z << ") is: " << eve::tan(z) << std::endl;
}
constexpr auto sqr
Computes the square of the parameter.
Definition sqr.hpp:92
constexpr auto sqrt
Computes the square root of the parameter.
Definition sqrt.hpp:80
constexpr auto eps
Computes a constant to the machine epsilon.
Definition eps.hpp:73
constexpr auto pio_4
Callable object computing the constant .
Definition pio_4.hpp:77
constexpr auto pio_3
Callable object computing the constant .
Definition pio_3.hpp:77
constexpr auto tan
elementwise_callable object computing the tangent.
Definition tan.hpp:91
Lightweight type-wrapper.
Definition as.hpp:29
Wrapper for SIMD registers.
Definition wide.hpp:70