CLHEP/Matrix/SymMatrix.h

00001 // -*- C++ -*-
00002 // CLASSDOC OFF
00003 // $Id: SymMatrix.h,v 1.16 2002/04/12 15:02:44 evc Exp $
00004 // ---------------------------------------------------------------------------
00005 // CLASSDOC ON
00006 // 
00007 // This file is a part of the CLHEP - a Class Library for High Energy Physics.
00008 // 
00009 // This is the definition of the HepSymMatrix class.
00010 // 
00011 // Copyright Cornell University 1993, 1996, All Rights Reserved.
00012 // 
00013 // This software written by Nobu Katayama and Mike Smyth, Cornell University.
00014 // 
00015 // Redistribution and use in source and binary forms, with or without
00016 // modification, are permitted provided that the following conditions
00017 // are met:
00018 // 1. Redistributions of source code must retain the above copyright
00019 //    notice and author attribution, this list of conditions and the
00020 //    following disclaimer. 
00021 // 2. Redistributions in binary form must reproduce the above copyright
00022 //    notice and author attribution, this list of conditions and the
00023 //    following disclaimer in the documentation and/or other materials
00024 //    provided with the distribution.
00025 // 3. Neither the name of the University nor the names of its contributors
00026 //    may be used to endorse or promote products derived from this software
00027 //    without specific prior written permission.
00028 // 
00029 // Creation of derivative forms of this software for commercial
00030 // utilization may be subject to restriction; written permission may be
00031 // obtained from Cornell University.
00032 // 
00033 // CORNELL MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED.  By way
00034 // of example, but not limitation, CORNELL MAKES NO REPRESENTATIONS OR
00035 // WARRANTIES OF MERCANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT
00036 // THE USE OF THIS SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS,
00037 // COPYRIGHTS, TRADEMARKS, OR OTHER RIGHTS.  Cornell University shall not be
00038 // held liable for any liability with respect to any claim by the user or any
00039 // other party arising from use of the program.
00040 //
00041 // .SS Usage
00042 //
00043 //   This is very much like the Matrix, except of course it is restricted to
00044 //   Symmetric Matrix.  All the operations for Matrix can also be done here
00045 //   (except for the +=,-=,*= that don't yield a symmetric matrix.  e.g.
00046 //    +=(const Matrix &) is not defined)
00047    
00048 //   The matrix is stored as a lower triangular matrix.
00049 //   In addition to the (row, col) method of finding element, fast(row, col)
00050 //   returns an element with checking to see if row and col need to be 
00051 //   interchanged so that row >= col.
00052 
00053 //   New operations are:
00054 //
00055 // .ft B
00056 //  sym = s.similarity(m);
00057 //
00058 //  This returns m*s*m.T(). This is a similarity
00059 //  transform when m is orthogonal, but nothing
00060 //  restricts m to be orthogonal.  It is just
00061 //  a more efficient way to calculate m*s*m.T,
00062 //  and it realizes that this should be a 
00063 //  HepSymMatrix (the explicit operation m*s*m.T
00064 //  will return a Matrix, not realizing that 
00065 //  it is symmetric).
00066 //
00067 // .ft B
00068 //  sym =  similarityT(m);
00069 //
00070 // This returns m.T()*s*m.
00071 //
00072 // .ft B
00073 // s << m;
00074 //
00075 // This takes the matrix m, and treats it
00076 // as symmetric matrix that is copied to s.
00077 // This is useful for operations that yield
00078 // symmetric matrix, but which the computer
00079 // is too dumb to realize.
00080 //
00081 // .ft B
00082 // s = vT_times_v(const HepVector v);
00083 //
00084 //  calculates v.T()*v.
00085 //
00086 // ./"This code has been written by Mike Smyth, and the algorithms used are
00087 // ./"described in the thesis "A Tracking Library for a Silicon Vertex Detector"
00088 // ./"(Mike Smyth, Cornell University, June 1993).
00089 // ./"Copyright (C) Cornell University 1993. Permission is granted to copy and 
00090 // ./"distribute this code, provided this copyright is not changed or deleted.
00091 // ./"You may modify your copy, providing that you cause the modified file to
00092 // ./"carry prominent notices stating that you changed the files, and the date
00093 // ./"of any change. This code may not be sold, nor may it be contained in
00094 // ./"programs that are to be sold without the written permission of the author.
00095 // ./"You may, however, charge a fee for the physical act of transferring a
00096 // ./"copy of this code. The code is offered "as is" without warranty of any 
00097 // ./"kind, either expressed or implied.  By copying, distributing, or 
00098 // ./"modifying this code you indicate your acceptance of this license to
00099 // ./"do so, and all its terms and conditions.
00100 // ./"This is file contains C++ stuff for doing things with Matrixes.
00101 // ./"To turn on bound checking, define MATRIX_BOUND_CHECK before including
00102 // ./"this file.
00103 //
00104 
00105 #ifndef _SYMMatrix_H_
00106 #define _SYMMatrix_H_
00107 
00108 #ifdef GNUPRAGMA
00109 #pragma interface
00110 #endif
00111 
00112 #include "CLHEP/Matrix/GenMatrix.h"
00113 #ifdef HEP_USE_RANDOM
00114 class HepRandom;
00115 #endif
00116 
00117 #ifdef HEP_NO_INLINE_IN_DECLARATION
00118 #define inline
00119 #endif
00120 
00121 class HepMatrix;
00122 class HepDiagMatrix;
00123 class HepVector;
00124 
00129 class HepSymMatrix : public HepGenMatrix {
00130 public:
00131    inline HepSymMatrix();
00132    // Default constructor. Gives 0x0 symmetric matrix.
00133    // Another SymMatrix can be assigned to it.
00134 
00135    explicit HepSymMatrix(int p);
00136    HepSymMatrix(int p, int);
00137    // Constructor. Gives p x p symmetric matrix.
00138    // With a second argument, the matrix is initialized. 0 means a zero
00139    // matrix, 1 means the identity matrix.
00140 
00141 #ifdef HEP_USE_RANDOM
00142    HepSymMatrix(int p, HepRandom &r);
00143 #endif
00144 
00145    HepSymMatrix(const HepSymMatrix &m1);
00146    // Copy constructor.
00147 
00148    HepSymMatrix(const HepDiagMatrix &m1);
00149    // Constructor from DiagMatrix
00150 
00151    virtual ~HepSymMatrix();
00152    // Destructor.
00153 
00154    inline int num_row() const;
00155    inline int num_col() const;
00156    // Returns number of rows/columns.
00157 
00158    const double & operator()(int row, int col) const; 
00159    double & operator()(int row, int col);
00160    // Read and write a SymMatrix element.
00161    // ** Note that indexing starts from (1,1). **
00162 
00163    const double & fast(int row, int col) const;
00164    double & fast(int row, int col);
00165    // fast element access.
00166    // Must be row>=col;
00167    // ** Note that indexing starts from (1,1). **
00168 
00169    void assign(const HepMatrix &m2);
00170    // Assigns m2 to s, assuming m2 is a symmetric matrix.
00171 
00172    void assign(const HepSymMatrix &m2);
00173    // Another form of assignment. For consistency.
00174 
00175    HepSymMatrix & operator*=(double t);
00176    // Multiply a SymMatrix by a floating number.
00177 
00178    HepSymMatrix & operator/=(double t); 
00179    // Divide a SymMatrix by a floating number.
00180 
00181    HepSymMatrix & operator+=( const HepSymMatrix &m2);
00182    HepSymMatrix & operator+=( const HepDiagMatrix &m2);
00183    HepSymMatrix & operator-=( const HepSymMatrix &m2);
00184    HepSymMatrix & operator-=( const HepDiagMatrix &m2);
00185    // Add or subtract a SymMatrix.
00186 
00187    HepSymMatrix & operator=( const HepSymMatrix &m2);
00188    HepSymMatrix & operator=( const HepDiagMatrix &m2);
00189    // Assignment operators. Notice that there is no SymMatrix = Matrix.
00190 
00191    HepSymMatrix operator- () const;
00192    // unary minus, ie. flip the sign of each element.
00193 
00194    HepSymMatrix T() const;
00195    // Returns the transpose of a SymMatrix (which is itself).
00196 
00197    HepSymMatrix apply(double (*f)(double, int, int)) const;
00198    // Apply a function to all elements of the matrix.
00199 
00200    HepSymMatrix similarity(const HepMatrix &m1) const;
00201    HepSymMatrix similarity(const HepSymMatrix &m1) const;
00202    // Returns m1*s*m1.T().
00203 
00204    HepSymMatrix similarityT(const HepMatrix &m1) const;
00205    // temporary. test of new similarity.
00206    // Returns m1.T()*s*m1.
00207 
00208    double similarity(const HepVector &v) const;
00209    // Returns v.T()*s*v (This is a scaler).
00210 
00211    HepSymMatrix sub(int min_row, int max_row) const;
00212    // Returns a sub matrix of a SymMatrix.
00213    void sub(int row, const HepSymMatrix &m1);
00214    // Sub matrix of this SymMatrix is replaced with m1.
00215 #ifdef HEP_CC_NEED_SUB_WITHOUT_CONST
00216    HepSymMatrix sub(int min_row, int max_row);
00217    // SGI CC bug. I have to have both with/without const. I should not need
00218    // one without const.
00219 #endif
00220 
00221    inline HepSymMatrix inverse(int &ifail) const;
00222    // Invert a Matrix. The matrix is not changed
00223    // Returns 0 when successful, otherwise non-zero.
00224 
00225    void invert(int &ifail);
00226    // Invert a Matrix.
00227    // N.B. the contents of the matrix are replaced by the inverse.
00228    // Returns ierr = 0 when successful, otherwise non-zero. 
00229    // This method has less overhead then inverse().
00230 
00231    double determinant() const;
00232    // calculate the determinant of the matrix.
00233 
00234    double trace() const;
00235    // calculate the trace of the matrix (sum of diagonal elements).
00236 
00237    class HepSymMatrix_row {
00238    public:
00239       inline HepSymMatrix_row(HepSymMatrix&,int);
00240       inline double & operator[](int);
00241    private:
00242       HepSymMatrix& _a;
00243       int _r;
00244    };
00245    class HepSymMatrix_row_const {
00246    public:
00247       inline HepSymMatrix_row_const(const HepSymMatrix&,int);
00248       inline const double & operator[](int) const;
00249    private:
00250       const HepSymMatrix& _a;
00251       int _r;
00252    };
00253    // helper class to implement m[i][j]
00254 
00255    inline HepSymMatrix_row operator[] (int);
00256    inline HepSymMatrix_row_const operator[] (int) const;
00257    // Read or write a matrix element.
00258    // While it may not look like it, you simply do m[i][j] to get an
00259    // element. 
00260    // ** Note that the indexing starts from [0][0]. **
00261 
00262    // Special-case inversions for 5x5 and 6x6 symmetric positive definite:
00263    // These set ifail=0 and invert if the matrix was positive definite;
00264    // otherwise ifail=1 and the matrix is left unaltered.
00265    void invertCholesky5 (int &ifail);  
00266    void invertCholesky6 (int &ifail);
00267 
00268    // Inversions for 5x5 and 6x6 forcing use of specific methods:  The
00269    // behavior (though not the speed) will be identical to invert(ifail).
00270    void invertHaywood4 (int & ifail);  
00271    void invertHaywood5 (int &ifail);  
00272    void invertHaywood6 (int &ifail);
00273    void invertBunchKaufman (int &ifail);  
00274 
00275 protected:
00276    inline int num_size() const;
00277   
00278 private:
00279    friend class HepSymMatrix_row;
00280    friend class HepSymMatrix_row_const;
00281    friend class HepMatrix;
00282    friend class HepDiagMatrix;
00283 
00284    friend void tridiagonal(HepSymMatrix *a,HepMatrix *hsm);
00285    friend double condition(const HepSymMatrix &m);
00286    friend void diag_step(HepSymMatrix *t,int begin,int end);
00287    friend void diag_step(HepSymMatrix *t,HepMatrix *u,int begin,int end);
00288    friend HepMatrix diagonalize(HepSymMatrix *s);
00289    friend HepVector house(const HepSymMatrix &a,int row,int col);
00290    friend void house_with_update2(HepSymMatrix *a,HepMatrix *v,int row,int col);
00291 
00292    friend HepSymMatrix operator+(const HepSymMatrix &m1, 
00293                                   const HepSymMatrix &m2);
00294    friend HepSymMatrix operator-(const HepSymMatrix &m1, 
00295                                   const HepSymMatrix &m2);
00296    friend HepMatrix operator*(const HepSymMatrix &m1, const HepSymMatrix &m2);
00297    friend HepMatrix operator*(const HepSymMatrix &m1, const HepMatrix &m2);
00298    friend HepMatrix operator*(const HepMatrix &m1, const HepSymMatrix &m2);
00299    friend HepVector operator*(const HepSymMatrix &m1, const HepVector &m2);
00300    // Multiply a Matrix by a Matrix or Vector.
00301    
00302    friend HepSymMatrix vT_times_v(const HepVector &v);
00303    // Returns v * v.T();
00304 
00305    double *m;
00306    int nrow;
00307    int size;                                 // total number of elements
00308 
00309    static double posDefFraction5x5;
00310    static double adjustment5x5;
00311    static const  double CHOLESKY_THRESHOLD_5x5;
00312    static const  double CHOLESKY_CREEP_5x5;
00313 
00314    static double posDefFraction6x6;
00315    static double adjustment6x6;
00316    static const double CHOLESKY_THRESHOLD_6x6;
00317    static const double CHOLESKY_CREEP_6x6;
00318 
00319    void invert4  (int & ifail);
00320    void invert5  (int & ifail);
00321    void invert6  (int & ifail);
00322 
00323 };
00324 
00325 //
00326 // Operations other than member functions for Matrix, SymMatrix, DiagMatrix
00327 // and Vectors implemented in Matrix.cc and Matrix.icc (inline).
00328 //
00329 
00330 HepStd::ostream& operator<<(HepStd::ostream &s, const HepSymMatrix &q);
00331 // Write out Matrix, SymMatrix, DiagMatrix and Vector into ostream.
00332 
00333 HepMatrix operator*(const HepMatrix &m1, const HepSymMatrix &m2);
00334 HepMatrix operator*(const HepSymMatrix &m1, const HepMatrix &m2);
00335 HepMatrix operator*(const HepSymMatrix &m1, const HepSymMatrix &m2);
00336 HepSymMatrix operator*(double t, const HepSymMatrix &s1);
00337 HepSymMatrix operator*(const HepSymMatrix &s1, double t);
00338 // Multiplication operators.
00339 // Note that m *= m1 is always faster than m = m * m1
00340 
00341 HepSymMatrix operator/(const HepSymMatrix &m1, double t);
00342 // s = s1 / t. (s /= t is faster if you can use it.)
00343 
00344 HepMatrix operator+(const HepMatrix &m1, const HepSymMatrix &s2);
00345 HepMatrix operator+(const HepSymMatrix &s1, const HepMatrix &m2);
00346 HepSymMatrix operator+(const HepSymMatrix &s1, const HepSymMatrix &s2);
00347 // Addition operators
00348 
00349 HepMatrix operator-(const HepMatrix &m1, const HepSymMatrix &s2);
00350 HepMatrix operator-(const HepSymMatrix &m1, const HepMatrix &m2);
00351 HepSymMatrix operator-(const HepSymMatrix &s1, const HepSymMatrix &s2);
00352 // subtraction operators
00353 
00354 HepSymMatrix dsum(const HepSymMatrix &s1, const HepSymMatrix &s2);
00355 // Direct sum of two symmetric matrices;
00356 
00357 double condition(const HepSymMatrix &m);
00358 // Find the conditon number of a symmetric matrix.
00359 
00360 void diag_step(HepSymMatrix *t, int begin, int end);
00361 void diag_step(HepSymMatrix *t, HepMatrix *u, int begin, int end);
00362 // Implicit symmetric QR step with Wilkinson Shift
00363 
00364 HepMatrix diagonalize(HepSymMatrix *s);
00365 // Diagonalize a symmetric matrix.
00366 // It returns the matrix U so that s_old = U * s_diag * U.T()
00367 
00368 HepVector house(const HepSymMatrix &a, int row=1, int col=1);
00369 void house_with_update2(HepSymMatrix *a, HepMatrix *v, int row=1, int col=1);
00370 // Finds and does Householder reflection on matrix.
00371 
00372 void tridiagonal(HepSymMatrix *a, HepMatrix *hsm);
00373 HepMatrix tridiagonal(HepSymMatrix *a);
00374 // Does a Householder tridiagonalization of a symmetric matrix.
00375 
00376 #ifdef HEP_NO_INLINE_IN_DECLARATION
00377 #undef inline
00378 #endif
00379 
00380 #if defined(HEP_SHORT_NAMES)
00381 typedef HepSymMatrix SymMatrix;
00382 #endif
00383 
00384 #ifndef HEP_DEBUG_INLINE
00385 #include "CLHEP/Matrix/SymMatrix.icc"
00386 #endif
00387 
00388 #endif 

Class Library for High Energy Physics (version 1.8)