SphericalHarmonic1.hpp 12 KB

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