CLHEP/Matrix/DiagMatrix.h

00001 // -*- C++ -*-
00002 // CLASSDOC OFF
00003 // $Id: DiagMatrix.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 // 
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 "CLHEP/Matrix/GenMatrix.h"
00055 #ifdef HEP_USE_RANDOM
00056 class HepRandom;
00057 #endif
00058 
00059 #ifdef HEP_NO_INLINE_IN_DECLARATION
00060 #define inline
00061 #endif
00062 
00063 class HepMatrix;
00064 class HepSymMatrix;
00065 class HepVector;
00066 
00071 class HepDiagMatrix: public HepGenMatrix {
00072 public:
00073    inline HepDiagMatrix();
00074    // Default constructor. Gives 0x0 matrix. Another Matrix can be assigned
00075    // to it.
00076 
00077    explicit HepDiagMatrix(int p);
00078    HepDiagMatrix(int p, int);
00079    // Constructor. Gives p x p diagonal matrix.
00080    // With a second argument, either 0 or 1, the matrix is initialized.
00081    // 0 means a zero matrix, 1 means the identity matrix.
00082 
00083 #ifdef HEP_USE_RANDOM
00084    HepDiagMatrix(int p, HepRandom &r);
00085 #endif
00086 
00087    HepDiagMatrix(const HepDiagMatrix &m1);
00088    // Copy constructor.
00089 
00090    virtual ~HepDiagMatrix();
00091    // Destructor.
00092 
00093    inline int num_row() const;
00094    inline int num_col() const;
00095    // Returns the number of rows/columns. (Should be equal.)
00096 
00097    double &operator()(int row, int col);
00098    const double &operator()(int row, int col) const; 
00099    // Read or write a matrix element. row must be equal to col.
00100    // ** Note that indexing starts from (1,1). **
00101    
00102    double &fast(int row, int col);
00103    const double &fast(int row, int col) const;
00104    // fast element access.
00105    // Must be row>=col;
00106    // ** Note that indexing starts from (1,1). **
00107 
00108    void assign(const HepMatrix &m2);
00109    // Assigns m2 to d, assuming m2 is a diagnal matrix.
00110 
00111    void assign(const HepSymMatrix &m2);
00112    // Assigns m2 to d, assuming m2 is a diagnal matrix.
00113 
00114    void assign(const HepDiagMatrix &m2);
00115    // Another form of assignment. For consistency.
00116 
00117    HepDiagMatrix & operator*=(double t);
00118    // Multiply a DiagMatrix by a floating number
00119 
00120    HepDiagMatrix & operator/=(double t); 
00121    // Divide a DiagMatrix by a floating number
00122 
00123    HepDiagMatrix & operator+=( const HepDiagMatrix &m2);
00124    HepDiagMatrix & operator-=( const HepDiagMatrix &m2);
00125    // Add or subtract a DiagMatrix.
00126 
00127    HepDiagMatrix & operator=( const HepDiagMatrix &m2);
00128    // Assignment operator. To assign SymMatrix to DiagMatrix, use d<<s.
00129 
00130    HepDiagMatrix operator- () const;
00131    // unary minus, ie. flip the sign of each element.
00132 
00133    HepDiagMatrix T() const;
00134    // Returns the transpose of a DiagMatrix (which is itself).
00135 
00136    HepDiagMatrix apply(double (*f)(double,
00137                                                int, int)) const;
00138    // Apply a function to all elements of the matrix.
00139 
00140    HepSymMatrix similarity(const HepMatrix &m1) const;
00141    // Returns m1*s*m1.T().
00142    HepSymMatrix similarityT(const HepMatrix &m1) const;
00143    // Returns m1.T()*s*m1.
00144 
00145    double similarity(const HepVector &) const;
00146    // Returns v.T()*s*v (This is a scaler).
00147 
00148    HepDiagMatrix sub(int min_row, int max_row) const;
00149    // Returns a sub matrix of a SymMatrix.
00150 #ifdef HEP_CC_NEED_SUB_WITHOUT_CONST
00151    HepDiagMatrix sub(int min_row, int max_row);
00152    // SGI CC bug. I have to have both with/without const. I should not need
00153    // one without const.
00154 #endif
00155 
00156    void sub(int row, const HepDiagMatrix &m1);
00157    // Sub matrix of this SymMatrix is replaced with m1.
00158 
00159    HepDiagMatrix inverse(int&ierr) const;
00160    // Invert a Matrix. The matrix is not changed
00161    // Returns 0 when successful, otherwise non-zero.
00162 
00163    void invert(int&ierr);
00164    // Invert a Matrix.
00165    // N.B. the contents of the matrix are replaced by the inverse.
00166    // Returns ierr = 0 when successful, otherwise non-zero. 
00167    // This method has less overhead then inverse().
00168 
00169    double determinant() const;
00170    // calculate the determinant of the matrix.
00171 
00172    double trace() const;
00173    // calculate the trace of the matrix (sum of diagonal elements).
00174 
00175    class HepDiagMatrix_row {
00176    public:
00177       inline HepDiagMatrix_row(HepDiagMatrix&,int);
00178       inline double & operator[](int);
00179    private:
00180       HepDiagMatrix& _a;
00181       int _r;
00182    };
00183    class HepDiagMatrix_row_const {
00184    public:
00185       inline HepDiagMatrix_row_const(const HepDiagMatrix&,int);
00186       inline const double & operator[](int) const;
00187    private:
00188       const HepDiagMatrix& _a;
00189       int _r;
00190    };
00191    // helper classes to implement m[i][j]
00192 
00193    inline HepDiagMatrix_row operator[] (int);
00194    inline HepDiagMatrix_row_const operator[] (int) const;
00195    // Read or write a matrix element.
00196    // While it may not look like it, you simply do m[i][j] to get an
00197    // element. 
00198    // ** Note that the indexing starts from [0][0]. **
00199 
00200 protected:
00201    inline int num_size() const;
00202 
00203 private:
00204    friend class HepDiagMatrix_row;
00205    friend class HepDiagMatrix_row_const;
00206    friend class HepMatrix;
00207    friend class HepSymMatrix;
00208 
00209    friend HepDiagMatrix operator*(const HepDiagMatrix &m1,
00210                                    const HepDiagMatrix &m2);
00211    friend HepDiagMatrix operator+(const HepDiagMatrix &m1,
00212                                    const HepDiagMatrix &m2);
00213    friend HepDiagMatrix operator-(const HepDiagMatrix &m1,
00214                                    const HepDiagMatrix &m2);
00215    friend HepMatrix operator*(const HepDiagMatrix &m1, const HepMatrix &m2);
00216    friend HepMatrix operator*(const HepMatrix &m1, const HepDiagMatrix &m2);
00217    friend HepVector operator*(const HepDiagMatrix &m1, const HepVector &m2);
00218 
00219    double *m;
00220    int nrow;
00221 #if defined(__sun) || !defined(__GNUG__)
00222 //
00223 // Sun CC 4.0.1 has this bug.
00224 //
00225    static double zero;
00226 #else
00227    static const double zero;
00228 #endif
00229 };
00230 
00231 HepStd::ostream& operator<<(HepStd::ostream &s, const HepDiagMatrix &q);
00232 // Write out Matrix, SymMatrix, DiagMatrix and Vector into ostream.
00233 
00234 HepMatrix operator*(const HepMatrix &m1, const HepDiagMatrix &m2);
00235 HepMatrix operator*(const HepDiagMatrix &m1, const HepMatrix &m2);
00236 HepDiagMatrix operator*(double t, const HepDiagMatrix &d1);
00237 HepDiagMatrix operator*(const HepDiagMatrix &d1, double t);
00238 // Multiplication operators
00239 // Note that m *= m1 is always faster than m = m * m1
00240 
00241 HepDiagMatrix operator/(const HepDiagMatrix &m1, double t);
00242 // d = d1 / t. (d /= t is faster if you can use it.)
00243 
00244 HepMatrix operator+(const HepMatrix &m1, const HepDiagMatrix &d2);
00245 HepMatrix operator+(const HepDiagMatrix &d1, const HepMatrix &m2);
00246 HepDiagMatrix operator+(const HepDiagMatrix &m1, const HepDiagMatrix &d2);
00247 HepSymMatrix operator+(const HepSymMatrix &s1, const HepDiagMatrix &d2);
00248 HepSymMatrix operator+(const HepDiagMatrix &d1, const HepSymMatrix &s2);
00249 // Addition operators
00250 
00251 HepMatrix operator-(const HepMatrix &m1, const HepDiagMatrix &d2);
00252 HepMatrix operator-(const HepDiagMatrix &d1, const HepMatrix &m2);
00253 HepDiagMatrix operator-(const HepDiagMatrix &d1, const HepDiagMatrix &d2);
00254 HepSymMatrix operator-(const HepSymMatrix &s1, const HepDiagMatrix &d2);
00255 HepSymMatrix operator-(const HepDiagMatrix &d1, const HepSymMatrix &s2);
00256 // Subtraction operators
00257 
00258 HepDiagMatrix dsum(const HepDiagMatrix &s1, const HepDiagMatrix &s2);
00259 // Direct sum of two diagonal matricies;
00260 
00261 #ifdef HEP_NO_INLINE_IN_DECLARATION
00262 #undef inline
00263 #endif
00264 
00265 #if defined(HEP_SHORT_NAMES)
00266 typedef HepDiagMatrix DiagMatrix;
00267 #endif
00268 
00269 #ifndef HEP_DEBUG_INLINE
00270 #include "CLHEP/Matrix/DiagMatrix.icc"
00271 #endif
00272 
00273 #endif 

Class Library for High Energy Physics (version 1.8)