Skip to content

Commit

Permalink
[matrix] Fast element setter method (#15606)
Browse files Browse the repository at this point in the history
* [matrix] Add SetElement method

* [matrix] Add SetElement test
  • Loading branch information
mdessole authored May 24, 2024
1 parent a36c746 commit 985077f
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
18 changes: 18 additions & 0 deletions math/matrix/inc/TMatrixT.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "Rtypes.h"
#include "TError.h"

#include <cassert>

template<class Element> class TMatrixTSym;
template<class Element> class TMatrixTSparse;
Expand Down Expand Up @@ -107,6 +108,8 @@ template<class Element> class TMatrixT : public TMatrixTBase<Element> {
void MultT(const TMatrixTSym<Element> &a,const TMatrixT <Element> &b);
void MultT(const TMatrixTSym<Element> &a,const TMatrixTSym<Element> &b) { Mult(a,b); }

inline void SetElement(Int_t rown, Int_t coln, Element val);

const Element *GetMatrixArray () const override;
Element *GetMatrixArray () override;
const Int_t *GetRowIndexArray() const override { return nullptr; }
Expand Down Expand Up @@ -283,6 +286,21 @@ template <class Element> inline Element &TMatrixT<Element>::operator()(Int_t row
return (fElements[arown*this->fNcols+acoln]);
}

////////////////////////////////////////////////////////////////////////////////
/// Efficiently sets element (rown,coln) equal to val
/// Index bound checks can be deactivated by defining NDEBUG

template <class Element>
inline void TMatrixT<Element>::SetElement(Int_t rown, Int_t coln, Element val)
{
assert(this->IsValid());
rown = rown - this->fRowLwb;
coln = coln - this->fColLwb;
assert((rown < this->fNrows && rown >= 0) && "SetElement() error: row index outside matrix range");
assert((coln < this->fNcols && coln >= 0) && "SetElement() error: column index outside matrix range");
fElements[rown * this->fNcols + coln] = val;
}

inline namespace TMatrixTAutoloadOps {

template <class Element> TMatrixT<Element> operator+ (const TMatrixT <Element> &source1,const TMatrixT <Element> &source2);
Expand Down
19 changes: 19 additions & 0 deletions math/matrix/inc/TMatrixTSym.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#include "TMatrixTBase.h"
#include "TMatrixTUtils.h"

#include <cassert>

template<class Element>class TMatrixT;
template<class Element>class TMatrixTSymLazy;
template<class Element>class TVectorT;
Expand Down Expand Up @@ -79,6 +81,8 @@ template<class Element> class TMatrixTSym : public TMatrixTBase<Element> {
void Plus (const TMatrixTSym<Element> &a,const TMatrixTSym<Element> &b);
void Minus(const TMatrixTSym<Element> &a,const TMatrixTSym<Element> &b);

inline void SetElement(Int_t rown, Int_t coln, Element val);

const Element *GetMatrixArray () const override;
Element *GetMatrixArray () override;
const Int_t *GetRowIndexArray() const override { return nullptr; }
Expand Down Expand Up @@ -235,6 +239,21 @@ template <class Element> inline Element &TMatrixTSym<Element>::operator()(Int_t
return (fElements[arown*this->fNcols+acoln]);
}

////////////////////////////////////////////////////////////////////////////////
/// Efficiently sets element (rown,coln) equal to val
/// Index bound checks can be deactivated by defining NDEBUG

template <class Element>
inline void TMatrixTSym<Element>::SetElement(Int_t rown, Int_t coln, Element val)
{
assert(this->IsValid());
rown = rown - this->fRowLwb;
coln = coln - this->fColLwb;
assert((rown < this->fNrows && rown >= 0) && "SetElement() error: row index outside matrix range");
assert((coln < this->fNcols && coln >= 0) && "SetElement() error: column index outside matrix range");
fElements[rown * this->fNcols + coln] = val;
}

template <class Element> Bool_t operator== (const TMatrixTSym<Element> &source1,const TMatrixTSym<Element> &source2);
template <class Element> TMatrixTSym<Element> operator+ (const TMatrixTSym<Element> &source1,const TMatrixTSym<Element> &source2);
template <class Element> TMatrixTSym<Element> operator+ (const TMatrixTSym<Element> &source1, Element val);
Expand Down
16 changes: 16 additions & 0 deletions math/matrix/test/testMatrixT.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,22 @@ TYPED_TEST(testMatrix, Invert)
CompareTMatrix(c, TestFixture::eye);
}

TYPED_TEST(testMatrix, SetElement)
{
TypeParam b(n, n);
TypeParam c(n, n);

for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
b(i, j) = n * i + j + 1 * (i == j);

for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
c.SetElement(i, j, n * i + j + 1 * (i == j));

CompareTMatrix(b, c);
}

class testMatrixD : public testing::Test {
protected:
void SetUp() override
Expand Down

0 comments on commit 985077f

Please sign in to comment.