source: src/main/java/agents/anac/y2015/agentBuyogV2/flanagan/interpolation/CubicSplineFast.java

Last change on this file was 127, checked in by Wouter Pasman, 6 years ago

#41 ROLL BACK of rev.126 . So this version is equal to rev. 125

File size: 6.6 KB
Line 
1/**********************************************************
2*
3* Class CubicSplineFast
4*
5* Class for performing an interpolation using a cubic spline
6* setTabulatedArrays and interpolate adapted, with modification to
7* an object-oriented approach, from Numerical Recipes in C (http://www.nr.com/)
8* Stripped down version of CubicSpline - all data checks have been removed for faster running
9*
10*
11* WRITTEN BY: Dr Michael Thomas Flanagan
12*
13* DATE: 26 December 2009 (Stripped down version of CubicSpline: May 2002 - 31 October 2009)
14* UPDATE: 14 January 2010
15*
16* DOCUMENTATION:
17* See Michael Thomas Flanagan's Java library on-line web page:
18* http://www.ee.ucl.ac.uk/~mflanaga/java/CubicSplineFast.html
19* http://www.ee.ucl.ac.uk/~mflanaga/java/
20*
21* Copyright (c) 2002 - 2010 Michael Thomas Flanagan
22*
23* PERMISSION TO COPY:
24*
25* Permission to use, copy and modify this software and its documentation for NON-COMMERCIAL purposes is granted, without fee,
26* provided that an acknowledgement to the author, Dr Michael Thomas Flanagan at www.ee.ucl.ac.uk/~mflanaga, appears in all copies
27* and associated documentation or publications.
28*
29* Redistributions of the source code of this source code, or parts of the source codes, must retain the above copyright notice,
30* this list of conditions and the following disclaimer and requires written permission from the Michael Thomas Flanagan:
31*
32* Redistribution in binary form of all or parts of this class must reproduce the above copyright notice, this list of conditions and
33* the following disclaimer in the documentation and/or other materials provided with the distribution and requires written permission
34* from the Michael Thomas Flanagan:
35*
36* Dr Michael Thomas Flanagan makes no representations about the suitability or fitness of the software for any or for a particular purpose.
37* Dr Michael Thomas Flanagan shall not be liable for any damages suffered as a result of using, modifying or distributing this software
38* or its derivatives.
39*
40***************************************************************************************/
41
42
43package agents.anac.y2015.agentBuyogV2.flanagan.interpolation;
44
45public class CubicSplineFast{
46
47 private int nPoints = 0; // no. of tabulated points
48 private double[] y = null; // y=f(x) tabulated function
49 private double[] x = null; // x in tabulated function f(x)
50 private double[] d2ydx2 = null; // second derivatives of y
51 private boolean derivCalculated = false; // = true when the derivatives have been calculated
52
53 // Constructors
54 // Constructor with data arrays initialised to arrays x and y
55 public CubicSplineFast(double[] x, double[] y){
56 this.nPoints=x.length;
57 this.x = new double[nPoints];
58 this.y = new double[nPoints];
59 this.d2ydx2 = new double[nPoints];
60 for(int i=0; i<this.nPoints; i++){
61 this.x[i]=x[i];
62 this.y[i]=y[i];
63 }
64 this.calcDeriv();
65 }
66
67 // Constructor with data arrays initialised to zero
68 // Primarily for use by BiCubicSplineFast
69 public CubicSplineFast(int nPoints){
70 this.nPoints=nPoints;
71 this.x = new double[nPoints];
72 this.y = new double[nPoints];
73 this.d2ydx2 = new double[nPoints];
74 }
75
76 // METHODS
77
78 // Resets the x y data arrays - primarily for use in BiCubicSplineFast
79 public void resetData(double[] x, double[] y){
80 for(int i=0; i<this.nPoints; i++){
81 this.x[i]=x[i];
82 this.y[i]=y[i];
83 }
84 }
85
86 // Returns a new CubicSplineFast setting array lengths to n and all array values to zero with natural spline default
87 // Primarily for use in BiCubicSplineFast
88 public static CubicSplineFast zero(int n){
89 if(n<3)throw new IllegalArgumentException("A minimum of three data points is needed");
90 CubicSplineFast aa = new CubicSplineFast(n);
91 return aa;
92 }
93
94 // Create a one dimensional array of cubic spline objects of length n each of array length m
95 // Primarily for use in BiCubicSplineFast
96 public static CubicSplineFast[] oneDarray(int n, int m){
97 CubicSplineFast[] a =new CubicSplineFast[n];
98 for(int i=0; i<n; i++){
99 a[i]=CubicSplineFast.zero(m);
100 }
101 return a;
102 }
103
104
105 // Calculates the second derivatives of the tabulated function
106 // for use by the cubic spline interpolation method (.interpolate)
107 // This method follows the procedure in Numerical Methods C language procedure for calculating second derivatives
108 public void calcDeriv(){
109 double p=0.0D,qn=0.0D,sig=0.0D,un=0.0D;
110 double[] u = new double[nPoints];
111
112 d2ydx2[0]=u[0]=0.0;
113 for(int i=1;i<=this.nPoints-2;i++){
114 sig=(this.x[i]-this.x[i-1])/(this.x[i+1]-this.x[i-1]);
115 p=sig*this.d2ydx2[i-1]+2.0;
116 this.d2ydx2[i]=(sig-1.0)/p;
117 u[i]=(this.y[i+1]-this.y[i])/(this.x[i+1]-this.x[i]) - (this.y[i]-this.y[i-1])/(this.x[i]-this.x[i-1]);
118 u[i]=(6.0*u[i]/(this.x[i+1]-this.x[i-1])-sig*u[i-1])/p;
119 }
120
121 qn=un=0.0;
122 this.d2ydx2[this.nPoints-1]=(un-qn*u[this.nPoints-2])/(qn*this.d2ydx2[this.nPoints-2]+1.0);
123 for(int k=this.nPoints-2;k>=0;k--){
124 this.d2ydx2[k]=this.d2ydx2[k]*this.d2ydx2[k+1]+u[k];
125 }
126 this.derivCalculated = true;
127 }
128
129 // INTERPOLATE
130 // Returns an interpolated value of y for a value of x from a tabulated function y=f(x)
131 // after the data has been entered via a constructor.
132 // The derivatives are calculated, bt calcDeriv(), on the first call to this method ands are
133 // then stored for use on all subsequent calls
134 public double interpolate(double xx){
135
136 double h=0.0D,b=0.0D,a=0.0D, yy=0.0D;
137 int k=0;
138 int klo=0;
139 int khi=this.nPoints-1;
140 while (khi-klo > 1){
141 k=(khi+klo) >> 1;
142 if(this.x[k] > xx){
143 khi=k;
144 }
145 else{
146 klo=k;
147 }
148 }
149 h=this.x[khi]-this.x[klo];
150
151 if (h == 0.0){
152 throw new IllegalArgumentException("Two values of x are identical: point "+klo+ " ("+this.x[klo]+") and point "+khi+ " ("+this.x[khi]+")" );
153 }
154 else{
155 a=(this.x[khi]-xx)/h;
156 b=(xx-this.x[klo])/h;
157 yy=a*this.y[klo]+b*this.y[khi]+((a*a*a-a)*this.d2ydx2[klo]+(b*b*b-b)*this.d2ydx2[khi])*(h*h)/6.0;
158 }
159 return yy;
160 }
161}
Note: See TracBrowser for help on using the repository browser.