Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/root/TVectorT.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // @(#)root/matrix:$Id$
0002 // Authors: Fons Rademakers, Eddy Offermann   Nov 2003
0003 
0004 /*************************************************************************
0005  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
0006  * All rights reserved.                                                  *
0007  *                                                                       *
0008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
0009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
0010  *************************************************************************/
0011 
0012 #ifndef ROOT_TVectorT
0013 #define ROOT_TVectorT
0014 
0015 //////////////////////////////////////////////////////////////////////////
0016 //                                                                      //
0017 // TVectorT                                                             //
0018 //                                                                      //
0019 // Template class of Vectors in the linear algebra package              //
0020 //                                                                      //
0021 //////////////////////////////////////////////////////////////////////////
0022 
0023 #include "TMatrixT.h"
0024 #include "TMatrixTSym.h"
0025 #include "TMatrixTSparse.h"
0026 
0027 #include <ROOT/RSpan.hxx>
0028 
0029 template<class Element> class TVectorT : public TObject {
0030 
0031 protected:
0032    Int_t    fNrows{0};             // number of rows
0033    Int_t    fRowLwb{0};            // lower bound of the row index
0034    Element *fElements{nullptr};    //[fNrows] elements themselves
0035 
0036    enum {kSizeMax = 5};            // size data container on stack, see New_m(),Delete_m()
0037    enum {kWorkMax = 100};          // size of work array's in several routines
0038 
0039    Element  fDataStack[kSizeMax];  //! data container
0040    Bool_t   fIsOwner{kTRUE};       //!default kTRUE, when Use array kFALSE
0041 
0042    Element* New_m   (Int_t size);
0043    void     Delete_m(Int_t size,Element*&);
0044    Int_t    Memcpy_m(Element *newp,const Element *oldp,Int_t copySize,
0045                      Int_t newSize,Int_t oldSize);
0046 
0047    void     Allocate(Int_t nrows,Int_t row_lwb = 0,Int_t init = 0);
0048 
0049    enum EVectorStatusBits {
0050      kStatus = BIT(14) // set if vector object is valid
0051    };
0052 
0053 public:
0054 
0055    TVectorT() : fNrows(0), fRowLwb(0), fElements(nullptr), fDataStack (), fIsOwner(kTRUE) { }
0056    explicit TVectorT(Int_t n);
0057    TVectorT(Int_t lwb,Int_t upb);
0058    TVectorT(Int_t n,const Element *elements);
0059    TVectorT(Int_t lwb,Int_t upb,const Element *elements);
0060    TVectorT(const TVectorT            <Element> &another);
0061    TVectorT(const TMatrixTRow_const   <Element> &mr);
0062    TVectorT(const TMatrixTColumn_const<Element> &mc);
0063    TVectorT(const TMatrixTDiag_const  <Element> &md);
0064    template <class Element2> TVectorT(const TVectorT<Element2> &another)
0065    {
0066       R__ASSERT(another.IsValid());
0067       Allocate(another.GetUpb()-another.GetLwb()+1,another.GetLwb());
0068       *this = another;
0069    }
0070    TVectorT(Int_t lwb,Int_t upb,Double_t iv1, ...);
0071    ~TVectorT() override { TVectorT::Clear(); }
0072 
0073    inline          Int_t     GetLwb       () const { return fRowLwb; }
0074    inline          Int_t     GetUpb       () const { return fNrows+fRowLwb-1; }
0075    inline          Int_t     GetNrows     () const { return fNrows; }
0076    inline          Int_t     GetNoElements() const { return fNrows; }
0077 
0078    inline          Element  *GetMatrixArray  ()       { return fElements; }
0079    inline const    Element  *GetMatrixArray  () const { return fElements; }
0080 
0081    // For compatibility with STL classes
0082    inline          std::size_t size() const { return fNrows; }
0083 
0084    inline          Element  *data()       { return fElements; }
0085    inline const    Element  *data() const { return fElements; }
0086 
0087    // Implicit conversion of TVectorT to std::span, both non-const and const
0088    // version. Can be removed once the minimum C++ standard in C++20, because
0089    // then it's enough to implement the contiguous_range and sized_range
0090    // concepts. This should alredy be the case since data() and size() are
0091    // available.
0092    inline operator std::span<Element>()
0093    {
0094       return std::span<Element>{data(), size()};
0095    }
0096    inline operator std::span<const Element>() const
0097    {
0098       return std::span<const Element>{data(), size()};
0099    }
0100 
0101    inline void     Invalidate ()       { SetBit(kStatus); }
0102    inline void     MakeValid  ()       { ResetBit(kStatus); }
0103    inline Bool_t   IsValid    () const { return !TestBit(kStatus); }
0104    inline Bool_t   IsOwner    () const { return fIsOwner; }
0105    inline void     SetElements(const Element *elements) { R__ASSERT(IsValid());
0106                                                           memcpy(fElements,elements,fNrows*sizeof(Element)); }
0107    inline TVectorT<Element> &Shift     (Int_t row_shift)            { fRowLwb += row_shift; return *this; }
0108           TVectorT<Element> &ResizeTo  (Int_t lwb,Int_t upb);
0109    inline TVectorT<Element> &ResizeTo  (Int_t n)                    { return ResizeTo(0,n-1); }
0110    inline TVectorT<Element> &ResizeTo  (const TVectorT<Element> &v) { return ResizeTo(v.GetLwb(),v.GetUpb()); }
0111 
0112           TVectorT<Element> &Use       (Int_t lwb,Int_t upb,Element *data);
0113    const  TVectorT<Element> &Use       (Int_t lwb,Int_t upb,const Element *data) const
0114           { return (const TVectorT<Element>&)(const_cast<TVectorT<Element> *>(this))->Use(lwb,upb,const_cast<Element *>(data)); }
0115           TVectorT<Element> &Use       (Int_t n,Element *data);
0116    const  TVectorT<Element> &Use       (Int_t n,const Element *data) const ;
0117           TVectorT<Element> &Use       (TVectorT<Element> &v);
0118    const  TVectorT<Element> &Use       (const TVectorT<Element> &v) const ;
0119 
0120           TVectorT<Element> &GetSub    (Int_t row_lwb,Int_t row_upb,TVectorT<Element> &target,Option_t *option="S") const;
0121           TVectorT<Element>  GetSub    (Int_t row_lwb,Int_t row_upb,Option_t *option="S") const;
0122           TVectorT<Element> &SetSub    (Int_t row_lwb,const TVectorT<Element> &source);
0123 
0124    TVectorT<Element> &Zero();
0125    TVectorT<Element> &Abs ();
0126    TVectorT<Element> &Sqr ();
0127    TVectorT<Element> &Sqrt();
0128    TVectorT<Element> &Invert();
0129    TVectorT<Element> &SelectNonZeros(const TVectorT<Element> &select);
0130 
0131    Element Norm1   () const;
0132    Element Norm2Sqr() const;
0133    Element NormInf () const;
0134    Int_t   NonZeros() const;
0135    Element Sum     () const;
0136    Element Min     () const;
0137    Element Max     () const;
0138 
0139    inline const Element &operator()(Int_t index) const;
0140    inline       Element &operator()(Int_t index);
0141    inline const Element &operator[](Int_t index) const { return (*this)(index); }
0142    inline       Element &operator[](Int_t index)       { return (*this)(index); }
0143 
0144    TVectorT<Element> &operator= (const TVectorT                <Element> &source);
0145    TVectorT<Element> &operator= (const TMatrixTRow_const       <Element> &mr);
0146    TVectorT<Element> &operator= (const TMatrixTColumn_const    <Element> &mc);
0147    TVectorT<Element> &operator= (const TMatrixTDiag_const      <Element> &md);
0148    TVectorT<Element> &operator= (const TMatrixTSparseRow_const <Element> &md);
0149    TVectorT<Element> &operator= (const TMatrixTSparseDiag_const<Element> &md);
0150    template <class Element2> TVectorT<Element> &operator= (const TVectorT<Element2> &source)
0151    {
0152       if (!AreCompatible(*this,source)) {
0153          Error("operator=(const TVectorT2 &)","vectors not compatible");
0154          return *this;
0155       }
0156 
0157      TObject::operator=(source);
0158      const Element2 * const ps = source.GetMatrixArray();
0159            Element  * const pt = GetMatrixArray();
0160      for (Int_t i = 0; i < this->fNrows; i++)
0161         pt[i] = ps[i];
0162      return *this;
0163    }
0164 
0165    TVectorT<Element> &operator= (Element val);
0166    TVectorT<Element> &operator+=(Element val);
0167    TVectorT<Element> &operator-=(Element val);
0168    TVectorT<Element> &operator*=(Element val);
0169 
0170    TVectorT<Element> &operator+=(const TVectorT      <Element> &source);
0171    TVectorT<Element> &operator-=(const TVectorT      <Element> &source);
0172    TVectorT<Element> &operator*=(const TMatrixT      <Element> &a);
0173    TVectorT<Element> &operator*=(const TMatrixTSym   <Element> &a);
0174    TVectorT<Element> &operator*=(const TMatrixTSparse<Element> &a);
0175 
0176    Bool_t operator==(Element val) const;
0177    Bool_t operator!=(Element val) const;
0178    Bool_t operator< (Element val) const;
0179    Bool_t operator<=(Element val) const;
0180    Bool_t operator> (Element val) const;
0181    Bool_t operator>=(Element val) const;
0182 
0183    Bool_t MatchesNonZeroPattern(const TVectorT<Element> &select);
0184    Bool_t SomePositive         (const TVectorT<Element> &select);
0185    void   AddSomeConstant      (Element val,const TVectorT<Element> &select);
0186 
0187    void   Randomize            (Element alpha,Element beta,Double_t &seed);
0188 
0189    TVectorT<Element> &Apply(const TElementActionT   <Element> &action);
0190    TVectorT<Element> &Apply(const TElementPosActionT<Element> &action);
0191 
0192    void Add(const TVectorT<Element> &v);
0193    void Add(const TVectorT<Element> &v1, const TVectorT<Element> &v2);
0194    void Clear(Option_t * /*option*/ = "") override
0195    {
0196       if (fIsOwner)
0197          Delete_m(fNrows, fElements);
0198       else
0199          fElements = nullptr;
0200       fNrows = 0;
0201    }
0202    void Draw(Option_t *option = "") override;       // *MENU*
0203    void Print(Option_t *option = "") const override;  // *MENU*
0204 
0205    ClassDefOverride(TVectorT,4)  // Template of Vector class
0206 };
0207 
0208 // When building with -fmodules, it instantiates all pending instantiations,
0209 // instead of delaying them until the end of the translation unit.
0210 // We 'got away with' probably because the use and the definition of the
0211 // explicit specialization do not occur in the same TU.
0212 //
0213 // In case we are building with -fmodules, we need to forward declare the
0214 // specialization in order to compile the dictionary G__Matrix.cxx.
0215 template <> TClass *TVectorT<double>::Class();
0216 
0217 template<class Element> inline       TVectorT<Element> &TVectorT<Element>::Use     (Int_t n,Element *data) { return Use(0,n-1,data); }
0218 template<class Element> inline const TVectorT<Element> &TVectorT<Element>::Use     (Int_t n,const Element *data) const { return Use(0,n-1,data); }
0219 template<class Element> inline       TVectorT<Element> &TVectorT<Element>::Use     (TVectorT &v)
0220                                                                                    {
0221                                                                                      R__ASSERT(v.IsValid());
0222                                                                                      return Use(v.GetLwb(),v.GetUpb(),v.GetMatrixArray());
0223                                                                                    }
0224 template<class Element> inline const TVectorT<Element> &TVectorT<Element>::Use     (const TVectorT &v) const
0225                                                                                    {
0226                                                                                      R__ASSERT(v.IsValid());
0227                                                                                      return Use(v.GetLwb(),v.GetUpb(),v.GetMatrixArray());
0228                                                                                    }
0229 template<class Element> inline       TVectorT<Element>  TVectorT<Element>::GetSub  (Int_t row_lwb,Int_t row_upb,Option_t *option) const
0230                                                                                    {
0231                                                                                      TVectorT tmp;
0232                                                                                      this->GetSub(row_lwb,row_upb,tmp,option);
0233                                                                                      return tmp;
0234                                                                                    }
0235 
0236 template<class Element> inline const Element &TVectorT<Element>::operator()(Int_t ind) const
0237 {
0238    // Access a vector element.
0239 
0240    R__ASSERT(IsValid());
0241    const Int_t aind = ind-fRowLwb;
0242    if (aind >= fNrows || aind < 0) {
0243       Error("operator()","Request index(%d) outside vector range of %d - %d",ind,fRowLwb,fRowLwb+fNrows);
0244       return TMatrixTBase<Element>::NaNValue();
0245    }
0246 
0247    return fElements[aind];
0248 }
0249 template<class Element> inline Element &TVectorT<Element>::operator()(Int_t ind)
0250 {
0251    // Access a vector element.
0252 
0253    R__ASSERT(IsValid());
0254    const Int_t aind = ind-fRowLwb;
0255    if (aind >= fNrows || aind < 0) {
0256       Error("operator()","Request index(%d) outside vector range of %d - %d",ind,fRowLwb,fRowLwb+fNrows);
0257       return TMatrixTBase<Element>::NaNValue();
0258    }
0259 
0260    return fElements[aind];
0261 }
0262 inline namespace TMatrixTAutoloadOps {
0263 
0264 template<class Element> Bool_t              operator==  (const TVectorT      <Element>  &source1,const TVectorT <Element>  &source2);
0265 template<class Element> TVectorT<Element>   operator+   (const TVectorT      <Element>  &source1,const TVectorT <Element>  &source2);
0266 template<class Element> TVectorT<Element>   operator-   (const TVectorT      <Element>  &source1,const TVectorT <Element>  &source2);
0267 template<class Element> Element             operator*   (const TVectorT      <Element>  &source1,const TVectorT <Element>  &source2);
0268 template<class Element> TVectorT<Element>   operator*   (const TMatrixT      <Element>  &a,      const TVectorT <Element>  &source);
0269 template<class Element> TVectorT<Element>   operator*   (const TMatrixTSym   <Element>  &a,      const TVectorT <Element>  &source);
0270 template<class Element> TVectorT<Element>   operator*   (const TMatrixTSparse<Element>  &a,      const TVectorT <Element>  &source);
0271 template<class Element> TVectorT<Element>   operator*   (      Element                   val,    const TVectorT <Element>  &source);
0272 template<class Element>
0273 inline
0274 TVectorT<Element> operator*   (const TVectorT <Element>  &source, Element val) { return val * source; }
0275 
0276 template<class Element> Element             Dot         (const TVectorT      <Element>  &source1,const TVectorT <Element>  &source2);
0277 template <class Element1,class Element2>
0278                         TMatrixT<Element1>  OuterProduct(const TVectorT      <Element1> &v1,     const TVectorT <Element2> &v2);
0279 template <class Element1,class Element2,class Element3>
0280                         TMatrixT<Element1> &OuterProduct(      TMatrixT      <Element1> &target, const TVectorT <Element2> &v1,     const TVectorT      <Element3> &v2);
0281 template <class Element1,class Element2,class Element3>
0282                         Element1            Mult        (const TVectorT      <Element1> &v1,     const TMatrixT <Element2> &m,      const TVectorT      <Element3> &v2);
0283 
0284 template<class Element> TVectorT<Element>  &Add         (      TVectorT      <Element>  &target,       Element              scalar, const TVectorT      <Element>  &source);
0285 template<class Element> TVectorT<Element>  &Add         (      TVectorT      <Element>  &target,       Element              scalar, const TMatrixT      <Element>  &a,
0286                                                          const TVectorT<Element> &source);
0287 template<class Element> TVectorT<Element>  &Add         (      TVectorT      <Element>  &target,       Element              scalar, const TMatrixTSym   <Element>  &a,
0288                                                          const TVectorT<Element> &source);
0289 template<class Element> TVectorT<Element>  &Add         (      TVectorT      <Element>  &target,       Element              scalar, const TMatrixTSparse<Element>  &a,
0290                                                          const TVectorT<Element> &source);
0291 template<class Element> TVectorT<Element>  &AddElemMult (      TVectorT      <Element>  &target,       Element              scalar, const TVectorT      <Element>  &source1,
0292                                                          const TVectorT      <Element>  &source2);
0293 template<class Element> TVectorT<Element>  &AddElemMult (      TVectorT      <Element>  &target,       Element              scalar, const TVectorT      <Element>  &source1,
0294                                                          const TVectorT      <Element>  &source2,const TVectorT <Element>  &select);
0295 template<class Element> TVectorT<Element>  &AddElemDiv  (      TVectorT      <Element>  &target,       Element              scalar, const TVectorT      <Element>  &source1,
0296                                                          const TVectorT      <Element>  &source2);
0297 template<class Element> TVectorT<Element>  &AddElemDiv  (      TVectorT      <Element>  &target,       Element              scalar, const TVectorT      <Element>  &source1,
0298                                                          const TVectorT      <Element>  &source2,const TVectorT <Element>  &select);
0299 template<class Element> TVectorT<Element>  &ElementMult (      TVectorT      <Element>  &target, const TVectorT <Element>  &source);
0300 template<class Element> TVectorT<Element>  &ElementMult (      TVectorT      <Element>  &target, const TVectorT <Element>  &source, const TVectorT      <Element>  &select);
0301 template<class Element> TVectorT<Element>  &ElementDiv  (      TVectorT      <Element>  &target, const TVectorT <Element>  &source);
0302 template<class Element> TVectorT<Element>  &ElementDiv  (      TVectorT      <Element>  &target, const TVectorT <Element>  &source, const TVectorT      <Element>  &select);
0303 
0304 template<class Element1,class Element2> Bool_t AreCompatible(const TVectorT<Element1> &v1,const TVectorT<Element2> &v2,Int_t verbose=0);
0305 // Check matrix and vector for compatibility in multiply:  M * v and v * M
0306 template<class Element1,class Element2> Bool_t AreCompatible(const TMatrixT<Element1> &m, const TVectorT<Element2> &v, Int_t verbose=0);
0307 template<class Element1,class Element2> Bool_t AreCompatible(const TVectorT<Element1> &v, const TMatrixT<Element2> &m, Int_t verbose=0);
0308 
0309 template<class Element> void   Compare              (const TVectorT <Element>  &source1,const TVectorT <Element>  &source2);
0310 template<class Element> Bool_t VerifyVectorValue    (const TVectorT <Element>  &m,            Element val,Int_t verbose, Element maxDevAllow);
0311 template<class Element> Bool_t VerifyVectorValue    (const TVectorT <Element>  &m,            Element val,Int_t verbose)
0312                                                      { return VerifyVectorValue(m,val,verbose,Element(0.0)); }
0313 template<class Element> Bool_t VerifyVectorValue    (const TVectorT <Element>  &m,            Element val)
0314                                                      { return VerifyVectorValue(m,val,1,Element(0.0)); }
0315 template<class Element> Bool_t VerifyVectorIdentity (const TVectorT <Element>  &m1,const TVectorT <Element> &m2, Int_t verbose, Element maxDevAllow);
0316 template<class Element> Bool_t VerifyVectorIdentity (const TVectorT <Element>  &m1,const TVectorT <Element> &m2, Int_t verbose)
0317                                                      { return VerifyVectorIdentity(m1,m2,verbose,Element(0.0)); }
0318 template<class Element> Bool_t VerifyVectorIdentity (const TVectorT <Element>  &m1,const TVectorT <Element> &m2)
0319                                                      { return VerifyVectorIdentity(m1,m2,1,Element(0.0)); }
0320 } // inline namespace TMatrixTAutoloadOps
0321 #endif