std::legendre, std::legendref, std::legendrel

来自cppreference.com
 
 
 
 
在标头 <cmath> 定义
(1)
float       legendre (unsignedint n, float x );

double      legendre (unsignedint n, double x );

longdouble legendre (unsignedint n, longdouble x );
(C++17 起)
(C++23 前)
/* 浮点数类型 */ legendre(unsignedint n,
                                    /* 浮点数类型 */ x );
(C++23 起)
float       legendref(unsignedint n, float x );
(2) (C++17 起)
longdouble legendrel(unsignedint n, longdouble x );
(3) (C++17 起)
在标头 <cmath> 定义
template<class Integer >
double      legendre (unsignedint n, Integer x );
(A) (C++17 起)
1-3) 计算实参 xn 次非关联勒让德多项式标准库提供所有以无 cv 限定的浮点数类型作为形参 x 的类型的 std::legendre 重载。(C++23 起)
A) 为所有整数类型提供额外重载,将它们当做 double

目录

[编辑]参数

n - 多项式的次数
x - 实参,浮点数或整数

[编辑]返回值

如果没有发生错误,那么返回 xn 阶非关联勒让德多项式的值,即
1
2n
n!
dn
dxn
(x2
-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
2
(3x2
- 1)
legendre(3, x)
1
2
(5x3
- 3x)
legendre(4, x)     
1
8
(35x4
- 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){return0.5*(5*std::pow(x, 3)-3* x);}   double P4(double x){return0.125*(35*std::pow(x, 4)-30* x * x +3);}   int main(){// 点检查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. “勒让德多项式”来自 MathWorld--A Wolfram Web Resource。
close