1 | /**********************************************************
|
---|
2 | *
|
---|
3 | * BiCubicSplineFirstDerivative.java
|
---|
4 | *
|
---|
5 | * Class for performing an interpolation on the tabulated
|
---|
6 | * function y = f(x1,x2) using a natural bicubic spline
|
---|
7 | * Assumes second derivatives at end points = 0 (natural spine)
|
---|
8 | * Calculates the interpolated value of y and
|
---|
9 | * the first derivatives of y with respect to both x1 and x2
|
---|
10 | *
|
---|
11 | * WRITTEN BY: Dr Michael Thomas Flanagan
|
---|
12 | *
|
---|
13 | * DATE: January 2010 - modified version of BiCubicSpline (2003 - 2008)
|
---|
14 | * UPDATE:
|
---|
15 | *
|
---|
16 | * DOCUMENTATION:
|
---|
17 | * See Michael Thomas Flanagan's Java library on-line web page:
|
---|
18 | * http://www.ee.ucl.ac.uk/~mflanaga/java/BiCubicSplineFirstDerivative.html
|
---|
19 | * http://www.ee.ucl.ac.uk/~mflanaga/java/
|
---|
20 | *
|
---|
21 | * Copyright (c) 2003 - 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, this list of conditions
|
---|
30 | * 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 from the Michael Thomas Flanagan:
|
---|
34 | *
|
---|
35 | * Dr Michael Thomas Flanagan makes no representations about the suitability or fitness of the software for any or for a particular purpose.
|
---|
36 | * Dr Michael Thomas Flanagan shall not be liable for any damages suffered as a result of using, modifying or distributing this software
|
---|
37 | * or its derivatives.
|
---|
38 | *
|
---|
39 | ***************************************************************************************/
|
---|
40 |
|
---|
41 | package agents.anac.y2015.agentBuyogV2.flanagan.interpolation;
|
---|
42 |
|
---|
43 | import agents.anac.y2015.agentBuyogV2.flanagan.math.Fmath;
|
---|
44 |
|
---|
45 | public class BiCubicSplineFirstDerivative{
|
---|
46 |
|
---|
47 | private int nPoints = 0; // no. of x1 tabulated points
|
---|
48 | private int mPoints = 0; // no. of x2 tabulated points
|
---|
49 | private double[][] y = null; // y=f(x1,x2) tabulated function
|
---|
50 | private double[][] yTranspose = null; // transposed tabulated function, y=f(x2,x1)
|
---|
51 | private double[] x1 = null; // x1 in tabulated function f(x1,x2)
|
---|
52 | private double[] x2 = null; // x2 in tabulated function f(x1,x2)
|
---|
53 | private double[] xMin = new double[2]; // minimum values of x1 and x2
|
---|
54 | private double[] xMax = new double[2]; // maximum values of x1 and x2
|
---|
55 | private BiCubicSplinePartialDerivative cspdY = null; // BiCubicSplinePartialDerivative instance for entered data
|
---|
56 | private BiCubicSplinePartialDerivative cspdYt = null; // BiCubicSplinePartialDerivative instance for transposed data
|
---|
57 | private boolean averageIdenticalAbscissae = false; // if true: the the ordinate values for identical abscissae are averaged
|
---|
58 | // if false: the abscissae values are separated by 0.001 of the total abscissae range;
|
---|
59 | private static double potentialRoundingError = 5e-15; // potential rounding error used in checking wheter a value lies within the interpolation bounds (static value)
|
---|
60 | 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)
|
---|
61 |
|
---|
62 |
|
---|
63 | // Constructor
|
---|
64 | // Constructor with data arrays initialised to arrays x and y
|
---|
65 | public BiCubicSplineFirstDerivative(double[] x1, double[] x2, double[][] y){
|
---|
66 |
|
---|
67 |
|
---|
68 | this.nPoints=x1.length;
|
---|
69 | this.mPoints=x2.length;
|
---|
70 | if(this.nPoints!=y.length)throw new IllegalArgumentException("Arrays x1 and y-row are of different length " + this.nPoints + " " + y.length);
|
---|
71 | if(this.mPoints!=y[0].length)throw new IllegalArgumentException("Arrays x2 and y-column are of different length "+ this.mPoints + " " + y[0].length);
|
---|
72 | if(this.nPoints<3 || this.mPoints<3)throw new IllegalArgumentException("The data matrix must have a minimum size of 3 X 3");
|
---|
73 |
|
---|
74 | this.x1 = new double[this.nPoints];
|
---|
75 | this.x2 = new double[this.mPoints];
|
---|
76 | this.y = new double[this.nPoints][this.mPoints];
|
---|
77 | this.yTranspose = new double[this.mPoints][this.nPoints];
|
---|
78 | for(int i=0; i<this.nPoints; i++){
|
---|
79 | this.x1[i]=x1[i];
|
---|
80 | }
|
---|
81 | this.xMin[0] = Fmath.minimum(this.x1);
|
---|
82 | this.xMax[0] = Fmath.maximum(this.x1);
|
---|
83 | for(int j=0; j<this.mPoints; j++){
|
---|
84 | this.x2[j]=x2[j];
|
---|
85 | }
|
---|
86 | this.xMin[1] = Fmath.minimum(this.x2);
|
---|
87 | this.xMax[1] = Fmath.maximum(this.x2);
|
---|
88 | for(int i =0; i<this.nPoints; i++){
|
---|
89 | for(int j=0; j<this.mPoints; j++){
|
---|
90 | this.y[i][j]=y[i][j];
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | // Transpose
|
---|
95 | for(int i =0; i<this.nPoints; i++){
|
---|
96 | for(int j=0; j<this.mPoints; j++){
|
---|
97 | this.yTranspose[j][i] = this.y[i][j];
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 | // Instantiate CubicSplinePartialDerivative for both y and yTranspose
|
---|
102 | this.cspdY = new BiCubicSplinePartialDerivative(x1, x2, y);
|
---|
103 | this.cspdYt = new BiCubicSplinePartialDerivative(x2, x1, yTranspose);
|
---|
104 |
|
---|
105 | }
|
---|
106 |
|
---|
107 | // METHODS
|
---|
108 |
|
---|
109 | // Reset rounding error check option
|
---|
110 | // Default option: points outside the interpolation bounds by less than the potential rounding error rounded to the bounds limit
|
---|
111 | // This method causes this check to be ignored and an exception to be thrown if any point lies outside the interpolation bounds
|
---|
112 | public static void noRoundingErrorCheck(){
|
---|
113 | BiCubicSplineFirstDerivative.roundingCheck = false;
|
---|
114 | CubicSpline.noRoundingErrorCheck();
|
---|
115 | }
|
---|
116 |
|
---|
117 | // Reset potential rounding error value
|
---|
118 | // Default option: points outside the interpolation bounds by less than the potential rounding error rounded to the bounds limit
|
---|
119 | // The default value for the potential rounding error is 5e-15*times the 10^exponent of the value outside the bounds
|
---|
120 | // This method allows the 5e-15 to be reset
|
---|
121 | public static void potentialRoundingError(double potentialRoundingError){
|
---|
122 | BiCubicSplineFirstDerivative.potentialRoundingError = potentialRoundingError;
|
---|
123 | CubicSpline.potentialRoundingError(potentialRoundingError);
|
---|
124 | }
|
---|
125 |
|
---|
126 | // Reset the default handing of identical abscissae with different ordinates
|
---|
127 | // from the default option of separating the two relevant abscissae by 0.001 of the range
|
---|
128 | // to avraging the relevant ordinates
|
---|
129 | public void averageIdenticalAbscissae(){
|
---|
130 | this.averageIdenticalAbscissae = true;
|
---|
131 | this.cspdY.averageIdenticalAbscissae();
|
---|
132 | this.cspdYt.averageIdenticalAbscissae();
|
---|
133 | }
|
---|
134 |
|
---|
135 | // Get minimum limits
|
---|
136 | public double[] getXmin(){
|
---|
137 | return this.xMin;
|
---|
138 | }
|
---|
139 |
|
---|
140 | // Get maximum limits
|
---|
141 | public double[] getXmax(){
|
---|
142 | return this.xMax;
|
---|
143 | }
|
---|
144 |
|
---|
145 | // Get limits to x
|
---|
146 | public double[] getLimits(){
|
---|
147 | double[] limits = {xMin[0], xMax[0], xMin[1], xMax[1]};
|
---|
148 | return limits;
|
---|
149 | }
|
---|
150 |
|
---|
151 | // Display limits to x
|
---|
152 | public void displayLimits(){
|
---|
153 | System.out.println(" ");
|
---|
154 | for(int i=0; i<2; i++){
|
---|
155 | System.out.println("The limits to the x array " + i + " are " + xMin[i] + " and " + xMax[i]);
|
---|
156 | }
|
---|
157 | System.out.println(" ");
|
---|
158 | }
|
---|
159 |
|
---|
160 |
|
---|
161 | // Returns an interpolated value of y for a value of x
|
---|
162 | // from a tabulated function y=f(x1,x2)
|
---|
163 | public double[] interpolate(double xx1, double xx2){
|
---|
164 |
|
---|
165 | double[] interpY = cspdY.interpolate(xx1, xx2);
|
---|
166 | double[] interpYt = cspdYt.interpolate(xx2, xx1);
|
---|
167 | double averageY = (interpY[0] + interpYt[0])/2.0;
|
---|
168 | double[] ret = {averageY, interpY[1], interpYt[1], interpY[0], interpYt[0]};
|
---|
169 |
|
---|
170 | return ret;
|
---|
171 | }
|
---|
172 | }
|
---|
173 |
|
---|