std::legendre, std::legendref, std::legendrel
来自 cppreference.cn
定义于头文件 <cmath> |
||
(1) | ||
float legendre ( unsigned int n, float x ); double legendre ( unsigned int n, double x ); |
(C++17 起) (直至 C++23) |
|
/* 浮点类型 */ legendre( unsigned int n, /* 浮点类型 */ x ); |
(C++23 起) | |
float legendref( unsigned int n, float x ); |
(2) | (C++17 起) |
long double legendrel( unsigned int n, long double x ); |
(3) | (C++17 起) |
定义于头文件 <cmath> |
||
template< class Integer > double legendre ( unsigned int n, Integer x ); |
(A) | (C++17 起) |
A) 为所有整数类型提供了额外的重载,它们被视为 double。
目录 |
[编辑] 参数
n | - | 多项式的次数 |
x | - | 变量,浮点或整数值 |
[编辑] 返回值
如果没有错误发生,则返回 x 的 n 阶非关联勒让德多项式的值,即1 |
2n n! |
dn |
dxn |
-1)n
。
[编辑] 错误处理
错误可能按 math_errhandling 中指定的方式报告。
- 如果参数是 NaN,则返回 NaN,不报告域错误
- 函数不要求对 |x|>1 定义
- 如果 n 大于或等于 128,则行为由实现定义
[编辑] 注意
不支持 C++17 但支持 ISO 29124:2010 的实现,若实现将 __STDCPP_MATH_SPEC_FUNCS__
定义为至少 201003L 且用户在包含任何标准库头文件前定义 __STDCPP_WANT_MATH_SPEC_FUNCS__
,则提供此函数。
不支持 ISO 29124:2010 但支持 TR 19768:2007 (TR1) 的实现,在头文件 tr1/cmath
和命名空间 std::tr1
中提供此函数。
此函数的一个实现也在 boost.math 中可用。
前几个勒让德多项式是:
函数 | 多项式 | ||
---|---|---|---|
legendre(0, x) | 1 | ||
legendre(1, x) | x | ||
legendre(2, x) |
- 1) | ||
legendre(3, x) |
- 3x) | ||
legendre(4, x) |
- 30x2 + 3) |
不要求精确提供 (A) 中所示的额外重载。它们只需足以确保对于整型参数 num,std::legendre(int_num, num) 的效果与 std::legendre(int_num, static_cast<double>(num)) 相同。
[编辑] 示例
运行此代码
#include <cmath> #include <iostream> double P3(double x) { return 0.5 * (5 * std::pow(x, 3) - 3 * x); } double P4(double x) { return 0.125 * (35 * std::pow(x, 4) - 30 * x * x + 3); } int main() { // spot-checks std::cout << std::legendre(3, 0.25) << '=' << P3(0.25) << '\n' << std::legendre(4, 0.25) << '=' << P4(0.25) << '\n'; }
输出
-0.335938=-0.335938 0.157715=0.157715
[编辑] 参阅
(C++17)(C++17)(C++17) |
拉盖尔多项式 (函数) |
(C++17)(C++17)(C++17) |
埃尔米特多项式 (函数) |
[编辑] 外部链接
Weisstein, Eric W. "Legendre Polynomial." From MathWorld — A Wolfram Web Resource. |