3 <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
4 <title>Spherical Harmonics</title>
5 <link rel="stylesheet" href="../../math.css" type="text/css">
6 <meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
7 <link rel="home" href="../../index.html" title="Math Toolkit 2.11.0">
8 <link rel="up" href="../sf_poly.html" title="Polynomials">
9 <link rel="prev" href="chebyshev.html" title="Chebyshev Polynomials">
10 <link rel="next" href="cardinal_b_splines.html" title="Cardinal B-splines">
12 <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
13 <table cellpadding="2" width="100%"><tr>
14 <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td>
15 <td align="center"><a href="../../../../../../index.html">Home</a></td>
16 <td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td>
17 <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
18 <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
19 <td align="center"><a href="../../../../../../more/index.htm">More</a></td>
22 <div class="spirit-nav">
23 <a accesskey="p" href="chebyshev.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="cardinal_b_splines.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
26 <div class="titlepage"><div><div><h3 class="title">
27 <a name="math_toolkit.sf_poly.sph_harm"></a><a class="link" href="sph_harm.html" title="Spherical Harmonics">Spherical Harmonics</a>
28 </h3></div></div></div>
30 <a name="math_toolkit.sf_poly.sph_harm.h0"></a>
31 <span class="phrase"><a name="math_toolkit.sf_poly.sph_harm.synopsis"></a></span><a class="link" href="sph_harm.html#math_toolkit.sf_poly.sph_harm.synopsis">Synopsis</a>
33 <pre class="programlisting"><span class="preprocessor">#include</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">special_functions</span><span class="special">/</span><span class="identifier">spherical_harmonic</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
35 <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>
37 <span class="keyword">template</span> <span class="special"><</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>
38 <span class="identifier">std</span><span class="special">::</span><span class="identifier">complex</span><span class="special"><</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="special">></span> <span class="identifier">spherical_harmonic</span><span class="special">(</span><span class="keyword">unsigned</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">T1</span> <span class="identifier">theta</span><span class="special">,</span> <span class="identifier">T2</span> <span class="identifier">phi</span><span class="special">);</span>
40 <span class="keyword">template</span> <span class="special"><</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> <a class="link" href="../../policy.html" title="Chapter 20. Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">></span>
41 <span class="identifier">std</span><span class="special">::</span><span class="identifier">complex</span><span class="special"><</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="special">></span> <span class="identifier">spherical_harmonic</span><span class="special">(</span><span class="keyword">unsigned</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">T1</span> <span class="identifier">theta</span><span class="special">,</span> <span class="identifier">T2</span> <span class="identifier">phi</span><span class="special">,</span> <span class="keyword">const</span> <a class="link" href="../../policy.html" title="Chapter 20. Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">&);</span>
43 <span class="keyword">template</span> <span class="special"><</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>
44 <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">spherical_harmonic_r</span><span class="special">(</span><span class="keyword">unsigned</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">T1</span> <span class="identifier">theta</span><span class="special">,</span> <span class="identifier">T2</span> <span class="identifier">phi</span><span class="special">);</span>
46 <span class="keyword">template</span> <span class="special"><</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> <a class="link" href="../../policy.html" title="Chapter 20. Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">></span>
47 <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">spherical_harmonic_r</span><span class="special">(</span><span class="keyword">unsigned</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">T1</span> <span class="identifier">theta</span><span class="special">,</span> <span class="identifier">T2</span> <span class="identifier">phi</span><span class="special">,</span> <span class="keyword">const</span> <a class="link" href="../../policy.html" title="Chapter 20. Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">&);</span>
49 <span class="keyword">template</span> <span class="special"><</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>
50 <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">spherical_harmonic_i</span><span class="special">(</span><span class="keyword">unsigned</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">T1</span> <span class="identifier">theta</span><span class="special">,</span> <span class="identifier">T2</span> <span class="identifier">phi</span><span class="special">);</span>
52 <span class="keyword">template</span> <span class="special"><</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> <a class="link" href="../../policy.html" title="Chapter 20. Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">></span>
53 <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">spherical_harmonic_i</span><span class="special">(</span><span class="keyword">unsigned</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">T1</span> <span class="identifier">theta</span><span class="special">,</span> <span class="identifier">T2</span> <span class="identifier">phi</span><span class="special">,</span> <span class="keyword">const</span> <a class="link" href="../../policy.html" title="Chapter 20. Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">&);</span>
55 <span class="special">}}</span> <span class="comment">// namespaces</span>
58 <a name="math_toolkit.sf_poly.sph_harm.h1"></a>
59 <span class="phrase"><a name="math_toolkit.sf_poly.sph_harm.description"></a></span><a class="link" href="sph_harm.html#math_toolkit.sf_poly.sph_harm.description">Description</a>
62 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
63 type calculation rules</em></span></a> when T1 and T2 are different types.
66 The final <a class="link" href="../../policy.html" title="Chapter 20. Policies: Controlling Precision, Error Handling etc">Policy</a> argument is optional and can
67 be used to control the behaviour of the function: how it handles errors,
68 what level of precision to use etc. Refer to the <a class="link" href="../../policy.html" title="Chapter 20. Policies: Controlling Precision, Error Handling etc">policy
69 documentation for more details</a>.
71 <pre class="programlisting"><span class="keyword">template</span> <span class="special"><</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>
72 <span class="identifier">std</span><span class="special">::</span><span class="identifier">complex</span><span class="special"><</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="special">></span> <span class="identifier">spherical_harmonic</span><span class="special">(</span><span class="keyword">unsigned</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">T1</span> <span class="identifier">theta</span><span class="special">,</span> <span class="identifier">T2</span> <span class="identifier">phi</span><span class="special">);</span>
74 <span class="keyword">template</span> <span class="special"><</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> <a class="link" href="../../policy.html" title="Chapter 20. Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">></span>
75 <span class="identifier">std</span><span class="special">::</span><span class="identifier">complex</span><span class="special"><</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="special">></span> <span class="identifier">spherical_harmonic</span><span class="special">(</span><span class="keyword">unsigned</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">T1</span> <span class="identifier">theta</span><span class="special">,</span> <span class="identifier">T2</span> <span class="identifier">phi</span><span class="special">,</span> <span class="keyword">const</span> <a class="link" href="../../policy.html" title="Chapter 20. Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">&);</span>
78 Returns the value of the Spherical Harmonic Y<sub>n</sub><sup>m</sup>(theta, phi):
80 <div class="blockquote"><blockquote class="blockquote"><p>
81 <span class="inlinemediaobject"><img src="../../../equations/spherical_0.svg"></span>
83 </p></blockquote></div>
85 The spherical harmonics Y<sub>n</sub><sup>m</sup>(theta, phi) are the angular portion of the solution
86 to Laplace's equation in spherical coordinates where azimuthal symmetry is
89 <div class="caution"><table border="0" summary="Caution">
91 <td rowspan="2" align="center" valign="top" width="25"><img alt="[Caution]" src="../../../../../../doc/src/images/caution.png"></td>
92 <th align="left">Caution</th>
94 <tr><td align="left" valign="top">
96 Care must be taken in correctly identifying the arguments to this function:
97 θ is taken as the polar (colatitudinal) coordinate with θ in [0, π], and φas
98 the azimuthal (longitudinal) coordinate with φin [0,2π). This is the convention
99 used in Physics, and matches the definition used by <a href="http://documents.wolfram.com/mathematica/functions/SphericalHarmonicY" target="_top">Mathematica
100 in the function SpericalHarmonicY</a>, but is opposite to the usual
101 mathematical conventions.
104 Some other sources include an additional Condon-Shortley phase term of
105 (-1)<sup>m</sup> in the definition of this function: note however that our definition
106 of the associated Legendre polynomial already includes this term.
109 This implementation returns zero for m > n
112 For θ outside [0, π] and φ outside [0, 2π] this implementation follows the convention
113 used by Mathematica: the function is periodic with period π in θ and 2π in φ.
114 Please note that this is not the behaviour one would get from a casual
115 application of the function's definition. Cautious users should keep θ and
116 φ to the range [0, π] and [0, 2π] respectively.
119 See: <a href="http://mathworld.wolfram.com/SphericalHarmonic.html" target="_top">Weisstein,
120 Eric W. "Spherical Harmonic." From MathWorld--A Wolfram Web Resource</a>.
124 <pre class="programlisting"><span class="keyword">template</span> <span class="special"><</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>
125 <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">spherical_harmonic_r</span><span class="special">(</span><span class="keyword">unsigned</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">T1</span> <span class="identifier">theta</span><span class="special">,</span> <span class="identifier">T2</span> <span class="identifier">phi</span><span class="special">);</span>
127 <span class="keyword">template</span> <span class="special"><</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> <a class="link" href="../../policy.html" title="Chapter 20. Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">></span>
128 <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">spherical_harmonic_r</span><span class="special">(</span><span class="keyword">unsigned</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">T1</span> <span class="identifier">theta</span><span class="special">,</span> <span class="identifier">T2</span> <span class="identifier">phi</span><span class="special">,</span> <span class="keyword">const</span> <a class="link" href="../../policy.html" title="Chapter 20. Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">&);</span>
131 Returns the real part of Y<sub>n</sub><sup>m</sup>(theta, phi):
133 <div class="blockquote"><blockquote class="blockquote"><p>
134 <span class="inlinemediaobject"><img src="../../../equations/spherical_1.svg"></span>
136 </p></blockquote></div>
137 <pre class="programlisting"><span class="keyword">template</span> <span class="special"><</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>
138 <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">spherical_harmonic_i</span><span class="special">(</span><span class="keyword">unsigned</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">T1</span> <span class="identifier">theta</span><span class="special">,</span> <span class="identifier">T2</span> <span class="identifier">phi</span><span class="special">);</span>
140 <span class="keyword">template</span> <span class="special"><</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> <a class="link" href="../../policy.html" title="Chapter 20. Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">></span>
141 <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">spherical_harmonic_i</span><span class="special">(</span><span class="keyword">unsigned</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">T1</span> <span class="identifier">theta</span><span class="special">,</span> <span class="identifier">T2</span> <span class="identifier">phi</span><span class="special">,</span> <span class="keyword">const</span> <a class="link" href="../../policy.html" title="Chapter 20. Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">&);</span>
144 Returns the imaginary part of Y<sub>n</sub><sup>m</sup>(theta, phi):
146 <div class="blockquote"><blockquote class="blockquote"><p>
147 <span class="inlinemediaobject"><img src="../../../equations/spherical_2.svg"></span>
149 </p></blockquote></div>
151 <a name="math_toolkit.sf_poly.sph_harm.h2"></a>
152 <span class="phrase"><a name="math_toolkit.sf_poly.sph_harm.accuracy"></a></span><a class="link" href="sph_harm.html#math_toolkit.sf_poly.sph_harm.accuracy">Accuracy</a>
155 The following table shows peak errors for various domains of input arguments.
156 Note that only results for the widest floating point type on the system are
157 given as narrower types have <a class="link" href="../relative_error.html#math_toolkit.relative_error.zero_error">effectively
158 zero error</a>. Peak errors are the same for both the real and imaginary
159 parts, as the error is dominated by calculation of the associated Legendre
160 polynomials: especially near the roots of the associated Legendre function.
163 All values are in units of epsilon.
166 <a name="math_toolkit.sf_poly.sph_harm.table_spherical_harmonic_r"></a><p class="title"><b>Table 8.38. Error rates for spherical_harmonic_r</b></p>
167 <div class="table-contents"><table class="table" summary="Error rates for spherical_harmonic_r">
180 GNU C++ version 7.1.0<br> linux<br> double
185 GNU C++ version 7.1.0<br> linux<br> long double
190 Sun compiler version 0x5150<br> Sun Solaris<br> long double
195 Microsoft Visual C++ version 14.1<br> Win32<br> double
207 <span class="blue">Max = 1.58ε (Mean = 0.0707ε)</span>
212 <span class="blue">Max = 2.89e+03ε (Mean = 108ε)</span>
217 <span class="blue">Max = 1.03e+04ε (Mean = 327ε)</span>
222 <span class="blue">Max = 2.27e+04ε (Mean = 725ε)</span>
228 <br class="table-break"><div class="table">
229 <a name="math_toolkit.sf_poly.sph_harm.table_spherical_harmonic_i"></a><p class="title"><b>Table 8.39. Error rates for spherical_harmonic_i</b></p>
230 <div class="table-contents"><table class="table" summary="Error rates for spherical_harmonic_i">
243 GNU C++ version 7.1.0<br> linux<br> double
248 GNU C++ version 7.1.0<br> linux<br> long double
253 Sun compiler version 0x5150<br> Sun Solaris<br> long double
258 Microsoft Visual C++ version 14.1<br> Win32<br> double
270 <span class="blue">Max = 1.36ε (Mean = 0.0765ε)</span>
275 <span class="blue">Max = 2.89e+03ε (Mean = 108ε)</span>
280 <span class="blue">Max = 1.03e+04ε (Mean = 327ε)</span>
285 <span class="blue">Max = 2.27e+04ε (Mean = 725ε)</span>
291 <br class="table-break"><p>
292 Note that the worst errors occur when the degree increases, values greater
293 than ~120 are very unlikely to produce sensible results, especially when
294 the order is also large. Further the relative errors are likely to grow arbitrarily
295 large when the function is very close to a root.
298 <a name="math_toolkit.sf_poly.sph_harm.h3"></a>
299 <span class="phrase"><a name="math_toolkit.sf_poly.sph_harm.testing"></a></span><a class="link" href="sph_harm.html#math_toolkit.sf_poly.sph_harm.testing">Testing</a>
302 A mixture of spot tests of values calculated using functions.wolfram.com,
303 and randomly generated test data are used: the test data was computed using
304 <a href="http://shoup.net/ntl/doc/RR.txt" target="_top">NTL::RR</a> at 1000-bit
308 <a name="math_toolkit.sf_poly.sph_harm.h4"></a>
309 <span class="phrase"><a name="math_toolkit.sf_poly.sph_harm.implementation"></a></span><a class="link" href="sph_harm.html#math_toolkit.sf_poly.sph_harm.implementation">Implementation</a>
312 These functions are implemented fairly naively using the formulae given above.
313 Some extra care is taken to prevent roundoff error when converting from polar
314 coordinates (so for example the <span class="emphasis"><em>1-x<sup>2</sup></em></span> term used by the
315 associated Legendre functions is calculated without roundoff error using
316 <span class="emphasis"><em>x = cos(theta)</em></span>, and <span class="emphasis"><em>1-x<sup>2</sup> = sin<sup>2</sup>(theta)</em></span>).
317 The limiting factor in the error rates for these functions is the need to
318 calculate values near the roots of the associated Legendre functions.
321 <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
322 <td align="left"></td>
323 <td align="right"><div class="copyright-footer">Copyright © 2006-2019 Nikhar
324 Agrawal, Anton Bikineev, Paul A. Bristow, Marco Guazzone, Christopher Kormanyos,
325 Hubert Holin, Bruno Lalande, John Maddock, Jeremy Murphy, Matthew Pulver, Johan
326 Råde, Gautam Sewani, Benjamin Sobotta, Nicholas Thompson, Thijs van den Berg,
327 Daryle Walker and Xiaogang Zhang<p>
328 Distributed under the Boost Software License, Version 1.0. (See accompanying
329 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>)
334 <div class="spirit-nav">
335 <a accesskey="p" href="chebyshev.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="cardinal_b_splines.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>