source: src/main/java/agents/anac/y2015/agentBuyogV2/flanagan/interpolation/QuadriCubicSpline.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: 10.8 KB
Line 
1/**********************************************************
2*
3* QuadriCubicSpline.java
4*
5* Class for performing an interpolation on the tabulated
6* function y = f(x1,x2,x3,x4) using a natural quadricubic spline
7* Assumes second derivatives at end points = 0 (natural spine)
8*
9* WRITTEN BY: Dr Michael Thomas Flanagan
10*
11* DATE: May 2003
12* UPDATE July 2007, 4 December 2007, 21 September 2008, 12 October 2009, 31 October 2009
13*
14* DOCUMENTATION:
15* See Michael Thomas Flanagan's Java library on-line web page:
16* http://www.ee.ucl.ac.uk/~mflanaga/java/QuadriCubicSpline.html
17* http://www.ee.ucl.ac.uk/~mflanaga/java/
18*
19* Copyright (c) 2003 - 2009 Michael Thomas Flanagan
20*
21* PERMISSION TO COPY:
22*
23* Permission to use, copy and modify this software and its documentation for NON-COMMERCIAL purposes is granted, without fee,
24* provided that an acknowledgement to the author, Dr Michael Thomas Flanagan at www.ee.ucl.ac.uk/~mflanaga, appears in all copies
25* and associated documentation or publications.
26*
27* Redistributions of the source code of this source code, or parts of the source codes, must retain the above copyright notice, this list of conditions
28* and the following disclaimer and requires written permission from the Michael Thomas Flanagan:
29*
30* Redistribution in binary form of all or parts of this class must reproduce the above copyright notice, this list of conditions and
31* the following disclaimer in the documentation and/or other materials provided with the distribution and requires written permission from the Michael Thomas Flanagan:
32*
33* Dr Michael Thomas Flanagan makes no representations about the suitability or fitness of the software for any or for a particular purpose.
34* Dr Michael Thomas Flanagan shall not be liable for any damages suffered as a result of using, modifying or distributing this software
35* or its derivatives.
36*
37***************************************************************************************/
38
39package agents.anac.y2015.agentBuyogV2.flanagan.interpolation;
40
41import agents.anac.y2015.agentBuyogV2.flanagan.math.Fmath;
42
43public class QuadriCubicSpline{
44
45 private int nPoints = 0; // no. of x1 tabulated points
46 private int mPoints = 0; // no. of x2 tabulated points
47 private int lPoints = 0; // no. of x3 tabulated points
48 private int kPoints = 0; // no. of x4 tabulated points
49
50 private double[][][][] y = null; // y=f(x1,x2,x3,x4) tabulated function
51 private double[] x1 = null; // x1 in tabulated function f(x1,x2,x3,x4)
52 private double[] x2 = null; // x2 in tabulated function f(x1,x2,x3,x4)
53 private double[] x3 = null; // x3 in tabulated function f(x1,x2,x3,x4)
54 private double[] x4 = null; // x4 in tabulated function f(x1,x2,x3,x4)
55 private double[] xMin = new double[4]; // minimum values of x1, x2, x3 and x4
56 private double[] xMax = new double[4]; // maximum values of x1, x2, x3 and x4
57
58 private TriCubicSpline[] tcsn = null; // nPoints array of TriCubicSpline instances
59 private CubicSpline csm = null; // CubicSpline instance
60 private double[][][][] d2ydx2inner = null; // inner matrix of second derivatives
61 private boolean derivCalculated = false; // = true when the called triicubic spline derivatives have been calculated
62 private boolean averageIdenticalAbscissae = false; // if true: the the ordinate values for identical abscissae are averaged
63 // If false: the abscissae values are separated by 0.001 of the total abscissae range;
64 private static double potentialRoundingError = 5e-15; // potential rounding error used in checking wheter a value lies within the interpolation bounds (static value)
65 private static boolean roundingCheck = true; // = true: points outside the interpolation bounds by less than the potential rounding error rounded to the bounds limit (static value)
66
67
68 // Constructor
69 public QuadriCubicSpline(double[] x1, double[] x2, double[] x3, double[] x4, double[][][][] y){
70 this.nPoints=x1.length;
71 this.mPoints=x2.length;
72 this.lPoints=x3.length;
73 this.kPoints=x4.length;
74 if(this.nPoints!=y.length)throw new IllegalArgumentException("Arrays x1 and y-row are of different length " + this.nPoints + " " + y.length);
75 if(this.mPoints!=y[0].length)throw new IllegalArgumentException("Arrays x2 and y-column are of different length "+ this.mPoints + " " + y[0].length);
76 if(this.lPoints!=y[0][0].length)throw new IllegalArgumentException("Arrays x3 and y-column are of different length "+ this.mPoints + " " + y[0][0].length);
77 if(this.kPoints!=y[0][0][0].length)throw new IllegalArgumentException("Arrays x4 and y-column are of different length "+ this.kPoints + " " + y[0][0][0].length);
78 if(this.nPoints<3 || this.mPoints<3 || this.lPoints<3 || this.kPoints<3)throw new IllegalArgumentException("The tabulated 4D array must have a minimum size of 3 X 3 X 3 X 3");
79
80 this.csm = new CubicSpline(this.nPoints);
81 this.tcsn = TriCubicSpline.oneDarray(this.nPoints, this.mPoints, this.lPoints, this.kPoints);
82 this.x1 = new double[this.nPoints];
83 this.x2 = new double[this.mPoints];
84 this.x3 = new double[this.lPoints];
85 this.x4 = new double[this.kPoints];
86
87 this.y = new double[this.nPoints][this.mPoints][this.lPoints][this.kPoints];
88 this.d2ydx2inner = new double[this.nPoints][this.mPoints][this.lPoints][this.kPoints];
89 for(int i=0; i<this.nPoints; i++){
90 this.x1[i]=x1[i];
91 }
92 this.xMin[0] = Fmath.minimum(this.x1);
93 this.xMax[0] = Fmath.maximum(this.x1);
94
95 for(int j=0; j<this.mPoints; j++){
96 this.x2[j]=x2[j];
97 }
98 this.xMin[1] = Fmath.minimum(this.x2);
99 this.xMax[1] = Fmath.maximum(this.x2);
100
101 for(int j=0; j<this.lPoints; j++){
102 this.x3[j]=x3[j];
103 }
104 this.xMin[2] = Fmath.minimum(this.x3);
105 this.xMax[2] = Fmath.maximum(this.x3);
106
107 for(int j=0; j<this.kPoints; j++){
108 this.x4[j]=x4[j];
109 }
110 this.xMin[3] = Fmath.minimum(this.x4);
111 this.xMax[3] = Fmath.maximum(this.x4);
112
113 for(int i =0; i<this.nPoints; i++){
114 for(int j=0; j<this.mPoints; j++){
115 for(int k=0; k<this.lPoints; k++){
116 for(int l=0; l<this.kPoints; l++){
117 this.y[i][j][k][l]=y[i][j][k][l];
118 }
119 }
120 }
121 }
122
123 double[][][] yTempml = new double[this.mPoints][this.lPoints][this.kPoints];
124 for(int i=0; i<this.nPoints; i++){
125 for(int j=0; j<this.mPoints; j++){
126 for(int k=0; k<this.lPoints; k++){
127 for(int l=0; l<this.kPoints; l++){
128 yTempml[j][k][l]=y[i][j][k][l];
129 }
130 }
131 }
132 this.tcsn[i].resetData(x2,x3,x4,yTempml);
133 d2ydx2inner[i] = this.tcsn[i].getDeriv();
134 }
135 double[] yTempm = new double[nPoints];
136 this.derivCalculated = true;
137 }
138
139 // METHODS
140
141 // Reset rounding error check option
142 // Default option: points outside the interpolation bounds by less than the potential rounding error rounded to the bounds limit
143 // This method causes this check to be ignored and an exception to be thrown if any point lies outside the interpolation bounds
144 public static void noRoundingErrorCheck(){
145 QuadriCubicSpline.roundingCheck = false;
146 TriCubicSpline.noRoundingErrorCheck();
147 BiCubicSpline.noRoundingErrorCheck();
148 CubicSpline.noRoundingErrorCheck();
149 }
150
151 // Reset potential rounding error value
152 // Default option: points outside the interpolation bounds by less than the potential rounding error rounded to the bounds limit
153 // The default value for the potential rounding error is 5e-15*times the 10^exponent of the value outside the bounds
154 // This method allows the 5e-15 to be reset
155 public static void potentialRoundingError(double potentialRoundingError){
156 QuadriCubicSpline.potentialRoundingError = potentialRoundingError;
157 TriCubicSpline.potentialRoundingError(potentialRoundingError);
158 BiCubicSpline.potentialRoundingError(potentialRoundingError);
159 CubicSpline.potentialRoundingError(potentialRoundingError);
160 }
161
162 // Reset the default handing of identical abscissae with different ordinates
163 // from the default option of separating the two relevant abscissae by 0.001 of the range
164 // to avraging the relevant ordinates
165 public void averageIdenticalAbscissae(){
166 this.averageIdenticalAbscissae = true;
167 for(int i=0; i<this.tcsn.length; i++)this.tcsn[i].averageIdenticalAbscissae();
168 this.csm.averageIdenticalAbscissae();
169 }
170
171 // Get minimum limits
172 public double[] getXmin(){
173 return this.xMin;
174 }
175
176 // Get maximum limits
177 public double[] getXmax(){
178 return this.xMax;
179 }
180
181 // Get limits to x
182 public double[] getLimits(){
183 double[] limits = {xMin[0], xMax[0], xMin[1], xMax[1], xMin[2], xMax[2], xMin[3], xMax[3]};
184 return limits;
185 }
186
187 // Display limits to x
188 public void displayLimits(){
189 System.out.println(" ");
190 for(int i=0; i<2; i++){
191 System.out.println("The limits to the x array " + i + " are " + xMin[i] + " and " + xMax[i]);
192 }
193 System.out.println(" ");
194 }
195
196 // Returns an interpolated value of y for values of x1, x2, x3 and x4
197 // from a tabulated function y=f(x1,x2,x3,x4)
198 public double interpolate(double xx1, double xx2, double xx3, double xx4){
199
200
201 double[] yTempm = new double[nPoints];
202 for (int i=0;i<nPoints;i++){
203 yTempm[i]=this.tcsn[i].interpolate(xx2, xx3, xx4);
204 }
205 this.csm.resetData(x1,yTempm);
206 return this.csm.interpolate(xx1);
207 }
208
209
210 // Get inner matrix of derivatives
211 // Primarily used by PolyCubicSpline
212 public double[][][][] getDeriv(){
213 return this.d2ydx2inner;
214 }
215
216 // Set inner matrix of derivatives
217 // Primarily used by PolyCubicSpline
218 public void setDeriv(double[][][][] d2ydx2){
219 this.d2ydx2inner = d2ydx2;
220 this.derivCalculated = true;
221 }
222}
223
Note: See TracBrowser for help on using the repository browser.