CLHEP VERSION Reference Documentation
   
CLHEP Home Page     CLHEP Documentation     CLHEP Bug Reports

Matrix/Matrix/DiagMatrix.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 // CLASSDOC OFF
00003 // ---------------------------------------------------------------------------
00004 // CLASSDOC ON
00005 //
00006 // This file is a part of the CLHEP - a Class Library for High Energy Physics.
00007 // 
00008 // This software written by Nobu Katayama and Mike Smyth, Cornell University.
00009 //
00010 // DiagMatrix is a class for diagonal matrix. This is useful for a covariance
00011 // matrix of measured quantities since they are uncorrelated to each other
00012 // and therefore diagonal. It is obviously smaller and faster to manipulate.
00013 //
00014 
00015 #ifndef _DIAGMatrix_H_
00016 #define _DIAGMatrix_H_
00017 
00018 #ifdef GNUPRAGMA
00019 #pragma interface
00020 #endif
00021 
00022 #include <vector>
00023 
00024 #include "CLHEP/Matrix/defs.h"
00025 #include "CLHEP/Matrix/GenMatrix.h"
00026 
00027 namespace CLHEP {
00028 
00029 class HepRandom;
00030 
00031 class HepMatrix;
00032 class HepSymMatrix;
00033 class HepVector;
00034 
00039 class HepDiagMatrix: public HepGenMatrix {
00040 public:
00041    inline HepDiagMatrix();
00042    // Default constructor. Gives 0x0 matrix. Another Matrix can be assigned
00043    // to it.
00044 
00045    explicit HepDiagMatrix(int p);
00046    HepDiagMatrix(int p, int);
00047    // Constructor. Gives p x p diagonal matrix.
00048    // With a second argument, either 0 or 1, the matrix is initialized.
00049    // 0 means a zero matrix, 1 means the identity matrix.
00050 
00051    HepDiagMatrix(int p, HepRandom &r);
00052 
00053    HepDiagMatrix(const HepDiagMatrix &m1);
00054    // Copy constructor.
00055 
00056    virtual ~HepDiagMatrix();
00057    // Destructor.
00058 
00059    inline int num_row() const;
00060    inline int num_col() const;
00061    // Returns the number of rows/columns. (Should be equal.)
00062 
00063    double &operator()(int row, int col);
00064    const double &operator()(int row, int col) const; 
00065    // Read or write a matrix element. row must be equal to col.
00066    // ** Note that indexing starts from (1,1). **
00067    
00068    double &fast(int row, int col);
00069    const double &fast(int row, int col) const;
00070    // fast element access.
00071    // Must be row>=col;
00072    // ** Note that indexing starts from (1,1). **
00073 
00074    void assign(const HepMatrix &m2);
00075    // Assigns m2 to d, assuming m2 is a diagnal matrix.
00076 
00077    void assign(const HepSymMatrix &m2);
00078    // Assigns m2 to d, assuming m2 is a diagnal matrix.
00079 
00080    void assign(const HepDiagMatrix &m2);
00081    // Another form of assignment. For consistency.
00082 
00083    HepDiagMatrix & operator*=(double t);
00084    // Multiply a DiagMatrix by a floating number
00085 
00086    HepDiagMatrix & operator/=(double t); 
00087    // Divide a DiagMatrix by a floating number
00088 
00089    HepDiagMatrix & operator+=( const HepDiagMatrix &m2);
00090    HepDiagMatrix & operator-=( const HepDiagMatrix &m2);
00091    // Add or subtract a DiagMatrix.
00092 
00093    HepDiagMatrix & operator=( const HepDiagMatrix &m2);
00094    // Assignment operator. To assign SymMatrix to DiagMatrix, use d<<s.
00095 
00096    HepDiagMatrix operator- () const;
00097    // unary minus, ie. flip the sign of each element.
00098 
00099    HepDiagMatrix T() const;
00100    // Returns the transpose of a DiagMatrix (which is itself).
00101 
00102    HepDiagMatrix apply(double (*f)(double,
00103                                                int, int)) const;
00104    // Apply a function to all elements of the matrix.
00105 
00106    HepSymMatrix similarity(const HepMatrix &m1) const;
00107    // Returns m1*s*m1.T().
00108    HepSymMatrix similarityT(const HepMatrix &m1) const;
00109    // Returns m1.T()*s*m1.
00110 
00111    double similarity(const HepVector &) const;
00112    // Returns v.T()*s*v (This is a scaler).
00113 
00114    HepDiagMatrix sub(int min_row, int max_row) const;
00115    // Returns a sub matrix of a SymMatrix.
00116    HepDiagMatrix sub(int min_row, int max_row);
00117    // SGI CC bug. I have to have both with/without const. I should not need
00118    // one without const.
00119 
00120    void sub(int row, const HepDiagMatrix &m1);
00121    // Sub matrix of this SymMatrix is replaced with m1.
00122 
00123    HepDiagMatrix inverse(int&ierr) const;
00124    // Invert a Matrix. The matrix is not changed
00125    // Returns 0 when successful, otherwise non-zero.
00126 
00127    void invert(int&ierr);
00128    // Invert a Matrix.
00129    // N.B. the contents of the matrix are replaced by the inverse.
00130    // Returns ierr = 0 when successful, otherwise non-zero. 
00131    // This method has less overhead then inverse().
00132 
00133    double determinant() const;
00134    // calculate the determinant of the matrix.
00135 
00136    double trace() const;
00137    // calculate the trace of the matrix (sum of diagonal elements).
00138 
00139    class HepDiagMatrix_row {
00140    public:
00141       inline HepDiagMatrix_row(HepDiagMatrix&,int);
00142       inline double & operator[](int);
00143    private:
00144       HepDiagMatrix& _a;
00145       int _r;
00146    };
00147    class HepDiagMatrix_row_const {
00148    public:
00149       inline HepDiagMatrix_row_const(const HepDiagMatrix&,int);
00150       inline const double & operator[](int) const;
00151    private:
00152       const HepDiagMatrix& _a;
00153       int _r;
00154    };
00155    // helper classes to implement m[i][j]
00156 
00157    inline HepDiagMatrix_row operator[] (int);
00158    inline HepDiagMatrix_row_const operator[] (int) const;
00159    // Read or write a matrix element.
00160    // While it may not look like it, you simply do m[i][j] to get an
00161    // element. 
00162    // ** Note that the indexing starts from [0][0]. **
00163 
00164 protected:
00165    inline int num_size() const;
00166 
00167 private:
00168    friend class HepDiagMatrix_row;
00169    friend class HepDiagMatrix_row_const;
00170    friend class HepMatrix;
00171    friend class HepSymMatrix;
00172 
00173    friend HepDiagMatrix operator*(const HepDiagMatrix &m1,
00174                                    const HepDiagMatrix &m2);
00175    friend HepDiagMatrix operator+(const HepDiagMatrix &m1,
00176                                    const HepDiagMatrix &m2);
00177    friend HepDiagMatrix operator-(const HepDiagMatrix &m1,
00178                                    const HepDiagMatrix &m2);
00179    friend HepMatrix operator*(const HepDiagMatrix &m1, const HepMatrix &m2);
00180    friend HepMatrix operator*(const HepMatrix &m1, const HepDiagMatrix &m2);
00181    friend HepVector operator*(const HepDiagMatrix &m1, const HepVector &m2);
00182 
00183 #ifdef DISABLE_ALLOC
00184    std::vector<double > m;
00185 #else
00186    std::vector<double,Alloc<double,25> > m;
00187 #endif
00188    int nrow;
00189 #if defined(__sun) || !defined(__GNUG__)
00190 //
00191 // Sun CC 4.0.1 has this bug.
00192 //
00193    static double zero;
00194 #else
00195    static const double zero;
00196 #endif
00197 };
00198 
00199 std::ostream& operator<<(std::ostream &s, const HepDiagMatrix &q);
00200 // Write out Matrix, SymMatrix, DiagMatrix and Vector into ostream.
00201 
00202 HepMatrix operator*(const HepMatrix &m1, const HepDiagMatrix &m2);
00203 HepMatrix operator*(const HepDiagMatrix &m1, const HepMatrix &m2);
00204 HepDiagMatrix operator*(double t, const HepDiagMatrix &d1);
00205 HepDiagMatrix operator*(const HepDiagMatrix &d1, double t);
00206 // Multiplication operators
00207 // Note that m *= m1 is always faster than m = m * m1
00208 
00209 HepDiagMatrix operator/(const HepDiagMatrix &m1, double t);
00210 // d = d1 / t. (d /= t is faster if you can use it.)
00211 
00212 HepMatrix operator+(const HepMatrix &m1, const HepDiagMatrix &d2);
00213 HepMatrix operator+(const HepDiagMatrix &d1, const HepMatrix &m2);
00214 HepDiagMatrix operator+(const HepDiagMatrix &m1, const HepDiagMatrix &d2);
00215 HepSymMatrix operator+(const HepSymMatrix &s1, const HepDiagMatrix &d2);
00216 HepSymMatrix operator+(const HepDiagMatrix &d1, const HepSymMatrix &s2);
00217 // Addition operators
00218 
00219 HepMatrix operator-(const HepMatrix &m1, const HepDiagMatrix &d2);
00220 HepMatrix operator-(const HepDiagMatrix &d1, const HepMatrix &m2);
00221 HepDiagMatrix operator-(const HepDiagMatrix &d1, const HepDiagMatrix &d2);
00222 HepSymMatrix operator-(const HepSymMatrix &s1, const HepDiagMatrix &d2);
00223 HepSymMatrix operator-(const HepDiagMatrix &d1, const HepSymMatrix &s2);
00224 // Subtraction operators
00225 
00226 HepDiagMatrix dsum(const HepDiagMatrix &s1, const HepDiagMatrix &s2);
00227 // Direct sum of two diagonal matricies;
00228 
00229 }  // namespace CLHEP
00230 
00231 #ifdef ENABLE_BACKWARDS_COMPATIBILITY
00232 //  backwards compatibility will be enabled ONLY in CLHEP 1.9
00233 using namespace CLHEP;
00234 #endif
00235 
00236 #ifndef HEP_DEBUG_INLINE
00237 #include "CLHEP/Matrix/DiagMatrix.icc"
00238 #endif
00239 
00240 #endif