CLHEP/Matrix/GenMatrix.h

00001 // -*- C++ -*-
00002 // CLASSDOC OFF
00003 // $Id: GenMatrix.h,v 1.14 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 // This is the definition of the HepGenMatrix, base class for HepMatrix,
00042 // HepSymMatrix and HepDiagMatrix. This is an abstract cless.
00043 // See definitions in Matrix.h, SymMatrix.h, DiagMatrix.h and Vector.h
00044 
00045 #ifndef _GENMatrix_H_
00046 #define _GENMatrix_H_
00047 
00048 #ifdef GNUPRAGMA
00049 #pragma interface
00050 #endif
00051 
00052 #include <vector>
00053 #include "CLHEP/config/CLHEP.h"
00054 #include "CLHEP/config/iostream.h"
00055 
00056 class HepGenMatrix_row;
00057 class HepGenMatrix_row_const;
00058 class HepGenMatrix;
00059 
00064 class HepGenMatrix {
00065 
00066 public:
00067    virtual ~HepGenMatrix() {}
00068 
00069 
00070   
00071   template <class T, size_t size> class Alloc 
00072   {
00073 
00074   public:  
00075     typedef T value_type;  
00076     typedef size_t size_type;  
00077     typedef ptrdiff_t difference_type;  
00078     typedef T* pointer;  
00079     typedef const T* const_pointer;  
00080     typedef T& reference;  
00081     typedef const T& const_reference;
00082 
00083     pointer address(reference r) const { return &r; }  
00084     const_pointer address(const_reference r) const { return &r; }  
00085     Alloc() throw() {}  
00086     Alloc(const Alloc<T,size>&) throw() {}   
00087     ~Alloc() throw() {}  
00088     pointer allocate(size_type n ) { if( n <= size ) return pool; else return new T[n]; }  
00089     void deallocate(pointer p, size_type n) { if (p == pool ) return; delete [] p; }  
00090     void construct(pointer p, const T& val ) { new(p) T(val); }  
00091     void destroy(pointer p) { p->~T(); }  
00092     size_type max_size() const throw() { size_type c = (size_type)(-1) /sizeof(T); return (0 < c ? c : 1); }  
00093     template<class O> struct rebind { typedef Alloc<O,size> other; };
00094 
00095   private:  
00096     T pool[size];
00097   };
00098   
00099 
00100   typedef std::vector<double,Alloc<double,25> >::iterator mIter;
00101   typedef std::vector<double,Alloc<double,25> >::const_iterator mcIter;
00102 
00103    virtual int num_row() const = 0;
00104    virtual int num_col() const = 0;
00105 
00106    virtual const double & operator()(int row, int col) const =0;
00107    virtual double & operator()(int row, int col) =0;
00108    // Read or write a matrix element. 
00109    // ** Note that the indexing starts from (1,1). **
00110 
00111    virtual void invert(int&) = 0;
00112 
00113    class HepGenMatrix_row {
00114    public:
00115       inline HepGenMatrix_row(HepGenMatrix&,int);
00116       double & operator[](int);
00117    private:
00118       HepGenMatrix& _a;
00119       int _r;
00120    };
00121    class HepGenMatrix_row_const {
00122    public:
00123       inline HepGenMatrix_row_const (const HepGenMatrix&,int);
00124       const double & operator[](int) const;
00125    private:
00126       const HepGenMatrix& _a;
00127       int _r;
00128    };
00129    // helper classes to implement m[i][j]
00130 
00131    inline HepGenMatrix_row operator[] (int);
00132    inline const HepGenMatrix_row_const operator[] (int) const;
00133    // Read or write a matrix element.
00134    // While it may not look like it, you simply do m[i][j] to get an
00135    // element. 
00136    // ** Note that the indexing starts from [0][0]. **
00137 
00138    inline static void swap(int&,int&);
00139    inline static void swap(std::vector<double,Alloc<double,25> >&, std::vector<double,Alloc<double,25> >&);
00140 
00141    virtual bool operator== ( const HepGenMatrix& ) const;
00142    // equality operator for matrices (BaBar)
00143 
00144    static void error(const char *s);
00145 
00146 protected: 
00147 
00148    virtual int num_size() const = 0;
00149    void delete_m(int size, double*);
00150    double* new_m(int size);
00151 
00152 public:
00153    enum{size_max = 25};
00154    // This is not the maximum size of the Matrix. It is the maximum length of
00155    // the array (1D) which can be put on the pile.
00156    //
00157    // This enum used to be private, but it then is not accessible
00158    // in the definition of array_pile in the .cc file for Sun CC 4.0.1.
00159    // efrank@upenn5.hep.upenn.edu
00160  
00161 private:
00162    void operator=(const HepGenMatrix &) {}
00163    // Remove default operator for HepGenMatrix.
00164 
00165    friend class HepGenMatrix_row;
00166    friend class HepGenMatrix_row_const;
00167 
00168   double data_array[size_max];
00169 
00170 };
00171 
00172 double norm(const HepGenMatrix &m);
00173 double norm1(const HepGenMatrix &m);
00174 double norm_infinity(const HepGenMatrix &m);
00175 // 2, 1 or infinity-norm of a matrix.
00176 
00177 #ifdef HEP_NO_INLINE_IN_DECLARATION
00178 #undef inline
00179 #endif
00180 
00181 #ifdef HEP_SHORT_NAMES
00182 typedef HepGenMatrix GenMatrix;
00183 #endif
00184 
00185 #ifndef HEP_DEBUG_INLINE
00186 #include "CLHEP/Matrix/GenMatrix.icc"
00187 #endif
00188 
00189 
00190 #endif

Class Library for High Energy Physics (version 1.8)