Main Lemma Repository
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

CubicSplineInterpolator.h 5.7KB

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