View Javadoc

1   /*
2    * CovarianceMatrixTransformer.java
3    *
4    * Created on March 30, 2006, 3:35 PM
5    *
6    * $Id: CovarianceMatrixTransformer.java,v 1.1.1.1 2010/11/30 21:32:00 jeremy Exp $
7    */
8   
9   package org.lcsim.math.coordinatetransform;
10  import static java.lang.Math.sqrt;
11  import static java.lang.Math.cos;
12  import static java.lang.Math.sin;
13  
14  /**
15   * Utility class to handle transformations of covariance matrices
16   * Inspired by Nick Sinev's CMTransform.java.
17   * @author Norman Graf
18   */
19  public class CovarianceMatrixTransformer
20  {
21      
22      /** No need to create, all methods static*/
23      private CovarianceMatrixTransformer()
24      {
25      }
26      
27      /**
28       * Convert covariance matrix elements in cartesian coordinates (x,y) to cylindrical (r,phi).
29       * @param x The cartesian x coordinate.
30       * @param y The cartesian y coordinate.
31       * @param sxx The covariance matrix term for x at (x,y).
32       * @param syy The covariance matrix term for y at (x,y).
33       * @param sxy The covariance matrix term for xy at (x,y).
34       * @return The packed r-phi covariance matrix terms:
35       * <table>
36       * <tr><td> cov[0] </td><td> r-r </td><tr>
37       * <tr><td> cov[1] </td><td> phi-phi </td><tr>
38       * <tr><td> cov[2] </td><td> r-phi</td><tr>
39       * </table>
40       */
41      public static double[] xy2rphi(double x, double y, double sxx, double syy, double sxy)
42      {
43          double[] cov = new double[3];
44          double xx = x*x;
45          double xy = x*y;
46          double yy = y*y;
47          double rr = xx+yy;
48          double r = sqrt(rr);
49          double oneOverR2 = 1/rr;
50          
51          // srr
52          cov[0] = oneOverR2*(sxx*xx + syy*yy + 2.*sxy*xy);
53          
54          // sphiphi
55          cov[1] = oneOverR2*oneOverR2*(sxx*yy - 2.*sxy*xy + syy*xx);
56          
57          // srphi
58          cov[2] = oneOverR2*(-sxx*xy + sxy*(xx-yy) + syy*xy)/r;
59          
60          return cov;
61      }
62      
63      /**
64       *Convert covariance matrix elements in cylindrical (r,phi) to cartesian coordinates (x,y).
65       * @param r The cylindrical radius.
66       * @param phi The cylindrical angle.
67       * @param srr The covariance matrix term for r at (r, phi).
68       * @param sff The covariance matrix term for phi at (r, phi).
69       * @param srf The covariance matrix term for r-phi at (r, phi).
70       * @return The packed x-y covariance matrix terms:
71       * <table>
72       * <tr><td> cov[0] </td><td> x-x </td><tr>
73       * <tr><td> cov[1] </td><td> y-y </td><tr>
74       * <tr><td> cov[2] </td><td> x-y</td><tr>
75       * </table>
76       */
77       
78      public static double[] rphi2xy(double r, double phi, double srr, double sff, double srf)
79      {
80          double[] cov = new double[3];
81          // cosine^2(phi)
82          double cf = cos(phi);
83          double cc = cf*cf;
84          //sine^2(phi)
85          double sf = sin(phi);
86          double ss = sf*sf;
87          
88          // cosine(phi)*sine(phi)
89          double cs = cf*sf;
90          
91          // sxx
92          cov[0] = srr*cc - 2.*srf*r*cs + sff*r*r*ss;
93          
94          // syy
95          cov[1] = srr*ss + 2.*srf*r*cs + sff*r*r*cc;
96          
97          // sxy
98          cov[2] = srr*cs + srf*r*(cc - ss) - sff*r*r*cs;
99          
100         return cov;
101     }
102 }