math/doc/html/math_toolkit/sf_poly/legendre.html
2019-10-31 17:55:35 +00:00

658 lines
50 KiB
HTML

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Legendre (and Associated) Polynomials</title>
<link rel="stylesheet" href="../../math.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../index.html" title="Math Toolkit 2.11.0">
<link rel="up" href="../sf_poly.html" title="Polynomials">
<link rel="prev" href="../sf_poly.html" title="Polynomials">
<link rel="next" href="legendre_stieltjes.html" title="Legendre-Stieltjes Polynomials">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../sf_poly.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../sf_poly.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="legendre_stieltjes.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="math_toolkit.sf_poly.legendre"></a><a class="link" href="legendre.html" title="Legendre (and Associated) Polynomials">Legendre (and Associated)
Polynomials</a>
</h3></div></div></div>
<h5>
<a name="math_toolkit.sf_poly.legendre.h0"></a>
<span class="phrase"><a name="math_toolkit.sf_poly.legendre.synopsis"></a></span><a class="link" href="legendre.html#math_toolkit.sf_poly.legendre.synopsis">Synopsis</a>
</h5>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">math</span><span class="special">/</span><span class="identifier">special_functions</span><span class="special">/</span><span class="identifier">legendre</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
</pre>
<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">math</span><span class="special">{</span>
<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span>
<a class="link" href="../result_type.html" title="Calculation of the Type of the Result"><span class="emphasis"><em>calculated-result-type</em></span></a> <span class="identifier">legendre_p</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">n</span><span class="special">,</span> <span class="identifier">T</span> <span class="identifier">x</span><span class="special">);</span>
<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">,</span> <span class="keyword">class</span> <a class="link" href="../../policy.html" title="Chapter&#160;20.&#160;Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">&gt;</span>
<a class="link" href="../result_type.html" title="Calculation of the Type of the Result"><span class="emphasis"><em>calculated-result-type</em></span></a> <span class="identifier">legendre_p</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">n</span><span class="special">,</span> <span class="identifier">T</span> <span class="identifier">x</span><span class="special">,</span> <span class="keyword">const</span> <a class="link" href="../../policy.html" title="Chapter&#160;20.&#160;Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">&amp;);</span>
<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span>
<a class="link" href="../result_type.html" title="Calculation of the Type of the Result"><span class="emphasis"><em>calculated-result-type</em></span></a> <span class="identifier">legendre_p_prime</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">n</span><span class="special">,</span> <span class="identifier">T</span> <span class="identifier">x</span><span class="special">);</span>
<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">,</span> <span class="keyword">class</span> <a class="link" href="../../policy.html" title="Chapter&#160;20.&#160;Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">&gt;</span>
<a class="link" href="../result_type.html" title="Calculation of the Type of the Result"><span class="emphasis"><em>calculated-result-type</em></span></a> <span class="identifier">legendre_p_prime</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">n</span><span class="special">,</span> <span class="identifier">T</span> <span class="identifier">x</span><span class="special">,</span> <span class="keyword">const</span> <a class="link" href="../../policy.html" title="Chapter&#160;20.&#160;Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">&amp;);</span>
<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">,</span> <span class="keyword">class</span> <a class="link" href="../../policy.html" title="Chapter&#160;20.&#160;Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">&gt;</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span> <span class="identifier">legendre_p_zeros</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">l</span><span class="special">,</span> <span class="keyword">const</span> <a class="link" href="../../policy.html" title="Chapter&#160;20.&#160;Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">&amp;);</span>
<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span> <span class="identifier">legendre_p_zeros</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">l</span><span class="special">);</span>
<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span>
<a class="link" href="../result_type.html" title="Calculation of the Type of the Result"><span class="emphasis"><em>calculated-result-type</em></span></a> <span class="identifier">legendre_p</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">n</span><span class="special">,</span> <span class="keyword">int</span> <span class="identifier">m</span><span class="special">,</span> <span class="identifier">T</span> <span class="identifier">x</span><span class="special">);</span>
<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">,</span> <span class="keyword">class</span> <a class="link" href="../../policy.html" title="Chapter&#160;20.&#160;Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">&gt;</span>
<a class="link" href="../result_type.html" title="Calculation of the Type of the Result"><span class="emphasis"><em>calculated-result-type</em></span></a> <span class="identifier">legendre_p</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">n</span><span class="special">,</span> <span class="keyword">int</span> <span class="identifier">m</span><span class="special">,</span> <span class="identifier">T</span> <span class="identifier">x</span><span class="special">,</span> <span class="keyword">const</span> <a class="link" href="../../policy.html" title="Chapter&#160;20.&#160;Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">&amp;);</span>
<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span>
<a class="link" href="../result_type.html" title="Calculation of the Type of the Result"><span class="emphasis"><em>calculated-result-type</em></span></a> <span class="identifier">legendre_q</span><span class="special">(</span><span class="keyword">unsigned</span> <span class="identifier">n</span><span class="special">,</span> <span class="identifier">T</span> <span class="identifier">x</span><span class="special">);</span>
<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">,</span> <span class="keyword">class</span> <a class="link" href="../../policy.html" title="Chapter&#160;20.&#160;Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">&gt;</span>
<a class="link" href="../result_type.html" title="Calculation of the Type of the Result"><span class="emphasis"><em>calculated-result-type</em></span></a> <span class="identifier">legendre_q</span><span class="special">(</span><span class="keyword">unsigned</span> <span class="identifier">n</span><span class="special">,</span> <span class="identifier">T</span> <span class="identifier">x</span><span class="special">,</span> <span class="keyword">const</span> <a class="link" href="../../policy.html" title="Chapter&#160;20.&#160;Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">&amp;);</span>
<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T1</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">T2</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">T3</span><span class="special">&gt;</span>
<a class="link" href="../result_type.html" title="Calculation of the Type of the Result"><span class="emphasis"><em>calculated-result-type</em></span></a> <span class="identifier">legendre_next</span><span class="special">(</span><span class="keyword">unsigned</span> <span class="identifier">l</span><span class="special">,</span> <span class="identifier">T1</span> <span class="identifier">x</span><span class="special">,</span> <span class="identifier">T2</span> <span class="identifier">Pl</span><span class="special">,</span> <span class="identifier">T3</span> <span class="identifier">Plm1</span><span class="special">);</span>
<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T1</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">T2</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">T3</span><span class="special">&gt;</span>
<a class="link" href="../result_type.html" title="Calculation of the Type of the Result"><span class="emphasis"><em>calculated-result-type</em></span></a> <span class="identifier">legendre_next</span><span class="special">(</span><span class="keyword">unsigned</span> <span class="identifier">l</span><span class="special">,</span> <span class="keyword">unsigned</span> <span class="identifier">m</span><span class="special">,</span> <span class="identifier">T1</span> <span class="identifier">x</span><span class="special">,</span> <span class="identifier">T2</span> <span class="identifier">Pl</span><span class="special">,</span> <span class="identifier">T3</span> <span class="identifier">Plm1</span><span class="special">);</span>
<span class="special">}}</span> <span class="comment">// namespaces</span>
</pre>
<p>
The return type of these functions is computed using the <a class="link" href="../result_type.html" title="Calculation of the Type of the Result"><span class="emphasis"><em>result
type calculation rules</em></span></a>: note than when there is a single
template argument the result is the same type as that argument or <code class="computeroutput"><span class="keyword">double</span></code> if the template argument is an integer
type.
</p>
<p>
The final <a class="link" href="../../policy.html" title="Chapter&#160;20.&#160;Policies: Controlling Precision, Error Handling etc">Policy</a> argument is optional and can
be used to control the behaviour of the function: how it handles errors,
what level of precision to use etc. Refer to the <a class="link" href="../../policy.html" title="Chapter&#160;20.&#160;Policies: Controlling Precision, Error Handling etc">policy
documentation for more details</a>.
</p>
<h5>
<a name="math_toolkit.sf_poly.legendre.h1"></a>
<span class="phrase"><a name="math_toolkit.sf_poly.legendre.description"></a></span><a class="link" href="legendre.html#math_toolkit.sf_poly.legendre.description">Description</a>
</h5>
<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span>
<a class="link" href="../result_type.html" title="Calculation of the Type of the Result"><span class="emphasis"><em>calculated-result-type</em></span></a> <span class="identifier">legendre_p</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">l</span><span class="special">,</span> <span class="identifier">T</span> <span class="identifier">x</span><span class="special">);</span>
<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">,</span> <span class="keyword">class</span> <a class="link" href="../../policy.html" title="Chapter&#160;20.&#160;Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">&gt;</span>
<a class="link" href="../result_type.html" title="Calculation of the Type of the Result"><span class="emphasis"><em>calculated-result-type</em></span></a> <span class="identifier">legendre_p</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">l</span><span class="special">,</span> <span class="identifier">T</span> <span class="identifier">x</span><span class="special">,</span> <span class="keyword">const</span> <a class="link" href="../../policy.html" title="Chapter&#160;20.&#160;Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">&amp;);</span>
</pre>
<p>
Returns the Legendre Polynomial of the first kind:
</p>
<div class="blockquote"><blockquote class="blockquote"><p>
<span class="inlinemediaobject"><img src="../../../equations/legendre_0.svg"></span>
</p></blockquote></div>
<p>
Requires -1 &lt;= x &lt;= 1, otherwise returns the result of <a class="link" href="../error_handling.html#math_toolkit.error_handling.domain_error">domain_error</a>.
</p>
<p>
Negative orders are handled via the reflection formula:
</p>
<div class="blockquote"><blockquote class="blockquote"><p>
P<sub>-l-1</sub>(x) = P<sub>l</sub>(x)
</p></blockquote></div>
<p>
The following graph illustrates the behaviour of the first few Legendre Polynomials:
</p>
<div class="blockquote"><blockquote class="blockquote"><p>
<span class="inlinemediaobject"><img src="../../../graphs/legendre_p.svg" align="middle"></span>
</p></blockquote></div>
<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span>
<a class="link" href="../result_type.html" title="Calculation of the Type of the Result"><span class="emphasis"><em>calculated-result-type</em></span></a> <span class="identifier">legendre_p_prime</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">n</span><span class="special">,</span> <span class="identifier">T</span> <span class="identifier">x</span><span class="special">);</span>
<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">,</span> <span class="keyword">class</span> <a class="link" href="../../policy.html" title="Chapter&#160;20.&#160;Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">&gt;</span>
<a class="link" href="../result_type.html" title="Calculation of the Type of the Result"><span class="emphasis"><em>calculated-result-type</em></span></a> <span class="identifier">legendre_p_prime</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">n</span><span class="special">,</span> <span class="identifier">T</span> <span class="identifier">x</span><span class="special">,</span> <span class="keyword">const</span> <a class="link" href="../../policy.html" title="Chapter&#160;20.&#160;Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">&amp;);</span>
</pre>
<p>
Returns the derivatives of the Legendre polynomials.
</p>
<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">,</span> <span class="keyword">class</span> <a class="link" href="../../policy.html" title="Chapter&#160;20.&#160;Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">&gt;</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span> <span class="identifier">legendre_p_zeros</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">l</span><span class="special">,</span> <span class="keyword">const</span> <a class="link" href="../../policy.html" title="Chapter&#160;20.&#160;Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">&amp;);</span>
<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span> <span class="identifier">legendre_p_zeros</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">l</span><span class="special">);</span>
</pre>
<p>
The zeros of the Legendre polynomials are calculated by Newton's method using
an initial guess given by Tricomi with root bracketing provided by Szego.
</p>
<p>
Since the Legendre polynomials are alternatively even and odd, only the non-negative
zeros are returned. For the odd Legendre polynomials, the first zero is always
zero. The rest of the zeros are returned in increasing order.
</p>
<p>
Note that the argument to the routine is an integer, and the output is a
floating-point type. Hence the template argument is mandatory. The time to
extract a single root is linear in <code class="computeroutput"><span class="identifier">l</span></code>
(this is scaling to evaluate the Legendre polynomials), so recovering all
roots is &#119926;(<code class="computeroutput"><span class="identifier">l</span></code><sup>2</sup>). Algorithms
with linear scaling <a href="https://doi.org/10.1137/06067016X" target="_top">exist</a>
for recovering all roots, but requires tooling not currently built into boost.math.
This implementation proceeds under the assumption that calculating zeros
of these functions will not be a bottleneck for any workflow.
</p>
<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span>
<a class="link" href="../result_type.html" title="Calculation of the Type of the Result"><span class="emphasis"><em>calculated-result-type</em></span></a> <span class="identifier">legendre_p</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">l</span><span class="special">,</span> <span class="keyword">int</span> <span class="identifier">m</span><span class="special">,</span> <span class="identifier">T</span> <span class="identifier">x</span><span class="special">);</span>
<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">,</span> <span class="keyword">class</span> <a class="link" href="../../policy.html" title="Chapter&#160;20.&#160;Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">&gt;</span>
<a class="link" href="../result_type.html" title="Calculation of the Type of the Result"><span class="emphasis"><em>calculated-result-type</em></span></a> <span class="identifier">legendre_p</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">l</span><span class="special">,</span> <span class="keyword">int</span> <span class="identifier">m</span><span class="special">,</span> <span class="identifier">T</span> <span class="identifier">x</span><span class="special">,</span> <span class="keyword">const</span> <a class="link" href="../../policy.html" title="Chapter&#160;20.&#160;Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">&amp;);</span>
</pre>
<p>
Returns the associated Legendre polynomial of the first kind:
</p>
<div class="blockquote"><blockquote class="blockquote"><p>
<span class="inlinemediaobject"><img src="../../../equations/legendre_1.svg"></span>
</p></blockquote></div>
<p>
Requires -1 &lt;= x &lt;= 1, otherwise returns the result of <a class="link" href="../error_handling.html#math_toolkit.error_handling.domain_error">domain_error</a>.
</p>
<p>
Negative values of <span class="emphasis"><em>l</em></span> and <span class="emphasis"><em>m</em></span> are
handled via the identity relations:
</p>
<div class="blockquote"><blockquote class="blockquote"><p>
<span class="inlinemediaobject"><img src="../../../equations/legendre_3.svg"></span>
</p></blockquote></div>
<div class="caution"><table border="0" summary="Caution">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Caution]" src="../../../../../../doc/src/images/caution.png"></td>
<th align="left">Caution</th>
</tr>
<tr><td align="left" valign="top">
<p>
The definition of the associated Legendre polynomial used here includes
a leading Condon-Shortley phase term of (-1)<sup>m</sup>. This matches the definition
given by Abramowitz and Stegun (8.6.6) and that used by <a href="http://mathworld.wolfram.com/LegendrePolynomial.html" target="_top">Mathworld</a>
and <a href="http://documents.wolfram.com/mathematica/functions/LegendreP" target="_top">Mathematica's
LegendreP function</a>. However, uses in the literature do not always
include this phase term, and strangely the specification for the associated
Legendre function in the C++ TR1 (assoc_legendre) also omits it, in spite
of stating that it uses Abramowitz and Stegun as the final arbiter on these
matters.
</p>
<p>
See:
</p>
<p>
<a href="http://mathworld.wolfram.com/LegendrePolynomial.html" target="_top">Weisstein,
Eric W. "Legendre Polynomial." From MathWorld--A Wolfram Web
Resource</a>.
</p>
<p>
Abramowitz, M. and Stegun, I. A. (Eds.). "Legendre Functions"
and "Orthogonal Polynomials." Ch. 22 in Chs. 8 and 22 in Handbook
of Mathematical Functions with Formulas, Graphs, and Mathematical Tables,
9th printing. New York: Dover, pp. 331-339 and 771-802, 1972.
</p>
</td></tr>
</table></div>
<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span>
<a class="link" href="../result_type.html" title="Calculation of the Type of the Result"><span class="emphasis"><em>calculated-result-type</em></span></a> <span class="identifier">legendre_q</span><span class="special">(</span><span class="keyword">unsigned</span> <span class="identifier">n</span><span class="special">,</span> <span class="identifier">T</span> <span class="identifier">x</span><span class="special">);</span>
<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">,</span> <span class="keyword">class</span> <a class="link" href="../../policy.html" title="Chapter&#160;20.&#160;Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">&gt;</span>
<a class="link" href="../result_type.html" title="Calculation of the Type of the Result"><span class="emphasis"><em>calculated-result-type</em></span></a> <span class="identifier">legendre_q</span><span class="special">(</span><span class="keyword">unsigned</span> <span class="identifier">n</span><span class="special">,</span> <span class="identifier">T</span> <span class="identifier">x</span><span class="special">,</span> <span class="keyword">const</span> <a class="link" href="../../policy.html" title="Chapter&#160;20.&#160;Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">&amp;);</span>
</pre>
<p>
Returns the value of the Legendre polynomial that is the second solution
to the Legendre differential equation, for example:
</p>
<div class="blockquote"><blockquote class="blockquote"><p>
<span class="inlinemediaobject"><img src="../../../equations/legendre_2.svg"></span>
</p></blockquote></div>
<p>
Requires -1 &lt;= x &lt;= 1, otherwise <a class="link" href="../error_handling.html#math_toolkit.error_handling.domain_error">domain_error</a>
is called.
</p>
<p>
The following graph illustrates the first few Legendre functions of the second
kind:
</p>
<div class="blockquote"><blockquote class="blockquote"><p>
<span class="inlinemediaobject"><img src="../../../graphs/legendre_q.svg" align="middle"></span>
</p></blockquote></div>
<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T1</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">T2</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">T3</span><span class="special">&gt;</span>
<a class="link" href="../result_type.html" title="Calculation of the Type of the Result"><span class="emphasis"><em>calculated-result-type</em></span></a> <span class="identifier">legendre_next</span><span class="special">(</span><span class="keyword">unsigned</span> <span class="identifier">l</span><span class="special">,</span> <span class="identifier">T1</span> <span class="identifier">x</span><span class="special">,</span> <span class="identifier">T2</span> <span class="identifier">Pl</span><span class="special">,</span> <span class="identifier">T3</span> <span class="identifier">Plm1</span><span class="special">);</span>
</pre>
<p>
Implements the three term recurrence relation for the Legendre polynomials,
this function can be used to create a sequence of values evaluated at the
same <span class="emphasis"><em>x</em></span>, and for rising <span class="emphasis"><em>l</em></span>. This
recurrence relation holds for Legendre Polynomials of both the first and
second kinds.
</p>
<div class="blockquote"><blockquote class="blockquote"><p>
<span class="inlinemediaobject"><img src="../../../equations/legendre_4.svg"></span>
</p></blockquote></div>
<p>
For example we could produce a vector of the first 10 polynomial values using:
</p>
<pre class="programlisting"><span class="keyword">double</span> <span class="identifier">x</span> <span class="special">=</span> <span class="number">0.5</span><span class="special">;</span> <span class="comment">// Abscissa value</span>
<span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;</span> <span class="identifier">v</span><span class="special">;</span>
<span class="identifier">v</span><span class="special">.</span><span class="identifier">push_back</span><span class="special">(</span><span class="identifier">legendre_p</span><span class="special">(</span><span class="number">0</span><span class="special">,</span> <span class="identifier">x</span><span class="special">));</span>
<span class="identifier">v</span><span class="special">.</span><span class="identifier">push_back</span><span class="special">(</span><span class="identifier">legendre_p</span><span class="special">(</span><span class="number">1</span><span class="special">,</span> <span class="identifier">x</span><span class="special">));</span>
<span class="keyword">for</span><span class="special">(</span><span class="keyword">unsigned</span> <span class="identifier">l</span> <span class="special">=</span> <span class="number">1</span><span class="special">;</span> <span class="identifier">l</span> <span class="special">&lt;</span> <span class="number">10</span><span class="special">;</span> <span class="special">++</span><span class="identifier">l</span><span class="special">)</span>
<span class="identifier">v</span><span class="special">.</span><span class="identifier">push_back</span><span class="special">(</span><span class="identifier">legendre_next</span><span class="special">(</span><span class="identifier">l</span><span class="special">,</span> <span class="identifier">x</span><span class="special">,</span> <span class="identifier">v</span><span class="special">[</span><span class="identifier">l</span><span class="special">],</span> <span class="identifier">v</span><span class="special">[</span><span class="identifier">l</span><span class="special">-</span><span class="number">1</span><span class="special">]));</span>
<span class="comment">// Double check values:</span>
<span class="keyword">for</span><span class="special">(</span><span class="keyword">unsigned</span> <span class="identifier">l</span> <span class="special">=</span> <span class="number">1</span><span class="special">;</span> <span class="identifier">l</span> <span class="special">&lt;</span> <span class="number">10</span><span class="special">;</span> <span class="special">++</span><span class="identifier">l</span><span class="special">)</span>
<span class="identifier">assert</span><span class="special">(</span><span class="identifier">v</span><span class="special">[</span><span class="identifier">l</span><span class="special">]</span> <span class="special">==</span> <span class="identifier">legendre_p</span><span class="special">(</span><span class="identifier">l</span><span class="special">,</span> <span class="identifier">x</span><span class="special">));</span>
</pre>
<p>
Formally the arguments are:
</p>
<div class="variablelist">
<p class="title"><b></b></p>
<dl class="variablelist">
<dt><span class="term">l</span></dt>
<dd><p>
The degree of the last polynomial calculated.
</p></dd>
<dt><span class="term">x</span></dt>
<dd><p>
The abscissa value
</p></dd>
<dt><span class="term">Pl</span></dt>
<dd><p>
The value of the polynomial evaluated at degree <span class="emphasis"><em>l</em></span>.
</p></dd>
<dt><span class="term">Plm1</span></dt>
<dd><p>
The value of the polynomial evaluated at degree <span class="emphasis"><em>l-1</em></span>.
</p></dd>
</dl>
</div>
<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T1</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">T2</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">T3</span><span class="special">&gt;</span>
<a class="link" href="../result_type.html" title="Calculation of the Type of the Result"><span class="emphasis"><em>calculated-result-type</em></span></a> <span class="identifier">legendre_next</span><span class="special">(</span><span class="keyword">unsigned</span> <span class="identifier">l</span><span class="special">,</span> <span class="keyword">unsigned</span> <span class="identifier">m</span><span class="special">,</span> <span class="identifier">T1</span> <span class="identifier">x</span><span class="special">,</span> <span class="identifier">T2</span> <span class="identifier">Pl</span><span class="special">,</span> <span class="identifier">T3</span> <span class="identifier">Plm1</span><span class="special">);</span>
</pre>
<p>
Implements the three term recurrence relation for the Associated Legendre
polynomials, this function can be used to create a sequence of values evaluated
at the same <span class="emphasis"><em>x</em></span>, and for rising <span class="emphasis"><em>l</em></span>.
</p>
<div class="blockquote"><blockquote class="blockquote"><p>
<span class="inlinemediaobject"><img src="../../../equations/legendre_5.svg"></span>
</p></blockquote></div>
<p>
For example we could produce a vector of the first m+10 polynomial values
using:
</p>
<pre class="programlisting"><span class="keyword">double</span> <span class="identifier">x</span> <span class="special">=</span> <span class="number">0.5</span><span class="special">;</span> <span class="comment">// Abscissa value</span>
<span class="keyword">int</span> <span class="identifier">m</span> <span class="special">=</span> <span class="number">10</span><span class="special">;</span> <span class="comment">// order</span>
<span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;</span> <span class="identifier">v</span><span class="special">;</span>
<span class="identifier">v</span><span class="special">.</span><span class="identifier">push_back</span><span class="special">(</span><span class="identifier">legendre_p</span><span class="special">(</span><span class="identifier">m</span><span class="special">,</span> <span class="identifier">m</span><span class="special">,</span> <span class="identifier">x</span><span class="special">));</span>
<span class="identifier">v</span><span class="special">.</span><span class="identifier">push_back</span><span class="special">(</span><span class="identifier">legendre_p</span><span class="special">(</span><span class="number">1</span> <span class="special">+</span> <span class="identifier">m</span><span class="special">,</span> <span class="identifier">m</span><span class="special">,</span> <span class="identifier">x</span><span class="special">));</span>
<span class="keyword">for</span><span class="special">(</span><span class="keyword">unsigned</span> <span class="identifier">l</span> <span class="special">=</span> <span class="number">1</span><span class="special">;</span> <span class="identifier">l</span> <span class="special">&lt;</span> <span class="number">10</span><span class="special">;</span> <span class="special">++</span><span class="identifier">l</span><span class="special">)</span>
<span class="identifier">v</span><span class="special">.</span><span class="identifier">push_back</span><span class="special">(</span><span class="identifier">legendre_next</span><span class="special">(</span><span class="identifier">l</span> <span class="special">+</span> <span class="number">10</span><span class="special">,</span> <span class="identifier">m</span><span class="special">,</span> <span class="identifier">x</span><span class="special">,</span> <span class="identifier">v</span><span class="special">[</span><span class="identifier">l</span><span class="special">],</span> <span class="identifier">v</span><span class="special">[</span><span class="identifier">l</span><span class="special">-</span><span class="number">1</span><span class="special">]));</span>
<span class="comment">// Double check values:</span>
<span class="keyword">for</span><span class="special">(</span><span class="keyword">unsigned</span> <span class="identifier">l</span> <span class="special">=</span> <span class="number">1</span><span class="special">;</span> <span class="identifier">l</span> <span class="special">&lt;</span> <span class="number">10</span><span class="special">;</span> <span class="special">++</span><span class="identifier">l</span><span class="special">)</span>
<span class="identifier">assert</span><span class="special">(</span><span class="identifier">v</span><span class="special">[</span><span class="identifier">l</span><span class="special">]</span> <span class="special">==</span> <span class="identifier">legendre_p</span><span class="special">(</span><span class="number">10</span> <span class="special">+</span> <span class="identifier">l</span><span class="special">,</span> <span class="identifier">m</span><span class="special">,</span> <span class="identifier">x</span><span class="special">));</span>
</pre>
<p>
Formally the arguments are:
</p>
<div class="variablelist">
<p class="title"><b></b></p>
<dl class="variablelist">
<dt><span class="term">l</span></dt>
<dd><p>
The degree of the last polynomial calculated.
</p></dd>
<dt><span class="term">m</span></dt>
<dd><p>
The order of the Associated Polynomial.
</p></dd>
<dt><span class="term">x</span></dt>
<dd><p>
The abscissa value
</p></dd>
<dt><span class="term">Pl</span></dt>
<dd><p>
The value of the polynomial evaluated at degree <span class="emphasis"><em>l</em></span>.
</p></dd>
<dt><span class="term">Plm1</span></dt>
<dd><p>
The value of the polynomial evaluated at degree <span class="emphasis"><em>l-1</em></span>.
</p></dd>
</dl>
</div>
<h5>
<a name="math_toolkit.sf_poly.legendre.h2"></a>
<span class="phrase"><a name="math_toolkit.sf_poly.legendre.accuracy"></a></span><a class="link" href="legendre.html#math_toolkit.sf_poly.legendre.accuracy">Accuracy</a>
</h5>
<p>
The following table shows peak errors (in units of epsilon) for various domains
of input arguments. Note that only results for the widest floating point
type on the system are given as narrower types have <a class="link" href="../relative_error.html#math_toolkit.relative_error.zero_error">effectively
zero error</a>.
</p>
<div class="table">
<a name="math_toolkit.sf_poly.legendre.table_legendre_p"></a><p class="title"><b>Table&#160;8.32.&#160;Error rates for legendre_p</b></p>
<div class="table-contents"><table class="table" summary="Error rates for legendre_p">
<colgroup>
<col>
<col>
<col>
<col>
<col>
</colgroup>
<thead><tr>
<th>
</th>
<th>
<p>
GNU C++ version 7.1.0<br> linux<br> double
</p>
</th>
<th>
<p>
GNU C++ version 7.1.0<br> linux<br> long double
</p>
</th>
<th>
<p>
Sun compiler version 0x5150<br> Sun Solaris<br> long double
</p>
</th>
<th>
<p>
Microsoft Visual C++ version 14.1<br> Win32<br> double
</p>
</th>
</tr></thead>
<tbody>
<tr>
<td>
<p>
Legendre Polynomials: Small Values
</p>
</td>
<td>
<p>
<span class="blue">Max = 0.732&#949; (Mean = 0.0619&#949;)</span><br>
<br> (<span class="emphasis"><em>GSL 2.1:</em></span> Max = 211&#949; (Mean = 20.4&#949;))
</p>
</td>
<td>
<p>
<span class="blue">Max = 69.2&#949; (Mean = 9.58&#949;)</span><br> <br>
(<span class="emphasis"><em>&lt;cmath&gt;:</em></span> Max = 124&#949; (Mean = 13.2&#949;))
</p>
</td>
<td>
<p>
<span class="blue">Max = 69.2&#949; (Mean = 9.58&#949;)</span>
</p>
</td>
<td>
<p>
<span class="blue">Max = 211&#949; (Mean = 20.4&#949;)</span>
</p>
</td>
</tr>
<tr>
<td>
<p>
Legendre Polynomials: Large Values
</p>
</td>
<td>
<p>
<span class="blue">Max = 0.632&#949; (Mean = 0.0693&#949;)</span><br>
<br> (<span class="emphasis"><em>GSL 2.1:</em></span> Max = 300&#949; (Mean = 33.2&#949;))
</p>
</td>
<td>
<p>
<span class="blue">Max = 699&#949; (Mean = 59.6&#949;)</span><br> <br>
(<span class="emphasis"><em>&lt;cmath&gt;:</em></span> Max = 343&#949; (Mean = 32.1&#949;))
</p>
</td>
<td>
<p>
<span class="blue">Max = 699&#949; (Mean = 59.6&#949;)</span>
</p>
</td>
<td>
<p>
<span class="blue">Max = 300&#949; (Mean = 33.2&#949;)</span>
</p>
</td>
</tr>
</tbody>
</table></div>
</div>
<br class="table-break"><div class="table">
<a name="math_toolkit.sf_poly.legendre.table_legendre_q"></a><p class="title"><b>Table&#160;8.33.&#160;Error rates for legendre_q</b></p>
<div class="table-contents"><table class="table" summary="Error rates for legendre_q">
<colgroup>
<col>
<col>
<col>
<col>
<col>
</colgroup>
<thead><tr>
<th>
</th>
<th>
<p>
GNU C++ version 7.1.0<br> linux<br> double
</p>
</th>
<th>
<p>
GNU C++ version 7.1.0<br> linux<br> long double
</p>
</th>
<th>
<p>
Sun compiler version 0x5150<br> Sun Solaris<br> long double
</p>
</th>
<th>
<p>
Microsoft Visual C++ version 14.1<br> Win32<br> double
</p>
</th>
</tr></thead>
<tbody>
<tr>
<td>
<p>
Legendre Polynomials: Small Values
</p>
</td>
<td>
<p>
<span class="blue">Max = 0.612&#949; (Mean = 0.0517&#949;)</span><br>
<br> (<span class="emphasis"><em>GSL 2.1:</em></span> Max = 46.4&#949; (Mean = 7.46&#949;))
</p>
</td>
<td>
<p>
<span class="blue">Max = 50.9&#949; (Mean = 9&#949;)</span>
</p>
</td>
<td>
<p>
<span class="blue">Max = 50.9&#949; (Mean = 8.98&#949;)</span>
</p>
</td>
<td>
<p>
<span class="blue">Max = 46.4&#949; (Mean = 7.32&#949;)</span>
</p>
</td>
</tr>
<tr>
<td>
<p>
Legendre Polynomials: Large Values
</p>
</td>
<td>
<p>
<span class="blue">Max = 2.49&#949; (Mean = 0.202&#949;)</span><br> <br>
(<span class="emphasis"><em>GSL 2.1:</em></span> Max = 4.6e+03&#949; (Mean = 366&#949;))
</p>
</td>
<td>
<p>
<span class="blue">Max = 5.98e+03&#949; (Mean = 478&#949;)</span>
</p>
</td>
<td>
<p>
<span class="blue">Max = 5.98e+03&#949; (Mean = 478&#949;)</span>
</p>
</td>
<td>
<p>
<span class="blue">Max = 4.6e+03&#949; (Mean = 366&#949;)</span>
</p>
</td>
</tr>
</tbody>
</table></div>
</div>
<br class="table-break"><div class="table">
<a name="math_toolkit.sf_poly.legendre.table_legendre_p_associated_"></a><p class="title"><b>Table&#160;8.34.&#160;Error rates for legendre_p (associated)</b></p>
<div class="table-contents"><table class="table" summary="Error rates for legendre_p (associated)">
<colgroup>
<col>
<col>
<col>
<col>
<col>
</colgroup>
<thead><tr>
<th>
</th>
<th>
<p>
GNU C++ version 7.1.0<br> linux<br> double
</p>
</th>
<th>
<p>
GNU C++ version 7.1.0<br> linux<br> long double
</p>
</th>
<th>
<p>
Sun compiler version 0x5150<br> Sun Solaris<br> long double
</p>
</th>
<th>
<p>
Microsoft Visual C++ version 14.1<br> Win32<br> double
</p>
</th>
</tr></thead>
<tbody><tr>
<td>
<p>
Associated Legendre Polynomials: Small Values
</p>
</td>
<td>
<p>
<span class="blue">Max = 0.999&#949; (Mean = 0.05&#949;)</span><br> <br>
(<span class="emphasis"><em>GSL 2.1:</em></span> Max = 121&#949; (Mean = 6.75&#949;) <a class="link" href="../logs_and_tables/logs.html#errors_GNU_C_version_7_1_0_linux_double_legendre_p_associated__GSL_2_1_Associated_Legendre_Polynomials_Small_Values">And
other failures.</a>)
</p>
</td>
<td>
<p>
<span class="blue">Max = 175&#949; (Mean = 9.88&#949;)</span><br> <br>
(<span class="emphasis"><em>&lt;cmath&gt;:</em></span> Max = 175&#949; (Mean = 9.36&#949;) <a class="link" href="../logs_and_tables/logs.html#errors_GNU_C_version_7_1_0_linux_long_double_legendre_p_associated___cmath__Associated_Legendre_Polynomials_Small_Values">And
other failures.</a>)
</p>
</td>
<td>
<p>
<span class="blue">Max = 77.7&#949; (Mean = 5.59&#949;)</span>
</p>
</td>
<td>
<p>
<span class="blue">Max = 121&#949; (Mean = 7.14&#949;)</span>
</p>
</td>
</tr></tbody>
</table></div>
</div>
<br class="table-break"><p>
Note that the worst errors occur when the order increases, values greater
than ~120 are very unlikely to produce sensible results, especially in the
associated polynomial case when the degree is also large. Further the relative
errors are likely to grow arbitrarily large when the function is very close
to a root.
</p>
<h5>
<a name="math_toolkit.sf_poly.legendre.h3"></a>
<span class="phrase"><a name="math_toolkit.sf_poly.legendre.testing"></a></span><a class="link" href="legendre.html#math_toolkit.sf_poly.legendre.testing">Testing</a>
</h5>
<p>
A mixture of spot tests of values calculated using functions.wolfram.com,
and randomly generated test data are used: the test data was computed using
<a href="http://shoup.net/ntl/doc/RR.txt" target="_top">NTL::RR</a> at 1000-bit
precision.
</p>
<h5>
<a name="math_toolkit.sf_poly.legendre.h4"></a>
<span class="phrase"><a name="math_toolkit.sf_poly.legendre.implementation"></a></span><a class="link" href="legendre.html#math_toolkit.sf_poly.legendre.implementation">Implementation</a>
</h5>
<p>
These functions are implemented using the stable three term recurrence relations.
These relations guarantee low absolute error but cannot guarantee low relative
error near one of the roots of the polynomials.
</p>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright &#169; 2006-2019 Nikhar
Agrawal, Anton Bikineev, Paul A. Bristow, Marco Guazzone, Christopher Kormanyos,
Hubert Holin, Bruno Lalande, John Maddock, Jeremy Murphy, Matthew Pulver, Johan
R&#229;de, Gautam Sewani, Benjamin Sobotta, Nicholas Thompson, Thijs van den Berg,
Daryle Walker and Xiaogang Zhang<p>
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
</p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../sf_poly.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../sf_poly.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="legendre_stieltjes.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>