View Javadoc

1   /*
2    * BilinearInterpolatorTest.java
3    * JUnit based test
4    *
5    * Created on June 3, 2008, 4:06 PM
6    *
7    * $Id: BilinearInterpolatorTest.java,v 1.1 2010/12/01 01:25:26 jeremy Exp $
8    */
9   
10  package org.lcsim.math.interpolation;
11  
12  import junit.framework.*;
13  
14  /**
15   *
16   * @author Norman Graf
17   */
18  public class BilinearInterpolatorTest extends TestCase
19  {
20      
21      /**
22       * Test of interpolateValueAt method, of class org.lcsim.math.BilinearInterpolator.
23       */
24      public void testInterpolateValueAt()
25      {
26          double[] a = {-2, -1, 0, 1, 2};
27          double[] b = {-2, -1, 0, 1, 2};
28          double[][] vals = new double[a.length][b.length];
29          
30          for(int i=0; i<a.length; ++i)
31          {
32              for(int j =0; j<b.length; ++j)
33              {
34                  vals[i][j] = a[i]*a[i]+b[j]*b[j];
35              }
36          }
37          
38          BilinearInterpolator instance = new BilinearInterpolator(a, b, vals);
39          
40          double x = 0.0;
41          double y = 0.0;
42          
43          for(int i=0; i<a.length; ++i)
44          {
45              for(int j=0; j<b.length; ++j)
46              {
47  //                System.out.println("a= "+a[i]+", b= "+b[j]+" ,  interpolates to "+instance.interpolateValueAt(a[i],b[j]));
48  //                System.out.println("should be "+vals[i][j]);
49                  assertEquals(instance.interpolateValueAt(a[i],b[j]), vals[i][j]);
50              }
51          }
52          
53          for(double i=a[0]; i<a[a.length-1]; i+=.2)
54          {
55              for(double j=b[0]; j<b[b.length-1]; j+=.2)
56              {
57                  double pred = instance.interpolateValueAt(i,j);
58                  double real = i*i+j*j;
59  //                System.out.format("( %4.2f , %4.2f ) interpolates to %4.2f%n",i, j, pred);
60  //                System.out.println("should be "+real);
61                  assertEquals(real, pred ,.5);
62              }
63          }
64      }
65  }