ATLAS Offline Software
Loading...
Searching...
No Matches
Trk::AlSpaMat Class Reference

contains the implementation for handling sparse matrices More...

#include <AlSpaMat.h>

Inheritance diagram for Trk::AlSpaMat:
Collaboration diagram for Trk::AlSpaMat:

Public Member Functions

 AlSpaMat ()
 AlSpaMat (long int N)
 AlSpaMat (const AlSpaMat &m)
 AlSpaMat (const AlSymMat &m)
 ~AlSpaMat ()
AlSpaMatoperator= (const double &)
AlSpaMatoperator= (const AlSpaMat &)
AlSpaMatoperator= (const AlSymMat &)
AlSpaMatoperator= (const AlMat &)
AlSpaMat operator+ (const AlSpaMat &) const
AlSpaMatoperator+= (const AlSpaMat &)
AlSpaMat operator- (const AlSpaMat &) const
AlSpaMatoperator-= (const AlSpaMat &)
AlMat operator* (const AlSymMatBase &) const
AlVec operator* (const AlVec &) const
AlMat operator* (const AlMat &) const
AlSpaMat operator* (const double &) const
AlSpaMatoperator*= (const double &)
int SolveWithEigen (AlVec &RHS)
virtual void RemoveModule (int) override final
virtual void RemoveAlignPar (int, int) override final
void RemoveDoF (int, int nelem=1)
virtual int RemoveCollsRows (std::vector< int >) override final
virtual void reSize (long int) override final
virtual void SetPathBin (const std::string &) override final
virtual void SetPathTxt (const std::string &) override final
virtual StatusCode Write (const std::string &, bool, bool, double, float) override final
virtual StatusCode CheckMatVersion (const std::string &, bool &) override final
virtual StatusCode Read (const std::string &, int &, bool &, float &) override final
virtual StatusCode ReadProjected (const std::string &, int &, bool &, float &) override final
virtual int invert () override final
virtual int diagonalize (char jobz, AlVec &w, AlMat &z) override final
virtual double & elemr (long int, long int) override final
virtual double elemc (long int, long int) const override final
long int nele ()
virtual double determinant () override final
void elem (const indices &, long int &, long int &) const
const std::string & pathBin () const
const std::string & pathTxt () const
virtual TMatrixDSparsemakeTMatrix () override final
AlSymMatBase_row operator[] (long int)
AlSymMatBase_row_const operator[] (long int) const
long int nrow () const
long int ncol () const
long int size () const
int matrix_type () const
const datamapptrMap () const

Protected Member Functions

void copy (const AlSpaMat &m)
void copy (const AlSymMat &m)
void copy (const AlMat &m)

Static Protected Member Functions

static indices elem (long int, long int)

Protected Attributes

std::string m_pathbin
std::string m_pathtxt
int * m_ptr_row = nullptr
int * m_ptr_col = nullptr
int m_matrix_type = 0
datamap m_ptr_map
long int m_size = 0
long int m_nele = 0

Detailed Description

contains the implementation for handling sparse matrices

Definition at line 27 of file AlSpaMat.h.

Constructor & Destructor Documentation

◆ AlSpaMat() [1/4]

Trk::AlSpaMat::AlSpaMat ( )

Definition at line 43 of file AlSpaMat.cxx.

44 : m_ptr_row(nullptr)
45 , m_ptr_col(nullptr)
46{
47 m_matrix_type = 2;
48 m_size = 0;
49 m_nele = 0;
50}
int * m_ptr_col
Definition AlSpaMat.h:89
int * m_ptr_row
Definition AlSpaMat.h:88

◆ AlSpaMat() [2/4]

Trk::AlSpaMat::AlSpaMat ( long int N)

Definition at line 53 of file AlSpaMat.cxx.

54 : m_ptr_row(nullptr)
55 , m_ptr_col(nullptr)
56{
57 m_matrix_type = 2;
58 m_size = N;
59 m_nele = 0;
60}
constexpr std::size_t N

◆ AlSpaMat() [3/4]

Trk::AlSpaMat::AlSpaMat ( const AlSpaMat & m)

Definition at line 63 of file AlSpaMat.cxx.

64 : AlSymMatBase(m)
65 , m_ptr_row(nullptr)
66 , m_ptr_col(nullptr)
67{
68 m_matrix_type = 2;
69 m_size = m.size();
70 copy(m);
71}
void copy(const AlSpaMat &m)
Definition AlSpaMat.cxx:92

◆ AlSpaMat() [4/4]

Trk::AlSpaMat::AlSpaMat ( const AlSymMat & m)

Definition at line 74 of file AlSpaMat.cxx.

75 : m_ptr_row(nullptr)
76 , m_ptr_col(nullptr)
77{
78 m_matrix_type = 2;
79 m_size = m.size();
80 copy(m);
81}

◆ ~AlSpaMat()

Trk::AlSpaMat::~AlSpaMat ( )

Definition at line 84 of file AlSpaMat.cxx.

85{
86 m_ptr_map.clear();
87 if( m_ptr_row != nullptr ) delete [] m_ptr_row;
88 if( m_ptr_col != nullptr ) delete [] m_ptr_col;
89}

Member Function Documentation

◆ CheckMatVersion()

StatusCode Trk::AlSpaMat::CheckMatVersion ( const std::string & filename,
bool & StdUnits )
finaloverridevirtual

Implements Trk::AlSymMatBase.

Definition at line 715 of file AlSpaMat.cxx.

716{
717 std::ifstream inmat((filename).c_str(), std::ios::binary);
718 if(inmat.fail())
719 return StatusCode::FAILURE;
720
721 m_ptr_map.clear();
722
723 int32_t msiz=0;
724 inmat.read(charAddress(msiz), sizeof (msiz));
725
726 float version=0.0;
727 inmat.read(charAddress(version), sizeof (version));
728
729 StdUnits = version>=2.0;
730
731 inmat.close();
732
733 return StatusCode::SUCCESS;
734}

◆ copy() [1/3]

void Trk::AlSpaMat::copy ( const AlMat & m)
protected

Definition at line 122 of file AlSpaMat.cxx.

123{
124 if( size() != m.nrow() || size() != m.ncol() ) {
125 throw std::range_error( "AlSpaMat::copy: size do not match!" );
126 }
127
128 // copy just the lower triangle:
129 m_ptr_map.clear();
130 m_nele=0;
131
132 //Convert Storage System
133 for (int i = 0; i<m_size; i++)
134 for (int j = 0; j<=i; j++)
135 if (m.elemc(i,j) != 0.) {
136 m_ptr_map.insert(std::make_pair(std::make_pair(i,j), m.elemc(i,j)));
137 m_nele++;
138 }
139
140 }
long int size() const
float j(const xAOD::IParticle &, const xAOD::TrackMeasurementValidation &hit, const Eigen::Matrix3d &jab_inv)

◆ copy() [2/3]

void Trk::AlSpaMat::copy ( const AlSpaMat & m)
protected

Definition at line 92 of file AlSpaMat.cxx.

93{
94 if( size() != m.size()) {
95 throw std::range_error( "AlSpaMat::copy: size do not match!" );
96 }
97 m_ptr_map.clear();
98 m_nele=m.m_nele;
99 m_ptr_map = m.m_ptr_map;
100}

◆ copy() [3/3]

void Trk::AlSpaMat::copy ( const AlSymMat & m)
protected

Definition at line 103 of file AlSpaMat.cxx.

104{
105 if( size() != m.size()) {
106 throw std::range_error( "AlSpaMat::copy: size do not match!" );
107 }
108 m_ptr_map.clear();
109 m_nele=0;
110
111 // Convert Storage System
112 for (int i = 0; i<m_size; i++)
113 for (int j = 0; j<=i; j++)
114 if (m.elemc(i,j) != 0.) {
115 m_ptr_map.insert(std::make_pair(std::make_pair(i,j), m.elemc(i,j)));
116 m_nele++;
117 }
118
119 }

◆ determinant()

double Trk::AlSpaMat::determinant ( )
finaloverridevirtual

Implements Trk::AlSymMatBase.

Definition at line 403 of file AlSpaMat.cxx.

404{
405 throw std::invalid_argument( "AlSpaMat::determinant: not implemented!" );
406}

◆ diagonalize()

int Trk::AlSpaMat::diagonalize ( char jobz,
AlVec & w,
AlMat & z )
finaloverridevirtual

Implements Trk::AlSymMatBase.

Definition at line 481 of file AlSpaMat.cxx.

482{
483 throw std::invalid_argument( "AlSpaMat::diagonalize: not implemented!" );
484}

◆ elem() [1/2]

void Trk::AlSpaMat::elem ( const indices & key,
long int & i,
long int & j ) const
inline

Definition at line 94 of file AlSpaMat.h.

94 {
95 i = key.first;
96 j = key.second;
97 if(j>i) throw std::out_of_range( "AlSpaMat::elem: i<j ! " );
98 return;
99}

◆ elem() [2/2]

indices Trk::AlSpaMat::elem ( long int i,
long int j )
staticprotected

Definition at line 209 of file AlSpaMat.cxx.

210{
211 // ATTENTION! the key value is returned:
212#ifdef _DEBUG
213 if( i<0 ) {
214 throw std::out_of_range( "AlSpaMat::elem: Index 1 < zero! " );
215 }
216 if( i>=size() ) {
217 throw std::out_of_range( "AlSpaMat::elem: Index 1 too large! " );
218 }
219 if( j<0 ) {
220 throw std::out_of_range( "AlSpaMat::elem: Index 2 < zero! " );
221 }
222 if( j>=size() ) {
223 throw std::out_of_range( "AlSpaMat::elem: Index 2 too large! " );
224 }
225#endif
226
227 indices key = j < i ? std::make_pair(i,j) : std::make_pair(j,i);
228 return key;
229}
std::pair< long int, long int > indices

◆ elemc()

double Trk::AlSpaMat::elemc ( long int i,
long int j ) const
finaloverridevirtual

Implements Trk::AlSymMatBase.

Definition at line 179 of file AlSpaMat.cxx.

180{
181#ifdef _DEBUG
182 if( i<0 ) {
183 throw std::out_of_range( "AlSpaMat::elemc: Index 1 < zero! " );
184 }
185 if( i>=size() ) {
186 throw std::out_of_range( "AlSpaMat::elemc: Index 1 too large! " );
187 }
188 if( j<0 ) {
189 throw std::out_of_range( "AlSpaMat::elemc: Index 2 < zero! " );
190 }
191 if( j>=size() ) {
192 throw std::out_of_range( "AlSpaMat::elemc: Index 2 too large! " );
193 }
194#endif
195 // try fast referencing:
197 indices key = j < i ? std::make_pair(i,j) : std::make_pair(j,i);
198 pos=m_ptr_map.find(key);
199
200 if( pos!=m_ptr_map.end() ) // already exists
201 return pos->second;
202
203 // does not yet exist
204 // return zero:
205 return 0.;
206}
datamap::const_iterator const_mapiterator

◆ elemr()

double & Trk::AlSpaMat::elemr ( long int i,
long int j )
finaloverridevirtual

Implements Trk::AlSymMatBase.

Definition at line 143 of file AlSpaMat.cxx.

144{
145#ifdef _DEBUG
146 if( i<0 ) {
147 throw std::out_of_range( "AlSpaMat::elemr: Index 1 < zero! " );
148 }
149 if( i>=size() ) {
150 throw std::out_of_range( "AlSpaMat::elemr: Index 1 too large! " );
151 }
152 if( j<0 ) {
153 throw std::out_of_range( "AlSpaMat::elemr: Index 2 < zero! " );
154 }
155 if( j>=size() ) {
156 throw std::out_of_range("AlSpaMat::elemr: Index 2 too large! " );;
157 }
158#endif
159 // try fast referencing:
161 indices key = j < i ? std::make_pair(i,j) : std::make_pair(j,i);
162 pos=m_ptr_map.find(key);
163 if( pos!=m_ptr_map.end() ) // already exists
164 return pos->second;
165 else { // does not yet exist
166 // create it with the null value:
167 m_nele++;
168 return (m_ptr_map.insert(std::make_pair(key, 0.))).first->second;
169 }
170 // simpler implementation of the above:
171 /*
172 m_ptr_map[key]=0;
173 m_nele = m_ptr_map.size();
174 return m_ptr_map[key];
175 */
176}
datamap::iterator mapiterator

◆ invert()

int Trk::AlSpaMat::invert ( )
finaloverridevirtual

Implements Trk::AlSymMatBase.

Definition at line 487 of file AlSpaMat.cxx.

488{
489 throw std::invalid_argument( "AlSpaMat::invert: not implemented!" );
490}

◆ makeTMatrix()

TMatrixDSparse * Trk::AlSpaMat::makeTMatrix ( )
finaloverridevirtual

Implements Trk::AlSymMatBase.

Definition at line 864 of file AlSpaMat.cxx.

865{
866
867 long int nonZeroElements = m_ptr_map.size();
868 int *irow = new int[nonZeroElements];
869 int *icol = new int[nonZeroElements];
870 double *val = new double[nonZeroElements];
871
872 long int i, j;
873 long int counter(0);
874 for (const datamap::value_type& p : m_ptr_map) {
875 i = p.first.first;
876 j = p.first.second;
877 *(val+counter)=p.second;
878 *(irow+counter)= i;
879 *(icol+counter)= j;
880 counter++;
881 }
882
883 TMatrixDSparse* myTMatrix = new TMatrixDSparse(0,m_size-1,0,m_size-1);
884 myTMatrix->SetMatrixArray(nonZeroElements,irow,icol,val);
885
886 delete [] irow;
887 delete [] icol;
888 delete [] val;
889 return myTMatrix;
890}
class TMatrixTSparse< double > TMatrixDSparse
Definition AlSpaMat.h:14

◆ matrix_type()

int Trk::AlSymMatBase::matrix_type ( ) const
inlineinherited

Definition at line 148 of file AlSymMatBase.h.

148{ return m_matrix_type; }

◆ ncol()

long int Trk::AlSymMatBase::ncol ( ) const
inlineinherited

Definition at line 147 of file AlSymMatBase.h.

147{ return m_size; }

◆ nele()

long int Trk::AlSpaMat::nele ( )
inline

Definition at line 101 of file AlSpaMat.h.

101 {
102 // first check for intrinsic consistency:
103 if( int(m_ptr_map.size()) != m_nele ) throw std::range_error( "AlSpaMat::m_nele has been corrupted!" );
104 m_nele = m_ptr_map.size();
105 return m_nele;
106}

◆ nrow()

long int Trk::AlSymMatBase::nrow ( ) const
inlineinherited

Definition at line 146 of file AlSymMatBase.h.

146{ return m_size; }

◆ operator*() [1/4]

AlMat Trk::AlSpaMat::operator* ( const AlMat & m) const

Definition at line 349 of file AlSpaMat.cxx.

349 {
350 if( size() != m.nrow() ) {
351 throw std::range_error( "AlSpaMat::operator*: size do not match!" );
352 }
353
354 long int isiz(size());
355 long int icol(m.ncol());
356 AlMat b( isiz, icol);
357
358 for( long int i=0; i<isiz; i++ )
359 for( long int j=0; j<icol; j++ )
360 for( long int k=0; k<isiz; k++)
361 b.elemr(i,j) += elemc(i,k)*m.elemc(k,j);
362
363 return b;
364}
virtual double elemc(long int, long int) const override final
Definition AlSpaMat.cxx:179

◆ operator*() [2/4]

AlMat Trk::AlSpaMat::operator* ( const AlSymMatBase & m) const

Definition at line 331 of file AlSpaMat.cxx.

332{
333 if( size() != m.size() ) {
334 throw std::range_error( "AlSpaMat::operator*: size do not match!" );
335 }
336
337 long int isiz(size());
338 AlMat b( isiz, isiz);
339
340 for( long int i=0; i<isiz; i++ )
341 for( long int j=0; j<isiz; j++ )
342 for( int k=0; k<isiz; k++)
343 b.elemr(i,j) += elemc(i,k)*m.elemc(k,j);
344
345 return b;
346}

◆ operator*() [3/4]

AlVec Trk::AlSpaMat::operator* ( const AlVec & v) const

Definition at line 367 of file AlSpaMat.cxx.

368{
369 if( size() != v.size() ) {
370 throw std::range_error( "AlSpaMat::operator*: size do not match! " );
371 }
372
373 long int isiz(size());
374 AlVec b(isiz);
375
376 for( long int i=0; i<isiz; i++ )
377 for( long int j=0; j<isiz; j++ )
378 b[i] += elemc(i,j)*v[j];
379
380 return b;
381}
@ v
Definition ParamDefs.h:78

◆ operator*() [4/4]

AlSpaMat Trk::AlSpaMat::operator* ( const double & d) const

Definition at line 393 of file AlSpaMat.cxx.

394{
395 AlSpaMat a(size());
396 for (const datamap::value_type& p : m_ptr_map)
397 a.m_ptr_map.emplace(p.first, p.second*d);
398
399 return a;
400}
static Double_t a

◆ operator*=()

AlSpaMat & Trk::AlSpaMat::operator*= ( const double & d)

Definition at line 384 of file AlSpaMat.cxx.

385{
386 for (datamap::value_type& p : m_ptr_map)
387 p.second *= d;
388
389 return *this;
390}

◆ operator+()

AlSpaMat Trk::AlSpaMat::operator+ ( const AlSpaMat & m) const

Definition at line 273 of file AlSpaMat.cxx.

274{
275 if( size() != m.size()) {
276 throw std::range_error( "AlSpaMat::operator+: size do not match!" );
277 }
278
279 AlSpaMat b(m);
280 for (const datamap::value_type& p : m_ptr_map)
281 b.m_ptr_map[p.first] += p.second;
282 b.m_nele = b.m_ptr_map.size();
283
284 return b;
285}

◆ operator+=()

AlSpaMat & Trk::AlSpaMat::operator+= ( const AlSpaMat & m)

Definition at line 288 of file AlSpaMat.cxx.

289{
290 if( size() != m.size()) {
291 throw std::range_error( "AlSpaMat::operator+=: size do not match!" );
292 }
293
294 for (const datamap::value_type& p : m.m_ptr_map)
295 (*this).m_ptr_map[p.first] += p.second;
296 m_nele = m_ptr_map.size();
297
298 return *this;
299}

◆ operator-()

AlSpaMat Trk::AlSpaMat::operator- ( const AlSpaMat & m) const

Definition at line 302 of file AlSpaMat.cxx.

303{
304 if( size() != m.size()) {
305 throw std::range_error( "AlSpaMat::operator-: size do not match!" );
306 }
307
308 AlSpaMat b(m);
309 for (const datamap::value_type& p : m_ptr_map)
310 b.m_ptr_map[p.first] -= p.second;
311 b.m_nele = b.m_ptr_map.size();
312
313 return b;
314}

◆ operator-=()

AlSpaMat & Trk::AlSpaMat::operator-= ( const AlSpaMat & m)

Definition at line 317 of file AlSpaMat.cxx.

318{
319 if( size() != m.size()) {
320 throw std::range_error( "AlSpaMat::operator-=: size do not match!" );
321 }
322
323 for (const datamap::value_type& p : m.m_ptr_map)
324 (*this).m_ptr_map[p.first] -= p.second;
325 m_nele = m_ptr_map.size();
326
327 return *this;
328}

◆ operator=() [1/4]

AlSpaMat & Trk::AlSpaMat::operator= ( const AlMat & m)

Definition at line 252 of file AlSpaMat.cxx.

253{
254 if( m.nrow() != m.ncol() ) {
255 throw std::range_error( "AlSpaMat::=operator: allowed for square matrices only!" );
256 }
257
258 m_size=m.nrow();
259 copy(m);
260 return *this;
261}

◆ operator=() [2/4]

AlSpaMat & Trk::AlSpaMat::operator= ( const AlSpaMat & m)

Definition at line 233 of file AlSpaMat.cxx.

234{
235
236 if ( this != &m ) {
237 m_size=m.size();
238 copy(m);
239 }
240 return *this;
241}

◆ operator=() [3/4]

AlSpaMat & Trk::AlSpaMat::operator= ( const AlSymMat & m)

Definition at line 244 of file AlSpaMat.cxx.

245{
246 m_size=m.size();
247 copy(m);
248 return *this;
249}

◆ operator=() [4/4]

AlSpaMat & Trk::AlSpaMat::operator= ( const double & d)

Definition at line 264 of file AlSpaMat.cxx.

265{
266 for (datamap::value_type& p : m_ptr_map)
267 p.second = d;
268
269 return *this;
270}

◆ operator[]() [1/2]

AlSymMatBase::AlSymMatBase_row Trk::AlSymMatBase::operator[] ( long int r)
inlineinherited

Definition at line 109 of file AlSymMatBase.h.

109 {
110 AlSymMatBase_row b(*this,r);
111 return b;
112}
int r
Definition globals.cxx:22

◆ operator[]() [2/2]

AlSymMatBase::AlSymMatBase_row_const Trk::AlSymMatBase::operator[] ( long int r) const
inlineinherited

Definition at line 114 of file AlSymMatBase.h.

114 {
115 const AlSymMatBase_row_const b(*this,r);
116 return b;
117}

◆ pathBin()

const std::string & Trk::AlSpaMat::pathBin ( ) const
inline

Definition at line 108 of file AlSpaMat.h.

108 {
109 return m_pathbin;
110}
std::string m_pathbin
Definition AlSpaMat.h:85

◆ pathTxt()

const std::string & Trk::AlSpaMat::pathTxt ( ) const
inline

Definition at line 112 of file AlSpaMat.h.

112 {
113 return m_pathtxt;
114}
std::string m_pathtxt
Definition AlSpaMat.h:86

◆ ptrMap()

const datamap * Trk::AlSymMatBase::ptrMap ( ) const
inlineinherited

Definition at line 149 of file AlSymMatBase.h.

149{ return &m_ptr_map; }

◆ Read()

StatusCode Trk::AlSpaMat::Read ( const std::string & filename,
int & dofs,
bool & triang,
float & version )
finaloverridevirtual

Implements Trk::AlSymMatBase.

Definition at line 737 of file AlSpaMat.cxx.

738{
739 bool stdUnits = true;
740 if (StatusCode::SUCCESS != CheckMatVersion(m_pathbin+filename, stdUnits))
741 return StatusCode::FAILURE;
742
743 std::ifstream inmat((m_pathbin+filename).c_str(), std::ios::binary);
744 if(inmat.fail())
745 return StatusCode::FAILURE;
746
747 m_ptr_map.clear();
748
749 int32_t msiz=0;
750 int32_t nelem;
751 inmat.read(charAddress(msiz), sizeof (msiz));
752 if( msiz>999999 )
753 dofs = msiz-1000000;
754 else
755 dofs = abs(msiz);
756 m_size = dofs;
757
758 if (stdUnits)
759 inmat.read(charAddress(version), sizeof (version));
760
761 double melem=0;
762 int32_t i, j;
763
764 if(msiz>999999) { // sparse format
765 triang=false;
766 inmat.read(charAddress(nelem), sizeof (nelem));
767 m_nele=nelem;
768 for(int k=0; k<nelem; k++) {
769 inmat.read(charAddress(i), sizeof (i));
770 inmat.read(charAddress(j), sizeof (j));
771 inmat.read(charAddress(melem), sizeof (melem));
772 m_ptr_map.insert(std::make_pair(std::make_pair(i,j), melem));
773 }
774 }
775 else if(msiz>0) { // square format
776 triang=false;
777 for(int32_t i=0; i<msiz; i++) {
778 for(int32_t j=0; j<msiz; j++) {
779 inmat.read(charAddress(melem), sizeof (melem));
780 if( i>=j && melem!=0. )
781 m_ptr_map.insert(std::make_pair(std::make_pair(i,j), melem));
782 }
783 }
784 }
785 else { // triangular format
786 triang=true;
787 msiz = (-1)*msiz;
788 for( int32_t i=0; i<msiz; i++) {
789 for( int32_t j=0; j<=i; j++) {
790 inmat.read(charAddress(melem), sizeof (melem));
791 if( melem!=0. )
792 m_ptr_map.insert(std::make_pair(std::make_pair(i,j), melem));
793 }
794 }
795 }
796
797 m_nele = m_ptr_map.size();
798 inmat.close();
799 return StatusCode::SUCCESS;
800}
virtual StatusCode CheckMatVersion(const std::string &, bool &) override final
Definition AlSpaMat.cxx:715

◆ ReadProjected()

StatusCode Trk::AlSpaMat::ReadProjected ( const std::string & filename,
int & dofs,
bool & triang,
float & version )
finaloverridevirtual

Implements Trk::AlSymMatBase.

Definition at line 803 of file AlSpaMat.cxx.

805{
806 std::ifstream inmat((m_pathbin+filename).c_str(), std::ios::binary);
807 if(inmat.fail())
808 return StatusCode::FAILURE;
809
810 m_ptr_map.clear();
811
812 int32_t msiz=0;
813 int32_t nelem;
814 inmat.read(charAddress(msiz), sizeof (msiz));
815 if( msiz>999999 )
816 dofs = msiz-1000000;
817 else
818 dofs = std::abs(msiz);
819 m_size = dofs;
820
821 inmat.read(charAddress(version), sizeof (version));
822
823 double melem=0;
824 int32_t i, j;
825
826 if(msiz>999999) { // sparse format
827 triang=false;
828 inmat.read(charAddress(nelem), sizeof (nelem));
829 m_nele=nelem;
830 for(int k=0; k<nelem; k++) {
831 inmat.read(charAddress(i), sizeof (i));
832 inmat.read(charAddress(j), sizeof (j));
833 inmat.read(charAddress(melem), sizeof (melem));
834 m_ptr_map.insert(std::make_pair(std::make_pair(i,j), melem));
835 }
836 }
837 else if(msiz>0) { // square format
838 triang=false;
839 for(int32_t i=0; i<msiz; i++) {
840 for(int32_t j=0; j<msiz; j++) {
841 inmat.read(charAddress(melem), sizeof (melem));
842 if( i>=j && melem!=0. )
843 m_ptr_map.insert(std::make_pair(std::make_pair(i,j), melem));
844 }
845 }
846 }
847 else { // triangular format
848 triang=true;
849 msiz = (-1)*msiz;
850 for( int32_t i=0; i<msiz; i++) {
851 for( int32_t j=0; j<=i; j++) {
852 inmat.read(charAddress(melem), sizeof (melem));
853 if( melem!=0. )
854 m_ptr_map.insert(std::make_pair(std::make_pair(i,j), melem));
855 }
856 }
857 }
858
859 m_nele = m_ptr_map.size();
860 inmat.close();
861 return StatusCode::SUCCESS;
862}

◆ RemoveAlignPar()

void Trk::AlSpaMat::RemoveAlignPar ( int index,
int control )
finaloverridevirtual

Implements Trk::AlSymMatBase.

Definition at line 565 of file AlSpaMat.cxx.

566{
567 // Dynamic shiftRow
568 int shiftRow = 1;
569 int counterRow = 0;
570
571 // Dynamic shiftCol
572 int shiftCol;
573 int counterCol = 0;
574
575 index = index-control;
576
578
579 for(int row=index; row<(size()-shiftRow); row++) {
580 shiftCol = 0;
581 counterCol = 0;
582
583 for(int col=0; col<row+1; col++) {
584
585 if (col==index) {
586 shiftCol = 1;
587 counterCol=0;
588 }
589
590 pos = m_ptr_map.find(std::make_pair(row+shiftRow,col+shiftCol));
591 if( pos!=m_ptr_map.end() ) { // exists
592 elemr(row,col) = pos->second;
593 m_ptr_map.erase(pos);
594 }
595 else {
596 pos = m_ptr_map.find(std::make_pair(row,col));
597 if( pos!=m_ptr_map.end() )
598 m_ptr_map.erase(pos);
599 }
600 counterCol++;
601 if (counterCol==5-control) {
602 counterCol=0;
603 shiftCol++;
604 }
605 }
606
607 counterRow++;
608 if (counterRow==5-control) {
609 counterRow=0;
610 shiftRow++;
611 }
612 }
613
614 m_nele = m_ptr_map.size();
615}
virtual double & elemr(long int, long int) override final
Definition AlSpaMat.cxx:143
str index
Definition DeMoScan.py:362
row
Appending html table to final .html summary file.

◆ RemoveCollsRows()

int Trk::AlSpaMat::RemoveCollsRows ( std::vector< int > indices)
finaloverridevirtual

Implements Trk::AlSymMatBase.

Definition at line 531 of file AlSpaMat.cxx.

532{
533 int n = indices.size();
534 if (n>m_size) [[unlikely]]{
535 throw std::invalid_argument( "AlSpaMat::RemoveCollsRows: Vector of indices larger than matrix size." );
536 }
537
538 if (n==0) {
539 return m_size;
540 }
541
542
543 // first sort the list of indices descending
544 // maybe not the fastest way to do that but it works
545 for (int i=0;i<n-1;i++)
546 for (int j=i+1; j<n; j++)
547 if (indices[j] > indices[i]) {
548 int itmp = indices[i];
549 indices[i] = indices[j];
550 indices[j] = itmp;
551 }
552
553 // remove rows and columns starting from largest indices
554 for (int i=0;i<n;i++) {
555 if (indices[i] > m_size-1) {
556 throw std::out_of_range( "AlSpaMat::RemoveCollsRows: Index goes beyond matrix." );
557 }
558 RemoveDoF(indices[i]);
559 }
560
561 return m_size;
562}
void RemoveDoF(int, int nelem=1)
Definition AlSpaMat.cxx:493
#define unlikely(x)

◆ RemoveDoF()

void Trk::AlSpaMat::RemoveDoF ( int index,
int nelem = 1 )

Definition at line 493 of file AlSpaMat.cxx.

494{
495 // index here is the first alignment parameter to remove, nelem number of consecutive ones
496
497 m_size-=nelem;
498 long int n=index+nelem-1;
500 mapiterator pos_obs=m_ptr_map.end();
501 for (pos = m_ptr_map.begin(); pos!=m_ptr_map.end(); ++pos) {
502
503 if( pos_obs!=m_ptr_map.end() )
504 m_ptr_map.erase(pos_obs);
505
506 pos_obs = m_ptr_map.end();
507
508 long int k, l;
509 elem(pos->first, k, l);
510
511 if( (k>=index && k<=n) || (l>=index && l<=n) )
512 pos_obs = pos;
513 else {
514 if( k > n )
515 k -= nelem;
516 if( l > n )
517 l -= nelem;
518 if( k >= index || l >= index ) {
519 elemr(k, l) = pos->second;
520 pos_obs = pos;
521 }
522 }
523 }
524
525 if( pos_obs!=m_ptr_map.end() )
526 m_ptr_map.erase(pos_obs);
527 m_nele = m_ptr_map.size();
528}
void elem(const indices &, long int &, long int &) const
Definition AlSpaMat.h:94
l
Printing final latex table to .tex output file.

◆ RemoveModule()

void Trk::AlSpaMat::RemoveModule ( int index)
finaloverridevirtual

Implements Trk::AlSymMatBase.

Definition at line 618 of file AlSpaMat.cxx.

619{
620 // index here is module number within matrix (size=6*max_index)
621 const int shift=6;
622 const int ind=shift*index;
623
624 RemoveDoF(ind, shift);
625}
virtual void shift(size_t pos, ptrdiff_t offs) override
Shift the elements of the container.

◆ reSize()

void Trk::AlSpaMat::reSize ( long int n)
finaloverridevirtual

Implements Trk::AlSymMatBase.

Definition at line 628 of file AlSpaMat.cxx.

629{
630 // balanced binary tree!
631 if( n!=m_size ) {
632 // make a temporary copy:
633 AlSpaMat m(*this);
634 m_ptr_map.clear();
635 m_size = n;
636 long int i, j;
637 for (const datamap::value_type& p : m.m_ptr_map) {
638 m.elem(p.first, i, j);
639 if( i<n && j<n )
640 m_ptr_map.insert(p);
641 }
642 }
643
644 m_nele = m_ptr_map.size();
645}

◆ SetPathBin()

void Trk::AlSpaMat::SetPathBin ( const std::string & path)
finaloverridevirtual

Implements Trk::AlSymMatBase.

Definition at line 648 of file AlSpaMat.cxx.

649{
650 m_pathbin.assign(path);
651}

◆ SetPathTxt()

void Trk::AlSpaMat::SetPathTxt ( const std::string & path)
finaloverridevirtual

Implements Trk::AlSymMatBase.

Definition at line 654 of file AlSpaMat.cxx.

655{
656 m_pathtxt.assign(path);
657}

◆ size()

long int Trk::AlSymMatBase::size ( ) const
inlineinherited

Definition at line 145 of file AlSymMatBase.h.

145{ return m_size; }

◆ SolveWithEigen()

int Trk::AlSpaMat::SolveWithEigen ( AlVec & RHS)

Definition at line 408 of file AlSpaMat.cxx.

408 {
409
410 if(RHS.size() != size() ){
411 throw std::range_error( "AlSpaMat::SolveWithEigen vector size is incorrect" );
412 }
413
414 Eigen::VectorXd eigenBigVector( RHS.size() );
415 for(int i(0); i< RHS.size(); ++i ){
416 eigenBigVector[i] = RHS[i];
417 }
418
419 Eigen::SparseMatrix<double> eigenBigMatrix( m_size, m_size );
420
421 using Triplet = Eigen::Triplet<double>;
422 std::vector<Triplet> tripletList;
423 tripletList.reserve(m_nele);
424 long int i, j;
425 for (const datamap::value_type& p : m_ptr_map) {
426 elem(p.first, i, j);
427 tripletList.emplace_back(i,j,p.second);
428 if(i!=j) tripletList.emplace_back(j,i,p.second);
429 }
430 eigenBigMatrix.setFromTriplets(tripletList.begin(), tripletList.end());
431
432 // Eigen::CholmodSupernodalLLT is much much quicker (x50) so it would be great to move to that in the future
433 // requires an external package SuiteSparse
434 // BiCGSTAB was the fastest iterative solver in Eigen from a quick test
435 // SimplicialLDLT was the fastest direct solver in Eigen (x2 slower than BiCGSTAB )
436
437 // Eigen::BiCGSTAB<Eigen::SparseMatrix<double> > solver;
438 Eigen::SimplicialLDLT<Eigen::SparseMatrix<double> > solver;
439
440 solver.compute(eigenBigMatrix);
441 if(solver.info()!=Eigen::Success) {
442 // decomposition failed
443 throw std::domain_error("AlSpaMat::SolveWithEigen: failed to compute: -- is the input matrix singular?" );
444 return 1;
445 }
446
447 Eigen::VectorXd x = solver.solve( eigenBigVector );
448 if(solver.info()!=Eigen::Success) {
449 // solving failed
450 throw std::domain_error("AlSpaMat::SolveWithEigen: Failed to solve: -- is your matrix singular? ");
451 return 2;
452 }
453
454 //Copy results into vector
455 for(int i(0); i< RHS.size(); ++i ){
456 RHS[i] = x[i];
457 }
458
459 //Check accuracy
460 Eigen::VectorXd residual = eigenBigMatrix * x - eigenBigVector;
461 double sumresidual = 0;
462 for( int i=0; i<residual.size(); ++i){
463 sumresidual += fabs(residual[i]);
464 }
465 sumresidual /= (double) residual.size();
466
467 if( sumresidual > 1e-3 ){
468 throw std::overflow_error( "AlSpaMat::SolveWithEigen: your solution is no good! ");
469 return 3;
470 }
471
472 return 0;
473
474
475}
@ x
Definition ParamDefs.h:55

◆ Write()

StatusCode Trk::AlSpaMat::Write ( const std::string & filename,
bool binary,
bool ,
double ,
float version )
finaloverridevirtual

Implements Trk::AlSymMatBase.

Definition at line 662 of file AlSpaMat.cxx.

664{
665 std::ofstream outmat;
666
667 int32_t msizz = 1000000+size();
668 int32_t nelem = m_ptr_map.size();
669
670 if(binary) {
671 outmat.open((m_pathbin+filename).c_str(), std::ios::binary);
672 if(outmat.fail())
673 return StatusCode::FAILURE;
674 // add 10^6 to the size to distinguish
675 // from dense format
676 outmat.write(charAddress(msizz), sizeof (msizz));
677 outmat.write(charAddress(version), sizeof (version));
678 outmat.write(charAddress(nelem), sizeof (nelem));
679 }
680 else {
681 outmat.open((m_pathtxt+filename).c_str());
682 if(outmat.fail())
683 return StatusCode::FAILURE;
684 outmat.setf(std::ios::fixed);
685 outmat.setf(std::ios::showpoint);
686 outmat.precision(3);
687 outmat << "msizz: " << std::setw(6) << msizz << std::endl;
688 outmat << "nelem: " << std::setw(6) << nelem << std::endl;
689 outmat << "AlSpaMat version: " << std::setw(6) << version << std::endl;
690 }
691
692 double melem=0;
693 long int ii, jj;
694 int32_t i, j;
695
696 for (const datamap::value_type& p : m_ptr_map) {
697 melem = p.second;
698 elem(p.first, ii, jj); i=ii; j=jj; // just a type conversion
699 if(binary) {
700 //horrible, but deliberate, pointer as array
701 //coverity[ARRAY_VS_SINGLETON]
702 outmat.write(charAddress((i)), sizeof (i));
703 outmat.write(charAddress((j)), sizeof (j));
704 outmat.write(charAddress((melem)), sizeof (melem));
705 }
706 else
707 outmat << std::setw(6) << i << std::setw(6) << j << std::setw(18) << melem << std::endl;
708 }
709 outmat.close();
710
711 return StatusCode::SUCCESS;
712}

Member Data Documentation

◆ m_matrix_type

int Trk::AlSymMatBase::m_matrix_type = 0
protectedinherited

Definition at line 100 of file AlSymMatBase.h.

◆ m_nele

long int Trk::AlSymMatBase::m_nele = 0
protectedinherited

Definition at line 104 of file AlSymMatBase.h.

◆ m_pathbin

std::string Trk::AlSpaMat::m_pathbin
protected

Definition at line 85 of file AlSpaMat.h.

◆ m_pathtxt

std::string Trk::AlSpaMat::m_pathtxt
protected

Definition at line 86 of file AlSpaMat.h.

◆ m_ptr_col

int* Trk::AlSpaMat::m_ptr_col = nullptr
protected

Definition at line 89 of file AlSpaMat.h.

◆ m_ptr_map

datamap Trk::AlSymMatBase::m_ptr_map
protectedinherited

Definition at line 101 of file AlSymMatBase.h.

◆ m_ptr_row

int* Trk::AlSpaMat::m_ptr_row = nullptr
protected

Definition at line 88 of file AlSpaMat.h.

◆ m_size

long int Trk::AlSymMatBase::m_size = 0
protectedinherited

Definition at line 103 of file AlSymMatBase.h.


The documentation for this class was generated from the following files: