Lemma is an Electromagnetics API
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.cpp 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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 02/07/2014 12:50:52 PM
  11. * @version $Id$
  12. * @author Trevor Irons (ti)
  13. * @email Trevor.Irons@xri-geo.com
  14. * @copyright Copyright (c) 2014, Trevor Irons
  15. */
  16. #include "CubicSplineInterpolator.h"
  17. #include<iostream>
  18. #include<fstream>
  19. #include<vector>
  20. #include<algorithm>
  21. #include<cmath>
  22. namespace Lemma {
  23. // ==================== FRIEND METHODS =====================
  24. std::ostream &operator << (std::ostream &stream, const CubicSplineInterpolator &ob) {
  25. stream << ob.Serialize() << "\n---\n"; // End of doc --- as a direct stream should encapulste thingy
  26. return stream;
  27. }
  28. // ==================== LIFECYCLE =======================
  29. //--------------------------------------------------------------------------------------
  30. // Class: CubicSplineInterpolator
  31. // Method: CubicSplineInterpolator
  32. // Description: constructor (protected)
  33. //--------------------------------------------------------------------------------------
  34. CubicSplineInterpolator::CubicSplineInterpolator ( ) : LemmaObject( ) {
  35. } // ----- end of method CubicSplineInterpolator::CubicSplineInterpolator (constructor) -----
  36. //--------------------------------------------------------------------------------------
  37. // Class: CubicSplineInterpolator
  38. // Method: NewSP()
  39. // Description: public constructor
  40. //--------------------------------------------------------------------------------------
  41. std::shared_ptr<CubicSplineInterpolator> CubicSplineInterpolator::NewSP() {
  42. std::shared_ptr<CubicSplineInterpolator> sp(new CubicSplineInterpolator( ), LemmaObjectDeleter() );
  43. return sp;
  44. }
  45. //--------------------------------------------------------------------------------------
  46. // Class: CubicSplineInterpolator
  47. // Method: ~CubicSplineInterpolator
  48. // Description: destructor (protected)
  49. //--------------------------------------------------------------------------------------
  50. CubicSplineInterpolator::~CubicSplineInterpolator () {
  51. } // ----- end of method CubicSplineInterpolator::~CubicSplineInterpolator (destructor) -----
  52. //--------------------------------------------------------------------------------------
  53. // Class: CubicSplineInterpolator
  54. // Method: Release
  55. // Description: destructor (protected)
  56. //--------------------------------------------------------------------------------------
  57. void CubicSplineInterpolator::Release() {
  58. delete this;
  59. }
  60. // ==================== OPERATIONS =======================
  61. //--------------------------------------------------------------------------------------
  62. // Class: CubicSplineInterpolator
  63. // Method: SetKnots
  64. //--------------------------------------------------------------------------------------
  65. void CubicSplineInterpolator::SetKnots ( const VectorXr& x, const VectorXr& y ) {
  66. int n = x.size()-1;
  67. Spline = SplineSet(n);
  68. Spline.a = y;
  69. Spline.x = x;
  70. VectorXr h = VectorXr::Zero(n);
  71. for(int i=0; i<n; ++i)
  72. h(i) = Spline.x(i+1)-Spline.x(i);
  73. VectorXr alpha(n-1);
  74. for(int i=1; i<n-1; ++i)
  75. alpha(i) = 3.*(Spline.a[i+1]-Spline.a[i])/h[i] - 3.*(Spline.a[i]-Spline.a[i-1])/h[i-1] ;
  76. VectorXr l = VectorXr::Zero(n+1);
  77. VectorXr mu = VectorXr::Zero(n+1);
  78. VectorXr z = VectorXr::Zero(n+1);
  79. l[0] = 1;
  80. mu[0] = 0;
  81. z[0] = 0;
  82. for(int i = 1; i < n-1; ++i) {
  83. l[i] = 2 *(Spline.x[i+1]-Spline.x[i-1])-h[i-1]*mu[i-1];
  84. mu[i] = h[i]/l[i];
  85. z[i] = (alpha[i]-h[i-1]*z[i-1])/l[i];
  86. }
  87. l[n] = 1;
  88. z[n] = 0;
  89. for(int j = n-1; j >= 0; --j) {
  90. Spline.c[j] = z[j] - mu[j] * Spline.c[j+1];
  91. Spline.b[j] = (Spline.a[j+1]-Spline.a[j])/h[j]-h[j]*(Spline.c[j+1]+2*Spline.c[j])/3;
  92. Spline.d[j] = (Spline.c[j+1]-Spline.c[j])/3/h[j];
  93. }
  94. // On OSX, this causes a strange bug 'sometimes', alignment?
  95. //Spline.c = Spline.c.head(n);
  96. return;
  97. } // ----- end of method CubicSplineInterpolator::SetKnots -----
  98. //--------------------------------------------------------------------------------------
  99. // Class: CubicSplineInterplator
  100. // Method: ResetKnotOrdinate
  101. //--------------------------------------------------------------------------------------
  102. void CubicSplineInterpolator::ResetKnotOrdinate ( const VectorXr& y ) {
  103. VectorXr x = Spline.x;
  104. SetKnots(x, y);
  105. return ;
  106. } // ----- end of method CubicSplineInterplator::ResetKnotOrdinate -----
  107. //--------------------------------------------------------------------------------------
  108. // Class: CubicSplineInterpolator
  109. // Method: InterpolateOrderedSet
  110. //--------------------------------------------------------------------------------------
  111. VectorXr CubicSplineInterpolator::InterpolateOrderedSet ( const VectorXr& x ) {
  112. VectorXr y = VectorXr::Zero(x.size());
  113. int ii = 0;
  114. for (int iy=0; iy<y.size(); ++iy) {
  115. y[iy] = Interpolate(x[iy], ii);
  116. }
  117. return y;
  118. } // ----- end of method CubicSplineInterpolator::InterpolateOrderedSet -----
  119. //--------------------------------------------------------------------------------------
  120. // Class: CubicSplineInterpolator
  121. // Method: Interpolate
  122. //--------------------------------------------------------------------------------------
  123. Real CubicSplineInterpolator::Interpolate ( const Real& x, int& i) {
  124. // O(n) search, could do bisection, but if these are sorted, then this is quick
  125. while(Spline.x[i] < x && i<Spline.x.size()) {
  126. ++i;
  127. }
  128. --i;
  129. //if ( x > Spline.x[i] ) {
  130. // throw std::runtime_error("CubicSplineInterpolator::Interpolate ATTEMPT TO INTERPOLATE PAST LAST KNOT");
  131. //}
  132. return Spline.a[i] + Spline.b[i]*(x-Spline.x[i]) + Spline.c[i]*((x-Spline.x[i])*(x-Spline.x[i])) +
  133. Spline.d[i]*((x-Spline.x[i])*(x-Spline.x[i])*(x-Spline.x[i]) );
  134. } // ----- end of method CubicSplineInterpolator::Interpolate -----
  135. //--------------------------------------------------------------------------------------
  136. // Class: CubicSplineInterpolator
  137. // Method: Interpolate
  138. //--------------------------------------------------------------------------------------
  139. Real CubicSplineInterpolator::Interpolate ( const Real& x ) {
  140. int ii(0);
  141. return Interpolate(x, ii);
  142. }
  143. //--------------------------------------------------------------------------------------
  144. // Class: CubicSplineInterpolator
  145. // Method: Integrate
  146. //--------------------------------------------------------------------------------------
  147. Real CubicSplineInterpolator::Integrate ( const Real& x0, const Real& x1, const int& n ) {
  148. assert(n > 0);
  149. // force n to be even?
  150. //n += (n % 2);
  151. Real S = Interpolate(x0) + Interpolate(x1);
  152. Real h = (x1 - x0) / Real(n);
  153. int ik = 0;
  154. for (int i=1; i<n; i+=2) {
  155. S += 4. * Interpolate(x0 + (Real)(i)*h, ik);
  156. }
  157. ik = 0;
  158. for (int i=2; i<n-1; i+=2) {
  159. S += 2. * Interpolate(x0 + (Real)(i)*h, ik);
  160. }
  161. return S * h / 3.;
  162. }
  163. //--------------------------------------------------------------------------------------
  164. // Class: CubicSplineInterpolator
  165. // Method: Integrate
  166. //--------------------------------------------------------------------------------------
  167. Real CubicSplineInterpolator::Integrate ( const Real& x0, const Real& x1 ) {
  168. int i0 = Interval(x0);
  169. int i1 = Interval(x1);
  170. Real h0 = x0 - Spline.x(i0);
  171. if (mflag == -1) h0 = 0;
  172. Real h1 = x1 - Spline.x(i1);
  173. Real cubint = (((Spline.d(i1)*h1/4.0 + Spline.c(i1) )*h1/3.0 +
  174. Spline.b(i1) )*h1/2.0 + Spline.a(i1) )*h1
  175. - (((Spline.d(i0)*h0/4.0 + Spline.c(i0) )*h0/3.0 +
  176. Spline.b(i0) )*h0/2.0 + Spline.a(i0) )*h0;
  177. // Include integrals over intervening intervals.
  178. if (i1 > i0) {
  179. for (int i=i0; i<i1-1; ++i) {
  180. Real h = Spline.x(i+1) - Spline.x(i);
  181. cubint += (((Spline.d(i)*h/4.0 + Spline.c(i) )*h/3.0 +
  182. Spline.b(i))*h/2.0 + Spline.a(i) )*h;
  183. }
  184. }
  185. return cubint;
  186. } // ----- end of method CubicSplineInterpolator::Integrate -----
  187. //--------------------------------------------------------------------------------------
  188. // Class: CubicSplineInterpolator
  189. // Method: Interval
  190. //--------------------------------------------------------------------------------------
  191. int CubicSplineInterpolator::Interval ( const Real& x ) {
  192. std::cerr << "ENTERING CubicSplineInterpolator::Inverval. Known bugs here" << std::endl;
  193. int nx = Spline.x.size() - 2; // TODO check if this is correct or just -1
  194. // when x not in range
  195. if (x <= Spline.x(0) || nx <= 1 ) {
  196. mflag = -1;
  197. return 1;
  198. }
  199. if (x >= Spline.x(nx)) {
  200. mflag = 1;
  201. return nx;
  202. }
  203. mflag = 0;
  204. if (ilo >= nx) ilo = nx-1;
  205. int ihi = ilo+1;
  206. // if x is already in the interval
  207. if ( x<Spline.x(ihi) && x >= Spline.x(ilo) ) {
  208. //std::cout << "TRIVIAL INTERVAL " << Spline.x(ilo) << "\t" << x << "\t" << Spline.x(ihi) << std::endl;
  209. return ilo;
  210. }
  211. if (x <= Spline.x(ilo)) { // decrease ilo to capture
  212. int istep = 1;
  213. for (int ix=1; ix<nx; ++ix) {
  214. ihi = ilo;
  215. ilo = ihi - istep;
  216. ilo = std::max(1, ilo);
  217. if (x >= Spline.x(ilo) || ilo == 1) break;
  218. istep *= 2;
  219. }
  220. } else if (x >= Spline.x(ihi)) { // increase ihi to capture
  221. int istep = 1;
  222. for (int ix=1; ix<nx; ++ix) {
  223. ilo = ihi;
  224. ihi = ilo + istep;
  225. ihi = std::min(ihi, nx);
  226. if (x <= Spline.x(ihi) || ihi == nx) break;
  227. istep *= 2;
  228. }
  229. }
  230. // Now Spline.x(ilo) <= x < Spline.x(ihi) --> Narrow the interval.
  231. //std::cout << "WIDE INTERVAL " << Spline.x(ilo) << "\t" << x << "\t" << Spline.x(ihi) << std::endl;
  232. for (int ix=1; ix<nx; ++ix) {
  233. int middle = (ilo+ihi) / 2;
  234. if (middle == ilo) break;
  235. if (x < Spline.x(middle)) {
  236. ihi = middle;
  237. } else {
  238. ilo = middle;
  239. }
  240. }
  241. assert ( Spline.x(ilo) < x && x < Spline.x(ihi) );
  242. return ilo;
  243. } // ----- end of method CubicSplineInterpolator::Inverval -----
  244. //--------------------------------------------------------------------------------------
  245. // Class: CubicSplineInterplator
  246. // Method: GetAbscissa
  247. //--------------------------------------------------------------------------------------
  248. VectorXr CubicSplineInterpolator::GetKnotAbscissa ( ) {
  249. return Spline.x;
  250. } // ----- end of method CubicSplineInterplator::get_GetAbscissa -----
  251. //--------------------------------------------------------------------------------------
  252. // Class: CubicSplineInterpolator
  253. // Method: GetKnotOrdinate
  254. //--------------------------------------------------------------------------------------
  255. VectorXr CubicSplineInterpolator::GetKnotOrdinate ( ) {
  256. return Spline.a;
  257. } // ----- end of method CubicSplineInterpolator::GetKnotOrdinate -----
  258. } // ----- end of Lemma name -----