#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) |
Private Attributes | |
const long | _size |
const int | _nrrows |
const int | _nrcol |
double * | _matrix |
int * | _columns |
int * | _rows |
double * | _nothing |
bool | _verb |
int | _MsgFlag |
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)
Definition at line 47 of file EmcLSSMatrix.h.
EmcLSSMatrix::EmcLSSMatrix | ( | ) |
EmcLSSMatrix::EmcLSSMatrix | ( | int | rows, | |
int | nonzero_col | |||
) |
Definition at line 49 of file EmcLSSMatrix.cxx.
References _columns, _matrix, _MsgFlag, _nothing, _rows, _size, _verb, and genRecEmupikp::i.
00050 : _size(rows * nonzero_col), 00051 _nrrows(rows), 00052 _nrcol(nonzero_col) 00053 { 00054 _matrix = new double[_size]; 00055 _rows = new int[_size]; 00056 _columns = new int[_size]; 00057 for (int i=0; i<_size;i++) 00058 { 00059 _matrix[i] =0.; 00060 _rows[i] = -1; 00061 _columns[i] = -1; 00062 } 00063 _nothing = new double; 00064 _verb = false; 00065 00066 _MsgFlag =0; 00067 }
EmcLSSMatrix::EmcLSSMatrix | ( | int | rows, | |
int | nonzero_col, | |||
int | MsgFlag | |||
) |
Definition at line 69 of file EmcLSSMatrix.cxx.
References _columns, _matrix, _MsgFlag, _nothing, _rows, _size, _verb, and genRecEmupikp::i.
00070 : _size(rows * nonzero_col), 00071 _nrrows(rows), 00072 _nrcol(nonzero_col) 00073 { 00074 _matrix = new double[_size]; 00075 _rows = new int[_size]; 00076 _columns = new int[_size]; 00077 for (int i=0; i<_size;i++) 00078 { 00079 _matrix[i] =0.; 00080 _rows[i] = -1; 00081 _columns[i] = -1; 00082 } 00083 _nothing = new double; 00084 _verb = false; 00085 00086 _MsgFlag = MsgFlag; 00087 }
EmcLSSMatrix::EmcLSSMatrix | ( | const EmcLSSMatrix & | m1 | ) |
Definition at line 89 of file EmcLSSMatrix.cxx.
References _columns, _matrix, _nothing, _rows, _size, _verb, and genRecEmupikp::i.
00090 : _size(m1._size), 00091 _nrrows(m1._nrrows), 00092 _nrcol(m1._nrcol) 00093 00094 { 00095 _matrix = new double[_size]; 00096 _rows = new int[_size]; 00097 _columns = new int[_size]; 00098 _nothing = m1._nothing; 00099 _verb = m1._verb; 00100 00101 for ( long int i=0;i<_size;i++) 00102 { 00103 _matrix[i] = m1._matrix[i]; 00104 _rows[i] = m1._rows[i]; 00105 _columns[i] = m1._columns[i]; 00106 }; 00107 }
EmcLSSMatrix::~EmcLSSMatrix | ( | ) |
Definition at line 112 of file EmcLSSMatrix.cxx.
References _columns, _matrix, _nothing, and _rows.
00112 { 00113 if ( 0 != _matrix) 00114 { 00115 delete [] _matrix; 00116 _matrix = 0; 00117 } 00118 if ( 0 != _rows) 00119 { 00120 delete [] _rows; 00121 _rows = 0; 00122 } 00123 if ( 0 != _columns) 00124 { 00125 delete [] _columns; 00126 _columns = 0; 00127 } 00128 if ( 0 != _nothing) 00129 { 00130 delete _nothing; 00131 _nothing = 0; 00132 } 00133 }
int * EmcLSSMatrix::column | ( | const int & | rowind = 0 |
) | const |
Definition at line 295 of file EmcLSSMatrix.cxx.
References _columns, _MsgFlag, _nrcol, and _nrrows.
Referenced by EmcBhaCalib::solveEqua().
00296 { 00297 int* here=0; 00298 00299 if (rowind < _nrrows) 00300 { 00301 here = (_columns+(rowind*_nrcol)); 00302 } 00303 else 00304 { 00305 00306 if (_MsgFlag <= 5) { 00307 std::cout <<"EmcLSSMatrix::ERROR " 00308 << "EmcLSSMatrix.column: Error " 00309 << "- larger row index than existing requested !" 00310 << endl; 00311 } 00312 here = 0; 00313 } 00314 return here; 00315 }
long int EmcLSSMatrix::find | ( | int | row, | |
int | col | |||
) | [protected] |
Definition at line 164 of file EmcLSSMatrix.cxx.
References _columns, _matrix, _MsgFlag, _nrcol, _nrrows, and _rows.
Referenced by operator()(), and readIn().
00165 { 00166 int smaller,larger; 00167 00168 00169 if ( col > row ) 00170 { 00171 smaller = row; 00172 larger = col; 00173 } 00174 else 00175 { 00176 smaller = col; 00177 larger = row; 00178 } 00179 00180 int* col_p; 00181 int* row_p; 00182 double* matr_p; 00183 double* matr_row_max; 00184 00185 if (larger < 0 || larger > _nrrows || smaller < 0 || smaller > _nrrows ) 00186 { 00187 if (_MsgFlag <= 5) { 00188 std::cout <<"EmcLSSMatrix::ERROR" 00189 << "!!! ERROR in bound check of EmcLSSMatrix !!!" 00190 << "!!! Return zero !!!" 00191 << endl; 00192 } 00193 matr_p = 0; 00194 } 00195 else 00196 { 00197 col_p = (_columns + (larger * _nrcol)); 00198 row_p = (_rows + (larger * _nrcol)); 00199 matr_p = (_matrix + (larger * _nrcol)); 00200 matr_row_max = (matr_p + _nrcol); 00201 00202 while ( matr_p < matr_row_max ) { 00203 00204 if (_MsgFlag <= 1) { 00205 std::cout <<"EmcLSSMatrix::VERBOSE" 00206 << "C: " << larger << " " << smaller << " " 00207 << col_p << " " << *col_p << " " 00208 << matr_p << " " << *matr_p << " " 00209 << (_matrix+(matr_p-_matrix)) << " " 00210 << *(_matrix+(matr_p-_matrix)) << endl; 00211 } 00212 /* 00213 cout<< "C: " << larger << " " << smaller << " " 00214 << col_p << " " << *col_p << " " 00215 << matr_p << " " << *matr_p << " " 00216 << (_matrix+(matr_p-_matrix)) << " " 00217 << *(_matrix+(matr_p-_matrix)) <<endl; 00218 */ 00219 if ( matr_p == (matr_row_max-1) ) 00220 { 00221 if (_MsgFlag <= 4) { 00222 std::cout <<"EmcLSSMatrix::WARNING " 00223 << "!! WARNING: Reached maximum number of columns " 00224 << "in LSSMatrix when searching for row " 00225 << larger << " column " << smaller << " !!" 00226 << endl 00227 << "!! Return zero pointer !! " << endl; 00228 } 00229 matr_p = 0; 00230 break; 00231 } 00232 00233 //if row does already exist 00234 if ( *col_p == smaller ) 00235 { 00236 break; 00237 } 00238 00239 //if at the end of the list, use this as new element 00240 if ( (*matr_p == 0.) ) 00241 { 00242 *col_p = smaller; 00243 *row_p = larger; 00244 // if (*row_p == 1616 ) { 00245 // nun=(row_p-(_rows + (larger * _nrcol))); 00246 //if (_MsgFlag <= 2) { 00247 // std::cout <<"EmcLSSMatrix::DEBUG " 00248 // << nun << " " << larger << " " << smaller << " " 00249 // << *row_p << " " << *col_p << " " << *matr_p << endl; 00250 //} 00251 // } 00252 break; 00253 } 00254 00255 matr_p++; 00256 col_p++; 00257 row_p++; 00258 00259 } 00260 } 00261 00262 long int diff = matr_p-_matrix; 00263 00264 if (matr_p == 0) 00265 { 00266 diff = -1; 00267 } 00268 00269 return (diff); 00270 }
double * EmcLSSMatrix::matrix | ( | const int & | rowind = 0 |
) | const |
Definition at line 273 of file EmcLSSMatrix.cxx.
References _matrix, _MsgFlag, _nrcol, and _nrrows.
Referenced by EmcBhaCalib::solveEqua().
00274 { 00275 double* here=0; 00276 00277 if (rowind < _nrrows) 00278 { 00279 here = (_matrix+(rowind*_nrcol)); 00280 } 00281 else 00282 { 00283 if (_MsgFlag <= 5) { 00284 std::cout <<"EmcLSSMatrix::ERROR " 00285 << "EmcLSSMatrix::matrix: Error " 00286 << "- larger row index than existing requested !" 00287 << endl; 00288 } 00289 here = 0; 00290 } 00291 return here; 00292 }
const int& EmcLSSMatrix::num_cols | ( | ) | const [inline] |
int EmcLSSMatrix::num_filled_cols | ( | const int | row | ) | const |
Definition at line 318 of file EmcLSSMatrix.cxx.
References _matrix, and _nrcol.
Referenced by print_row().
00319 { 00320 double * search_i = _matrix + ( row * _nrcol ); 00321 double * max_i = search_i + _nrcol; 00322 int nonZeroCol=0; 00323 00324 while ( (*search_i != 0.) && (search_i < max_i) ) 00325 { 00326 nonZeroCol++; 00327 search_i++; 00328 } 00329 return nonZeroCol; 00330 }
int EmcLSSMatrix::num_filled_rows | ( | const int | col | ) | const |
Definition at line 333 of file EmcLSSMatrix.cxx.
References _columns, _matrix, _size, and genRecEmupikp::i.
00334 { 00335 int nonZeroRows = 0; 00336 for ( long int i=0; i<_size; i++ ) 00337 { 00338 if ( (_matrix[i] != 0.) && (_columns[i] == col) ) 00339 { 00340 nonZeroRows++; 00341 } 00342 } 00343 00344 return nonZeroRows; 00345 }
long int EmcLSSMatrix::num_nonZeros | ( | ) |
Definition at line 349 of file EmcLSSMatrix.cxx.
References _columns, _matrix, and _size.
Referenced by EmcBhaCalib::readInFromFile(), and writeOut().
00350 { 00351 int* col_p = _columns; 00352 double* ele_p = _matrix; 00353 double* mat_max_p = (_matrix + _size); 00354 long int nrele = 0; 00355 00356 while ( ele_p < mat_max_p ) 00357 { 00358 if ( *ele_p != 0.) nrele++; 00359 col_p++; 00360 ele_p++; 00361 } 00362 00363 return nrele; 00364 }
const int& EmcLSSMatrix::num_rows | ( | ) | const [inline] |
double & EmcLSSMatrix::operator() | ( | int | row, | |
int | col | |||
) |
Definition at line 139 of file EmcLSSMatrix.cxx.
References _matrix, _MsgFlag, _nothing, and find().
00139 { 00140 00141 long int found = find( row, col ); 00142 00143 00144 if ( -1 == found ) 00145 { 00146 if (_MsgFlag <= 5) { 00147 std::cout << "EmcLSSMatrix:: ERROR " 00148 << "EmcLSSMatrix: matrix element not found !!" << endl 00149 << "EmcLSSMatrix: Return zero !" << endl; 00150 } 00151 _nothing = 0; 00152 return *_nothing; 00153 } 00154 00155 return (*(_matrix+found)); 00156 }
void EmcLSSMatrix::print_NonZeros | ( | ) |
Definition at line 460 of file EmcLSSMatrix.cxx.
References _columns, _matrix, _MsgFlag, _rows, and _size.
Referenced by EmcBhaCalibData::readIn().
00461 { 00462 int* col_p = _columns; 00463 int* row_p = _rows; 00464 double* ele_p = _matrix; 00465 double* mat_max_p = (_matrix + _size); 00466 long int wo = 0; 00467 long int nrele = 0; 00468 00469 00470 if (_MsgFlag <= 2) { 00471 std::cout <<"EmcLSSMatrix::DEBUG " 00472 <<"List of nonzero Matrix elements (Matrix size: " 00473 << _size << " ) : " << endl; 00474 } 00475 00476 while ( ele_p < mat_max_p ) 00477 { 00478 if ( *ele_p != 0. ) 00479 { 00480 nrele++; 00481 if (_MsgFlag <= 2) { 00482 std::cout <<"EmcLSSMatrix::DEBUG " 00483 << "nr: " << nrele 00484 << " M( " << *row_p << " , " << *col_p 00485 << " ): " << *ele_p 00486 << " array index: " << wo << endl; 00487 } 00488 /* 00489 cout<< "nr: " << nrele 00490 << " M( " << *row_p << " , " << *col_p 00491 << " ): " << *ele_p 00492 << " array index: " << wo << endl; 00493 */ 00494 } 00495 wo++; 00496 col_p++; 00497 row_p++; 00498 ele_p++; 00499 } 00500 00501 }
void EmcLSSMatrix::print_row | ( | int | rownr | ) |
Definition at line 505 of file EmcLSSMatrix.cxx.
References _columns, _matrix, _MsgFlag, _nrcol, _rows, and num_filled_cols().
00506 { 00507 int* col_p = _columns+(_nrcol*rownr); 00508 int* row_p = _rows+(_nrcol*rownr); 00509 double* ele_p = _matrix+(_nrcol*rownr); 00510 double* mat_max_p = (ele_p + _nrcol); 00511 long int wo = 0; 00512 long int nrele = 0; 00513 00514 if (_MsgFlag <= 2) { 00515 std::cout <<"EmcLSSMatrix::DEBUG " 00516 <<"row length: " << num_filled_cols(rownr) << endl; 00517 } 00518 while ( ele_p < mat_max_p ) 00519 { 00520 if ( *ele_p != 0. ) 00521 { 00522 nrele++; 00523 if (_MsgFlag <= 2) { 00524 std::cout <<"EmcLSSMatrix::DEBUG " 00525 << "nr: " << nrele 00526 << " M( " << *row_p << " , " << *col_p 00527 << " ): " << *ele_p 00528 << " array index: " << wo << endl; 00529 } 00530 /* 00531 cout<< "nr: " << nrele 00532 << " M( " << *row_p << " , " << *col_p 00533 << " ): " << *ele_p 00534 << " array index: " << wo << endl; 00535 */ 00536 } 00537 wo++; 00538 col_p++; 00539 row_p++; 00540 ele_p++; 00541 } 00542 00543 }
void EmcLSSMatrix::readIn | ( | istream & | In | ) |
Definition at line 580 of file EmcLSSMatrix.cxx.
References _columns, _matrix, _MsgFlag, _rows, _verb, find(), and genRecEmupikp::i.
Referenced by EmcBhaCalibData::readIn().
00581 { 00582 // long int nonz = num_nonZeros(); 00583 long int nonz = 0; 00584 In >> nonz; 00585 00586 cout<<"nonz="<<nonz<<endl; 00587 00588 int theRow; 00589 int theCol; 00590 long int index; 00591 double theEle; 00592 00593 00594 for (long int i=0; i<nonz; i++ ) 00595 { 00596 In >> theRow >> theCol >> theEle; 00597 index = find (theRow,theCol); 00598 /* 00599 cout<<"index = "<<index 00600 <<"row="<<theRow 00601 <<"col="<<theCol<<endl; 00602 */ 00603 if ( -1 != index ) 00604 { 00605 _matrix[index] += theEle; 00606 if (_verb) 00607 { 00608 if ( i < 50 || i > (nonz-10) ) 00609 { 00610 if (_MsgFlag <= 2) { 00611 std::cout <<"EmcLSSMatrix::DEBUG " 00612 << "M: " << _rows[index] << " " << _columns[index] 00613 << " " << _matrix[index] << " " << index << endl; 00614 } 00615 } 00616 } 00617 } 00618 } 00619 00620 if (_verb) 00621 { 00622 if (_MsgFlag <= 2) { 00623 std::cout <<"EmcLSSMatrix::DEBUG " 00624 << "Have read in " << nonz 00625 << " non zero matrix elements from file !!!" << endl; 00626 } 00627 } 00628 }
bool EmcLSSMatrix::reduce_Matrix | ( | int * | xRef_list | ) |
Definition at line 382 of file EmcLSSMatrix.cxx.
References _columns, _matrix, _MsgFlag, _rows, _size, and _verb.
Referenced by EmcBhaCalibData::reduce().
00383 { 00384 bool successful = true; 00385 00386 //delete all zero elements in matrix 00387 //save only non zero elements 00388 00389 long int _newIndx = 0; 00390 00391 00392 for ( long int _arrayIndx = 0; 00393 _arrayIndx < _size; _arrayIndx++) 00394 { 00395 00396 //add 1 to matrix indices because SLAP wants indices 1..N, 00397 //but Xtals counting in geometry starts with 0 00398 00399 if ( _matrix[_arrayIndx] > 0. ) 00400 { 00401 00402 if ( (xRef_list[(_rows[_arrayIndx])]) >= 0 00403 && (xRef_list[(_columns[_arrayIndx])]) >= 0 ) 00404 { 00405 _matrix[_newIndx] = _matrix[_arrayIndx]; 00406 _rows[_newIndx] = ((xRef_list[(_rows[_arrayIndx])])+1); 00407 _columns[_newIndx] = ((xRef_list[(_columns[_arrayIndx])])+1); 00408 _newIndx++; 00409 } 00410 else 00411 { 00412 00413 //int indxtal; 00414 if (xRef_list[(_rows[_arrayIndx])] < 0 ) 00415 { 00416 if (_MsgFlag <= 5) { 00417 std::cout <<"EmcLSSMatrix::ERROR " 00418 << "EmcLSSMatrix: Xtal index " 00419 << _rows[_arrayIndx] 00420 << " appears in matrix, " 00421 << "but not in vector !!! " 00422 << _rows[_arrayIndx] << " " 00423 << _columns[_arrayIndx] 00424 << endl; 00425 } 00426 } 00427 else 00428 { 00429 00430 if (_MsgFlag <= 5) { 00431 std::cout <<"EmcLSSMatrix::ERROR " 00432 << "EmcLSSMatrix: Xtal index " 00433 << _columns[_arrayIndx] 00434 << " appears in matrix, " 00435 << "but not in vector !!! " 00436 << _rows[_arrayIndx] << " " 00437 << _columns[_arrayIndx] 00438 << endl; 00439 } 00440 } 00441 successful=false; 00442 } 00443 } 00444 } 00445 00446 if (_verb) 00447 { 00448 if (_MsgFlag <= 2) { 00449 std::cout <<"EmcLSSMatrix::DEBUG " 00450 << "Reduced LSSMatrix !!! Number of non zeros: " 00451 << _newIndx << endl; 00452 } 00453 } 00454 00455 return successful; 00456 }
void EmcLSSMatrix::reset | ( | ) |
int* EmcLSSMatrix::row | ( | ) | const [inline] |
Definition at line 84 of file EmcLSSMatrix.h.
References _rows.
Referenced by EmcBhaCalib::solveEqua().
00084 { return _rows; }
void EmcLSSMatrix::set_verbose | ( | bool | setting | ) | [inline] |
const long& EmcLSSMatrix::size | ( | ) | const [inline] |
void EmcLSSMatrix::writeOut | ( | ostream & | Out | ) |
Definition at line 546 of file EmcLSSMatrix.cxx.
References _columns, _matrix, _MsgFlag, _rows, _size, and num_nonZeros().
Referenced by EmcBhaCalibData::writeOut().
00547 { 00548 int* col_p = _columns; 00549 int* row_p = _rows; 00550 double* ele_p = _matrix; 00551 double* mat_max_p = (_matrix + _size); 00552 // long int nrele = 0; 00553 00554 long int nonz = num_nonZeros(); 00555 Out << nonz << " "; 00556 00557 while ( ele_p < mat_max_p ) 00558 { 00559 if ( *ele_p != 0.) 00560 { 00561 Out<< *row_p << " " 00562 << *col_p << " " 00563 << *ele_p << " "; 00564 } 00565 col_p++; 00566 row_p++; 00567 ele_p++; 00568 } 00569 00570 if (_MsgFlag <= 2) { 00571 std::cout <<"EmcLSSMatrix::DEBUG " 00572 << "Wrote " << nonz 00573 << " non zero matrix elements to file !!!" << endl; 00574 } 00575 00576 }
int* EmcLSSMatrix::_columns [private] |
Definition at line 131 of file EmcLSSMatrix.h.
Referenced by column(), EmcLSSMatrix(), find(), num_filled_rows(), num_nonZeros(), print_NonZeros(), print_row(), readIn(), reduce_Matrix(), reset(), writeOut(), and ~EmcLSSMatrix().
double* EmcLSSMatrix::_matrix [private] |
Definition at line 130 of file EmcLSSMatrix.h.
Referenced by EmcLSSMatrix(), find(), matrix(), num_filled_cols(), num_filled_rows(), num_nonZeros(), operator()(), print_NonZeros(), print_row(), readIn(), reduce_Matrix(), reset(), writeOut(), and ~EmcLSSMatrix().
int EmcLSSMatrix::_MsgFlag [private] |
Definition at line 136 of file EmcLSSMatrix.h.
Referenced by column(), EmcLSSMatrix(), find(), matrix(), operator()(), print_NonZeros(), print_row(), readIn(), reduce_Matrix(), and writeOut().
double* EmcLSSMatrix::_nothing [private] |
Definition at line 133 of file EmcLSSMatrix.h.
Referenced by EmcLSSMatrix(), operator()(), and ~EmcLSSMatrix().
const int EmcLSSMatrix::_nrcol [private] |
Definition at line 129 of file EmcLSSMatrix.h.
Referenced by column(), find(), matrix(), num_cols(), num_filled_cols(), and print_row().
const int EmcLSSMatrix::_nrrows [private] |
Definition at line 128 of file EmcLSSMatrix.h.
Referenced by column(), find(), matrix(), and num_rows().
int* EmcLSSMatrix::_rows [private] |
Definition at line 132 of file EmcLSSMatrix.h.
Referenced by EmcLSSMatrix(), find(), print_NonZeros(), print_row(), readIn(), reduce_Matrix(), reset(), row(), writeOut(), and ~EmcLSSMatrix().
const long EmcLSSMatrix::_size [private] |
Definition at line 127 of file EmcLSSMatrix.h.
Referenced by EmcLSSMatrix(), num_filled_rows(), num_nonZeros(), print_NonZeros(), reduce_Matrix(), reset(), size(), and writeOut().
bool EmcLSSMatrix::_verb [private] |
Definition at line 134 of file EmcLSSMatrix.h.
Referenced by EmcLSSMatrix(), readIn(), reduce_Matrix(), and set_verbose().