Related Articles

Related Articles

std::legendre, std::legendref and std::legendrel functions in C++17
  • Last Updated : 24 Jul, 2018

The legendre , legendref and legendrel are built functions in C++ STL that are used to compute the value of unassociated polynomials of degree n and argument x. Value of order-n unassociated Legendre Polynomial of x is given by :

      \[         \ P_{n}(x)= \frac{1}{2^{n}n!}\frac{d^{n}}{dx^{n}}(x^{2}-1)^{n}      \]

The first few Legendre polynomials are

      \[      Legendre(0,x)= 1 \]  \[      Legendre(1,x)= x \]  \[      Legendre(2,x)= \frac{1}{2} ( 3x^{2}-1 ) \] \[      Legendre(3,x)= \frac{1}{2} ( 5x^{3}-3x ) \]

Syntax:



double legendre( unsigned int n, double x )
             or 
double legendre( unsigned int n, float x )
             or 
double legendre( unsigned int n, long double x )
             or
float legendref( unsigned int n, float x )
             or
long double legendrel( unsigned int n, long double x )

Parameters: The function accepts two mandatory parameters which are described below:

  • n: it specifies the degree of the polynomial.
  • x: it specifies the argument which denotes a value of a floating-point or integral type

Return Value: The function returns the value of order-n unassociated Legendre Polynomial for argument x. The return type depends on the parameters passed.

Note: The function runs in and above C++ 17(7.1).

Below program illustrates the above mentioned functions:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate the above 
// mentioned three functions
#include <cmath> 
#include <iostream>
using namespace std;
int main()
{
    // int and double as parameter 
    cout << "legendre(2,0.3)= " << legendre(2,0.3);
      
    //  x as double type parameter
    cout << "\nlegendre(3,(double)0.4)=" << legendre(3,(double)0.4);
      
     // x as float 
     cout << "\nlegendre(3,(float)0.4)= " << legendre(3,(float)0.4);
  
    //  legendref 
    cout << "\nlegendref(3, 0.45)= " << legendref(3, 0.45);
  
    // legendrel
    cout << "\nlegendrel(7, 0.50)= " << legendrel(7, 0.50);
      
    return 0;
}

chevron_right


Errors and Exceptions: The function throws an error on three cases which is described below:

  • If the argument is NaN, NaN is returned and domain error is not reported
  • The function is not required to be defined for |x|>1
  • If n is greater or equal than 128, the behavior is implementation-defined

Below programs illustrate the above errors:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate the above 
// mentioned three functions
// domain error
#include <cmath> 
#include <iostream>
using namespace std;
int main()
{
    // int and double as parameter 
    cout << "legendre(129, 2)= " << legendre(129, 2);
          
    return 0;
}

chevron_right


Output:

terminate called after throwing an instance of 'std::domain_error'
  what():  Argument out of range in __poly_legendre_p.
legendre(129, 2)=

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate the above 
// mentioned three functions
// when x is NaN
#include <cmath> 
#include <iostream>
using namespace std;
int main()
{
    // int and double as parameter 
    cout << "legendre(129, NaN)= " << legendre(129, sqrt(-2));
      
    return 0;
}

chevron_right


Output:

legendre(129, NaN)= nan

Program 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate the above 
// mentioned three functions
// domain error
#include <cmath> 
#include <iostream>
using namespace std;
int main()
{
    // int and double as parameter 
    cout << "legendre(129, 2)= " << legendre(129, 2);
      
    return 0;
}

chevron_right


Output:

terminate called after throwing an instance of 'std::domain_error'
  what():  Argument out of range in __poly_legendre_p.

Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.




My Personal Notes arrow_drop_up
Recommended Articles
Page :