TVectorT.h

Go to the documentation of this file.
00001 // @(#)root/matrix:$Id: TVectorT.h 30125 2009-09-14 05:03:58Z brun $
00002 // Authors: Fons Rademakers, Eddy Offermann   Nov 2003
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 #ifndef ROOT_TVectorT
00013 #define ROOT_TVectorT
00014 
00015 //////////////////////////////////////////////////////////////////////////
00016 //                                                                      //
00017 // TVectorT                                                             //
00018 //                                                                      //
00019 // Template class of Vectors in the linear algebra package              //
00020 //                                                                      //
00021 //////////////////////////////////////////////////////////////////////////
00022 
00023 #ifndef ROOT_TMatrixT
00024 #include "TMatrixT.h"
00025 #endif
00026 #ifndef ROOT_TMatrixTSym
00027 #include "TMatrixTSym.h"
00028 #endif
00029 #ifndef ROOT_TMatrixTSparse
00030 #include "TMatrixTSparse.h"
00031 #endif
00032 
00033 template<class Element> class TVectorT : public TObject {
00034 
00035 protected:
00036    Int_t    fNrows;                // number of rows
00037    Int_t    fRowLwb;               // lower bound of the row index
00038    Element *fElements;             //[fNrows] elements themselves
00039 
00040    enum {kSizeMax = 5};             // size data container on stack, see New_m(),Delete_m()
00041    enum {kWorkMax = 100};           // size of work array's in several routines
00042 
00043    Element  fDataStack[kSizeMax];  //! data container
00044    Bool_t   fIsOwner;              //!default kTRUE, when Use array kFALSE
00045 
00046    Element* New_m   (Int_t size);
00047    void     Delete_m(Int_t size,Element*&);
00048    Int_t    Memcpy_m(Element *newp,const Element *oldp,Int_t copySize,
00049                      Int_t newSize,Int_t oldSize);
00050 
00051    void     Allocate(Int_t nrows,Int_t row_lwb = 0,Int_t init = 0);
00052 
00053    enum EVectorStatusBits {
00054      kStatus = BIT(14) // set if vector object is valid
00055    };
00056 
00057 public:
00058 
00059    TVectorT() : fNrows(0), fRowLwb(0), fElements(0), fIsOwner(kTRUE) { }
00060    explicit TVectorT(Int_t n);
00061    TVectorT(Int_t lwb,Int_t upb);
00062    TVectorT(Int_t n,const Element *elements);
00063    TVectorT(Int_t lwb,Int_t upb,const Element *elements);
00064    TVectorT(const TVectorT            <Element> &another);
00065    TVectorT(const TMatrixTRow_const   <Element> &mr);
00066    TVectorT(const TMatrixTColumn_const<Element> &mc);
00067    TVectorT(const TMatrixTDiag_const  <Element> &md);
00068    template <class Element2> TVectorT(const TVectorT<Element2> &another)
00069    {
00070       R__ASSERT(another.IsValid());
00071       Allocate(another.GetUpb()-another.GetLwb()+1,another.GetLwb());
00072       *this = another;
00073    }
00074 #ifndef __CINT__
00075    TVectorT(Int_t lwb,Int_t upb,Element iv1, ...);
00076 #endif
00077    virtual ~TVectorT() { Clear(); }
00078 
00079    inline          Int_t     GetLwb       () const { return fRowLwb; }
00080    inline          Int_t     GetUpb       () const { return fNrows+fRowLwb-1; }
00081    inline          Int_t     GetNrows     () const { return fNrows; }
00082    inline          Int_t     GetNoElements() const { return fNrows; }
00083 
00084    inline          Element  *GetMatrixArray  ()       { return fElements; }
00085    inline const    Element  *GetMatrixArray  () const { return fElements; }
00086 
00087    inline void     Invalidate ()       { SetBit(kStatus); }
00088    inline void     MakeValid  ()       { ResetBit(kStatus); }
00089    inline Bool_t   IsValid    () const { return !TestBit(kStatus); }
00090    inline Bool_t   IsOwner    () const { return fIsOwner; }
00091    inline void     SetElements(const Element *elements) { R__ASSERT(IsValid());
00092                                                           memcpy(fElements,elements,fNrows*sizeof(Element)); }
00093    inline TVectorT<Element> &Shift     (Int_t row_shift)            { fRowLwb += row_shift; return *this; }
00094           TVectorT<Element> &ResizeTo  (Int_t lwb,Int_t upb);
00095    inline TVectorT<Element> &ResizeTo  (Int_t n)                    { return ResizeTo(0,n-1); }
00096    inline TVectorT<Element> &ResizeTo  (const TVectorT<Element> &v) { return ResizeTo(v.GetLwb(),v.GetUpb()); }
00097 
00098           TVectorT<Element> &Use       (Int_t lwb,Int_t upb,Element *data);
00099    const  TVectorT<Element> &Use       (Int_t lwb,Int_t upb,const Element *data) const
00100                                          { return (const TVectorT<Element>&)(const_cast<TVectorT<Element> *>(this))->Use(lwb,upb,const_cast<Element *>(data)); }
00101           TVectorT<Element> &Use       (Int_t n,Element *data);
00102    const  TVectorT<Element> &Use       (Int_t n,const Element *data) const ;
00103           TVectorT<Element> &Use       (TVectorT<Element> &v);
00104    const  TVectorT<Element> &Use       (const TVectorT<Element> &v) const ;
00105 
00106           TVectorT<Element> &GetSub    (Int_t row_lwb,Int_t row_upb,TVectorT<Element> &target,Option_t *option="S") const;
00107           TVectorT<Element>  GetSub    (Int_t row_lwb,Int_t row_upb,Option_t *option="S") const;
00108           TVectorT<Element> &SetSub    (Int_t row_lwb,const TVectorT<Element> &source);
00109 
00110    TVectorT<Element> &Zero();
00111    TVectorT<Element> &Abs ();
00112    TVectorT<Element> &Sqr ();
00113    TVectorT<Element> &Sqrt();
00114    TVectorT<Element> &Invert();
00115    TVectorT<Element> &SelectNonZeros(const TVectorT<Element> &select);
00116 
00117    Element Norm1   () const;
00118    Element Norm2Sqr() const;
00119    Element NormInf () const;
00120    Int_t   NonZeros() const;
00121    Element Sum     () const;
00122    Element Min     () const;
00123    Element Max     () const;
00124 
00125    inline const Element &operator()(Int_t index) const;
00126    inline       Element &operator()(Int_t index);
00127    inline const Element &operator[](Int_t index) const { return (*this)(index); }
00128    inline       Element &operator[](Int_t index)       { return (*this)(index); }
00129 
00130    TVectorT<Element> &operator= (const TVectorT                <Element> &source);
00131    TVectorT<Element> &operator= (const TMatrixTRow_const       <Element> &mr);
00132    TVectorT<Element> &operator= (const TMatrixTColumn_const    <Element> &mc);
00133    TVectorT<Element> &operator= (const TMatrixTDiag_const      <Element> &md);
00134    TVectorT<Element> &operator= (const TMatrixTSparseRow_const <Element> &md);
00135    TVectorT<Element> &operator= (const TMatrixTSparseDiag_const<Element> &md);
00136    template <class Element2> TVectorT<Element> &operator= (const TVectorT<Element2> &source)
00137    {
00138       if (!AreCompatible(*this,source)) {
00139          Error("operator=(const TVectorT2 &)","vectors not compatible");
00140          return *this;
00141       }
00142 
00143      TObject::operator=(source);
00144      const Element2 * const ps = source.GetMatrixArray();
00145            Element  * const pt = GetMatrixArray();
00146      for (Int_t i = 0; i < this->fNrows; i++)
00147         pt[i] = ps[i];
00148      return *this;
00149    }
00150 
00151    TVectorT<Element> &operator= (Element val);
00152    TVectorT<Element> &operator+=(Element val);
00153    TVectorT<Element> &operator-=(Element val);
00154    TVectorT<Element> &operator*=(Element val);
00155 
00156    TVectorT<Element> &operator+=(const TVectorT      <Element> &source);
00157    TVectorT<Element> &operator-=(const TVectorT      <Element> &source);
00158    TVectorT<Element> &operator*=(const TMatrixT      <Element> &a);
00159    TVectorT<Element> &operator*=(const TMatrixTSym   <Element> &a);
00160    TVectorT<Element> &operator*=(const TMatrixTSparse<Element> &a);
00161 
00162    Bool_t operator==(Element val) const;
00163    Bool_t operator!=(Element val) const;
00164    Bool_t operator< (Element val) const;
00165    Bool_t operator<=(Element val) const;
00166    Bool_t operator> (Element val) const;
00167    Bool_t operator>=(Element val) const;
00168 
00169    Bool_t MatchesNonZeroPattern(const TVectorT<Element> &select);
00170    Bool_t SomePositive         (const TVectorT<Element> &select);
00171    void   AddSomeConstant      (Element val,const TVectorT<Element> &select);
00172 
00173    void   Randomize            (Element alpha,Element beta,Double_t &seed);
00174 
00175    TVectorT<Element> &Apply(const TElementActionT   <Element> &action);
00176    TVectorT<Element> &Apply(const TElementPosActionT<Element> &action);
00177 
00178    void Add(const TVectorT<Element> &v);
00179    void Add(const TVectorT<Element> &v1, const TVectorT<Element> &v2);
00180    void Clear(Option_t * /*option*/ ="") { if (fIsOwner) Delete_m(fNrows,fElements);
00181                                            else fElements = 0; fNrows = 0; }
00182    void Draw (Option_t *option=""); // *MENU*
00183    void Print(Option_t *option="") const;  // *MENU*
00184 
00185    ClassDef(TVectorT,4)  // Template of Vector class
00186 };
00187 
00188 template<class Element> inline       TVectorT<Element> &TVectorT<Element>::Use     (Int_t n,Element *data) { return Use(0,n-1,data); }
00189 template<class Element> inline const TVectorT<Element> &TVectorT<Element>::Use     (Int_t n,const Element *data) const { return Use(0,n-1,data); }
00190 template<class Element> inline       TVectorT<Element> &TVectorT<Element>::Use     (TVectorT &v)
00191                                                                                    {
00192                                                                                      R__ASSERT(v.IsValid());
00193                                                                                      return Use(v.GetLwb(),v.GetUpb(),v.GetMatrixArray());
00194                                                                                    }
00195 template<class Element> inline const TVectorT<Element> &TVectorT<Element>::Use     (const TVectorT &v) const
00196                                                                                    {
00197                                                                                      R__ASSERT(v.IsValid());
00198                                                                                      return Use(v.GetLwb(),v.GetUpb(),v.GetMatrixArray());
00199                                                                                    }
00200 template<class Element> inline       TVectorT<Element>  TVectorT<Element>::GetSub  (Int_t row_lwb,Int_t row_upb,Option_t *option) const
00201                                                                                    {
00202                                                                                      TVectorT tmp;
00203                                                                                      this->GetSub(row_lwb,row_upb,tmp,option);
00204                                                                                      return tmp;
00205                                                                                    }
00206 
00207 template<class Element> inline const Element &TVectorT<Element>::operator()(Int_t ind) const
00208 {
00209    // Access a vector element.
00210 
00211    R__ASSERT(IsValid());
00212    const Int_t aind = ind-fRowLwb;
00213    if (aind >= fNrows || aind < 0) {
00214       Error("operator()","Request index(%d) outside vector range of %d - %d",ind,fRowLwb,fRowLwb+fNrows);
00215       return fElements[0];
00216    }
00217 
00218    return fElements[aind];
00219 }
00220 template<class Element> inline Element &TVectorT<Element>::operator()(Int_t ind)
00221 {
00222    // Access a vector element.
00223    
00224    R__ASSERT(IsValid());
00225    const Int_t aind = ind-fRowLwb;
00226    if (aind >= fNrows || aind < 0) {
00227       Error("operator()","Request index(%d) outside vector range of %d - %d",ind,fRowLwb,fRowLwb+fNrows);
00228       return fElements[0];
00229    }
00230    
00231    return fElements[aind];
00232 }
00233 
00234 template<class Element> Bool_t              operator==  (const TVectorT      <Element>  &source1,const TVectorT <Element>  &source2);
00235 template<class Element> TVectorT<Element>   operator+   (const TVectorT      <Element>  &source1,const TVectorT <Element>  &source2);
00236 template<class Element> TVectorT<Element>   operator-   (const TVectorT      <Element>  &source1,const TVectorT <Element>  &source2);
00237 template<class Element> Element             operator*   (const TVectorT      <Element>  &source1,const TVectorT <Element>  &source2);
00238 template<class Element> TVectorT<Element>   operator*   (const TMatrixT      <Element>  &a,      const TVectorT <Element>  &source);
00239 template<class Element> TVectorT<Element>   operator*   (const TMatrixTSym   <Element>  &a,      const TVectorT <Element>  &source);
00240 template<class Element> TVectorT<Element>   operator*   (const TMatrixTSparse<Element>  &a,      const TVectorT <Element>  &source);
00241 template<class Element> TVectorT<Element>   operator*   (      Element                   val,    const TVectorT <Element>  &source);
00242 
00243 template<class Element> Element             Dot         (const TVectorT      <Element>  &source1,const TVectorT <Element>  &source2);
00244 template <class Element1,class Element2>
00245                         TMatrixT<Element1>  OuterProduct(const TVectorT      <Element1> &v1,     const TVectorT <Element2> &v2);
00246 template <class Element1,class Element2,class Element3>
00247                         TMatrixT<Element1> &OuterProduct(      TMatrixT      <Element1> &target, const TVectorT <Element2> &v1,     const TVectorT      <Element3> &v2);
00248 template <class Element1,class Element2,class Element3>
00249                         Element1            Mult        (const TVectorT      <Element1> &v1,     const TMatrixT <Element2> &m,      const TVectorT      <Element3> &v2);
00250 
00251 template<class Element> TVectorT<Element>  &Add         (      TVectorT      <Element>  &target,       Element              scalar, const TVectorT      <Element>  &source);
00252 template<class Element> TVectorT<Element>  &Add         (      TVectorT      <Element>  &target,       Element              scalar, const TMatrixT      <Element>  &a,
00253                                                          const TVectorT<Element> &source);
00254 template<class Element> TVectorT<Element>  &Add         (      TVectorT      <Element>  &target,       Element              scalar, const TMatrixTSym   <Element>  &a,
00255                                                          const TVectorT<Element> &source);
00256 template<class Element> TVectorT<Element>  &Add         (      TVectorT      <Element>  &target,       Element              scalar, const TMatrixTSparse<Element>  &a,
00257                                                          const TVectorT<Element> &source);
00258 template<class Element> TVectorT<Element>  &AddElemMult (      TVectorT      <Element>  &target,       Element              scalar, const TVectorT      <Element>  &source1,
00259                                                          const TVectorT      <Element>  &source2);
00260 template<class Element> TVectorT<Element>  &AddElemMult (      TVectorT      <Element>  &target,       Element              scalar, const TVectorT      <Element>  &source1,
00261                                                          const TVectorT      <Element>  &source2,const TVectorT <Element>  &select);
00262 template<class Element> TVectorT<Element>  &AddElemDiv  (      TVectorT      <Element>  &target,       Element              scalar, const TVectorT      <Element>  &source1,
00263                                                          const TVectorT      <Element>  &source2);
00264 template<class Element> TVectorT<Element>  &AddElemDiv  (      TVectorT      <Element>  &target,       Element              scalar, const TVectorT      <Element>  &source1,
00265                                                          const TVectorT      <Element>  &source2,const TVectorT <Element>  &select);
00266 template<class Element> TVectorT<Element>  &ElementMult (      TVectorT      <Element>  &target, const TVectorT <Element>  &source);
00267 template<class Element> TVectorT<Element>  &ElementMult (      TVectorT      <Element>  &target, const TVectorT <Element>  &source, const TVectorT      <Element>  &select);
00268 template<class Element> TVectorT<Element>  &ElementDiv  (      TVectorT      <Element>  &target, const TVectorT <Element>  &source);
00269 template<class Element> TVectorT<Element>  &ElementDiv  (      TVectorT      <Element>  &target, const TVectorT <Element>  &source, const TVectorT      <Element>  &select);
00270 
00271 template<class Element1,class Element2> Bool_t AreCompatible(const TVectorT<Element1> &v1,const TVectorT<Element2> &v2,Int_t verbose=0);
00272 // Check matrix and vector for compatibility in multiply:  M * v and v * M
00273 template<class Element1,class Element2> Bool_t AreCompatible(const TMatrixT<Element1> &m, const TVectorT<Element2> &v, Int_t verbose=0);
00274 template<class Element1,class Element2> Bool_t AreCompatible(const TVectorT<Element1> &v, const TMatrixT<Element2> &m, Int_t verbose=0);
00275 
00276 template<class Element> void   Compare              (const TVectorT <Element>  &source1,const TVectorT <Element>  &source2);
00277 template<class Element> Bool_t VerifyVectorValue    (const TVectorT <Element>  &m,            Element val,Int_t verbose, Element maxDevAllow);
00278 template<class Element> Bool_t VerifyVectorValue    (const TVectorT <Element>  &m,            Element val,Int_t verbose)
00279                                                      { return VerifyVectorValue(m,val,verbose,Element(0.0)); }
00280 template<class Element> Bool_t VerifyVectorValue    (const TVectorT <Element>  &m,            Element val)
00281                                                      { return VerifyVectorValue(m,val,1,Element(0.0)); }
00282 template<class Element> Bool_t VerifyVectorIdentity (const TVectorT <Element>  &m1,const TVectorT <Element> &m2, Int_t verbose, Element maxDevAllow);
00283 template<class Element> Bool_t VerifyVectorIdentity (const TVectorT <Element>  &m1,const TVectorT <Element> &m2, Int_t verbose)
00284                                                      { return VerifyVectorIdentity(m1,m2,verbose,Element(0.0)); }
00285 template<class Element> Bool_t VerifyVectorIdentity (const TVectorT <Element>  &m1,const TVectorT <Element> &m2)
00286                                                      { return VerifyVectorIdentity(m1,m2,1,Element(0.0)); }
00287 
00288 #endif

Generated on Tue Jul 5 14:28:27 2011 for ROOT_528-00b_version by  doxygen 1.5.1