GeographicLib  1.45
SphericalHarmonic2.hpp
Go to the documentation of this file.
1 /**
2  * \file SphericalHarmonic2.hpp
3  * \brief Header for GeographicLib::SphericalHarmonic2 class
4  *
5  * Copyright (c) Charles Karney (2011-2012) <charles@karney.com> and licensed
6  * under the MIT/X11 License. For more information, see
7  * http://geographiclib.sourceforge.net/
8  **********************************************************************/
9 
10 #if !defined(GEOGRAPHICLIB_SPHERICALHARMONIC2_HPP)
11 #define GEOGRAPHICLIB_SPHERICALHARMONIC2_HPP 1
12 
13 #include <vector>
17 
18 namespace GeographicLib {
19 
20  /**
21  * \brief Spherical harmonic series with two corrections to the coefficients
22  *
23  * This classes is similar to SphericalHarmonic, except that the coefficients
24  * <i>C</i><sub><i>nm</i></sub> are replaced by
25  * <i>C</i><sub><i>nm</i></sub> + \e tau' <i>C'</i><sub><i>nm</i></sub> + \e
26  * tau'' <i>C''</i><sub><i>nm</i></sub> (and similarly for
27  * <i>S</i><sub><i>nm</i></sub>).
28  *
29  * Example of use:
30  * \include example-SphericalHarmonic2.cpp
31  **********************************************************************/
32 
33  // Don't include the GEOGRPAHIC_EXPORT because this header-only class isn't
34  // used by any other classes in the library.
35  class /*GEOGRAPHICLIB_EXPORT*/ SphericalHarmonic2 {
36  public:
37  /**
38  * Supported normalizations for associate Legendre polynomials.
39  **********************************************************************/
41  /**
42  * Fully normalized associated Legendre polynomials. See
43  * SphericalHarmonic::FULL for documentation.
44  *
45  * @hideinitializer
46  **********************************************************************/
48  /**
49  * Schmidt semi-normalized associated Legendre polynomials. See
50  * SphericalHarmonic::SCHMIDT for documentation.
51  *
52  * @hideinitializer
53  **********************************************************************/
55  /// \cond SKIP
56  // These are deprecated...
57  full = FULL,
58  schmidt = SCHMIDT,
59  /// \endcond
60  };
61 
62  private:
63  typedef Math::real real;
65  real _a;
66  unsigned _norm;
67 
68  public:
69  /**
70  * Constructor with a full set of coefficients specified.
71  *
72  * @param[in] C the coefficients <i>C</i><sub><i>nm</i></sub>.
73  * @param[in] S the coefficients <i>S</i><sub><i>nm</i></sub>.
74  * @param[in] N the maximum degree and order of the sum
75  * @param[in] C1 the coefficients <i>C'</i><sub><i>nm</i></sub>.
76  * @param[in] S1 the coefficients <i>S'</i><sub><i>nm</i></sub>.
77  * @param[in] N1 the maximum degree and order of the first correction
78  * coefficients <i>C'</i><sub><i>nm</i></sub> and
79  * <i>S'</i><sub><i>nm</i></sub>.
80  * @param[in] C2 the coefficients <i>C''</i><sub><i>nm</i></sub>.
81  * @param[in] S2 the coefficients <i>S''</i><sub><i>nm</i></sub>.
82  * @param[in] N2 the maximum degree and order of the second correction
83  * coefficients <i>C'</i><sub><i>nm</i></sub> and
84  * <i>S'</i><sub><i>nm</i></sub>.
85  * @param[in] a the reference radius appearing in the definition of the
86  * sum.
87  * @param[in] norm the normalization for the associated Legendre
88  * polynomials, either SphericalHarmonic2::FULL (the default) or
89  * SphericalHarmonic2::SCHMIDT.
90  * @exception GeographicErr if \e N and \e N1 do not satisfy \e N &ge;
91  * \e N1 &ge; &minus;1, and similarly for \e N2.
92  * @exception GeographicErr if any of the vectors of coefficients is not
93  * large enough.
94  *
95  * See SphericalHarmonic for the way the coefficients should be stored. \e
96  * N1 and \e N2 should satisfy \e N1 &le; \e N and \e N2 &le; \e N.
97  *
98  * The class stores <i>pointers</i> to the first elements of \e C, \e S, \e
99  * C', \e S', \e C'', and \e S''. These arrays should not be altered or
100  * destroyed during the lifetime of a SphericalHarmonic object.
101  **********************************************************************/
102  SphericalHarmonic2(const std::vector<real>& C,
103  const std::vector<real>& S,
104  int N,
105  const std::vector<real>& C1,
106  const std::vector<real>& S1,
107  int N1,
108  const std::vector<real>& C2,
109  const std::vector<real>& S2,
110  int N2,
111  real a, unsigned norm = FULL)
112  : _a(a)
113  , _norm(norm) {
114  if (!(N1 <= N && N2 <= N))
115  throw GeographicErr("N1 and N2 cannot be larger that N");
116  _c[0] = SphericalEngine::coeff(C, S, N);
117  _c[1] = SphericalEngine::coeff(C1, S1, N1);
118  _c[2] = SphericalEngine::coeff(C2, S2, N2);
119  }
120 
121  /**
122  * Constructor with a subset of coefficients specified.
123  *
124  * @param[in] C the coefficients <i>C</i><sub><i>nm</i></sub>.
125  * @param[in] S the coefficients <i>S</i><sub><i>nm</i></sub>.
126  * @param[in] N the degree used to determine the layout of \e C and \e S.
127  * @param[in] nmx the maximum degree used in the sum. The sum over \e n is
128  * from 0 thru \e nmx.
129  * @param[in] mmx the maximum order used in the sum. The sum over \e m is
130  * from 0 thru min(\e n, \e mmx).
131  * @param[in] C1 the coefficients <i>C'</i><sub><i>nm</i></sub>.
132  * @param[in] S1 the coefficients <i>S'</i><sub><i>nm</i></sub>.
133  * @param[in] N1 the degree used to determine the layout of \e C' and \e
134  * S'.
135  * @param[in] nmx1 the maximum degree used for \e C' and \e S'.
136  * @param[in] mmx1 the maximum order used for \e C' and \e S'.
137  * @param[in] C2 the coefficients <i>C''</i><sub><i>nm</i></sub>.
138  * @param[in] S2 the coefficients <i>S''</i><sub><i>nm</i></sub>.
139  * @param[in] N2 the degree used to determine the layout of \e C'' and \e
140  * S''.
141  * @param[in] nmx2 the maximum degree used for \e C'' and \e S''.
142  * @param[in] mmx2 the maximum order used for \e C'' and \e S''.
143  * @param[in] a the reference radius appearing in the definition of the
144  * sum.
145  * @param[in] norm the normalization for the associated Legendre
146  * polynomials, either SphericalHarmonic2::FULL (the default) or
147  * SphericalHarmonic2::SCHMIDT.
148  * @exception GeographicErr if the parameters do not satisfy \e N &ge; \e
149  * nmx &ge; \e mmx &ge; &minus;1; \e N1 &ge; \e nmx1 &ge; \e mmx1 &ge;
150  * &minus;1; \e N &ge; \e N1; \e nmx &ge; \e nmx1; \e mmx &ge; \e mmx1;
151  * and similarly for \e N2, \e nmx2, and \e mmx2.
152  * @exception GeographicErr if any of the vectors of coefficients is not
153  * large enough.
154  *
155  * The class stores <i>pointers</i> to the first elements of \e C, \e S, \e
156  * C', \e S', \e C'', and \e S''. These arrays should not be altered or
157  * destroyed during the lifetime of a SphericalHarmonic object.
158  **********************************************************************/
159  SphericalHarmonic2(const std::vector<real>& C,
160  const std::vector<real>& S,
161  int N, int nmx, int mmx,
162  const std::vector<real>& C1,
163  const std::vector<real>& S1,
164  int N1, int nmx1, int mmx1,
165  const std::vector<real>& C2,
166  const std::vector<real>& S2,
167  int N2, int nmx2, int mmx2,
168  real a, unsigned norm = FULL)
169  : _a(a)
170  , _norm(norm) {
171  if (!(nmx1 <= nmx && nmx2 <= nmx))
172  throw GeographicErr("nmx1 and nmx2 cannot be larger that nmx");
173  if (!(mmx1 <= mmx && mmx2 <= mmx))
174  throw GeographicErr("mmx1 and mmx2 cannot be larger that mmx");
175  _c[0] = SphericalEngine::coeff(C, S, N, nmx, mmx);
176  _c[1] = SphericalEngine::coeff(C1, S1, N1, nmx1, mmx1);
177  _c[2] = SphericalEngine::coeff(C2, S2, N2, nmx2, mmx2);
178  }
179 
180  /**
181  * A default constructor so that the object can be created when the
182  * constructor for another object is initialized. This default object can
183  * then be reset with the default copy assignment operator.
184  **********************************************************************/
186 
187  /**
188  * Compute a spherical harmonic sum with two correction terms.
189  *
190  * @param[in] tau1 multiplier for correction coefficients \e C' and \e S'.
191  * @param[in] tau2 multiplier for correction coefficients \e C'' and \e S''.
192  * @param[in] x cartesian coordinate.
193  * @param[in] y cartesian coordinate.
194  * @param[in] z cartesian coordinate.
195  * @return \e V the spherical harmonic sum.
196  *
197  * This routine requires constant memory and thus never throws an
198  * exception.
199  **********************************************************************/
200  Math::real operator()(real tau1, real tau2, real x, real y, real z)
201  const {
202  real f[] = {1, tau1, tau2};
203  real v = 0;
204  real dummy;
205  switch (_norm) {
206  case FULL:
207  v = SphericalEngine::Value<false, SphericalEngine::FULL, 3>
208  (_c, f, x, y, z, _a, dummy, dummy, dummy);
209  break;
210  case SCHMIDT:
211  v = SphericalEngine::Value<false, SphericalEngine::SCHMIDT, 3>
212  (_c, f, x, y, z, _a, dummy, dummy, dummy);
213  break;
214  }
215  return v;
216  }
217 
218  /**
219  * Compute a spherical harmonic sum with two correction terms and its
220  * gradient.
221  *
222  * @param[in] tau1 multiplier for correction coefficients \e C' and \e S'.
223  * @param[in] tau2 multiplier for correction coefficients \e C'' and \e S''.
224  * @param[in] x cartesian coordinate.
225  * @param[in] y cartesian coordinate.
226  * @param[in] z cartesian coordinate.
227  * @param[out] gradx \e x component of the gradient
228  * @param[out] grady \e y component of the gradient
229  * @param[out] gradz \e z component of the gradient
230  * @return \e V the spherical harmonic sum.
231  *
232  * This is the same as the previous function, except that the components of
233  * the gradients of the sum in the \e x, \e y, and \e z directions are
234  * computed. This routine requires constant memory and thus never throws
235  * an exception.
236  **********************************************************************/
237  Math::real operator()(real tau1, real tau2, real x, real y, real z,
238  real& gradx, real& grady, real& gradz) const {
239  real f[] = {1, tau1, tau2};
240  real v = 0;
241  switch (_norm) {
242  case FULL:
243  v = SphericalEngine::Value<true, SphericalEngine::FULL, 3>
244  (_c, f, x, y, z, _a, gradx, grady, gradz);
245  break;
246  case SCHMIDT:
247  v = SphericalEngine::Value<true, SphericalEngine::SCHMIDT, 3>
248  (_c, f, x, y, z, _a, gradx, grady, gradz);
249  break;
250  }
251  return v;
252  }
253 
254  /**
255  * Create a CircularEngine to allow the efficient evaluation of several
256  * points on a circle of latitude at fixed values of \e tau1 and \e tau2.
257  *
258  * @param[in] tau1 multiplier for correction coefficients \e C' and \e S'.
259  * @param[in] tau2 multiplier for correction coefficients \e C'' and \e S''.
260  * @param[in] p the radius of the circle.
261  * @param[in] z the height of the circle above the equatorial plane.
262  * @param[in] gradp if true the returned object will be able to compute the
263  * gradient of the sum.
264  * @exception std::bad_alloc if the memory for the CircularEngine can't be
265  * allocated.
266  * @return the CircularEngine object.
267  *
268  * SphericalHarmonic2::operator()() exchanges the order of the sums in the
269  * definition, i.e., &sum;<sub><i>n</i> = 0..<i>N</i></sub>
270  * &sum;<sub><i>m</i> = 0..<i>n</i></sub> becomes &sum;<sub><i>m</i> =
271  * 0..<i>N</i></sub> &sum;<sub><i>n</i> = <i>m</i>..<i>N</i></sub>..
272  * SphericalHarmonic2::Circle performs the inner sum over degree \e n
273  * (which entails about <i>N</i><sup>2</sup> operations). Calling
274  * CircularEngine::operator()() on the returned object performs the outer
275  * sum over the order \e m (about \e N operations).
276  *
277  * See SphericalHarmonic::Circle for an example of its use.
278  **********************************************************************/
279  CircularEngine Circle(real tau1, real tau2, real p, real z, bool gradp)
280  const {
281  real f[] = {1, tau1, tau2};
282  switch (_norm) {
283  case FULL:
284  return gradp ?
285  SphericalEngine::Circle<true, SphericalEngine::FULL, 3>
286  (_c, f, p, z, _a) :
287  SphericalEngine::Circle<false, SphericalEngine::FULL, 3>
288  (_c, f, p, z, _a);
289  break;
290  case SCHMIDT:
291  default: // To avoid compiler warnings
292  return gradp ?
293  SphericalEngine::Circle<true, SphericalEngine::SCHMIDT, 3>
294  (_c, f, p, z, _a) :
295  SphericalEngine::Circle<false, SphericalEngine::SCHMIDT, 3>
296  (_c, f, p, z, _a);
297  break;
298  }
299  }
300 
301  /**
302  * @return the zeroth SphericalEngine::coeff object.
303  **********************************************************************/
305  { return _c[0]; }
306  /**
307  * @return the first SphericalEngine::coeff object.
308  **********************************************************************/
310  { return _c[1]; }
311  /**
312  * @return the second SphericalEngine::coeff object.
313  **********************************************************************/
315  { return _c[2]; }
316  };
317 
318 } // namespace GeographicLib
319 
320 #endif // GEOGRAPHICLIB_SPHERICALHARMONIC2_HPP
GeographicLib::Math::real real
Definition: GeodSolve.cpp:32
Package up coefficients for SphericalEngine.
const SphericalEngine::coeff & Coefficients1() const
Namespace for GeographicLib.
Definition: Accumulator.cpp:12
const SphericalEngine::coeff & Coefficients2() const
Spherical harmonic series with two corrections to the coefficients.
CircularEngine Circle(real tau1, real tau2, real p, real z, bool gradp) const
SphericalHarmonic2(const std::vector< real > &C, const std::vector< real > &S, int N, int nmx, int mmx, const std::vector< real > &C1, const std::vector< real > &S1, int N1, int nmx1, int mmx1, const std::vector< real > &C2, const std::vector< real > &S2, int N2, int nmx2, int mmx2, real a, unsigned norm=FULL)
Header for GeographicLib::CircularEngine class.
Spherical harmonic sums for a circle.
Exception handling for GeographicLib.
Definition: Constants.hpp:386
Header for GeographicLib::Constants class.
SphericalHarmonic2(const std::vector< real > &C, const std::vector< real > &S, int N, const std::vector< real > &C1, const std::vector< real > &S1, int N1, const std::vector< real > &C2, const std::vector< real > &S2, int N2, real a, unsigned norm=FULL)
Header for GeographicLib::SphericalEngine class.
Math::real operator()(real tau1, real tau2, real x, real y, real z, real &gradx, real &grady, real &gradz) const
const SphericalEngine::coeff & Coefficients() const
Math::real operator()(real tau1, real tau2, real x, real y, real z) const