lcsim/test/org/lcsim/math/interpolation
diff -u -r1.1 -r1.2
--- BilinearInterpolatorTest.java 4 Jun 2008 05:16:39 -0000 1.1
+++ BilinearInterpolatorTest.java 6 Jun 2008 07:16:32 -0000 1.2
@@ -4,7 +4,7 @@
*
* Created on June 3, 2008, 4:06 PM
*
- * $Id: BilinearInterpolatorTest.java,v 1.1 2008/06/04 05:16:39 ngraf Exp $
+ * $Id: BilinearInterpolatorTest.java,v 1.2 2008/06/06 07:16:32 ngraf Exp $
*/
package org.lcsim.math.interpolation;
@@ -45,6 +45,7 @@
for(int j=0; j<b.length; ++j)
{
// System.out.println("a= "+a[i]+", b= "+b[j]+" , interpolates to "+instance.interpolateValueAt(a[i],b[j]));
+// System.out.println("should be "+vals[i][j]);
assertEquals(instance.interpolateValueAt(a[i],b[j]), vals[i][j]);
}
}
@@ -53,9 +54,12 @@
{
for(double j=b[0]; j<b[b.length-1]; j+=.2)
{
-// System.out.format("( %4.2f , %4.2f ) interpolates to %4.2f%n",i, j, instance.interpolateValueAt(i,j));
- assertEquals(i*i+j*j, instance.interpolateValueAt(i,j),.0001);
+ double pred = instance.interpolateValueAt(i,j);
+ double real = i*i+j*j;
+// System.out.format("( %4.2f , %4.2f ) interpolates to %4.2f%n",i, j, pred);
+// System.out.println("should be "+real);
+ assertEquals(real, pred ,.5);
}
}
- }
+ }
}
\ No newline at end of file
lcsim/src/org/lcsim/math/interpolation
diff -u -r1.1 -r1.2
--- BilinearInterpolator.java 4 Jun 2008 05:16:09 -0000 1.1
+++ BilinearInterpolator.java 6 Jun 2008 07:16:32 -0000 1.2
@@ -3,7 +3,7 @@
*
* Created on June 3, 2008, 4:04 PM
*
- * $Id: BilinearInterpolator.java,v 1.1 2008/06/04 05:16:09 ngraf Exp $
+ * $Id: BilinearInterpolator.java,v 1.2 2008/06/06 07:16:32 ngraf Exp $
*/
//
package org.lcsim.math.interpolation;
@@ -11,7 +11,7 @@
import static java.lang.Math.abs;
/**
- * A class to provide interpolated values for values determined at discrete points
+ * A class to provide interpolated values for values determined at discrete points
* on a 2D grid
*
* @author Norman Graf
@@ -19,7 +19,13 @@
public class BilinearInterpolator
{
private double[] _x;
+ private double _xmin;
+ private double _xmax;
+ private int _xDim;
private double[] _y;
+ private double _ymin;
+ private double _ymax;
+ private int _yDim;
private double[][] _val;
/**
* Creates a new instance of BilinearInterpolator
@@ -29,9 +35,25 @@
*/
public BilinearInterpolator(double[] x, double[] y, double[][] z)
{
- _x = x;
- _y = y;
- _val = z;
+ _x = new double[x.length];;
+ System.arraycopy(x,0,_x,0, x.length);
+ _xDim = _x.length;
+ _xmin = _x[0];
+ _xmax = _x[_xDim-1];
+
+ _y = new double[y.length];
+ System.arraycopy(y,0,_y,0, y.length);
+ _yDim = _y.length;
+ _ymin = _y[0];
+ _ymax = _y[_yDim-1];
+
+
+
+ _val = new double[z.length][z[0].length];
+ for(int i=0; i< z.length; ++i)
+ {
+ System.arraycopy(z[i],0,_val[i],0,z[i].length);
+ }
}
//TODO add protection for values out of range
@@ -43,7 +65,8 @@
*/
public double interpolateValueAt(double x, double y)
{
- return polin2(_x, _y, _val, x, y);
+ return trueBilinear( x, y );
+// return polin2(_x, _y, _val, x, y);
}
/*
@@ -82,7 +105,7 @@
double dy;
double[] c = new double[n];
double[] d = new double[n];
- dif=abs(x-xa[1]);
+ dif=abs(x-xa[0]);
for (int i=0;i<n;++i)
{ //Here we find the index ns of the closest table entry,
@@ -95,8 +118,8 @@
c[i]=ya[i]; // and initialize the table of c's and d's.
d[i]=ya[i];
}
- double y = ya[ns]; // This is the initial approximation to y.
- ns = ns-1;
+ double y = ya[ns--]; // This is the initial approximation to y.
+// ns = ns-1;
for (int m=1; m<n ; ++m)
{
//For each column of the table,
@@ -131,4 +154,42 @@
}
return y;
}
+
+ double trueBilinear(double x, double y)
+ {
+ // find bin for x
+ int ixlo = 0;
+ int ixhi = 0;
+ for(int i=0; i<_xDim-1; ++i)
+ {
+ if(x>=_x[i] && x<=_x[i+1])
+ {
+ ixlo = i;
+ ixhi = i+1;
+ break;
+ }
+ }
+
+ // find bin for y
+ int iylo = 0;
+ int iyhi = 0;
+ for(int i=0; i<_yDim-1; ++i)
+ {
+ if(y>=_y[i] && y<=_y[i+1])
+ {
+ iylo = i;
+ iyhi = i+1;
+ break;
+ }
+ }
+ double v1 = _val[ixlo][iylo];
+ double v2 = _val[ixhi][iylo];
+ double v3 = _val[ixhi][iyhi];
+ double v4 = _val[ixlo][iyhi];
+
+ double t = (x - _x[ixlo])/(_x[ixhi] - _x[ixlo]);
+ double u = (y - _y[iylo])/(_y[iyhi] - _y[iylo]);
+
+ return (1-t)*(1-u)*v1 +t*(1-u)*v2+t*u*v3+(1-t)*u*v4;
+ }
}
\ No newline at end of file