GeographicLib  1.37
SphericalHarmonic.hpp
Go to the documentation of this file.
1 /**
2  * \file SphericalHarmonic.hpp
3  * \brief Header for GeographicLib::SphericalHarmonic class
4  *
5  * Copyright (c) Charles Karney (2011) <charles@karney.com> and licensed under
6  * the MIT/X11 License. For more information, see
7  * http://geographiclib.sourceforge.net/
8  **********************************************************************/
9 
10 #if !defined(GEOGRAPHICLIB_SPHERICALHARMONIC_HPP)
11 #define GEOGRAPHICLIB_SPHERICALHARMONIC_HPP 1
12 
13 #include <vector>
18 
19 namespace GeographicLib {
20 
21  /**
22  * \brief Spherical harmonic series
23  *
24  * This class evaluates the spherical harmonic sum \verbatim
25  V(x, y, z) = sum(n = 0..N)[ q^(n+1) * sum(m = 0..n)[
26  (C[n,m] * cos(m*lambda) + S[n,m] * sin(m*lambda)) *
27  P[n,m](cos(theta)) ] ]
28  \endverbatim
29  * where
30  * - <i>p</i><sup>2</sup> = <i>x</i><sup>2</sup> + <i>y</i><sup>2</sup>,
31  * - <i>r</i><sup>2</sup> = <i>p</i><sup>2</sup> + <i>z</i><sup>2</sup>,
32  * - \e q = <i>a</i>/<i>r</i>,
33  * - &theta; = atan2(\e p, \e z) = the spherical \e colatitude,
34  * - &lambda; = atan2(\e y, \e x) = the longitude.
35  * - P<sub><i>nm</i></sub>(\e t) is the associated Legendre polynomial of
36  * degree \e n and order \e m.
37  *
38  * Two normalizations are supported for P<sub><i>nm</i></sub>
39  * - fully normalized denoted by SphericalHarmonic::FULL.
40  * - Schmidt semi-normalized denoted by SphericalHarmonic::SCHMIDT.
41  *
42  * Clenshaw summation is used for the sums over both \e n and \e m. This
43  * allows the computation to be carried out without the need for any
44  * temporary arrays. See SphericalEngine.cpp for more information on the
45  * implementation.
46  *
47  * References:
48  * - C. W. Clenshaw, A note on the summation of Chebyshev series,
49  * %Math. Tables Aids Comput. 9(51), 118--120 (1955).
50  * - R. E. Deakin, Derivatives of the earth's potentials, Geomatics
51  * Research Australasia 68, 31--60, (June 1998).
52  * - W. A. Heiskanen and H. Moritz, Physical Geodesy, (Freeman, San
53  * Francisco, 1967). (See Sec. 1-14, for a definition of Pbar.)
54  * - S. A. Holmes and W. E. Featherstone, A unified approach to the Clenshaw
55  * summation and the recursive computation of very high degree and order
56  * normalised associated Legendre functions, J. Geodesy 76(5),
57  * 279--299 (2002).
58  * - C. C. Tscherning and K. Poder, Some geodetic applications of Clenshaw
59  * summation, Boll. Geod. Sci. Aff. 41(4), 349--375 (1982).
60  *
61  * Example of use:
62  * \include example-SphericalHarmonic.cpp
63  **********************************************************************/
64 
66  public:
67  /**
68  * Supported normalizations for the associated Legendre polynomials.
69  **********************************************************************/
71  /**
72  * Fully normalized associated Legendre polynomials.
73  *
74  * These are defined by
75  * <i>P</i><sub><i>nm</i></sub><sup>full</sup>(\e z)
76  * = (&minus;1)<sup><i>m</i></sup>
77  * sqrt(\e k (2\e n + 1) (\e n &minus; \e m)! / (\e n + \e m)!)
78  * <b>P</b><sub><i>n</i></sub><sup><i>m</i></sup>(\e z), where
79  * <b>P</b><sub><i>n</i></sub><sup><i>m</i></sup>(\e z) is Ferrers
80  * function (also known as the Legendre function on the cut or the
81  * associated Legendre polynomial) http://dlmf.nist.gov/14.7.E10 and \e k
82  * = 1 for \e m = 0 and \e k = 2 otherwise.
83  *
84  * The mean squared value of
85  * <i>P</i><sub><i>nm</i></sub><sup>full</sup>(cos&theta;)
86  * cos(<i>m</i>&lambda;) and
87  * <i>P</i><sub><i>nm</i></sub><sup>full</sup>(cos&theta;)
88  * sin(<i>m</i>&lambda;) over the sphere is 1.
89  *
90  * @hideinitializer
91  **********************************************************************/
93  /**
94  * Schmidt semi-normalized associated Legendre polynomials.
95  *
96  * These are defined by
97  * <i>P</i><sub><i>nm</i></sub><sup>schmidt</sup>(\e z)
98  * = (&minus;1)<sup><i>m</i></sup>
99  * sqrt(\e k (\e n &minus; \e m)! / (\e n + \e m)!)
100  * <b>P</b><sub><i>n</i></sub><sup><i>m</i></sup>(\e z), where
101  * <b>P</b><sub><i>n</i></sub><sup><i>m</i></sup>(\e z) is Ferrers
102  * function (also known as the Legendre function on the cut or the
103  * associated Legendre polynomial) http://dlmf.nist.gov/14.7.E10 and \e k
104  * = 1 for \e m = 0 and \e k = 2 otherwise.
105  *
106  * The mean squared value of
107  * <i>P</i><sub><i>nm</i></sub><sup>schmidt</sup>(cos&theta;)
108  * cos(<i>m</i>&lambda;) and
109  * <i>P</i><sub><i>nm</i></sub><sup>schmidt</sup>(cos&theta;)
110  * sin(<i>m</i>&lambda;) over the sphere is 1/(2\e n + 1).
111  *
112  * @hideinitializer
113  **********************************************************************/
115  /// \cond SKIP
116  // These are deprecated...
117  full = FULL,
118  schmidt = SCHMIDT,
119  /// \endcond
120  };
121 
122  private:
123  typedef Math::real real;
125  real _a;
126  unsigned _norm;
127 
128  public:
129  /**
130  * Constructor with a full set of coefficients specified.
131  *
132  * @param[in] C the coefficients <i>C</i><sub><i>nm</i></sub>.
133  * @param[in] S the coefficients <i>S</i><sub><i>nm</i></sub>.
134  * @param[in] N the maximum degree and order of the sum
135  * @param[in] a the reference radius appearing in the definition of the
136  * sum.
137  * @param[in] norm the normalization for the associated Legendre
138  * polynomials, either SphericalHarmonic::full (the default) or
139  * SphericalHarmonic::schmidt.
140  * @exception GeographicErr if \e N does not satisfy \e N &ge; &minus;1.
141  * @exception GeographicErr if \e C or \e S is not big enough to hold the
142  * coefficients.
143  *
144  * The coefficients <i>C</i><sub><i>nm</i></sub> and
145  * <i>S</i><sub><i>nm</i></sub> are stored in the one-dimensional vectors
146  * \e C and \e S which must contain (\e N + 1)(\e N + 2)/2 and \e N (\e N +
147  * 1)/2 elements, respectively, stored in "column-major" order. Thus for
148  * \e N = 3, the order would be:
149  * <i>C</i><sub>00</sub>,
150  * <i>C</i><sub>10</sub>,
151  * <i>C</i><sub>20</sub>,
152  * <i>C</i><sub>30</sub>,
153  * <i>C</i><sub>11</sub>,
154  * <i>C</i><sub>21</sub>,
155  * <i>C</i><sub>31</sub>,
156  * <i>C</i><sub>22</sub>,
157  * <i>C</i><sub>32</sub>,
158  * <i>C</i><sub>33</sub>.
159  * In general the (\e n,\e m) element is at index \e m \e N &minus; \e m
160  * (\e m &minus; 1)/2 + \e n. The layout of \e S is the same except that
161  * the first column is omitted (since the \e m = 0 terms never contribute
162  * to the sum) and the 0th element is <i>S</i><sub>11</sub>
163  *
164  * The class stores <i>pointers</i> to the first elements of \e C and \e S.
165  * These arrays should not be altered or destroyed during the lifetime of a
166  * SphericalHarmonic object.
167  **********************************************************************/
168  SphericalHarmonic(const std::vector<real>& C,
169  const std::vector<real>& S,
170  int N, real a, unsigned norm = FULL)
171  : _a(a)
172  , _norm(norm)
173  { _c[0] = SphericalEngine::coeff(C, S, N); }
174 
175  /**
176  * Constructor with a subset of coefficients specified.
177  *
178  * @param[in] C the coefficients <i>C</i><sub><i>nm</i></sub>.
179  * @param[in] S the coefficients <i>S</i><sub><i>nm</i></sub>.
180  * @param[in] N the degree used to determine the layout of \e C and \e S.
181  * @param[in] nmx the maximum degree used in the sum. The sum over \e n is
182  * from 0 thru \e nmx.
183  * @param[in] mmx the maximum order used in the sum. The sum over \e m is
184  * from 0 thru min(\e n, \e mmx).
185  * @param[in] a the reference radius appearing in the definition of the
186  * sum.
187  * @param[in] norm the normalization for the associated Legendre
188  * polynomials, either SphericalHarmonic::FULL (the default) or
189  * SphericalHarmonic::SCHMIDT.
190  * @exception GeographicErr if \e N, \e nmx, and \e mmx do not satisfy
191  * \e N &ge; \e nmx &ge; \e mmx &ge; &minus;1.
192  * @exception GeographicErr if \e C or \e S is not big enough to hold the
193  * coefficients.
194  *
195  * The class stores <i>pointers</i> to the first elements of \e C and \e S.
196  * These arrays should not be altered or destroyed during the lifetime of a
197  * SphericalHarmonic object.
198  **********************************************************************/
199  SphericalHarmonic(const std::vector<real>& C,
200  const std::vector<real>& S,
201  int N, int nmx, int mmx,
202  real a, unsigned norm = FULL)
203  : _a(a)
204  , _norm(norm)
205  { _c[0] = SphericalEngine::coeff(C, S, N, nmx, mmx); }
206 
207  /**
208  * A default constructor so that the object can be created when the
209  * constructor for another object is initialized. This default object can
210  * then be reset with the default copy assignment operator.
211  **********************************************************************/
213 
214  /**
215  * Compute the spherical harmonic sum.
216  *
217  * @param[in] x cartesian coordinate.
218  * @param[in] y cartesian coordinate.
219  * @param[in] z cartesian coordinate.
220  * @return \e V the spherical harmonic sum.
221  *
222  * This routine requires constant memory and thus never throws an
223  * exception.
224  **********************************************************************/
225  Math::real operator()(real x, real y, real z) const {
226  real f[] = {1};
227  real v = 0;
228  real dummy;
229  switch (_norm) {
230  case FULL:
231  v = SphericalEngine::Value<false, SphericalEngine::FULL, 1>
232  (_c, f, x, y, z, _a, dummy, dummy, dummy);
233  break;
234  case SCHMIDT:
235  v = SphericalEngine::Value<false, SphericalEngine::SCHMIDT, 1>
236  (_c, f, x, y, z, _a, dummy, dummy, dummy);
237  break;
238  }
239  return v;
240  }
241 
242  /**
243  * Compute a spherical harmonic sum and its gradient.
244  *
245  * @param[in] x cartesian coordinate.
246  * @param[in] y cartesian coordinate.
247  * @param[in] z cartesian coordinate.
248  * @param[out] gradx \e x component of the gradient
249  * @param[out] grady \e y component of the gradient
250  * @param[out] gradz \e z component of the gradient
251  * @return \e V the spherical harmonic sum.
252  *
253  * This is the same as the previous function, except that the components of
254  * the gradients of the sum in the \e x, \e y, and \e z directions are
255  * computed. This routine requires constant memory and thus never throws
256  * an exception.
257  **********************************************************************/
258  Math::real operator()(real x, real y, real z,
259  real& gradx, real& grady, real& gradz) const {
260  real f[] = {1};
261  real v = 0;
262  switch (_norm) {
263  case FULL:
264  v = SphericalEngine::Value<true, SphericalEngine::FULL, 1>
265  (_c, f, x, y, z, _a, gradx, grady, gradz);
266  break;
267  case SCHMIDT:
268  v = SphericalEngine::Value<true, SphericalEngine::SCHMIDT, 1>
269  (_c, f, x, y, z, _a, gradx, grady, gradz);
270  break;
271  }
272  return v;
273  }
274 
275  /**
276  * Create a CircularEngine to allow the efficient evaluation of several
277  * points on a circle of latitude.
278  *
279  * @param[in] p the radius of the circle.
280  * @param[in] z the height of the circle above the equatorial plane.
281  * @param[in] gradp if true the returned object will be able to compute the
282  * gradient of the sum.
283  * @exception std::bad_alloc if the memory for the CircularEngine can't be
284  * allocated.
285  * @return the CircularEngine object.
286  *
287  * SphericalHarmonic::operator()() exchanges the order of the sums in the
288  * definition, i.e., &sum;<sub>n = 0..N</sub> &sum;<sub>m = 0..n</sub>
289  * becomes &sum;<sub>m = 0..N</sub> &sum;<sub>n = m..N</sub>.
290  * SphericalHarmonic::Circle performs the inner sum over degree \e n (which
291  * entails about <i>N</i><sup>2</sup> operations). Calling
292  * CircularEngine::operator()() on the returned object performs the outer
293  * sum over the order \e m (about \e N operations).
294  *
295  * Here's an example of computing the spherical sum at a sequence of
296  * longitudes without using a CircularEngine object \code
297  SphericalHarmonic h(...); // Create the SphericalHarmonic object
298  double r = 2, lat = 33, lon0 = 44, dlon = 0.01;
299  double
300  phi = lat * Math::degree<double>(),
301  z = r * sin(phi), p = r * cos(phi);
302  for (int i = 0; i <= 100; ++i) {
303  real
304  lon = lon0 + i * dlon,
305  lam = lon * Math::degree<double>();
306  std::cout << lon << " " << h(p * cos(lam), p * sin(lam), z) << "\n";
307  }
308  \endcode
309  * Here is the same calculation done using a CircularEngine object. This
310  * will be about <i>N</i>/2 times faster. \code
311  SphericalHarmonic h(...); // Create the SphericalHarmonic object
312  double r = 2, lat = 33, lon0 = 44, dlon = 0.01;
313  double
314  phi = lat * Math::degree<double>(),
315  z = r * sin(phi), p = r * cos(phi);
316  CircularEngine c(h(p, z, false)); // Create the CircularEngine object
317  for (int i = 0; i <= 100; ++i) {
318  real
319  lon = lon0 + i * dlon;
320  std::cout << lon << " " << c(lon) << "\n";
321  }
322  \endcode
323  **********************************************************************/
324  CircularEngine Circle(real p, real z, bool gradp) const {
325  real f[] = {1};
326  switch (_norm) {
327  case FULL:
328  return gradp ?
329  SphericalEngine::Circle<true, SphericalEngine::FULL, 1>
330  (_c, f, p, z, _a) :
331  SphericalEngine::Circle<false, SphericalEngine::FULL, 1>
332  (_c, f, p, z, _a);
333  break;
334  case SCHMIDT:
335  default: // To avoid compiler warnings
336  return gradp ?
337  SphericalEngine::Circle<true, SphericalEngine::SCHMIDT, 1>
338  (_c, f, p, z, _a) :
339  SphericalEngine::Circle<false, SphericalEngine::SCHMIDT, 1>
340  (_c, f, p, z, _a);
341  break;
342  }
343  }
344 
345  /**
346  * @return the zeroth SphericalEngine::coeff object.
347  **********************************************************************/
349  { return _c[0]; }
350  };
351 
352 } // namespace GeographicLib
353 
354 #endif // GEOGRAPHICLIB_SPHERICALHARMONIC_HPP
Math::real operator()(real x, real y, real z) const
#define GEOGRAPHICLIB_EXPORT
Definition: Constants.hpp:70
GeographicLib::Math::real real
Definition: GeodSolve.cpp:40
CircularEngine Circle(real p, real z, bool gradp) const
Math::real operator()(real x, real y, real z, real &gradx, real &grady, real &gradz) const
Package up coefficients for SphericalEngine.
Namespace for GeographicLib.
Definition: Accumulator.cpp:12
const SphericalEngine::coeff & Coefficients() const
Header for GeographicLib::Geocentric class.
Header for GeographicLib::CircularEngine class.
Spherical harmonic sums for a circle.
Header for GeographicLib::Constants class.
Spherical harmonic series.
Header for GeographicLib::SphericalEngine class.
SphericalHarmonic(const std::vector< real > &C, const std::vector< real > &S, int N, real a, unsigned norm=FULL)
SphericalHarmonic(const std::vector< real > &C, const std::vector< real > &S, int N, int nmx, int mmx, real a, unsigned norm=FULL)