Boost C++ 库

...世界上最受推崇和专业设计的 C++ 库项目之一。 Herb SutterAndrei Alexandrescu,《C++ 编码标准

PrevUpHomeNext

三项递推关系工具

概要
#include <boost/math/tools/recurrence.hpp>
namespace boost{ namespace math{ namespace tools{

template <class Recurrence, class T>
T function_ratio_from_backwards_recurrence(const Recurrence& r, const T& factor, std::uintmax_t& max_iter);

template <class Recurrence, class T>
T function_ratio_from_forwards_recurrence(const Recurrence& r, const T& factor, std::uintmax_t& max_iter);

template <class NextCoefs, class T>
T apply_recurrence_relation_forward(const NextCoefs& get_coefs, unsigned number_of_steps, T first, T second, long long* log_scaling = 0, T* previous = 0);

template <class T, class NextCoefs>
T apply_recurrence_relation_backward(const NextCoefs& get_coefs, unsigned number_of_steps, T first, T second, long long* log_scaling = 0, T* previous = 0);

template <class Recurrence>
struct forward_recurrence_iterator;

template <class Recurrence>
struct backward_recurrence_iterator;

}}} // namespaces
描述

此标头中的所有工具都需要对递推关系进行描述:这需要一个返回包含 3 个系数的元组的仿函数的形式,具体来说,给定一个递推关系

以及一个仿函数 F,则表达式

F(n);

返回一个包含 { an, bn, cn } 的元组。

例如,以这种形式编写的 Bessel J 和 Y 函数的递推关系是

因此,给定类型为 double 的局部变量 xv,Bessel J 和 Y 的递推关系可以像这样编码在 lambda 表达式中

auto recurrence_functor_jy = [&](int n) { return std::make_tuple(1.0, -2 * (v + n) / x, 1.0); };

类似地,Bessel I 和 K 递推关系仅最终项的符号不同

这可以编码为

auto recurrence_functor_ik = [&](int n) { return std::make_tuple(1.0, -2 * (v + n) / x, -1.0); };

这些工具如下

template <class Recurrence, class T>
T function_ratio_from_backwards_recurrence(const Recurrence& r, const T& factor, std::uintmax_t& max_iter);

给定一个仿函数 r,它在某个位置 n 处对函数 F 的递推关系进行编码,然后返回比率

只有当递推在反向方向稳定时,此计算才是稳定的。此外,计算出的比率是针对递推关系的主导解(在反向方向上)的,如果存在多个解,则不能保证这会找到您想要或期望的解。

参数 factor 是与递推关系关联的连分数的收敛所需的容差,并且不应小于机器 epsilon。参数 max_iter 设置关联的连分数中允许的最大迭代次数。

template <class Recurrence, class T>
T function_ratio_from_forwards_recurrence(const Recurrence& r, const T& factor, std::uintmax_t& max_iter);

给定一个仿函数 r,它在某个位置 n 处对函数 F 的递推关系进行编码,然后返回比率

只有当递推在正向方向稳定时,此计算才是稳定的。此外,计算出的比率是针对递推关系的主导解(在正向方向上)的,如果存在多个解,则不能保证这会找到您想要或期望的解。

参数 factor 是与递推关系关联的连分数的收敛所需的容差,并且不应小于机器 epsilon。参数 max_iter 设置关联的连分数中允许的最大迭代次数。

template <class NextCoefs, class T>
T apply_recurrence_relation_forward(const NextCoefs& get_coefs, unsigned number_of_steps, T first, T second, long long* log_scaling = 0, T* previous = 0);

在稳定的正向方向上应用递推关系,从值 Fn-1 和 Fn 开始。

get_coefs

返回递推关系系数的仿函数。系数应以位置 second 为中心。

number_of_steps

second 开始,向前应用递推关系的步数。

first

Fn-1 的值

second

Fn 的值

log_scaling

如果提供,则可以内部重新缩放递推关系以避免上溢/下溢问题。结果应乘以 exp(*log_scaling) 以获得结果的真实值。

previous

如果提供,则设置为 Fn + number_of_steps - 1 的值

返回 Fn + number_of_steps

template <class NextCoefs, class T>
T apply_recurrence_relation_backward(const NextCoefs& get_coefs, unsigned number_of_steps, T first, T second, long long* log_scaling = 0, T* previous = 0);

在稳定的反向方向上应用递推关系,从值 Fn+1 和 Fn 开始。

get_coefs

返回递推关系系数的仿函数。系数应以位置 second 为中心。

number_of_steps

second 开始,向后应用递推关系的步数。

first

Fn+1 的值

second

Fn 的值

log_scaling

如果提供,则可以内部重新缩放递推关系以避免上溢/下溢问题。结果应乘以 exp(*log_scaling) 以获得结果的真实值。

previous

如果提供,则设置为 Fn - number_of_steps + 1 的值

返回 Fn - number_of_steps

template <class Recurrence>
struct forward_recurrence_iterator
{
   typedef typename std::remove_reference<decltype(std::get<0>(std::declval<Recurrence&>()(0)))>::type value_type;

   forward_recurrence_iterator(const Recurrence& r, value_type f_n_minus_1, value_type f_n);
   forward_recurrence_iterator(const Recurrence& r, value_type f_n);
   /* Operators omitted for clarity */
};

类型 forward_recurrence_iterator 定义了在正向方向上稳定的递推关系的正向迭代器。构造函数接受递推关系,以及一个或两个值:如果仅提供一个值,则第二个值通过使用递推关系来计算函数比率。

template <class Recurrence>
struct backward_recurrence_iterator
{
   typedef typename std::remove_reference<decltype(std::get<0>(std::declval<Recurrence&>()(0)))>::type value_type;

   backward_recurrence_iterator(const Recurrence& r, value_type f_n_plus_1, value_type f_n);
   backward_recurrence_iterator(const Recurrence& r, value_type f_n);
   /* Operators omitted for clarity */
};

类型 backward_recurrence_iterator 定义了在反向方向上稳定的递推关系的正向迭代器。构造函数接受递推关系,以及一个或两个值:如果仅提供一个值,则第二个值通过使用递推关系来计算函数比率。

请注意,递增此迭代器会将返回的值依次移动到 Fn-1、Fn-2 等。


PrevUpHomeNext