命名空间
变体
操作

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

来自 cppreference.cn
 
 
 
 
定义于头文件 <cmath>
(1)
float       legendre ( unsigned int n, float x );

double      legendre ( unsigned int n, double x );

long double legendre ( unsigned int n, long double x );
(C++17 起)
(C++23 前)
/* floating-point-type */ legendre( unsigned int n,
                                    /* floating-point-type */ 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 起)
1-3) 计算 degree 为 n,自变量为 x 的非伴随 勒让德多项式 库为所有非 cv 限定的浮点类型作为形参 x 的类型提供了 std::legendre 的重载。(C++23 起)
A) 为所有整数类型提供了额外的重载,它们被视作 double

内容

[编辑] 参数

n - 多项式的 degree
x - 自变量,浮点或整数值

[编辑] 返回值

若无错误发生,则返回 order-n 的非伴随勒让德多项式在 x 处的值,即
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) 提供。它们仅需足以保证对于整数类型的自变量 numstd::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. "勒让德多项式。" 来自 MathWorld — Wolfram Web 资源。