00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "EmcBhaCalib/EmcLSSMatrix.h"
00021
00022
00023
00024
00025 extern "C" {
00026 }
00027
00028
00029
00030
00031
00032 #include <fstream>
00033
00034 using namespace std;
00035
00036
00037
00038
00039
00040
00041
00042
00043 EmcLSSMatrix::EmcLSSMatrix()
00044 : _size(0), _nrrows(0), _nrcol(0), _matrix(0), _columns(0), _rows(0),
00045 _nothing(0),_verb(false)
00046 {
00047 }
00048
00049 EmcLSSMatrix::EmcLSSMatrix( int rows, int nonzero_col)
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 }
00068
00069 EmcLSSMatrix::EmcLSSMatrix( int rows, int nonzero_col, int MsgFlag)
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 }
00088
00089 EmcLSSMatrix::EmcLSSMatrix( const EmcLSSMatrix &m1 )
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 }
00108
00109
00110
00111
00112 EmcLSSMatrix::~EmcLSSMatrix() {
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 }
00134
00135
00136
00137
00138 double&
00139 EmcLSSMatrix::operator()(int row, int col) {
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 }
00157
00158
00159
00160
00161
00162
00163 long int
00164 EmcLSSMatrix::find( int row, int col)
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
00214
00215
00216
00217
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
00234 if ( *col_p == smaller )
00235 {
00236 break;
00237 }
00238
00239
00240 if ( (*matr_p == 0.) )
00241 {
00242 *col_p = smaller;
00243 *row_p = larger;
00244
00245
00246
00247
00248
00249
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 }
00271
00272 double*
00273 EmcLSSMatrix::matrix( const int& rowind ) const
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 }
00293
00294 int*
00295 EmcLSSMatrix::column( const int& rowind ) const
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 }
00316
00317 int
00318 EmcLSSMatrix::num_filled_cols( const int row ) const
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 }
00331
00332 int
00333 EmcLSSMatrix::num_filled_rows( const int col ) const
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 }
00346
00347
00348 long int
00349 EmcLSSMatrix::num_nonZeros()
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 }
00365
00366
00367
00368
00369 void
00370 EmcLSSMatrix::reset()
00371 {
00372 for (int i=0; i<_size;i++)
00373 {
00374 _matrix[i] =0.;
00375 _rows[i] = -1;
00376 _columns[i] = -1;
00377 }
00378
00379 }
00380
00381 bool
00382 EmcLSSMatrix::reduce_Matrix(int* xRef_list)
00383 {
00384 bool successful = true;
00385
00386
00387
00388
00389 long int _newIndx = 0;
00390
00391
00392 for ( long int _arrayIndx = 0;
00393 _arrayIndx < _size; _arrayIndx++)
00394 {
00395
00396
00397
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
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 }
00457
00458
00459 void
00460 EmcLSSMatrix::print_NonZeros()
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
00490
00491
00492
00493
00494 }
00495 wo++;
00496 col_p++;
00497 row_p++;
00498 ele_p++;
00499 }
00500
00501 }
00502
00503
00504 void
00505 EmcLSSMatrix::print_row(int rownr)
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
00532
00533
00534
00535
00536 }
00537 wo++;
00538 col_p++;
00539 row_p++;
00540 ele_p++;
00541 }
00542
00543 }
00544
00545 void
00546 EmcLSSMatrix::writeOut( ostream& Out)
00547 {
00548 int* col_p = _columns;
00549 int* row_p = _rows;
00550 double* ele_p = _matrix;
00551 double* mat_max_p = (_matrix + _size);
00552
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 }
00577
00578
00579 void
00580 EmcLSSMatrix::readIn( istream& In)
00581 {
00582
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
00600
00601
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 }
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640