Lemma is an Electromagnetics API
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

CubicSplineInterpolator.cpp 12KB

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