CLHEP 2.0.4.7 Reference Documentation
   
CLHEP Home Page     CLHEP Documentation     CLHEP Bug Reports

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

Generated on Thu Jul 1 22:02:30 2010 for CLHEP by  doxygen 1.4.7