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

217 lines
25 KiB
HTML

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Gauss-Legendre quadrature</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="../quadrature.html" title="Chapter&#160;13.&#160;Quadrature and Differentiation">
<link rel="prev" href="trapezoidal.html" title="Trapezoidal Quadrature">
<link rel="next" href="gauss_kronrod.html" title="Gauss-Kronrod Quadrature">
</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="trapezoidal.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../quadrature.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="gauss_kronrod.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="math_toolkit.gauss"></a><a class="link" href="gauss.html" title="Gauss-Legendre quadrature">Gauss-Legendre quadrature</a>
</h2></div></div></div>
<h4>
<a name="math_toolkit.gauss.h0"></a>
<span class="phrase"><a name="math_toolkit.gauss.synopsis"></a></span><a class="link" href="gauss.html#math_toolkit.gauss.synopsis">Synopsis</a>
</h4>
<p>
<code class="computeroutput"><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">quadrature</span><span class="special">/</span><span class="identifier">gauss</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span></code>
</p>
<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">namespace</span> <span class="identifier">quadrature</span><span class="special">{</span>
<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">Real</span><span class="special">,</span> <span class="keyword">unsigned</span> <span class="identifier">Points</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">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">policies</span><span class="special">::</span><span class="identifier">policy</span><span class="special">&lt;&gt;</span> <span class="special">&gt;</span>
<span class="keyword">struct</span> <span class="identifier">gauss</span>
<span class="special">{</span>
<span class="keyword">static</span> <span class="keyword">const</span> <span class="identifier">RandomAccessContainer</span><span class="special">&amp;</span> <span class="identifier">abscissa</span><span class="special">();</span>
<span class="keyword">static</span> <span class="keyword">const</span> <span class="identifier">RandomAccessContainer</span><span class="special">&amp;</span> <span class="identifier">weights</span><span class="special">();</span>
<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">F</span><span class="special">&gt;</span>
<span class="keyword">static</span> <span class="keyword">auto</span> <span class="identifier">integrate</span><span class="special">(</span><span class="identifier">F</span> <span class="identifier">f</span><span class="special">,</span> <span class="identifier">Real</span><span class="special">*</span> <span class="identifier">pL1</span> <span class="special">=</span> <span class="keyword">nullptr</span><span class="special">)-&gt;</span><span class="keyword">decltype</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">declval</span><span class="special">&lt;</span><span class="identifier">F</span><span class="special">&gt;()(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">declval</span><span class="special">&lt;</span><span class="identifier">Real</span><span class="special">&gt;()))</span>
<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">F</span><span class="special">&gt;</span>
<span class="keyword">static</span> <span class="keyword">auto</span> <span class="identifier">integrate</span><span class="special">(</span><span class="identifier">F</span> <span class="identifier">f</span><span class="special">,</span> <span class="identifier">Real</span> <span class="identifier">a</span><span class="special">,</span> <span class="identifier">Real</span> <span class="identifier">b</span><span class="special">,</span> <span class="identifier">Real</span><span class="special">*</span> <span class="identifier">pL1</span> <span class="special">=</span> <span class="keyword">nullptr</span><span class="special">)-&gt;</span><span class="keyword">decltype</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">declval</span><span class="special">&lt;</span><span class="identifier">F</span><span class="special">&gt;()(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">declval</span><span class="special">&lt;</span><span class="identifier">Real</span><span class="special">&gt;()))</span>
<span class="special">};</span>
<span class="special">}}}</span> <span class="comment">// namespaces</span>
</pre>
<h4>
<a name="math_toolkit.gauss.h1"></a>
<span class="phrase"><a name="math_toolkit.gauss.description"></a></span><a class="link" href="gauss.html#math_toolkit.gauss.description">description</a>
</h4>
<p>
The <code class="computeroutput"><span class="identifier">gauss</span></code> class template performs
"one shot" non-adaptive Gauss-Legendre integration on some arbitrary
function <span class="emphasis"><em>f</em></span> using the number of evaluation points as specified
by <span class="emphasis"><em>Points</em></span>.
</p>
<p>
This is intentionally a very simple quadrature routine, it obtains no estimate
of the error, and is not adaptive, but is very efficient in simple cases that
involve integrating smooth "bell like" functions and functions with
rapidly convergent power series.
</p>
<pre class="programlisting"><span class="keyword">static</span> <span class="keyword">const</span> <span class="identifier">RandomAccessContainer</span><span class="special">&amp;</span> <span class="identifier">abscissa</span><span class="special">();</span>
<span class="keyword">static</span> <span class="keyword">const</span> <span class="identifier">RandomAccessContainer</span><span class="special">&amp;</span> <span class="identifier">weights</span><span class="special">();</span>
</pre>
<p>
These functions provide direct access to the abscissa and weights used to perform
the quadrature: the return type depends on the <span class="emphasis"><em>Points</em></span>
template parameter, but is always a RandomAccessContainer type. Note that only
positive (or zero) abscissa and weights are stored.
</p>
<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">F</span><span class="special">&gt;</span>
<span class="keyword">static</span> <span class="keyword">auto</span> <span class="identifier">integrate</span><span class="special">(</span><span class="identifier">F</span> <span class="identifier">f</span><span class="special">,</span> <span class="identifier">Real</span><span class="special">*</span> <span class="identifier">pL1</span> <span class="special">=</span> <span class="keyword">nullptr</span><span class="special">)-&gt;</span><span class="keyword">decltype</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">declval</span><span class="special">&lt;</span><span class="identifier">F</span><span class="special">&gt;()(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">declval</span><span class="special">&lt;</span><span class="identifier">Real</span><span class="special">&gt;()))</span>
</pre>
<p>
Integrates <span class="emphasis"><em>f</em></span> over (-1,1), and optionally sets <code class="computeroutput"><span class="special">*</span><span class="identifier">pL1</span></code> to the
L1 norm of the returned value: if this is substantially larger than the return
value, then the sum was ill-conditioned. Note however, that no error estimate
is available.
</p>
<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">F</span><span class="special">&gt;</span>
<span class="keyword">static</span> <span class="keyword">auto</span> <span class="identifier">integrate</span><span class="special">(</span><span class="identifier">F</span> <span class="identifier">f</span><span class="special">,</span> <span class="identifier">Real</span> <span class="identifier">a</span><span class="special">,</span> <span class="identifier">Real</span> <span class="identifier">b</span><span class="special">,</span> <span class="identifier">Real</span><span class="special">*</span> <span class="identifier">pL1</span> <span class="special">=</span> <span class="keyword">nullptr</span><span class="special">)-&gt;</span><span class="keyword">decltype</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">declval</span><span class="special">&lt;</span><span class="identifier">F</span><span class="special">&gt;()(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">declval</span><span class="special">&lt;</span><span class="identifier">Real</span><span class="special">&gt;()))</span>
</pre>
<p>
Integrates <span class="emphasis"><em>f</em></span> over (a,b), and optionally sets <code class="computeroutput"><span class="special">*</span><span class="identifier">pL1</span></code> to the
L1 norm of the returned value: if this is substantially larger than the return
value, then the sum was ill-conditioned. Note however, that no error estimate
is available. This function supports both finite and infinite <span class="emphasis"><em>a</em></span>
and <span class="emphasis"><em>b</em></span>, as long as <code class="computeroutput"><span class="identifier">a</span>
<span class="special">&lt;</span> <span class="identifier">b</span></code>.
</p>
<p>
The Gaussian quadrature routine support both real and complex-valued quadrature.
For example, the Lambert-W function admits the integral representation
</p>
<div class="blockquote"><blockquote class="blockquote"><p>
<span class="serif_italic"><span class="emphasis"><em>W(z) = 1/2&#928; &#8747;<sub>-&#928;</sub><sup>&#928;</sup> ((1-
v cot(v) )^2 + v^2)/(z + v csc(v) exp(-v cot(v))) dv</em></span></span>
</p></blockquote></div>
<p>
so it can be effectively computed via Gaussian quadrature using the following
code:
</p>
<pre class="programlisting"><span class="identifier">Complex</span> <span class="identifier">z</span><span class="special">{</span><span class="number">2</span><span class="special">,</span> <span class="number">3</span><span class="special">};</span>
<span class="keyword">auto</span> <span class="identifier">lw</span> <span class="special">=</span> <span class="special">[&amp;</span><span class="identifier">z</span><span class="special">](</span><span class="identifier">Real</span> <span class="identifier">v</span><span class="special">)-&gt;</span><span class="identifier">Complex</span> <span class="special">{</span>
<span class="keyword">using</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">cos</span><span class="special">;</span>
<span class="keyword">using</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">sin</span><span class="special">;</span>
<span class="keyword">using</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">exp</span><span class="special">;</span>
<span class="identifier">Real</span> <span class="identifier">sinv</span> <span class="special">=</span> <span class="identifier">sin</span><span class="special">(</span><span class="identifier">v</span><span class="special">);</span>
<span class="identifier">Real</span> <span class="identifier">cosv</span> <span class="special">=</span> <span class="identifier">cos</span><span class="special">(</span><span class="identifier">v</span><span class="special">);</span>
<span class="identifier">Real</span> <span class="identifier">cotv</span> <span class="special">=</span> <span class="identifier">cosv</span><span class="special">/</span><span class="identifier">sinv</span><span class="special">;</span>
<span class="identifier">Real</span> <span class="identifier">cscv</span> <span class="special">=</span> <span class="number">1</span><span class="special">/</span><span class="identifier">sinv</span><span class="special">;</span>
<span class="identifier">Real</span> <span class="identifier">t</span> <span class="special">=</span> <span class="special">(</span><span class="number">1</span><span class="special">-</span><span class="identifier">v</span><span class="special">*</span><span class="identifier">cotv</span><span class="special">)*(</span><span class="number">1</span><span class="special">-</span><span class="identifier">v</span><span class="special">*</span><span class="identifier">cotv</span><span class="special">)</span> <span class="special">+</span> <span class="identifier">v</span><span class="special">*</span><span class="identifier">v</span><span class="special">;</span>
<span class="identifier">Real</span> <span class="identifier">x</span> <span class="special">=</span> <span class="identifier">v</span><span class="special">*</span><span class="identifier">cscv</span><span class="special">*</span><span class="identifier">exp</span><span class="special">(-</span><span class="identifier">v</span><span class="special">*</span><span class="identifier">cotv</span><span class="special">);</span>
<span class="identifier">Complex</span> <span class="identifier">den</span> <span class="special">=</span> <span class="identifier">z</span> <span class="special">+</span> <span class="identifier">x</span><span class="special">;</span>
<span class="identifier">Complex</span> <span class="identifier">num</span> <span class="special">=</span> <span class="identifier">t</span><span class="special">*(</span><span class="identifier">z</span><span class="special">/</span><span class="identifier">pi</span><span class="special">&lt;</span><span class="identifier">Real</span><span class="special">&gt;());</span>
<span class="identifier">Complex</span> <span class="identifier">res</span> <span class="special">=</span> <span class="identifier">num</span><span class="special">/</span><span class="identifier">den</span><span class="special">;</span>
<span class="keyword">return</span> <span class="identifier">res</span><span class="special">;</span>
<span class="special">};</span>
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">quadrature</span><span class="special">::</span><span class="identifier">gauss</span><span class="special">&lt;</span><span class="identifier">Real</span><span class="special">,</span> <span class="number">30</span><span class="special">&gt;</span> <span class="identifier">integrator</span><span class="special">;</span>
<span class="identifier">Complex</span> <span class="identifier">W</span> <span class="special">=</span> <span class="identifier">integrator</span><span class="special">.</span><span class="identifier">integrate</span><span class="special">(</span><span class="identifier">lw</span><span class="special">,</span> <span class="special">(</span><span class="identifier">Real</span><span class="special">)</span> <span class="number">0</span><span class="special">,</span> <span class="identifier">pi</span><span class="special">&lt;</span><span class="identifier">Real</span><span class="special">&gt;());</span>
</pre>
<h4>
<a name="math_toolkit.gauss.h2"></a>
<span class="phrase"><a name="math_toolkit.gauss.choosing_the_number_of_points"></a></span><a class="link" href="gauss.html#math_toolkit.gauss.choosing_the_number_of_points">Choosing
the number of points</a>
</h4>
<p>
Internally class <code class="computeroutput"><span class="identifier">gauss</span></code> has
pre-computed tables of abscissa and weights for 7, 15, 20, 25 and 30 points
at up to 100-decimal digit precision. That means that using for example, <code class="computeroutput"><span class="identifier">gauss</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">,</span> <span class="number">30</span><span class="special">&gt;::</span><span class="identifier">integrate</span></code>
incurs absolutely zero set-up overhead from computing the abscissa/weight pairs.
When using multiprecision types with less than 100 digits of precision, then
there is a small initial one time cost, while the abscissa/weight pairs are
constructed from strings.
</p>
<p>
However, for types with higher precision, or numbers of points other than those
given above, the abscissa/weight pairs are computed when first needed and then
cached for future use, which does incur a noticeable overhead. If this is likely
to be an issue, then
</p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
Defining BOOST_MATH_GAUSS_NO_COMPUTE_ON_DEMAND will result in a compile-time
error, whenever a combination of number type and number of points is used
which does not have pre-computed values.
</li>
<li class="listitem">
There is a program <a href="../../../tools/gauss_kronrod_constants.cpp" target="_top">gauss_kronrod_constants.cpp</a>
which was used to provide the pre-computed values already in gauss.hpp.
The program can be trivially modified to generate code and constants for
other precisions and numbers of points.
</li>
</ul></div>
<h4>
<a name="math_toolkit.gauss.h3"></a>
<span class="phrase"><a name="math_toolkit.gauss.examples"></a></span><a class="link" href="gauss.html#math_toolkit.gauss.examples">Examples</a>
</h4>
<p>
We'll begin by integrating t<sup>2</sup> atan(t) over (0,1) using a 7 term Gauss-Legendre
rule, and begin by defining the function to integrate as a C++ lambda expression:
</p>
<pre class="programlisting"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">quadrature</span><span class="special">;</span>
<span class="keyword">auto</span> <span class="identifier">f</span> <span class="special">=</span> <span class="special">[](</span><span class="keyword">const</span> <span class="keyword">double</span><span class="special">&amp;</span> <span class="identifier">t</span><span class="special">)</span> <span class="special">{</span> <span class="keyword">return</span> <span class="identifier">t</span> <span class="special">*</span> <span class="identifier">t</span> <span class="special">*</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">atan</span><span class="special">(</span><span class="identifier">t</span><span class="special">);</span> <span class="special">};</span>
</pre>
<p>
Integration is simply a matter of calling the <code class="computeroutput"><span class="identifier">gauss</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">,</span>
<span class="number">7</span><span class="special">&gt;::</span><span class="identifier">integrate</span></code> method:
</p>
<pre class="programlisting"><span class="keyword">double</span> <span class="identifier">Q</span> <span class="special">=</span> <span class="identifier">gauss</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">,</span> <span class="number">7</span><span class="special">&gt;::</span><span class="identifier">integrate</span><span class="special">(</span><span class="identifier">f</span><span class="special">,</span> <span class="number">0</span><span class="special">,</span> <span class="number">1</span><span class="special">);</span>
</pre>
<p>
Which yields a value 0.2106572512 accurate to 1e-10.
</p>
<p>
For more accurate evaluations, we'll move to a multiprecision type and use
a 20-point integration scheme:
</p>
<pre class="programlisting"><span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">multiprecision</span><span class="special">::</span><span class="identifier">cpp_bin_float_quad</span><span class="special">;</span>
<span class="keyword">auto</span> <span class="identifier">f2</span> <span class="special">=</span> <span class="special">[](</span><span class="keyword">const</span> <span class="identifier">cpp_bin_float_quad</span><span class="special">&amp;</span> <span class="identifier">t</span><span class="special">)</span> <span class="special">{</span> <span class="keyword">return</span> <span class="identifier">t</span> <span class="special">*</span> <span class="identifier">t</span> <span class="special">*</span> <span class="identifier">atan</span><span class="special">(</span><span class="identifier">t</span><span class="special">);</span> <span class="special">};</span>
<span class="identifier">cpp_bin_float_quad</span> <span class="identifier">Q2</span> <span class="special">=</span> <span class="identifier">gauss</span><span class="special">&lt;</span><span class="identifier">cpp_bin_float_quad</span><span class="special">,</span> <span class="number">20</span><span class="special">&gt;::</span><span class="identifier">integrate</span><span class="special">(</span><span class="identifier">f2</span><span class="special">,</span> <span class="number">0</span><span class="special">,</span> <span class="number">1</span><span class="special">);</span>
</pre>
<p>
Which yields 0.2106572512258069881080923020669, which is accurate to 5e-28.
</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="trapezoidal.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../quadrature.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="gauss_kronrod.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>