CGEM BOSS 6.6.5.g
BESIII Offline Software System
Loading...
Searching...
No Matches
EmcLSSMatrix Class Reference

#include <EmcLSSMatrix.h>

Public Member Functions

 EmcLSSMatrix ()
 
 EmcLSSMatrix (int rows, int nonzero_col)
 
 EmcLSSMatrix (int rows, int nonzero_col, int MsgFlag)
 
 EmcLSSMatrix (const EmcLSSMatrix &m1)
 
 ~EmcLSSMatrix ()
 
double & operator() (int row, int col)
 
const int & num_rows () const
 
const int & num_cols () const
 
const long & size () const
 
double * matrix (const int &rowind=0) const
 
int * row () const
 
int * column (const int &rowind=0) const
 
int num_filled_rows (const int col) const
 
int num_filled_cols (const int row) const
 
long int num_nonZeros ()
 
void print_NonZeros ()
 
void print_row (int rownr)
 
void writeOut (ostream &Out)
 
void readIn (istream &In)
 
void set_verbose (bool setting)
 
void reset ()
 
bool reduce_Matrix (int *xRef_list)
 

Protected Member Functions

long int find (int row, int col)
 

Detailed Description

Implementation of a Large Sparse Symmetric Matrix,

Per row only the nonzero elements are stored together with their column indices. (the storiage of the row index is a tribute to SLAP which needs it)

See also
Author
Chunxiu Liu (originator/contributor etc.);

Definition at line 47 of file EmcLSSMatrix.h.

Constructor & Destructor Documentation

◆ EmcLSSMatrix() [1/4]

EmcLSSMatrix::EmcLSSMatrix ( )

Definition at line 43 of file EmcLSSMatrix.cxx.

44 : _size(0), _nrrows(0), _nrcol(0), _matrix(0), _columns(0), _rows(0),
45 _nothing(0),_verb(false)
46{
47}

◆ EmcLSSMatrix() [2/4]

EmcLSSMatrix::EmcLSSMatrix ( int  rows,
int  nonzero_col 
)

Definition at line 49 of file EmcLSSMatrix.cxx.

50 : _size(rows * nonzero_col),
51 _nrrows(rows),
52 _nrcol(nonzero_col)
53{
54 _matrix = new double[_size];
55 _rows = new int[_size];
56 _columns = new int[_size];
57 for (int i=0; i<_size;i++)
58 {
59 _matrix[i] =0.;
60 _rows[i] = -1;
61 _columns[i] = -1;
62 }
63 _nothing = new double;
64 _verb = false;
65
66 _MsgFlag =0;
67}

◆ EmcLSSMatrix() [3/4]

EmcLSSMatrix::EmcLSSMatrix ( int  rows,
int  nonzero_col,
int  MsgFlag 
)

Definition at line 69 of file EmcLSSMatrix.cxx.

70 : _size(rows * nonzero_col),
71 _nrrows(rows),
72 _nrcol(nonzero_col)
73{
74 _matrix = new double[_size];
75 _rows = new int[_size];
76 _columns = new int[_size];
77 for (int i=0; i<_size;i++)
78 {
79 _matrix[i] =0.;
80 _rows[i] = -1;
81 _columns[i] = -1;
82 }
83 _nothing = new double;
84 _verb = false;
85
86 _MsgFlag = MsgFlag;
87}

◆ EmcLSSMatrix() [4/4]

EmcLSSMatrix::EmcLSSMatrix ( const EmcLSSMatrix m1)

Definition at line 89 of file EmcLSSMatrix.cxx.

90 : _size(m1._size),
91 _nrrows(m1._nrrows),
92 _nrcol(m1._nrcol)
93
94{
95 _matrix = new double[_size];
96 _rows = new int[_size];
97 _columns = new int[_size];
98 _nothing = m1._nothing;
99 _verb = m1._verb;
100
101 for ( long int i=0;i<_size;i++)
102 {
103 _matrix[i] = m1._matrix[i];
104 _rows[i] = m1._rows[i];
105 _columns[i] = m1._columns[i];
106 };
107}

◆ ~EmcLSSMatrix()

EmcLSSMatrix::~EmcLSSMatrix ( )

Definition at line 112 of file EmcLSSMatrix.cxx.

112 {
113 if ( 0 != _matrix)
114 {
115 delete [] _matrix;
116 _matrix = 0;
117 }
118 if ( 0 != _rows)
119 {
120 delete [] _rows;
121 _rows = 0;
122 }
123 if ( 0 != _columns)
124 {
125 delete [] _columns;
126 _columns = 0;
127 }
128 if ( 0 != _nothing)
129 {
130 delete _nothing;
131 _nothing = 0;
132 }
133}

Member Function Documentation

◆ column()

int * EmcLSSMatrix::column ( const int &  rowind = 0) const

Definition at line 295 of file EmcLSSMatrix.cxx.

296{
297 int* here=0;
298
299 if (rowind < _nrrows)
300 {
301 here = (_columns+(rowind*_nrcol));
302 }
303 else
304 {
305
306 if (_MsgFlag <= 5) {
307 std::cout <<"EmcLSSMatrix::ERROR "
308 << "EmcLSSMatrix.column: Error "
309 << "- larger row index than existing requested !"
310 << endl;
311 }
312 here = 0;
313 }
314 return here;
315}

◆ find()

long int EmcLSSMatrix::find ( int  row,
int  col 
)
protected

Definition at line 164 of file EmcLSSMatrix.cxx.

165{
166 int smaller,larger;
167
168
169 if ( col > row )
170 {
171 smaller = row;
172 larger = col;
173 }
174 else
175 {
176 smaller = col;
177 larger = row;
178 }
179
180 int* col_p;
181 int* row_p;
182 double* matr_p;
183 double* matr_row_max;
184
185 if (larger < 0 || larger > _nrrows || smaller < 0 || smaller > _nrrows )
186 {
187 if (_MsgFlag <= 5) {
188 std::cout <<"EmcLSSMatrix::ERROR"
189 << "!!! ERROR in bound check of EmcLSSMatrix !!!"
190 << "!!! Return zero !!!"
191 << endl;
192 }
193 matr_p = 0;
194 }
195 else
196 {
197 col_p = (_columns + (larger * _nrcol));
198 row_p = (_rows + (larger * _nrcol));
199 matr_p = (_matrix + (larger * _nrcol));
200 matr_row_max = (matr_p + _nrcol);
201
202 while ( matr_p < matr_row_max ) {
203
204 if (_MsgFlag <= 1) {
205 std::cout <<"EmcLSSMatrix::VERBOSE"
206 << "C: " << larger << " " << smaller << " "
207 << col_p << " " << *col_p << " "
208 << matr_p << " " << *matr_p << " "
209 << (_matrix+(matr_p-_matrix)) << " "
210 << *(_matrix+(matr_p-_matrix)) << endl;
211 }
212 /*
213 cout<< "C: " << larger << " " << smaller << " "
214 << col_p << " " << *col_p << " "
215 << matr_p << " " << *matr_p << " "
216 << (_matrix+(matr_p-_matrix)) << " "
217 << *(_matrix+(matr_p-_matrix)) <<endl;
218 */
219 if ( matr_p == (matr_row_max-1) )
220 {
221 if (_MsgFlag <= 4) {
222 std::cout <<"EmcLSSMatrix::WARNING "
223 << "!! WARNING: Reached maximum number of columns "
224 << "in LSSMatrix when searching for row "
225 << larger << " column " << smaller << " !!"
226 << endl
227 << "!! Return zero pointer !! " << endl;
228 }
229 matr_p = 0;
230 break;
231 }
232
233 //if row does already exist
234 if ( *col_p == smaller )
235 {
236 break;
237 }
238
239 //if at the end of the list, use this as new element
240 if ( (*matr_p == 0.) )
241 {
242 *col_p = smaller;
243 *row_p = larger;
244 // if (*row_p == 1616 ) {
245 // nun=(row_p-(_rows + (larger * _nrcol)));
246 //if (_MsgFlag <= 2) {
247 // std::cout <<"EmcLSSMatrix::DEBUG "
248 // << nun << " " << larger << " " << smaller << " "
249 // << *row_p << " " << *col_p << " " << *matr_p << endl;
250 //}
251 // }
252 break;
253 }
254
255 matr_p++;
256 col_p++;
257 row_p++;
258
259 }
260 }
261
262 long int diff = matr_p-_matrix;
263
264 if (matr_p == 0)
265 {
266 diff = -1;
267 }
268
269 return (diff);
270}
int * row() const
Definition: EmcLSSMatrix.h:84

Referenced by operator()(), and readIn().

◆ matrix()

double * EmcLSSMatrix::matrix ( const int &  rowind = 0) const

Definition at line 273 of file EmcLSSMatrix.cxx.

274{
275 double* here=0;
276
277 if (rowind < _nrrows)
278 {
279 here = (_matrix+(rowind*_nrcol));
280 }
281 else
282 {
283 if (_MsgFlag <= 5) {
284 std::cout <<"EmcLSSMatrix::ERROR "
285 << "EmcLSSMatrix::matrix: Error "
286 << "- larger row index than existing requested !"
287 << endl;
288 }
289 here = 0;
290 }
291 return here;
292}

◆ num_cols()

const int & EmcLSSMatrix::num_cols ( ) const
inline

Definition at line 78 of file EmcLSSMatrix.h.

78{ return _nrcol ; }

◆ num_filled_cols()

int EmcLSSMatrix::num_filled_cols ( const int  row) const

Definition at line 318 of file EmcLSSMatrix.cxx.

319{
320 double * search_i = _matrix + ( row * _nrcol );
321 double * max_i = search_i + _nrcol;
322 int nonZeroCol=0;
323
324 while ( (*search_i != 0.) && (search_i < max_i) )
325 {
326 nonZeroCol++;
327 search_i++;
328 }
329 return nonZeroCol;
330}

Referenced by print_row().

◆ num_filled_rows()

int EmcLSSMatrix::num_filled_rows ( const int  col) const

Definition at line 333 of file EmcLSSMatrix.cxx.

334{
335 int nonZeroRows = 0;
336 for ( long int i=0; i<_size; i++ )
337 {
338 if ( (_matrix[i] != 0.) && (_columns[i] == col) )
339 {
340 nonZeroRows++;
341 }
342 }
343
344 return nonZeroRows;
345}

◆ num_nonZeros()

long int EmcLSSMatrix::num_nonZeros ( )

Definition at line 349 of file EmcLSSMatrix.cxx.

350{
351 int* col_p = _columns;
352 double* ele_p = _matrix;
353 double* mat_max_p = (_matrix + _size);
354 long int nrele = 0;
355
356 while ( ele_p < mat_max_p )
357 {
358 if ( *ele_p != 0.) nrele++;
359 col_p++;
360 ele_p++;
361 }
362
363 return nrele;
364}

Referenced by writeOut().

◆ num_rows()

const int & EmcLSSMatrix::num_rows ( ) const
inline

Definition at line 75 of file EmcLSSMatrix.h.

75{ return _nrrows; }

◆ operator()()

double & EmcLSSMatrix::operator() ( int  row,
int  col 
)

Definition at line 139 of file EmcLSSMatrix.cxx.

139 {
140
141 long int found = find( row, col );
142
143
144 if ( -1 == found )
145 {
146 if (_MsgFlag <= 5) {
147 std::cout << "EmcLSSMatrix:: ERROR "
148 << "EmcLSSMatrix: matrix element not found !!" << endl
149 << "EmcLSSMatrix: Return zero !" << endl;
150 }
151 _nothing = 0;
152 return *_nothing;
153 }
154
155 return (*(_matrix+found));
156}
long int find(int row, int col)

◆ print_NonZeros()

void EmcLSSMatrix::print_NonZeros ( )

Definition at line 460 of file EmcLSSMatrix.cxx.

461{
462 int* col_p = _columns;
463 int* row_p = _rows;
464 double* ele_p = _matrix;
465 double* mat_max_p = (_matrix + _size);
466 long int wo = 0;
467 long int nrele = 0;
468
469
470 if (_MsgFlag <= 2) {
471 std::cout <<"EmcLSSMatrix::DEBUG "
472 <<"List of nonzero Matrix elements (Matrix size: "
473 << _size << " ) : " << endl;
474 }
475
476 while ( ele_p < mat_max_p )
477 {
478 if ( *ele_p != 0. )
479 {
480 nrele++;
481 if (_MsgFlag <= 2) {
482 std::cout <<"EmcLSSMatrix::DEBUG "
483 << "nr: " << nrele
484 << " M( " << *row_p << " , " << *col_p
485 << " ): " << *ele_p
486 << " array index: " << wo << endl;
487 }
488 /*
489 cout<< "nr: " << nrele
490 << " M( " << *row_p << " , " << *col_p
491 << " ): " << *ele_p
492 << " array index: " << wo << endl;
493 */
494 }
495 wo++;
496 col_p++;
497 row_p++;
498 ele_p++;
499 }
500
501}

Referenced by EmcBhaCalibData::readIn().

◆ print_row()

void EmcLSSMatrix::print_row ( int  rownr)

Definition at line 505 of file EmcLSSMatrix.cxx.

506{
507 int* col_p = _columns+(_nrcol*rownr);
508 int* row_p = _rows+(_nrcol*rownr);
509 double* ele_p = _matrix+(_nrcol*rownr);
510 double* mat_max_p = (ele_p + _nrcol);
511 long int wo = 0;
512 long int nrele = 0;
513
514 if (_MsgFlag <= 2) {
515 std::cout <<"EmcLSSMatrix::DEBUG "
516 <<"row length: " << num_filled_cols(rownr) << endl;
517 }
518 while ( ele_p < mat_max_p )
519 {
520 if ( *ele_p != 0. )
521 {
522 nrele++;
523 if (_MsgFlag <= 2) {
524 std::cout <<"EmcLSSMatrix::DEBUG "
525 << "nr: " << nrele
526 << " M( " << *row_p << " , " << *col_p
527 << " ): " << *ele_p
528 << " array index: " << wo << endl;
529 }
530 /*
531 cout<< "nr: " << nrele
532 << " M( " << *row_p << " , " << *col_p
533 << " ): " << *ele_p
534 << " array index: " << wo << endl;
535 */
536 }
537 wo++;
538 col_p++;
539 row_p++;
540 ele_p++;
541 }
542
543}
int num_filled_cols(const int row) const

◆ readIn()

void EmcLSSMatrix::readIn ( istream &  In)

Definition at line 580 of file EmcLSSMatrix.cxx.

581{
582 // long int nonz = num_nonZeros();
583 long int nonz = 0;
584 In >> nonz;
585
586 cout<<"nonz="<<nonz<<endl;
587
588 int theRow;
589 int theCol;
590 long int index;
591 double theEle;
592
593
594 for (long int i=0; i<nonz; i++ )
595 {
596 In >> theRow >> theCol >> theEle;
597 index = find (theRow,theCol);
598 /*
599 cout<<"index = "<<index
600 <<"row="<<theRow
601 <<"col="<<theCol<<endl;
602 */
603 if ( -1 != index )
604 {
605 _matrix[index] += theEle;
606 if (_verb)
607 {
608 if ( i < 50 || i > (nonz-10) )
609 {
610 if (_MsgFlag <= 2) {
611 std::cout <<"EmcLSSMatrix::DEBUG "
612 << "M: " << _rows[index] << " " << _columns[index]
613 << " " << _matrix[index] << " " << index << endl;
614 }
615 }
616 }
617 }
618 }
619
620 if (_verb)
621 {
622 if (_MsgFlag <= 2) {
623 std::cout <<"EmcLSSMatrix::DEBUG "
624 << "Have read in " << nonz
625 << " non zero matrix elements from file !!!" << endl;
626 }
627 }
628}

Referenced by EmcBhaCalibData::readIn().

◆ reduce_Matrix()

bool EmcLSSMatrix::reduce_Matrix ( int *  xRef_list)

Definition at line 382 of file EmcLSSMatrix.cxx.

383{
384 bool successful = true;
385
386 //delete all zero elements in matrix
387 //save only non zero elements
388
389 long int _newIndx = 0;
390
391
392 for ( long int _arrayIndx = 0;
393 _arrayIndx < _size; _arrayIndx++)
394 {
395
396 //add 1 to matrix indices because SLAP wants indices 1..N,
397 //but Xtals counting in geometry starts with 0
398
399 if ( _matrix[_arrayIndx] > 0. )
400 {
401
402 if ( (xRef_list[(_rows[_arrayIndx])]) >= 0
403 && (xRef_list[(_columns[_arrayIndx])]) >= 0 )
404 {
405 _matrix[_newIndx] = _matrix[_arrayIndx];
406 _rows[_newIndx] = ((xRef_list[(_rows[_arrayIndx])])+1);
407 _columns[_newIndx] = ((xRef_list[(_columns[_arrayIndx])])+1);
408 _newIndx++;
409 }
410 else
411 {
412
413 //int indxtal;
414 if (xRef_list[(_rows[_arrayIndx])] < 0 )
415 {
416 if (_MsgFlag <= 5) {
417 std::cout <<"EmcLSSMatrix::ERROR "
418 << "EmcLSSMatrix: Xtal index "
419 << _rows[_arrayIndx]
420 << " appears in matrix, "
421 << "but not in vector !!! "
422 << _rows[_arrayIndx] << " "
423 << _columns[_arrayIndx]
424 << endl;
425 }
426 }
427 else
428 {
429
430 if (_MsgFlag <= 5) {
431 std::cout <<"EmcLSSMatrix::ERROR "
432 << "EmcLSSMatrix: Xtal index "
433 << _columns[_arrayIndx]
434 << " appears in matrix, "
435 << "but not in vector !!! "
436 << _rows[_arrayIndx] << " "
437 << _columns[_arrayIndx]
438 << endl;
439 }
440 }
441 successful=false;
442 }
443 }
444 }
445
446 if (_verb)
447 {
448 if (_MsgFlag <= 2) {
449 std::cout <<"EmcLSSMatrix::DEBUG "
450 << "Reduced LSSMatrix !!! Number of non zeros: "
451 << _newIndx << endl;
452 }
453 }
454
455 return successful;
456}

Referenced by EmcBhaCalibData::reduce().

◆ reset()

void EmcLSSMatrix::reset ( )

Definition at line 370 of file EmcLSSMatrix.cxx.

371{
372 for (int i=0; i<_size;i++)
373 {
374 _matrix[i] =0.;
375 _rows[i] = -1;
376 _columns[i] = -1;
377 }
378
379}

Referenced by EmcBhaCalibData::reset().

◆ row()

int * EmcLSSMatrix::row ( ) const
inline

Definition at line 84 of file EmcLSSMatrix.h.

84{ return _rows; }

Referenced by find(), num_filled_cols(), and operator()().

◆ set_verbose()

void EmcLSSMatrix::set_verbose ( bool  setting)
inline

Definition at line 111 of file EmcLSSMatrix.h.

111{ _verb = setting; }

◆ size()

const long & EmcLSSMatrix::size ( ) const
inline

Definition at line 81 of file EmcLSSMatrix.h.

81{ return _size; }

◆ writeOut()

void EmcLSSMatrix::writeOut ( ostream &  Out)

Definition at line 546 of file EmcLSSMatrix.cxx.

547{
548 int* col_p = _columns;
549 int* row_p = _rows;
550 double* ele_p = _matrix;
551 double* mat_max_p = (_matrix + _size);
552 // long int nrele = 0;
553
554 long int nonz = num_nonZeros();
555 Out << nonz << " ";
556
557 while ( ele_p < mat_max_p )
558 {
559 if ( *ele_p != 0.)
560 {
561 Out<< *row_p << " "
562 << *col_p << " "
563 << *ele_p << " ";
564 }
565 col_p++;
566 row_p++;
567 ele_p++;
568 }
569
570 if (_MsgFlag <= 2) {
571 std::cout <<"EmcLSSMatrix::DEBUG "
572 << "Wrote " << nonz
573 << " non zero matrix elements to file !!!" << endl;
574 }
575
576}
long int num_nonZeros()

Referenced by EmcBhaCalibData::writeOut().


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