View Javadoc

1   package org.lcsim.recon.tracking.trfbase;
2   
3   import org.lcsim.recon.tracking.trfutil.TRFMath;
4   import Jama.Matrix;
5   
6   /** Class TrackDerivative is the derivative of one track vector with
7    * respect to another; i.e. a non-symmetric 5x5 matrix.
8    *<p>
9    * TrackDerivative should support the following operations:
10   * <pre>
11   * construct: tv(), te(), td() (should fill with zeros)
12   * copy: tv2(tv1), te2(te1), td2(td1)
13   * assignment: tv2 = tv2, te2 = te1, td2 = td1
14   * indexing: tv(i), te(i,j), td(i,j)
15   * minimum element: tv.min(), te.min(), td.min()
16   * maximum element: tv.max(), te.max(), td.max()
17   * absolute minimum element: tv.amin(), te.amin(), td.amin()
18   * absolute maximum element: tv.amax(), te.amax(), td.amax()
19   * addition: tv2 += tv1, tv3 = tv1 + tv2,
20   *           te2 += te1, te3 = tv1 + te2,
21   *           td2 += td1, td3 = td1 + td2,
22   * subtraction: tv2 -= tv1, tv3 = tv1 - tv2,
23   *              te2 -= te1, te3 = tv1 - te2,
24   *              td2 -= td1, td3 = td1 - td2,
25   * inversion: int stat = invert( TrackError& te1 ) (0 for success)
26   * output stream: cout << tv << te << td
27   * equality: tv1==tv2, te1==te2, td1==td2
28   * inequality: tv!=tv2, te1!=te2, td1!=td2
29   * chisquare difference: chisq_diff(tv,te) = tv_T * te * tv
30   * (tv is the difference between two vectors and te is the inverse
31   * of the error matrix.)
32   * transpose: td.transpose()
33   * transform: te.xform(td)
34   * </pre>
35   *
36   * @author Norman A. Graf
37   * @version 1.0
38   */
39  
40  public class TrackDerivative
41  {
42      private Matrix _mat;
43      private int _size;
44      //
45      
46      /**
47       * Default constructor creates 5x5 matrix of zeros
48       *
49       */
50      public TrackDerivative( )
51      {
52          _size = 5;
53          Matrix tmp = Matrix.identity(5,5);
54          _mat = tmp;
55      }
56      
57      //
58      
59      /**
60       * Constructor from array
61       *
62       * @param   mat 5x5 array of doubles.
63       */
64      public TrackDerivative(double[][] mat)
65      {
66          _size = mat.length;
67          for (int i = 0; i < _size; i++)
68          {
69              if (mat[i].length != _size)
70              {
71                  throw new IllegalArgumentException("All rows must have the same length.");
72              }
73          }
74          _mat = new Matrix(mat, _size, _size);
75          
76      }
77      
78      //
79      
80      /**
81       * Constructor from Jama Matrix.
82       *
83       * @param   mat  5x5 Jama Matrix.
84       */
85      public TrackDerivative(Matrix mat)
86      {
87          _size = mat.getColumnDimension();
88          if( _size!= mat.getRowDimension()) throw new IllegalArgumentException("Matrix must be square!");
89          if( _size != 5) throw new IllegalArgumentException("Matrix must be 5x5!");
90          _mat = mat.copy();
91      }
92      
93      //
94      
95      /**
96       * Copy constructor
97       *
98       * @param   td  TrackDerivative to copy.
99       */
100     public TrackDerivative( TrackDerivative td)
101     {
102         _size = td._size;
103         _mat = td._mat.copy();
104     }
105     
106     //
107     
108     /**
109      * Return the underlying matrix.
110      *
111      * @return  5x5 array of doubles.
112      */
113     public double[][] matrix()
114     {
115         return _mat.getArrayCopy();
116     }
117     
118     
119     /**
120      * String representation of TrackDerivative.
121      *
122      * @return String representation of TrackDerivative.
123      */
124     public String toString()
125     {
126         String className = getClass().getName();
127         int lastDot = className.lastIndexOf('.');
128         if(lastDot!=-1)className = className.substring(lastDot+1);
129         
130         StringBuffer sb = new StringBuffer(className+"\n");
131         for(int i=0; i<_size; ++i)
132         {
133             for(int j = 0; j<_size; ++j)
134             {
135                 sb.append(_mat.get(i,j)).append(" ");
136             }
137             sb.append("\n");
138         }
139         return sb.append("\n").toString();
140     }
141     
142     
143     /**
144      * Return the TrackDerivative as a Jama Matrix.
145      *
146      * @return  5x5 Jama Matrix.
147      */
148     public Matrix getMatrix()
149     {
150         return _mat.copy();
151     }
152     
153     
154     /**
155      * Set an element of the TrackDerivative.
156      *
157      * @param   i  Row index.
158      * @param   j  Column index.
159      * @param   val  Value of element (i,j).
160      */
161     public void set(int i, int j, double val)
162     {
163         _mat.set(i,j,val);
164     }
165     
166     
167     /**
168      * Set the elements equal to those of an existing TrackDerivative.
169      *
170      * @param   td  TrackDerivative to be equivalent to.
171      */
172     public void set(TrackDerivative td)
173     {
174         for (int i = 0 ; i < _size ; ++i )
175         {
176             for (int j = 0 ; j < _size ;++j )
177             {
178                 set(i,j,td.get(i,j));
179             }
180         }
181     }
182     
183     
184     /**
185      * Get an element of the TrackDerivative.
186      *
187      * @param   i  Row index.
188      * @param   j  Column index.
189      * @return     Value of element (i,j).
190      */
191     public double get(int i, int j)
192     {
193         return _mat.get(i, j);
194     }
195     
196     
197     /**
198      * Convenience method to set TrackDerivative to the identity.
199      *
200      */
201     public void setIdentity()
202     {
203         _mat = Matrix.identity(_size, _size);
204     }
205     
206     
207     /**
208      * Matrix multiply
209      *
210      * @param   td  TrackDerivative by which to multiply.
211      * @return     <em>this</em> times TrackDerivative td.
212      */
213     public TrackDerivative times(TrackDerivative td)
214     {
215         return new TrackDerivative(_mat.times(td._mat));
216     }
217     
218     
219     
220     /**
221      * Absolute maximum TrackDerivative element.
222      *
223      * @return  absolute value of absolute maximum TrackDerivative element.
224      */
225     public double amax()
226     {
227         double[][] tmp = matrix();
228         double amax = Math.abs(tmp[0][0]);
229         for(int i = 0; i<5; ++i)
230         {
231             for(int j = 0; j<5; ++j)
232             {
233                 if( Math.abs(tmp[i][j])>amax) amax = Math.abs(tmp[i][j]);
234             }
235         }
236         return amax;
237     }
238     
239     
240     /**
241      * Maximum TrackDerivative element.
242      *
243      * @return  Maximum TrackDerivative element.
244      */
245     public double max()
246     {
247         double[][] tmp = matrix();
248         double max = tmp[0][0];
249         for(int i = 0; i<5; ++i)
250         {
251             for(int j = 0; j<5; ++j)
252             {
253                 if( tmp[i][j]>max) max = tmp[i][j];
254             }
255         }
256         return max;
257     }
258     
259     
260     /**
261      * Minimum TrackDerivative element.
262      *
263      * @return  minimum TrackDerivative element.
264      */
265     public double min()
266     {
267         double[][] tmp = matrix();
268         double min = tmp[0][0];
269         for(int i = 0; i<5; ++i)
270         {
271             for(int j = 0; j<5; ++j)
272             {
273                 if( tmp[i][j]<min) min = tmp[i][j];
274             }
275         }
276         return min;
277     }
278     
279     
280     /**
281      *Absolute minimum TrackDerivative element.
282      *
283      * @return  absolute value of absolute minimum TrackDerivative element.
284      */
285     public double amin()
286     {
287         double[][] tmp = matrix();
288         double amin = Math.abs(tmp[0][0]);
289         for(int i = 0; i<5; ++i)
290         {
291             for(int j = 0; j<5; ++j)
292             {
293                 if( Math.abs(tmp[i][j])<amin) amin = Math.abs(tmp[i][j]);
294             }
295         }
296         return amin;
297     }
298     
299     
300     
301     /**
302      * Math function minus.
303      *
304      * @param   td  TrackDerivative to be subtracted.
305      * @return     <em> this </em> minus TrackDerivative td.
306      */
307     public TrackDerivative minus(TrackDerivative td)
308     {
309         return new TrackDerivative(getMatrix().minus(td.getMatrix()));
310     }
311     
312     
313     /**
314      * Math function plus.
315      *
316      * @param   td  TrackDerivative to be added.
317      * @return     <em> this </em> plus TrackDerivative td.
318      */
319     public TrackDerivative plus(TrackDerivative td)
320     {
321         return new TrackDerivative(getMatrix().plus(td.getMatrix()));
322     }
323     
324     
325     /** Equality.
326      *
327      * @return true if TrackDerivatives are equal.
328      * @param td TrackDerivative to compare
329      */
330     public boolean equals( TrackDerivative td)
331     {
332         if( _size != td._size ) return false;
333         for (int i = 0 ;i < _size ; ++i )
334         {
335             for (int j = 0; j < _size ; ++j )
336             {
337                 if( _mat.get(i,j) != td._mat.get(i,j) ) return false;
338             }
339         }
340         return true;
341     }
342     
343     
344     /** Inequality convenience method.
345      *
346      * @return true if TrackDerivatives are <b> not </b> equal.
347      * @param td TrackDerivative to compare
348      */
349     public boolean notEquals(TrackDerivative td)
350     {
351         return !equals(td);
352     }
353     
354     
355     /** Equality within tolerance.
356      *
357      * @return true if TrackDerivatives are equal within tolerance.
358      * @param td TrackDerivative to compare
359      */
360     public boolean isEqual( TrackDerivative td)
361     {
362         if( _size != td._size ) return false;
363         for (int i = 0 ;i < _size ; ++i )
364         {
365             for (int j = 0; j < _size ; ++j )
366             {
367                 if( ! TRFMath.isEqual(_mat.get(i,j),td._mat.get(i,j) ) ) return false;
368             }
369         }
370         return true;
371     }
372     
373     
374     /**
375      * Matrix transpose.
376      * Transposes rows and columns of TrackDerivative in place.
377      */
378     public void transpose()
379     {
380         _mat = _mat.transpose();
381     }
382     
383 }