基于泰勒级数的Sin()实现,使用c++模板
Taylor Series based Sin() implementation using templates C++
我正在使用模板实现基于泰勒级数的sin()函数。到目前为止,我的代码如下:
template<unsigned I>
struct factorial{
enum{
value = I * factorial<I -1>::value
};
};
template<>
struct factorial<0>
{
enum{ value = 1};
};
template<unsigned pow>
inline double power(double const& value){
return value * power<pow-1>(value);
}
template<>
inline double power<1>(double const& value){
return value;
}
template <unsigned term>
inline double taylor_sine_term(double const& x) {
return (power<term>(-1) / factorial<(2*term)+1>::value) * power<(2*term)+1>(x);
}
我遇到的问题是创建函数来使用taylor_sine_term
函数来展开N个项的级数。该函数在功能上与以下函数等效。
template<unsigned terms>
inline double taylor_sine(double const& x){
return taylor_sine_term<1>(x)+taylor_sine_term<2>(x) + ... + taylor_sine_term<terms>(x);
}
基本上,我需要找出一种方法来生成一个整数常量序列,可以用作taylor_sine_term
函数中N个术语的术语数模板参数,并将它们全部加在一起。结果将展开为N个项的完整泰勒级数。问题是我不知道从哪里开始。
跟你写的一样:递归情况和基本情况:
template<unsigned terms>
inline double taylor_sine(double const& x) {
return taylor_sine_term<terms>(x) + // current term
taylor_sine<terms-1>(x); // rest of terms
}
template <>
inline double taylor_sine<0>(const double& x) {
return taylor_sine_term<0>(x);
}
或者,您可以使用索引序列技巧来获得所有这些:
template <unsigned terms>
inline double taylor_sine(double const& x) {
return taylor_sine(x, std::make_index_sequence<terms+1>{});
}
template <unsigned terms, size_t... Is>
inline double taylor_sine(const double& x, std::index_sequence<Is...> ) {
double res = 0.0;
for (double t : {taylor_sine_term<Is>(x)...}) {
res += t;
}
return res;
/* or in C++17
return (taylor_sine_term<Is>(x) + ...);
*/
}
注意,您应该将power
上的基本情况更改为power<0>
。
听起来你想要一个递归定义的模板
template<unsigned terms>
inline double taylor_sine(double const& x){
return taylor_sine<terms-1>(x)+taylor_sine_term<terms>(x);
}
则希望专门化以使0或1终止。但是我认为你不能专门化函数模板,所以你需要把所有这些都隐藏在一个可以专门化的函子模板中。
相关文章:
- 如何在c++中使用引用实现类似python的行为
- 使用简单类型列表实现的指数编译时间.为什么
- 我应该实现右值推送功能吗?我应该使用std::move吗
- 使用模板进行堆栈实现; "name followed by :: must be a class or namespace"
- 使用GSoap实现ONVIF
- C++如何仅使用MOV在x86上实现发布和获取
- 为什么大多数 pair 实现默认不使用压缩(空基优化)?
- 尝试使用继承和模板实现CRTP.Visual Studio正在生成编译器错误
- 如何使用lock_guard在c++11中实现scoped_lock功能
- 在c++队列中使用pop和visit实现线程安全
- 如何使用set实现无序数据结构?
- 在实现文件中使用头文件的通用 lambda
- 实现使用模板化类而不使原始类成为模板的类
- 如何实现使用 gcc-4.4 编译的大向量初始化?
- 为什么此单例实现使用私人类(C )
- 如何在 MATLAB 中实现/使用C++头文件
- 为什么对std::tuple实现使用递归继承不好
- 链表实现-使用单独的Insert(), Delete(), Print()函数
- 为什么libc++的map实现使用这种联合?
- 实现使用 C++ 存储任何类型的哈希