Main Lemma Repository
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CubicSplineInterpolator.h 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /* This file is part of Lemma, a geophysical modelling and inversion API.
  2. * More information is available at http://lemmasoftware.org
  3. */
  4. /* This Source Code Form is subject to the terms of the Mozilla Public
  5. * License, v. 2.0. If a copy of the MPL was not distributed with this
  6. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  7. */
  8. /**
  9. * @file
  10. * @date 09/25/2013 08:20:14 AM
  11. * @version $Id$
  12. * @author Trevor Irons (ti)
  13. * @email Trevor.Irons@xri-geo.com
  14. * @copyright Copyright (c) 2013, XRI Geophysics, LLC
  15. * @copyright Copyright (c) 2013, Trevor Irons
  16. */
  17. #ifndef CUBICSPLINEINTERPOLATOR_INC
  18. #define CUBICSPLINEINTERPOLATOR_INC
  19. #include "LemmaObject.h"
  20. namespace Lemma {
  21. // Simple struct to hold spline terms
  22. struct SplineSet{
  23. VectorXr a;
  24. VectorXr b;
  25. VectorXr c;
  26. VectorXr d;
  27. VectorXr x;
  28. SplineSet( ) {
  29. }
  30. SplineSet(const int&n) {
  31. a = VectorXr::Zero(n+1);
  32. b = VectorXr::Zero(n);
  33. c = VectorXr::Zero(n+1);
  34. d = VectorXr::Zero(n);
  35. x = VectorXr::Zero(n+1);
  36. }
  37. };
  38. /**
  39. * \ingroup LemmaCore
  40. * \brief Real 1D Natural cubic spline interpolator.
  41. * \details Splines are fit between knots \f$j\f$ according to the forulae
  42. * \f[ S_j(x) = a_j + b_j(x - x_j) + c_j(x-x_j)^2 + d_j(x-x_y)^3 \f]
  43. * The spline must satisfy the following conditions
  44. * \f{eqnarray} {
  45. * S_i(x_i) & = & y_i = S_{i-1}(x_i), i = 1,..., n-1 \\
  46. * S'_i(x_i) & = & S'_{i-1}(x_i), i = 1,..., n-1 \\
  47. * S''_i(x_i) & = & S''_{i-1}(x_i), i = 1,..., n-1 \\
  48. * S''_0(x_0) & = & S''_{n-1}(x_n) = 0
  49. * \f}
  50. */
  51. class CubicSplineInterpolator : public LemmaObject {
  52. friend std::ostream &operator<<(std::ostream &stream,
  53. const CubicSplineInterpolator &ob);
  54. public:
  55. // ==================== LIFECYCLE =======================
  56. /**
  57. * Factory method for generating concrete class.
  58. * @return a std::shared_ptr of type CubicSplineInterpolator
  59. */
  60. static std::shared_ptr<CubicSplineInterpolator> NewSP();
  61. /**
  62. * Uses YAML to serialize this object.
  63. * @return a YAML::Node
  64. */
  65. YAML::Node Serialize() const;
  66. /**
  67. * Constructs an object from a YAML::Node.
  68. */
  69. static std::shared_ptr< CubicSplineInterpolator > DeSerialize(const YAML::Node& node);
  70. // ==================== OPERATORS =======================
  71. // ==================== OPERATIONS =======================
  72. /** Sets the knots to use for interpolation.
  73. @param[in] x are the absissa values
  74. @param[in] y are the ordinate values
  75. */
  76. void SetKnots(const VectorXr& x, const VectorXr& y);
  77. /** Resets the knots to use for interpolation, when abscissa values haven't changed.
  78. @param[in] y are the ordinate values
  79. */
  80. void ResetKnotOrdinate( const VectorXr& y );
  81. /** Interpolate a monotonically increasing ordered set.
  82. @param[in] x are the interpolation abscissa points
  83. @return the ordinate values at x
  84. */
  85. VectorXr InterpolateOrderedSet(const VectorXr& x);
  86. /** integrates the spline from x0 to x1. Uses composite Simpson's rule and n is the number of segments
  87. * @param[in] x0 is left argument
  88. * @param[in] x1 is right argument
  89. * @param[in] n is the number of points, must be even
  90. */
  91. Real Integrate(const Real& x0, const Real& x1, const int& n);
  92. /** integrates using cubic spline values. Taken from AMRIRA P223F project code Leroi, which in turn was based on
  93. This is a modification of the FUNCTION PPVALU in the book
  94. "A PRACTICAL GUIDE TO SPLINES" by C. DE BOOR
  95. */
  96. Real Integrate(const Real& x0, const Real& x1);
  97. /** @returns the know abscissa values
  98. */
  99. VectorXr GetKnotAbscissa();
  100. /** @returns the know abscissa values
  101. */
  102. VectorXr GetKnotOrdinate();
  103. /** Interpolation at a single point.
  104. @param[in] x is the interpolation abscissa point
  105. @param[in] i is an optional index to start searching at. Defaults to zero
  106. @return the ordinate value at x
  107. */
  108. Real Interpolate(const Real& x, int& i);
  109. /** Interpolation at a single point.
  110. @param[in] x is the interpolation abscissa point
  111. @return the ordinate value at x
  112. */
  113. Real Interpolate(const Real& x);
  114. // ==================== ACCESS =======================
  115. // ==================== INQUIRY =======================
  116. /** Returns the name of the underlying class, similiar to Python's type */
  117. virtual inline std::string GetName() const {
  118. return CName;
  119. }
  120. protected:
  121. // ==================== LIFECYCLE =======================
  122. /** Default protected constructor, use New */
  123. CubicSplineInterpolator ( );
  124. /** Protected DeDerializing constructor, use factory DeSerialize method*/
  125. CubicSplineInterpolator (const YAML::Node& node);
  126. /** Default protected destructor, smart pointers auto delete */
  127. ~CubicSplineInterpolator ();
  128. /**
  129. * @copybrief LemmaObject::Release()
  130. * @copydetails LemmaObject::Release()
  131. */
  132. void Release();
  133. // ==================== OPERATIONS =======================
  134. /** Finds the interval of knots in spline to use for integration.
  135. */
  136. int Interval(const Real& x);
  137. private:
  138. /** ASCII string representation of the class name */
  139. static constexpr auto CName = "CubicSplineInterpolator";
  140. SplineSet Spline;
  141. int ilo;
  142. int mflag;
  143. // ==================== DATA MEMBERS =========================
  144. }; // ----- end of class CubicSplineInterpolator -----
  145. } // ----- end of Lemma name -----
  146. #endif // ----- #ifndef CUBICSPLINEINTERPOLATOR_INC -----
  147. /* vim: set tabstop=4 expandtab: */
  148. /* vim: set filetype=cpp: */