// @(#)root/matrix:$Id$ // Authors: Fons Rademakers, Eddy Offermann Nov 2003 /************************************************************************* * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. * * All rights reserved. * * * * For the licensing terms see $ROOTSYS/LICENSE. * * For the list of contributors see $ROOTSYS/README/CREDITS. * *************************************************************************/ #ifndef ROOT_TMatrixTSym #define ROOT_TMatrixTSym ////////////////////////////////////////////////////////////////////////// // // // TMatrixTSym // // // // Implementation of a symmetric matrix in the linear algebra package // // // // Note that in this implementation both matrix element m[i][j] and // // m[j][i] are updated and stored in memory . However, when making the // // object persistent only the upper right triangle is stored . // // // ////////////////////////////////////////////////////////////////////////// #include "TMatrixTBase.h" #include "TMatrixTUtils.h" templateclass TMatrixT; templateclass TMatrixTSymLazy; templateclass TVectorT; template class TMatrixTSym : public TMatrixTBase { protected: Element fDataStack[TMatrixTBase::kSizeMax]; //! data container Element *fElements; //[fNelems] elements themselves Element *New_m (Int_t size); void Delete_m(Int_t size,Element*&); Int_t Memcpy_m(Element *newp,const Element *oldp,Int_t copySize, Int_t newSize,Int_t oldSize); void Allocate(Int_t nrows,Int_t ncols,Int_t row_lwb = 0,Int_t col_lwb = 0,Int_t init = 0, Int_t /*nr_nonzeros*/ = -1); public: enum {kWorkMax = 100}; // size of work array enum EMatrixCreatorsOp1 { kZero,kUnit,kTransposed,kInverted,kAtA }; enum EMatrixCreatorsOp2 { kPlus,kMinus }; TMatrixTSym() { fElements = nullptr; } explicit TMatrixTSym(Int_t nrows); TMatrixTSym(Int_t row_lwb,Int_t row_upb); TMatrixTSym(Int_t nrows,const Element *data,Option_t *option=""); TMatrixTSym(Int_t row_lwb,Int_t row_upb,const Element *data,Option_t *option=""); TMatrixTSym(const TMatrixTSym &another); template TMatrixTSym(const TMatrixTSym &another) { R__ASSERT(another.IsValid()); Allocate(another.GetNrows(),another.GetNcols(),another.GetRowLwb(),another.GetColLwb()); *this = another; } TMatrixTSym(EMatrixCreatorsOp1 op,const TMatrixTSym &prototype); TMatrixTSym(EMatrixCreatorsOp1 op,const TMatrixT &prototype); TMatrixTSym(const TMatrixTSym &a,EMatrixCreatorsOp2 op,const TMatrixTSym &b); TMatrixTSym(const TMatrixTSymLazy &lazy_constructor); ~TMatrixTSym() override { TMatrixTSym::Clear(); } // Elementary constructors void TMult(const TMatrixT &a); void TMult(const TMatrixTSym &a); void Mult (const TMatrixTSym &a) { TMult(a); } void Plus (const TMatrixTSym &a,const TMatrixTSym &b); void Minus(const TMatrixTSym &a,const TMatrixTSym &b); const Element *GetMatrixArray () const override; Element *GetMatrixArray () override; const Int_t *GetRowIndexArray() const override { return nullptr; } Int_t *GetRowIndexArray() override { return nullptr; } const Int_t *GetColIndexArray() const override { return nullptr; } Int_t *GetColIndexArray() override { return nullptr; } TMatrixTBase &SetRowIndexArray(Int_t * /*data*/) override { MayNotUse("SetRowIndexArray(Int_t *)"); return *this; } TMatrixTBase &SetColIndexArray(Int_t * /*data*/) override { MayNotUse("SetColIndexArray(Int_t *)"); return *this; } void Clear (Option_t * /*option*/ ="") override { if (this->fIsOwner) Delete_m(this->fNelems,fElements); else fElements = nullptr; this->fNelems = 0; } Bool_t IsSymmetric() const override { return kTRUE; } TMatrixTSym &Use (Int_t row_lwb,Int_t row_upb,Element *data); const TMatrixTSym &Use (Int_t row_lwb,Int_t row_upb,const Element *data) const { return (const TMatrixTSym&) ((const_cast *>(this))->Use(row_lwb,row_upb,const_cast(data))); } TMatrixTSym &Use (Int_t nrows,Element *data); const TMatrixTSym &Use (Int_t nrows,const Element *data) const; TMatrixTSym &Use (TMatrixTSym &a); const TMatrixTSym &Use (const TMatrixTSym &a) const; TMatrixTSym &GetSub (Int_t row_lwb,Int_t row_upb,TMatrixTSym &target,Option_t *option="S") const; TMatrixTBase &GetSub (Int_t row_lwb,Int_t row_upb,Int_t col_lwb,Int_t col_upb, TMatrixTBase &target,Option_t *option="S") const override; TMatrixTSym GetSub (Int_t row_lwb,Int_t row_upb,Int_t col_lwb,Int_t col_upb,Option_t *option="S") const; TMatrixTSym &SetSub (Int_t row_lwb,const TMatrixTBase &source); TMatrixTBase &SetSub (Int_t row_lwb,Int_t col_lwb,const TMatrixTBase &source) override; TMatrixTBase &SetMatrixArray(const Element *data, Option_t *option="") override; TMatrixTBase &Shift (Int_t row_shift,Int_t col_shift) override; TMatrixTBase &ResizeTo (Int_t nrows,Int_t ncols,Int_t /*nr_nonzeros*/ =-1) override; TMatrixTBase &ResizeTo (Int_t row_lwb,Int_t row_upb,Int_t col_lwb,Int_t col_upb,Int_t /*nr_nonzeros*/ =-1) override; inline TMatrixTBase &ResizeTo (const TMatrixTSym &m) { return ResizeTo(m.GetRowLwb(),m.GetRowUpb(),m.GetColLwb(),m.GetColUpb()); } Double_t Determinant () const override; void Determinant (Double_t &d1,Double_t &d2) const override; TMatrixTSym &Invert (Double_t *det=nullptr); TMatrixTSym &InvertFast (Double_t *det=nullptr); TMatrixTSym &Transpose (const TMatrixTSym &source); inline TMatrixTSym &T () { return this->Transpose(*this); } TMatrixTSym &Rank1Update (const TVectorT &v,Element alpha=1.0); TMatrixTSym &Similarity (const TMatrixT &n); TMatrixTSym &Similarity (const TMatrixTSym &n); Element Similarity (const TVectorT &v) const; TMatrixTSym &SimilarityT (const TMatrixT &n); // Either access a_ij as a(i,j) inline Element operator()(Int_t rown,Int_t coln) const override; inline Element &operator()(Int_t rown,Int_t coln) override; // or as a[i][j] inline const TMatrixTRow_const operator[](Int_t rown) const { return TMatrixTRow_const(*this,rown); } inline TMatrixTRow operator[](Int_t rown) { return TMatrixTRow (*this,rown); } TMatrixTSym &operator= (const TMatrixTSym &source); TMatrixTSym &operator= (const TMatrixTSymLazy &source); template TMatrixTSym &operator= (const TMatrixTSym &source) { if (!AreCompatible(*this,source)) { Error("operator=(const TMatrixTSym2 &)","matrices not compatible"); return *this; } TObject::operator=(source); const Element2 * const ps = source.GetMatrixArray(); Element * const pt = this->GetMatrixArray(); for (Int_t i = 0; i < this->fNelems; i++) pt[i] = ps[i]; this->fTol = source.GetTol(); return *this; } TMatrixTSym &operator= (Element val); TMatrixTSym &operator-=(Element val); TMatrixTSym &operator+=(Element val); TMatrixTSym &operator*=(Element val); TMatrixTSym &operator+=(const TMatrixTSym &source); TMatrixTSym &operator-=(const TMatrixTSym &source); TMatrixTBase &Apply(const TElementActionT &action) override; TMatrixTBase &Apply(const TElementPosActionT &action) override; TMatrixTBase &Randomize (Element alpha,Element beta,Double_t &seed) override; virtual TMatrixTSym &RandomizePD(Element alpha,Element beta,Double_t &seed); const TMatrixT EigenVectors(TVectorT &eigenValues) const; ClassDefOverride(TMatrixTSym,2) // Template of Symmetric Matrix class }; #ifndef __CINT__ // When building with -fmodules, it instantiates all pending instantiations, // instead of delaying them until the end of the translation unit. // We 'got away with' probably because the use and the definition of the // explicit specialization do not occur in the same TU. // // In case we are building with -fmodules, we need to forward declare the // specialization in order to compile the dictionary G__Matrix.cxx. template <> TClass *TMatrixTSym::Class(); #endif // __CINT__ template inline const Element *TMatrixTSym::GetMatrixArray() const { return fElements; } template inline Element *TMatrixTSym::GetMatrixArray() { return fElements; } template inline TMatrixTSym &TMatrixTSym::Use (Int_t nrows,Element *data) { return Use(0,nrows-1,data); } template inline const TMatrixTSym &TMatrixTSym::Use (Int_t nrows,const Element *data) const { return Use(0,nrows-1,data); } template inline TMatrixTSym &TMatrixTSym::Use (TMatrixTSym &a) { return Use(a.GetRowLwb(),a.GetRowUpb(),a.GetMatrixArray()); } template inline const TMatrixTSym &TMatrixTSym::Use (const TMatrixTSym &a) const { return Use(a.GetRowLwb(),a.GetRowUpb(),a.GetMatrixArray()); } template inline TMatrixTSym TMatrixTSym::GetSub (Int_t row_lwb,Int_t row_upb,Int_t col_lwb,Int_t col_upb, Option_t *option) const { TMatrixTSym tmp; this->GetSub(row_lwb,row_upb,col_lwb,col_upb,tmp,option); return tmp; } template inline Element TMatrixTSym::operator()(Int_t rown,Int_t coln) const { R__ASSERT(this->IsValid()); const Int_t arown = rown-this->fRowLwb; const Int_t acoln = coln-this->fColLwb; if (arown >= this->fNrows || arown < 0) { Error("operator()","Request row(%d) outside matrix range of %d - %d",rown,this->fRowLwb,this->fRowLwb+this->fNrows); return TMatrixTBase::NaNValue(); } if (acoln >= this->fNcols || acoln < 0) { Error("operator()","Request column(%d) outside matrix range of %d - %d",coln,this->fColLwb,this->fColLwb+this->fNcols); return TMatrixTBase::NaNValue(); } return (fElements[arown*this->fNcols+acoln]); } template inline Element &TMatrixTSym::operator()(Int_t rown,Int_t coln) { R__ASSERT(this->IsValid()); const Int_t arown = rown-this->fRowLwb; const Int_t acoln = coln-this->fColLwb; if (arown >= this->fNrows || arown < 0) { Error("operator()","Request row(%d) outside matrix range of %d - %d",rown,this->fRowLwb,this->fRowLwb+this->fNrows); return TMatrixTBase::NaNValue(); } if (acoln >= this->fNcols || acoln < 0) { Error("operator()","Request column(%d) outside matrix range of %d - %d",coln,this->fColLwb,this->fColLwb+this->fNcols); return TMatrixTBase::NaNValue(); } return (fElements[arown*this->fNcols+acoln]); } template Bool_t operator== (const TMatrixTSym &source1,const TMatrixTSym &source2); template TMatrixTSym operator+ (const TMatrixTSym &source1,const TMatrixTSym &source2); template TMatrixTSym operator+ (const TMatrixTSym &source1, Element val); template TMatrixTSym operator+ ( Element val ,const TMatrixTSym &source2); template TMatrixTSym operator- (const TMatrixTSym &source1,const TMatrixTSym &source2); template TMatrixTSym operator- (const TMatrixTSym &source1, Element val); template TMatrixTSym operator- ( Element val ,const TMatrixTSym &source2); template TMatrixTSym operator* (const TMatrixTSym &source, Element val ); template TMatrixTSym operator* ( Element val, const TMatrixTSym &source ); // Preventing warnings with -Weffc++ in GCC since overloading the || and && operators was a design choice. #if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) >= 40600 #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Weffc++" #endif template TMatrixTSym operator&& (const TMatrixTSym &source1,const TMatrixTSym &source2); template TMatrixTSym operator|| (const TMatrixTSym &source1,const TMatrixTSym &source2); #if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) >= 40600 #pragma GCC diagnostic pop #endif template TMatrixTSym operator> (const TMatrixTSym &source1,const TMatrixTSym &source2); template TMatrixTSym operator>= (const TMatrixTSym &source1,const TMatrixTSym &source2); template TMatrixTSym operator<= (const TMatrixTSym &source1,const TMatrixTSym &source2); template TMatrixTSym operator< (const TMatrixTSym &source1,const TMatrixTSym &source2); template TMatrixTSym &Add (TMatrixTSym &target, Element scalar,const TMatrixTSym &source); template TMatrixTSym &ElementMult(TMatrixTSym &target,const TMatrixTSym &source); template TMatrixTSym &ElementDiv (TMatrixTSym &target,const TMatrixTSym &source); #endif