Commit in lcsim-math/src/test/java/org/lcsim/math on MAIN
coordinatetransform/CovarianceMatrixTransformerTest.java+113added 1.1
interpolation/BilinearInterpolatorTest.java+65added 1.1
moments/CentralMomentsCalculatorTest.java+130added 1.1
probability/BivariateDistributionTest.java+87added 1.1
           /ErfTest.java+26added 1.1
+421
5 added files
add lcsim-math tests

lcsim-math/src/test/java/org/lcsim/math/coordinatetransform
CovarianceMatrixTransformerTest.java added at 1.1
diff -N CovarianceMatrixTransformerTest.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ CovarianceMatrixTransformerTest.java	1 Dec 2010 01:25:26 -0000	1.1
@@ -0,0 +1,113 @@
+/*
+ * CovarianceMatrixTransformerTest.java
+ *
+ * Created on March 30, 2006, 3:57 PM
+ *
+ * $Id: CovarianceMatrixTransformerTest.java,v 1.1 2010/12/01 01:25:26 jeremy Exp $
+ */
+
+package org.lcsim.math.coordinatetransform;
+
+import junit.framework.TestCase;
+import static java.lang.Math.atan2;
+import static java.lang.Math.sqrt;
+import static java.lang.Math.PI;
+import static java.lang.Math.cos;
+import static java.lang.Math.sin;
+/**
+ *
+ * @author Norman Graf
+ */
+public class CovarianceMatrixTransformerTest extends TestCase
+{
+    
+    /** Creates a new instance of CovarianceMatrixTransformerTest */
+    public  void testCovarianceMatrixTransformer()
+    {
+        double eps = 1E-8;
+        double x = 1.0;
+        double y = 0.0;
+        double sx = .1;
+        double sy = 0.;
+        double sxy = 0.;
+        
+        double[] cov = CovarianceMatrixTransformer.xy2rphi(x, y, sx*sx, sy*sy, sxy);
+//        System.out.println("srr= "+cov[0]);
+//        System.out.println("sphiphi= "+cov[1]);
+//        System.out.println("srphi= "+cov[2]);
+        
+        assertEquals(cov[0], sx*sx, eps);
+        assertEquals(cov[1], sy*sy, eps);
+        assertEquals(cov[2], sxy, eps);
+        
+//        System.out.println(" ");
+        sx = 0.;
+        sy = .1;
+        cov = CovarianceMatrixTransformer.xy2rphi(x, y, sx*sx, sy*sy, sxy);
+//        System.out.println("srr= "+cov[0]);
+//        System.out.println("sphiphi= "+cov[1]);
+//        System.out.println("srphi= "+cov[2]);
+        
+        assertEquals(cov[0], sx*sx, eps);
+        assertEquals(cov[1], sy*sy, eps);
+        assertEquals(cov[2], sxy, eps);
+        
+//        System.out.println(" ");
+        x = .5;
+        y = .5;
+        sx = .1;
+        sy = .1;
+        sxy = -.1;
+        cov = CovarianceMatrixTransformer.xy2rphi(x, y, sx*sx, sy*sy, sxy);
+//        System.out.println("srr= "+cov[0]);
+//        System.out.println("sphiphi= "+cov[1]);
+//        System.out.println("srphi= "+cov[2]);
+        
+        // now let's round-trip...
+        double srr = cov[0];
+        double sphiphi = cov[1];
+        double srphi = cov[2];
+        double r = sqrt(x*x+y*y);
+        double phi = atan2(y,x);
+        
+        
+        cov = CovarianceMatrixTransformer.rphi2xy(r, phi, srr, sphiphi, srphi);
+//        for(int i=0; i<3; ++i) System.out.println("cov["+i+"]= "+cov[i]);
+        
+        
+        assertEquals(sqrt(cov[0]), sx, eps);
+        assertEquals(sqrt(cov[1]), sy, eps);
+        assertEquals(cov[2], sxy, eps);
+        
+        
+//        System.out.println(" ");
+        // start from rPhi...
+        r = 1.;
+        double dr = 0.;
+        phi= PI/4.;
+        double dPhi = .1;
+        srr = dr*dr;
+        sphiphi = dPhi*dPhi;
+        srphi = 0.;
+        
+        cov = CovarianceMatrixTransformer.rphi2xy(r, phi, srr, sphiphi, srphi);
+//        for(int i=0; i<3; ++i) System.out.println("cov["+i+"]= "+cov[i]);
+        
+        
+        // dx should equal dy
+        assertEquals(cov[0], cov[1], eps);
+        // cov term should be neg
+        assertTrue(cov[2]<0);
+        // normalized cov term should be -1
+        assertEquals(cov[2]/cov[0], -1, eps);
+        
+        // now round-trip...
+        x = r*cos(phi);
+        y = r*sin(phi);
+        cov = CovarianceMatrixTransformer.xy2rphi(x, y, cov[0], cov[1], cov[2]);
+//        for(int i=0; i<3; ++i) System.out.println("cov["+i+"]= "+cov[i]);
+        assertEquals(cov[0], srr, eps);
+        assertEquals(cov[1],sphiphi, eps);
+        assertEquals(cov[2], srphi, eps);
+    }
+}

lcsim-math/src/test/java/org/lcsim/math/interpolation
BilinearInterpolatorTest.java added at 1.1
diff -N BilinearInterpolatorTest.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ BilinearInterpolatorTest.java	1 Dec 2010 01:25:26 -0000	1.1
@@ -0,0 +1,65 @@
+/*
+ * BilinearInterpolatorTest.java
+ * JUnit based test
+ *
+ * Created on June 3, 2008, 4:06 PM
+ *
+ * $Id: BilinearInterpolatorTest.java,v 1.1 2010/12/01 01:25:26 jeremy Exp $
+ */
+
+package org.lcsim.math.interpolation;
+
+import junit.framework.*;
+
+/**
+ *
+ * @author Norman Graf
+ */
+public class BilinearInterpolatorTest extends TestCase
+{
+    
+    /**
+     * Test of interpolateValueAt method, of class org.lcsim.math.BilinearInterpolator.
+     */
+    public void testInterpolateValueAt()
+    {
+        double[] a = {-2, -1, 0, 1, 2};
+        double[] b = {-2, -1, 0, 1, 2};
+        double[][] vals = new double[a.length][b.length];
+        
+        for(int i=0; i<a.length; ++i)
+        {
+            for(int j =0; j<b.length; ++j)
+            {
+                vals[i][j] = a[i]*a[i]+b[j]*b[j];
+            }
+        }
+        
+        BilinearInterpolator instance = new BilinearInterpolator(a, b, vals);
+        
+        double x = 0.0;
+        double y = 0.0;
+        
+        for(int i=0; i<a.length; ++i)
+        {
+            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]);
+            }
+        }
+        
+        for(double i=a[0]; i<a[a.length-1]; i+=.2)
+        {
+            for(double j=b[0]; j<b[b.length-1]; j+=.2)
+            {
+                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-math/src/test/java/org/lcsim/math/moments
CentralMomentsCalculatorTest.java added at 1.1
diff -N CentralMomentsCalculatorTest.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ CentralMomentsCalculatorTest.java	1 Dec 2010 01:25:26 -0000	1.1
@@ -0,0 +1,130 @@
+/*
+ * CentralMomentsCalculatorTest.java
+ *
+ * Created on April 5, 2006, 1:41 PM
+ *
+ * $Id: CentralMomentsCalculatorTest.java,v 1.1 2010/12/01 01:25:26 jeremy Exp $
+ */
+
+package org.lcsim.math.moments;
+
+import java.util.Random;
+import junit.framework.TestCase;
+import org.lcsim.spacegeom.CartesianPoint;
+import org.lcsim.spacegeom.SpacePoint;
+
+import static java.lang.Math.PI;
+import static java.lang.Math.sin;
+import static java.lang.Math.cos;
+
+/**
+ *
+ * @author Norman Graf
+ */
+public class CentralMomentsCalculatorTest extends TestCase
+{
+    
+    /** Creates a new instance of CentralMomentsCalculatorTest */
+    public void testCentralMomentsCalculator()
+    {
+        double[] x = {0., 1., 2.};
+        double[] y = {0., 0., 0.};
+        double[] z = {0., 0., 0.};
+        double[] w = {1., 1., 1.};
+        
+        CentralMomentsCalculator mc = new CentralMomentsCalculator();
+        
+        mc.calculateMoments(x, y, z, w);
+        
+        double[] c = mc.centroid();
+        assertEquals(c[0],1.);
+        assertEquals(c[1],0.);
+        assertEquals(c[2],0.);
+        
+        w[2] = 2.;
+        mc.calculateMoments(x, y, z, w);
+        SpacePoint p = new CartesianPoint(mc.centroid());
+//        System.out.println(p);
+        
+        int nPoints = 1000;
+        x = new double[nPoints];
+        y = new double[nPoints];
+        z = new double[nPoints];
+        w = new double[nPoints];
+        
+        // try a sphere
+        // a = b = c = 10.
+//        System.out.println("");
+//        System.out.println("");
+//        System.out.println("Sphere");
+        generateEvents(10., 10., 10., nPoints, x, y, z, w);
+        mc.calculateMoments(x, y, z, w);
+        p = new CartesianPoint(mc.centroid());
+//        System.out.println(p);
+        double[] inv = mc.invariants();
+//        System.out.println("inv: "+inv[0]+" "+inv[1]+" "+inv[2]);
+        
+        
+        // try an ellipsoid
+        // a = 5
+        // b = 17
+        // c = 32
+//        System.out.println("");
+//        System.out.println("");
+//        System.out.println("Ellipsoid");
+        generateEvents(5., 17., 32., nPoints, x, y, z, w);
+        mc.calculateMoments(x, y, z, w);
+        p = new CartesianPoint(mc.centroid());
+//        System.out.println(p);
+        inv = mc.invariants();
+//        System.out.println("inv: "+inv[0]+" "+inv[1]+" "+inv[2]);
+        
+        // try a cigar
+        // a = 5
+        // b = 5
+        // c = 32
+//        System.out.println("");
+//        System.out.println("");
+//        System.out.println("Cigar");
+        generateEvents(5., 5., 32., nPoints, x, y, z, w);
+        mc.calculateMoments(x, y, z, w);
+        p = new CartesianPoint(mc.centroid());
+//        System.out.println(p);
+        inv = mc.invariants();
+//        System.out.println("inv: "+inv[0]+" "+inv[1]+" "+inv[2]);        
+        
+        // try a plate
+        // a = 5
+        // b = 20
+        // c = 20
+//        System.out.println("");
+//        System.out.println("");
+//        System.out.println("Plate");
+        generateEvents(5., 20., 20., nPoints, x, y, z, w);
+        mc.calculateMoments(x, y, z, w);
+        p = new CartesianPoint(mc.centroid());
+//        System.out.println(p);
+        inv = mc.invariants();
+//        System.out.println("inv: "+inv[0]+" "+inv[1]+" "+inv[2]);        
+        
+    }
+    
+    // generate nPoints events according to the ellipsoid equation:
+    // x = a cos(phi) sin(theta)
+    // y = b sin(phi) sin(theta)
+    // z = c cos(theta)
+    //
+    void generateEvents(double a, double b, double c, int nPoints, double[] x, double[] y, double[] z, double[] w)
+    {
+        Random r = new Random();
+        for(int i=0; i<nPoints; ++i)
+        {
+            double t = PI*r.nextDouble();
+            double p = 2.*PI*r.nextDouble();
+            x[i] = a*cos(p)*sin(t);
+            y[i] = b*sin(p)*sin(t);
+            z[i] = c*cos(t);
+            w[i] = 1.;
+        }
+    }
+}

lcsim-math/src/test/java/org/lcsim/math/probability
BivariateDistributionTest.java added at 1.1
diff -N BivariateDistributionTest.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ BivariateDistributionTest.java	1 Dec 2010 01:25:26 -0000	1.1
@@ -0,0 +1,87 @@
+/*
+ * BivariateDistributionTest class
+ */
+package org.lcsim.math.probability;
+
+import junit.framework.TestCase;
+
+/**
+ * Test case for BivariateDistribution class
+ *
+ * @author Richard Partridge
+ */
+public class BivariateDistributionTest extends TestCase {
+
+    /** Creates a new instance of HelicalTrackFitterTest */
+    public void testBivariateDistribution() {
+
+        //  Instantiate the BivariateDistribution class
+        BivariateDistribution b = new BivariateDistribution();
+
+        //  Set up the x coordinate binning
+        int nx = 140;
+        double dx = 0.1;
+        double xmin = -0.5 * nx * dx;
+        b.xBins(nx, xmin, dx);
+
+        //  Set up the y coordinate binning
+        int ny = 140;
+        double dy = 0.1;
+        double ymin = -0.5 * ny * dy;
+        b.yBins(ny, ymin, dy);
+
+        //  Set the bivariate Gaussian parameters to some semi-random values
+        double x0 = 0.526;
+        double y0 = -0.317;
+        double sigx0 = 0.642;
+        double sigy0 = 0.784;
+        double rho0 = 0.231;
+
+        //  Calculate the bivariate probabilities for our x-y bins
+        double[][] bi = b.Calculate(x0, y0, sigx0, sigy0, rho0);
+
+        //  Now calculate our parameter estimates from the binned data
+        double xave = 0.;
+        double yave = 0.;
+        double xysum = 0.;
+        double xxsum = 0.;
+        double yysum = 0.;
+        double psum = 0.;
+        for (int i = 0; i < nx; i++) {
+            for (int j = 0; j < ny; j++) {
+                double x = xmin + dx * (i + 0.5);
+                double y = ymin + dy * (j + 0.5);
+                double prob = bi[i][j];
+                xave += prob * x;
+                yave += prob * y;
+                xxsum += prob * x * x;
+                yysum += prob * y * y;
+                xysum += prob * x * y;
+                psum += prob;
+            }
+        }
+
+        //  Calculate the measured error matrix
+        double sigx = Math.sqrt(xxsum - xave * xave);
+        double sigy = Math.sqrt(yysum - yave * yave);
+        double rho = (xysum - xave * yave) / (sigx * sigy);
+
+        System.out.println(" x ave: " + xave + " y ave: " + yave);
+        System.out.println(" x sd: " + sigx + " y sd: " + sigy + " rho: " + rho);
+        System.out.println("PSum: " + psum);
+
+        //  Test that probability is conserved - this is the key test that
+        //  the method is working.  Estimate a 5 sigma round-off error from
+        //  summing nx*ny bins assuming 1e-16 precision per bin.
+        assertEquals("Probability sum failure", 1.0, psum, 5.0e-16*Math.sqrt(nx*ny));
+
+        //  Make some crude tests that the Gaussian parameters are reasonable
+        //  These are not precisely measured due to the coarse binning
+        assertEquals("x ave failure", x0, xave, 1e-10);
+        assertEquals("y ave failure", y0, yave, 1e-10);
+        assertEquals("sig x failure", sigx0, sigx, 2e-3);
+        assertEquals("sig y failure", sigy0, sigy, 2e-3);
+        assertEquals("rho failure", rho0, rho, 2e-3);
+
+    }
+}
\ No newline at end of file

lcsim-math/src/test/java/org/lcsim/math/probability
ErfTest.java added at 1.1
diff -N ErfTest.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ ErfTest.java	1 Dec 2010 01:25:26 -0000	1.1
@@ -0,0 +1,26 @@
+/*
+ * ErfTest Class
+ */
+
+package org.lcsim.math.probability;
+
+import junit.framework.TestCase;
+
+/**
+ *  Test case for Erf methods
+ *
+ * @author partridge
+ */
+public class ErfTest extends TestCase {
+
+    public void testErf() {
+
+        //  Value of erf(1) from Abramowitz and Steigun
+        double erf1 = 0.8427007929;
+        double root2 = Math.sqrt(2.);
+        assertEquals("Erf", erf1, Erf.erf(1.), 1e-10);
+        assertEquals("Erfc", 1. - erf1, Erf.erfc(1.), 1e-10);
+        assertEquals("Phi", 0.5 * (1. + erf1), Erf.phi(root2), 1e-10);
+        assertEquals("PhiC", 0.5 * (1. - erf1), Erf.phic(root2), 1e-10);
+    }
+}
CVSspam 0.2.8