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

Detailed Description

These functions computes continued fractions

Variables

constexpr auto eve::lentz_a = functor<lentz_a_t>
 Implement the Lentz scheme to evaluate a continued fraction with no leading term: \(\displaystyle \frac{a_0}{b_0+\frac{a_1}{b_1+\frac{a_2}{b_2+\cdots}}}\).
constexpr auto eve::lentz_b = functor<lentz_b_t>
 Implement the Lentz scheme to evaluate a continued fraction with leading term \(b_0\): \(\displaystyle b_0+\frac{a_1}{b_1+\frac{a_2}{b_2+\cdots}}\).

Variable Documentation

◆ lentz_a

auto eve::lentz_a = functor<lentz_a_t>
inlineconstexpr

Implement the Lentz scheme to evaluate a continued fraction with no leading term: \(\displaystyle \frac{a_0}{b_0+\frac{a_1}{b_1+\frac{a_2}{b_2+\cdots}}}\).

Defined in header

#include <eve/module/math.hpp>

Callable Signatures

namespace eve
{
template< typename Gen, eve::scalar_value T> auto lentz_a(Gen g, const T& tol, size_t & max_terms) noexcept;
}
constexpr auto lentz_a
Implement the Lentz scheme to evaluate a continued fraction with no leading term: .
Definition lentz_a.hpp:80
EVE Main Namespace.
Definition abi.hpp:19

Parameters

  • g : generator function.
  • tol : tolerance value. If negative the effective tolerance will be abs(tol)*eve::eps(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 \frac{a_0}{b_0+\frac{a_1}{b_1+\frac{a_2}{b_2+\cdots\vphantom{\frac{1}{1}} }}}\)

Note that the first a and b values (a0 and b0) generated are both used here.

Example

// revision 1
#include <eve/module/math.hpp>
#include <iostream>
template <class T>
struct const_fraction
{
typedef T result_type;
result_type operator()()
{
return T{1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0};
}
};
int main()
{
eve::wide z{1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0};
auto ref = (-z+eve::sqrt(eve::sqr(z)+4))/2;
std::cout << "ref constant fracs are: " << ref << std::endl;
const_fraction<decltype(z)> func;
auto gr = eve::lentz_a(func,eve::eps(eve::as<double>()), 100);
std::cout << " constant fracs are: " << gr << std::endl;
}
constexpr auto sqr
Computes the square of the parameter.
Definition sqr.hpp:98
constexpr auto sqrt
Computes the elementwise square root of the parameter.
Definition sqrt.hpp:86
constexpr auto eps
Computes a constant to the machine epsilon.
Definition eps.hpp:74
Lightweight type-wrapper.
Definition as.hpp:29
Wrapper for SIMD registers.
Definition wide.hpp:94

◆ lentz_b

auto eve::lentz_b = functor<lentz_b_t>
inlineconstexpr

Implement the Lentz scheme to evaluate a continued fraction with leading term \(b_0\): \(\displaystyle b_0+\frac{a_1}{b_1+\frac{a_2}{b_2+\cdots}}\).

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 a continued fraction with leading term : .
Definition lentz_b.hpp:93

Parameters

  • g : generator function.
  • tol : tolerance value. If negative the effective tolerance will be abs(tol)*eve::eps(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 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 pio_4
Callable object computing the constant .
Definition pio_4.hpp:76
constexpr auto pio_3
Callable object computing the constant .
Definition pio_3.hpp:76
constexpr auto tan
elementwise_callable object computing the tangent.
Definition tan.hpp:93
tuple(Ts &&...) -> tuple< std::unwrap_ref_decay_t< Ts >... >