CLHEP/Matrix/DiagMatrix.h

00001 // -*- C++ -*-
00002 // CLASSDOC OFF
00003 // $Id: DiagMatrix.h,v 1.17 2003/12/30 15:04:25 pfeiffer Exp $
00004 // ---------------------------------------------------------------------------
00005 // CLASSDOC ON
00006 //
00007 // This file is a part of the CLHEP - a Class Library for High Energy Physics.
00008 //
00009 // 
00010 // Copyright Cornell University 1993, 1996, All Rights Reserved.
00011 // 
00012 // This software written by Nobu Katayama and Mike Smyth, Cornell University.
00013 // 
00014 // Redistribution and use in source and binary forms, with or without
00015 // modification, are permitted provided that the following conditions
00016 // are met:
00017 // 1. Redistributions of source code must retain the above copyright
00018 //    notice and author attribution, this list of conditions and the
00019 //    following disclaimer. 
00020 // 2. Redistributions in binary form must reproduce the above copyright
00021 //    notice and author attribution, this list of conditions and the
00022 //    following disclaimer in the documentation and/or other materials
00023 //    provided with the distribution.
00024 // 3. Neither the name of the University nor the names of its contributors
00025 //    may be used to endorse or promote products derived from this software
00026 //    without specific prior written permission.
00027 // 
00028 // Creation of derivative forms of this software for commercial
00029 // utilization may be subject to restriction; written permission may be
00030 // obtained from Cornell University.
00031 // 
00032 // CORNELL MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED.  By way
00033 // of example, but not limitation, CORNELL MAKES NO REPRESENTATIONS OR
00034 // WARRANTIES OF MERCANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT
00035 // THE USE OF THIS SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS,
00036 // COPYRIGHTS, TRADEMARKS, OR OTHER RIGHTS.  Cornell University shall not be
00037 // held liable for any liability with respect to any claim by the user or any
00038 // other party arising from use of the program.
00039 //
00040 // 
00041 //
00042 // DiagMatrix is a class for diagonal matrix. This is useful for a covariance
00043 // matrix of measured quantities since they are uncorrelated to each other
00044 // and therefore diagonal. It is obviously smaller and faster to manipulate.
00045 //
00046 
00047 #ifndef _DIAGMatrix_H_
00048 #define _DIAGMatrix_H_
00049 
00050 #ifdef GNUPRAGMA
00051 #pragma interface
00052 #endif
00053 
00054 #include <vector>
00055 #include "CLHEP/Matrix/GenMatrix.h"
00056 #ifdef HEP_USE_RANDOM
00057 class HepRandom;
00058 #endif
00059 
00060 #ifdef HEP_NO_INLINE_IN_DECLARATION
00061 #define inline
00062 #endif
00063 
00064 class HepMatrix;
00065 class HepSymMatrix;
00066 class HepVector;
00067 
00072 class HepDiagMatrix: public HepGenMatrix {
00073 public:
00074    inline HepDiagMatrix();
00075    // Default constructor. Gives 0x0 matrix. Another Matrix can be assigned
00076    // to it.
00077 
00078    explicit HepDiagMatrix(int p);
00079    HepDiagMatrix(int p, int);
00080    // Constructor. Gives p x p diagonal matrix.
00081    // With a second argument, either 0 or 1, the matrix is initialized.
00082    // 0 means a zero matrix, 1 means the identity matrix.
00083 
00084 #ifdef HEP_USE_RANDOM
00085    HepDiagMatrix(int p, HepRandom &r);
00086 #endif
00087 
00088    HepDiagMatrix(const HepDiagMatrix &m1);
00089    // Copy constructor.
00090 
00091    virtual ~HepDiagMatrix();
00092    // Destructor.
00093 
00094    inline int num_row() const;
00095    inline int num_col() const;
00096    // Returns the number of rows/columns. (Should be equal.)
00097 
00098    double &operator()(int row, int col);
00099    const double &operator()(int row, int col) const; 
00100    // Read or write a matrix element. row must be equal to col.
00101    // ** Note that indexing starts from (1,1). **
00102    
00103    double &fast(int row, int col);
00104    const double &fast(int row, int col) const;
00105    // fast element access.
00106    // Must be row>=col;
00107    // ** Note that indexing starts from (1,1). **
00108 
00109    void assign(const HepMatrix &m2);
00110    // Assigns m2 to d, assuming m2 is a diagnal matrix.
00111 
00112    void assign(const HepSymMatrix &m2);
00113    // Assigns m2 to d, assuming m2 is a diagnal matrix.
00114 
00115    void assign(const HepDiagMatrix &m2);
00116    // Another form of assignment. For consistency.
00117 
00118    HepDiagMatrix & operator*=(double t);
00119    // Multiply a DiagMatrix by a floating number
00120 
00121    HepDiagMatrix & operator/=(double t); 
00122    // Divide a DiagMatrix by a floating number
00123 
00124    HepDiagMatrix & operator+=( const HepDiagMatrix &m2);
00125    HepDiagMatrix & operator-=( const HepDiagMatrix &m2);
00126    // Add or subtract a DiagMatrix.
00127 
00128    HepDiagMatrix & operator=( const HepDiagMatrix &m2);
00129    // Assignment operator. To assign SymMatrix to DiagMatrix, use d<<s.
00130 
00131    HepDiagMatrix operator- () const;
00132    // unary minus, ie. flip the sign of each element.
00133 
00134    HepDiagMatrix T() const;
00135    // Returns the transpose of a DiagMatrix (which is itself).
00136 
00137    HepDiagMatrix apply(double (*f)(double,
00138                                                int, int)) const;
00139    // Apply a function to all elements of the matrix.
00140 
00141    HepSymMatrix similarity(const HepMatrix &m1) const;
00142    // Returns m1*s*m1.T().
00143    HepSymMatrix similarityT(const HepMatrix &m1) const;
00144    // Returns m1.T()*s*m1.
00145 
00146    double similarity(const HepVector &) const;
00147    // Returns v.T()*s*v (This is a scaler).
00148 
00149    HepDiagMatrix sub(int min_row, int max_row) const;
00150    // Returns a sub matrix of a SymMatrix.
00151 #ifdef HEP_CC_NEED_SUB_WITHOUT_CONST
00152    HepDiagMatrix sub(int min_row, int max_row);
00153    // SGI CC bug. I have to have both with/without const. I should not need
00154    // one without const.
00155 #endif
00156 
00157    void sub(int row, const HepDiagMatrix &m1);
00158    // Sub matrix of this SymMatrix is replaced with m1.
00159 
00160    HepDiagMatrix inverse(int&ierr) const;
00161    // Invert a Matrix. The matrix is not changed
00162    // Returns 0 when successful, otherwise non-zero.
00163 
00164    void invert(int&ierr);
00165    // Invert a Matrix.
00166    // N.B. the contents of the matrix are replaced by the inverse.
00167    // Returns ierr = 0 when successful, otherwise non-zero. 
00168    // This method has less overhead then inverse().
00169 
00170    double determinant() const;
00171    // calculate the determinant of the matrix.
00172 
00173    double trace() const;
00174    // calculate the trace of the matrix (sum of diagonal elements).
00175 
00176    class HepDiagMatrix_row {
00177    public:
00178       inline HepDiagMatrix_row(HepDiagMatrix&,int);
00179       inline double & operator[](int);
00180    private:
00181       HepDiagMatrix& _a;
00182       int _r;
00183    };
00184    class HepDiagMatrix_row_const {
00185    public:
00186       inline HepDiagMatrix_row_const(const HepDiagMatrix&,int);
00187       inline const double & operator[](int) const;
00188    private:
00189       const HepDiagMatrix& _a;
00190       int _r;
00191    };
00192    // helper classes to implement m[i][j]
00193 
00194    inline HepDiagMatrix_row operator[] (int);
00195    inline HepDiagMatrix_row_const operator[] (int) const;
00196    // Read or write a matrix element.
00197    // While it may not look like it, you simply do m[i][j] to get an
00198    // element. 
00199    // ** Note that the indexing starts from [0][0]. **
00200 
00201 protected:
00202    inline int num_size() const;
00203 
00204 private:
00205    friend class HepDiagMatrix_row;
00206    friend class HepDiagMatrix_row_const;
00207    friend class HepMatrix;
00208    friend class HepSymMatrix;
00209 
00210    friend HepDiagMatrix operator*(const HepDiagMatrix &m1,
00211                                    const HepDiagMatrix &m2);
00212    friend HepDiagMatrix operator+(const HepDiagMatrix &m1,
00213                                    const HepDiagMatrix &m2);
00214    friend HepDiagMatrix operator-(const HepDiagMatrix &m1,
00215                                    const HepDiagMatrix &m2);
00216    friend HepMatrix operator*(const HepDiagMatrix &m1, const HepMatrix &m2);
00217    friend HepMatrix operator*(const HepMatrix &m1, const HepDiagMatrix &m2);
00218    friend HepVector operator*(const HepDiagMatrix &m1, const HepVector &m2);
00219 
00220    std::vector<double,Alloc<double,25> > m;
00221    int nrow;
00222 #if defined(__sun) || !defined(__GNUG__)
00223 //
00224 // Sun CC 4.0.1 has this bug.
00225 //
00226    static double zero;
00227 #else
00228    static const double zero;
00229 #endif
00230 };
00231 
00232 HepStd::ostream& operator<<(HepStd::ostream &s, const HepDiagMatrix &q);
00233 // Write out Matrix, SymMatrix, DiagMatrix and Vector into ostream.
00234 
00235 HepMatrix operator*(const HepMatrix &m1, const HepDiagMatrix &m2);
00236 HepMatrix operator*(const HepDiagMatrix &m1, const HepMatrix &m2);
00237 HepDiagMatrix operator*(double t, const HepDiagMatrix &d1);
00238 HepDiagMatrix operator*(const HepDiagMatrix &d1, double t);
00239 // Multiplication operators
00240 // Note that m *= m1 is always faster than m = m * m1
00241 
00242 HepDiagMatrix operator/(const HepDiagMatrix &m1, double t);
00243 // d = d1 / t. (d /= t is faster if you can use it.)
00244 
00245 HepMatrix operator+(const HepMatrix &m1, const HepDiagMatrix &d2);
00246 HepMatrix operator+(const HepDiagMatrix &d1, const HepMatrix &m2);
00247 HepDiagMatrix operator+(const HepDiagMatrix &m1, const HepDiagMatrix &d2);
00248 HepSymMatrix operator+(const HepSymMatrix &s1, const HepDiagMatrix &d2);
00249 HepSymMatrix operator+(const HepDiagMatrix &d1, const HepSymMatrix &s2);
00250 // Addition operators
00251 
00252 HepMatrix operator-(const HepMatrix &m1, const HepDiagMatrix &d2);
00253 HepMatrix operator-(const HepDiagMatrix &d1, const HepMatrix &m2);
00254 HepDiagMatrix operator-(const HepDiagMatrix &d1, const HepDiagMatrix &d2);
00255 HepSymMatrix operator-(const HepSymMatrix &s1, const HepDiagMatrix &d2);
00256 HepSymMatrix operator-(const HepDiagMatrix &d1, const HepSymMatrix &s2);
00257 // Subtraction operators
00258 
00259 HepDiagMatrix dsum(const HepDiagMatrix &s1, const HepDiagMatrix &s2);
00260 // Direct sum of two diagonal matricies;
00261 
00262 #ifdef HEP_NO_INLINE_IN_DECLARATION
00263 #undef inline
00264 #endif
00265 
00266 #if defined(HEP_SHORT_NAMES)
00267 typedef HepDiagMatrix DiagMatrix;
00268 #endif
00269 
00270 #ifndef HEP_DEBUG_INLINE
00271 #include "CLHEP/Matrix/DiagMatrix.icc"
00272 #endif
00273 
00274 #endif 

Class Library for High Energy Physics (version 1.8)