94 template<
class Element>
97 Allocate(row_upb-row_lwb+1,col_upb-col_lwb+1,row_lwb,col_lwb,1);
104 template<
class Element>
113 if (row[irowmin] < row_lwb || row[irowmax] > row_upb) {
114 Error(
"TMatrixTSparse",
"Inconsistency between row index and its range");
115 if (row[irowmin] < row_lwb) {
116 Info(
"TMatrixTSparse",
"row index lower bound adjusted to %d",row[irowmin]);
117 row_lwb = row[irowmin];
119 if (row[irowmax] > row_upb) {
120 Info(
"TMatrixTSparse",
"row index upper bound adjusted to %d",row[irowmax]);
121 col_lwb = col[irowmax];
124 if (col[icolmin] < col_lwb || col[icolmax] > col_upb) {
125 Error(
"TMatrixTSparse",
"Inconsistency between column index and its range");
126 if (col[icolmin] < col_lwb) {
127 Info(
"TMatrixTSparse",
"column index lower bound adjusted to %d",col[icolmin]);
128 col_lwb = col[icolmin];
130 if (col[icolmax] > col_upb) {
131 Info(
"TMatrixTSparse",
"column index upper bound adjusted to %d",col[icolmax]);
132 col_upb = col[icolmax];
136 Allocate(row_upb-row_lwb+1,col_upb-col_lwb+1,row_lwb,col_lwb,1,nr);
138 SetMatrixArray(nr,row,col,data);
143 template<
class Element>
156 template<
class Element>
169 template<
class Element>
174 Int_t nr_nonzeros = 0;
189 for (
Int_t i = rowLwb; i <= rowLwb+nrows-1; i++)
190 for (
Int_t j = colLwb; j <= colLwb+ncols-1; j++)
191 if (i==j) nr_nonzeros++;
192 Allocate(nrows,ncols,rowLwb,colLwb,1,nr_nonzeros);
211 Error(
"TMatrixTSparse(EMatrixCreatorOp1)",
"operation %d not yet implemented", op);
219 template<
class Element>
243 Error(
"TMatrixTSparse(EMatrixCreatorOp2)",
"operation %d not yet implemented",op);
251 template<
class Element>
275 Error(
"TMatrixTSparse(EMatrixCreatorOp2)",
"operation %d not yet implemented",op);
283 template<
class Element>
307 Error(
"TMatrixTSparse(EMatrixCreatorOp2)",
"operation %d not yet implemented",op);
316 template<
class Element>
320 if ( (nr_nonzeros > 0 && (no_rows == 0 || no_cols == 0)) ||
321 (no_rows < 0 || no_cols < 0 || nr_nonzeros < 0) )
323 Error(
"Allocate",
"no_rows=%d no_cols=%d non_zeros=%d",no_rows,no_cols,nr_nonzeros);
329 this->fNrows = no_rows;
330 this->fNcols = no_cols;
331 this->fRowLwb = row_lwb;
332 this->fColLwb = col_lwb;
333 this->fNrowIndex = this->fNrows+1;
334 this->fNelems = nr_nonzeros;
335 this->fIsOwner =
kTRUE;
338 fRowIndex =
new Int_t[this->fNrowIndex];
340 memset(fRowIndex,0,this->fNrowIndex*
sizeof(
Int_t));
342 if (this->fNelems > 0) {
343 fElements =
new Element[this->fNelems];
344 fColIndex =
new Int_t [this->fNelems];
346 memset(fElements,0,this->fNelems*
sizeof(Element));
347 memset(fColIndex,0,this->fNelems*
sizeof(
Int_t));
358 template<
class Element>
361 const Int_t arown = rown-this->fRowLwb;
362 const Int_t acoln = coln-this->fColLwb;
363 const Int_t nr = (n > 0) ? n : this->fNcols;
366 if (arown >= this->fNrows || arown < 0) {
367 Error(
"InsertRow",
"row %d out of matrix range",rown);
371 if (acoln >= this->fNcols || acoln < 0) {
372 Error(
"InsertRow",
"column %d out of matrix range",coln);
376 if (acoln+nr > this->fNcols || nr < 0) {
377 Error(
"InsertRow",
"row length %d out of range",nr);
382 const Int_t sIndex = fRowIndex[arown];
383 const Int_t eIndex = fRowIndex[arown+1];
390 Int_t lIndex = sIndex-1;
391 Int_t rIndex = sIndex-1;
393 for (index = sIndex; index < eIndex; index++) {
394 const Int_t icol = fColIndex[index];
396 if (icol >= acoln+nr)
break;
397 if (icol >= acoln) nslots++;
401 const Int_t nelems_old = this->fNelems;
402 const Int_t *colIndex_old = fColIndex;
403 const Element *elements_old = fElements;
405 const Int_t ndiff = nr-nslots;
406 this->fNelems += ndiff;
407 fColIndex =
new Int_t[this->fNelems];
408 fElements =
new Element[this->fNelems];
410 for (
Int_t irow = arown+1; irow < this->fNrows+1; irow++)
411 fRowIndex[irow] += ndiff;
414 memmove(fColIndex,colIndex_old,(lIndex+1)*
sizeof(
Int_t));
415 memmove(fElements,elements_old,(lIndex+1)*
sizeof(Element));
418 if (nelems_old > 0 && nelems_old-rIndex > 0) {
419 memmove(fColIndex+rIndex+ndiff,colIndex_old+rIndex,(nelems_old-rIndex)*
sizeof(
Int_t));
420 memmove(fElements+rIndex+ndiff,elements_old+rIndex,(nelems_old-rIndex)*
sizeof(Element));
424 for (
Int_t i = 0; i < nr; i++) {
425 fColIndex[index] = acoln+i;
426 fElements[index] = v[i];
430 if (colIndex_old)
delete [] (
Int_t*) colIndex_old;
431 if (elements_old)
delete [] (Element*) elements_old;
433 R__ASSERT(this->fNelems == fRowIndex[this->fNrowIndex-1]);
441 template<
class Element>
444 const Int_t arown = rown-this->fRowLwb;
445 const Int_t acoln = coln-this->fColLwb;
446 const Int_t nr = (n > 0) ? n : this->fNcols;
449 if (arown >= this->fNrows || arown < 0) {
450 Error(
"ExtractRow",
"row %d out of matrix range",rown);
454 if (acoln >= this->fNcols || acoln < 0) {
455 Error(
"ExtractRow",
"column %d out of matrix range",coln);
459 if (acoln+nr > this->fNcols || nr < 0) {
460 Error(
"ExtractRow",
"row length %d out of range",nr);
465 const Int_t sIndex = fRowIndex[arown];
466 const Int_t eIndex = fRowIndex[arown+1];
468 memset(v,0,nr*
sizeof(Element));
469 const Int_t *
const pColIndex = GetColIndexArray();
470 const Element *
const pData = GetMatrixArray();
471 for (
Int_t index = sIndex; index < eIndex; index++) {
472 const Int_t icol = pColIndex[index];
473 if (icol < acoln || icol >= acoln+nr)
continue;
474 v[icol-acoln] = pData[index];
482 template<
class Element>
490 Error(
"AMultBt",
"A and B columns incompatible");
495 Error(
"AMultB",
"this = &a");
500 Error(
"AMultB",
"this = &b");
516 Int_t nr_nonzero_rowa = 0;
519 if (pRowIndexa[irowa] < pRowIndexa[irowa+1])
522 Int_t nr_nonzero_rowb = 0;
525 if (pRowIndexb[irowb] < pRowIndexb[irowb+1])
529 const Int_t nc = nr_nonzero_rowa*nr_nonzero_rowb;
532 pRowIndexc = this->GetRowIndexArray();
533 pColIndexc = this->GetColIndexArray();
538 pRowIndexc[irowa+1] = pRowIndexc[irowa];
539 if (pRowIndexa[irowa] >= pRowIndexa[irowa+1])
continue;
541 if (pRowIndexb[irowb] >= pRowIndexb[irowb+1])
continue;
542 pRowIndexc[irowa+1]++;
543 pColIndexc[ielem++] = irowb;
547 pRowIndexc = this->GetRowIndexArray();
548 pColIndexc = this->GetColIndexArray();
553 Element *
const pDatac = this->GetMatrixArray();
555 for (
Int_t irowc = 0; irowc < this->GetNrows(); irowc++) {
556 const Int_t sIndexa = pRowIndexa[irowc];
557 const Int_t eIndexa = pRowIndexa[irowc+1];
558 for (
Int_t icolc = 0; icolc < this->GetNcols(); icolc++) {
559 const Int_t sIndexb = pRowIndexb[icolc];
560 const Int_t eIndexb = pRowIndexb[icolc+1];
562 Int_t indexb = sIndexb;
563 for (
Int_t indexa = sIndexa; indexa < eIndexa && indexb < eIndexb; indexa++) {
564 const Int_t icola = pColIndexa[indexa];
565 while (indexb < eIndexb && pColIndexb[indexb] <= icola) {
566 if (icola == pColIndexb[indexb]) {
567 sum += pDataa[indexa]*pDatab[indexb];
574 pColIndexc[indexc_r] = icolc;
575 pDatac[indexc_r] = sum;
579 pRowIndexc[irowc+1] = indexc_r;
583 SetSparseIndex(indexc_r);
590 template<
class Element>
598 Error(
"AMultBt",
"A and B columns incompatible");
603 Error(
"AMultB",
"this = &a");
608 Error(
"AMultB",
"this = &b");
622 Int_t nr_nonzero_rowa = 0;
625 if (pRowIndexa[irowa] < pRowIndexa[irowa+1])
630 const Int_t nc = nr_nonzero_rowa*nr_nonzero_rowb;
633 pRowIndexc = this->GetRowIndexArray();
634 pColIndexc = this->GetColIndexArray();
639 pRowIndexc[irowa+1] = pRowIndexc[irowa];
641 pRowIndexc[irowa+1]++;
642 pColIndexc[ielem++] = irowb;
646 pRowIndexc = this->GetRowIndexArray();
647 pColIndexc = this->GetColIndexArray();
652 Element *
const pDatac = this->GetMatrixArray();
654 for (
Int_t irowc = 0; irowc < this->GetNrows(); irowc++) {
655 const Int_t sIndexa = pRowIndexa[irowc];
656 const Int_t eIndexa = pRowIndexa[irowc+1];
657 for (
Int_t icolc = 0; icolc < this->GetNcols(); icolc++) {
660 for (
Int_t indexa = sIndexa; indexa < eIndexa; indexa++) {
661 const Int_t icola = pColIndexa[indexa];
662 sum += pDataa[indexa]*pDatab[off+icola];
665 pColIndexc[indexc_r] = icolc;
666 pDatac[indexc_r] = sum;
670 pRowIndexc[irowc+1]= indexc_r;
674 SetSparseIndex(indexc_r);
681 template<
class Element>
689 Error(
"AMultBt",
"A and B columns incompatible");
694 Error(
"AMultB",
"this = &a");
699 Error(
"AMultB",
"this = &b");
714 Int_t nr_nonzero_rowb = 0;
717 if (pRowIndexb[irowb] < pRowIndexb[irowb+1])
721 const Int_t nc = nr_nonzero_rowa*nr_nonzero_rowb;
724 pRowIndexc = this->GetRowIndexArray();
725 pColIndexc = this->GetColIndexArray();
730 pRowIndexc[irowa+1] = pRowIndexc[irowa];
732 if (pRowIndexb[irowb] >= pRowIndexb[irowb+1])
continue;
733 pRowIndexc[irowa+1]++;
734 pColIndexc[ielem++] = irowb;
738 pRowIndexc = this->GetRowIndexArray();
739 pColIndexc = this->GetColIndexArray();
744 Element *
const pDatac = this->GetMatrixArray();
746 for (
Int_t irowc = 0; irowc < this->GetNrows(); irowc++) {
748 for (
Int_t icolc = 0; icolc < this->GetNcols(); icolc++) {
749 const Int_t sIndexb = pRowIndexb[icolc];
750 const Int_t eIndexb = pRowIndexb[icolc+1];
752 for (
Int_t indexb = sIndexb; indexb < eIndexb; indexb++) {
753 const Int_t icolb = pColIndexb[indexb];
754 sum += pDataa[off+icolb]*pDatab[indexb];
757 pColIndexc[indexc_r] = icolc;
758 pDatac[indexc_r] = sum;
762 pRowIndexc[irowc+1] = indexc_r;
766 SetSparseIndex(indexc_r);
773 template<
class Element>
782 Error(
"APlusB(const TMatrixTSparse &,const TMatrixTSparse &",
"matrices not compatible");
787 Error(
"APlusB",
"this = &a");
792 Error(
"APlusB",
"this = &b");
804 SetSparseIndexAB(a,b);
807 Int_t *
const pRowIndexc = this->GetRowIndexArray();
808 Int_t *
const pColIndexc = this->GetColIndexArray();
812 Element *
const pDatac = this->GetMatrixArray();
814 for (
Int_t irowc = 0; irowc < this->GetNrows(); irowc++) {
815 const Int_t sIndexa = pRowIndexa[irowc];
816 const Int_t eIndexa = pRowIndexa[irowc+1];
817 const Int_t sIndexb = pRowIndexb[irowc];
818 const Int_t eIndexb = pRowIndexb[irowc+1];
819 Int_t indexa = sIndexa;
820 Int_t indexb = sIndexb;
821 for (
Int_t icolc = 0; icolc < this->GetNcols(); icolc++) {
823 while (indexa < eIndexa && pColIndexa[indexa] <= icolc) {
824 if (icolc == pColIndexa[indexa]) {
825 sum += pDataa[indexa];
830 while (indexb < eIndexb && pColIndexb[indexb] <= icolc) {
831 if (icolc == pColIndexb[indexb]) {
832 sum += pDatab[indexb];
839 pColIndexc[indexc_r] = icolc;
840 pDatac[indexc_r] = sum;
844 pRowIndexc[irowc+1] = indexc_r;
848 SetSparseIndex(indexc_r);
855 template<
class Element>
864 Error(
"APlusB(const TMatrixTSparse &,const TMatrixT &",
"matrices not compatible");
869 Error(
"APlusB",
"this = &a");
874 Error(
"APlusB",
"this = &b");
881 SetSparseIndexAB(a,b);
884 Int_t *
const pRowIndexc = this->GetRowIndexArray();
885 Int_t *
const pColIndexc = this->GetColIndexArray();
892 Element *
const pDatac = this->GetMatrixArray();
894 for (
Int_t irowc = 0; irowc < this->GetNrows(); irowc++) {
895 const Int_t sIndexa = pRowIndexa[irowc];
896 const Int_t eIndexa = pRowIndexa[irowc+1];
897 const Int_t off = irowc*this->GetNcols();
898 Int_t indexa = sIndexa;
899 for (
Int_t icolc = 0; icolc < this->GetNcols(); icolc++) {
900 Element sum = pDatab[off+icolc];
901 while (indexa < eIndexa && pColIndexa[indexa] <= icolc) {
902 if (icolc == pColIndexa[indexa]) {
903 sum += pDataa[indexa];
910 pColIndexc[indexc_r] = icolc;
911 pDatac[indexc_r] = sum;
915 pRowIndexc[irowc+1] = indexc_r;
919 SetSparseIndex(indexc_r);
926 template<
class Element>
935 Error(
"AMinusB(const TMatrixTSparse &,const TMatrixTSparse &",
"matrices not compatible");
940 Error(
"AMinusB",
"this = &a");
945 Error(
"AMinusB",
"this = &b");
957 SetSparseIndexAB(a,b);
960 Int_t *
const pRowIndexc = this->GetRowIndexArray();
961 Int_t *
const pColIndexc = this->GetColIndexArray();
965 Element *
const pDatac = this->GetMatrixArray();
967 for (
Int_t irowc = 0; irowc < this->GetNrows(); irowc++) {
968 const Int_t sIndexa = pRowIndexa[irowc];
969 const Int_t eIndexa = pRowIndexa[irowc+1];
970 const Int_t sIndexb = pRowIndexb[irowc];
971 const Int_t eIndexb = pRowIndexb[irowc+1];
972 Int_t indexa = sIndexa;
973 Int_t indexb = sIndexb;
974 for (
Int_t icolc = 0; icolc < this->GetNcols(); icolc++) {
976 while (indexa < eIndexa && pColIndexa[indexa] <= icolc) {
977 if (icolc == pColIndexa[indexa]) {
978 sum += pDataa[indexa];
983 while (indexb < eIndexb && pColIndexb[indexb] <= icolc) {
984 if (icolc == pColIndexb[indexb]) {
985 sum -= pDatab[indexb];
992 pColIndexc[indexc_r] = icolc;
993 pDatac[indexc_r] = sum;
997 pRowIndexc[irowc+1] = indexc_r;
1001 SetSparseIndex(indexc_r);
1008 template<
class Element>
1017 Error(
"AMinusB(const TMatrixTSparse &,const TMatrixT &",
"matrices not compatible");
1022 Error(
"AMinusB",
"this = &a");
1027 Error(
"AMinusB",
"this = &b");
1034 SetSparseIndexAB(a,b);
1037 Int_t *
const pRowIndexc = this->GetRowIndexArray();
1038 Int_t *
const pColIndexc = this->GetColIndexArray();
1045 Element *
const pDatac = this->GetMatrixArray();
1047 for (
Int_t irowc = 0; irowc < this->GetNrows(); irowc++) {
1048 const Int_t sIndexa = pRowIndexa[irowc];
1049 const Int_t eIndexa = pRowIndexa[irowc+1];
1050 const Int_t off = irowc*this->GetNcols();
1051 Int_t indexa = sIndexa;
1052 for (
Int_t icolc = 0; icolc < this->GetNcols(); icolc++) {
1053 Element sum = -pDatab[off+icolc];
1054 while (indexa < eIndexa && pColIndexa[indexa] <= icolc) {
1055 if (icolc == pColIndexa[indexa]) {
1056 sum += pDataa[indexa];
1063 pColIndexc[indexc_r] = icolc;
1064 pDatac[indexc_r] = sum;
1068 pRowIndexc[irowc+1] = indexc_r;
1072 SetSparseIndex(indexc_r);
1079 template<
class Element>
1088 Error(
"AMinusB(const TMatrixT &,const TMatrixTSparse &",
"matrices not compatible");
1093 Error(
"AMinusB",
"this = &a");
1098 Error(
"AMinusB",
"this = &b");
1105 SetSparseIndexAB(a,b);
1108 Int_t *
const pRowIndexc = this->GetRowIndexArray();
1109 Int_t *
const pColIndexc = this->GetColIndexArray();
1116 Element *
const pDatac = this->GetMatrixArray();
1118 for (
Int_t irowc = 0; irowc < this->GetNrows(); irowc++) {
1119 const Int_t sIndexb = pRowIndexb[irowc];
1120 const Int_t eIndexb = pRowIndexb[irowc+1];
1121 const Int_t off = irowc*this->GetNcols();
1122 Int_t indexb = sIndexb;
1123 for (
Int_t icolc = 0; icolc < this->GetNcols(); icolc++) {
1124 Element sum = pDataa[off+icolc];
1125 while (indexb < eIndexb && pColIndexb[indexb] <= icolc) {
1126 if (icolc == pColIndexb[indexb]) {
1127 sum -= pDatab[indexb];
1134 pColIndexc[indexc_r] = icolc;
1135 pDatac[indexc_r] = sum;
1139 pRowIndexc[irowc+1] = indexc_r;
1143 SetSparseIndex(indexc_r);
1149 template<
class Element>
1154 const Element *
const elem = GetMatrixArray();
1155 memcpy(data,elem,this->fNelems*
sizeof(Element));
1163 template<
class Element>
1168 Error(
"SetMatrixArray(Int_t,Int_t*,Int_t*,Element*",
"nr <= 0");
1177 R__ASSERT(row[irowmin] >= this->fRowLwb && row[irowmax] <= this->fRowLwb+this->fNrows-1);
1178 R__ASSERT(col[icolmin] >= this->fColLwb && col[icolmax] <= this->fColLwb+this->fNcols-1);
1180 if (row[irowmin] < this->fRowLwb || row[irowmax] > this->fRowLwb+this->fNrows-1) {
1181 Error(
"SetMatrixArray",
"Inconsistency between row index and its range");
1182 if (row[irowmin] < this->fRowLwb) {
1183 Info(
"SetMatrixArray",
"row index lower bound adjusted to %d",row[irowmin]);
1184 this->fRowLwb = row[irowmin];
1186 if (row[irowmax] > this->fRowLwb+this->fNrows-1) {
1187 Info(
"SetMatrixArray",
"row index upper bound adjusted to %d",row[irowmax]);
1188 this->fNrows = row[irowmax]-this->fRowLwb+1;
1191 if (col[icolmin] < this->fColLwb || col[icolmax] > this->fColLwb+this->fNcols-1) {
1192 Error(
"SetMatrixArray",
"Inconsistency between column index and its range");
1193 if (col[icolmin] < this->fColLwb) {
1194 Info(
"SetMatrixArray",
"column index lower bound adjusted to %d",col[icolmin]);
1195 this->fColLwb = col[icolmin];
1197 if (col[icolmax] > this->fColLwb+this->fNcols-1) {
1198 Info(
"SetMatrixArray",
"column index upper bound adjusted to %d",col[icolmax]);
1199 this->fNcols = col[icolmax]-this->fColLwb+1;
1205 Int_t nr_nonzeros = 0;
1206 const Element *ep = data;
1207 const Element *
const fp = data+nr;
1210 if (*ep++ != 0.0) nr_nonzeros++;
1212 if (nr_nonzeros != this->fNelems) {
1213 if (fColIndex) {
delete [] fColIndex; fColIndex = 0; }
1214 if (fElements) {
delete [] fElements; fElements = 0; }
1215 this->fNelems = nr_nonzeros;
1216 if (this->fNelems > 0) {
1217 fColIndex =
new Int_t[nr_nonzeros];
1218 fElements =
new Element[nr_nonzeros];
1225 if (this->fNelems <= 0)
1231 for (
Int_t irow = 1; irow < this->fNrows+1; irow++) {
1232 if (ielem < nr && row[ielem] < irow) {
1233 while (ielem < nr) {
1234 if (data[ielem] != 0.0) {
1235 fColIndex[nr_nonzeros] = col[ielem]-this->fColLwb;
1236 fElements[nr_nonzeros] = data[ielem];
1240 if (ielem >= nr || row[ielem] != row[ielem-1])
1244 fRowIndex[irow] = nr_nonzeros;
1253 template<
class Element>
1256 if (nelems_new != this->fNelems) {
1258 Int_t *oIp = fColIndex;
1259 fColIndex =
new Int_t[nelems_new];
1261 memmove(fColIndex,oIp,nr*
sizeof(
Int_t));
1264 Element *oDp = fElements;
1265 fElements =
new Element[nelems_new];
1267 memmove(fElements,oDp,nr*
sizeof(Element));
1270 this->fNelems = nelems_new;
1271 if (nelems_new > nr) {
1272 memset(fElements+nr,0,(nelems_new-nr)*
sizeof(Element));
1273 memset(fColIndex+nr,0,(nelems_new-nr)*
sizeof(
Int_t));
1275 for (
Int_t irow = 0; irow < this->fNrowIndex; irow++)
1276 if (fRowIndex[irow] > nelems_new)
1277 fRowIndex[irow] = nelems_new;
1287 template<
class Element>
1292 if (this->GetNrows() != source.
GetNrows() || this->GetNcols() != source.
GetNcols() ||
1293 this->GetRowLwb() != source.
GetRowLwb() || this->GetColLwb() != source.
GetColLwb()) {
1294 Error(
"SetSparseIndex",
"matrices not compatible");
1301 if (nr_nonzeros != this->fNelems)
1302 SetSparseIndex(nr_nonzeros);
1310 for (
Int_t irow = 0; irow < this->fNrows; irow++) {
1311 fRowIndex[irow] = nr;
1312 for (
Int_t icol = 0; icol < this->fNcols; icol++) {
1314 fColIndex[nr] = icol;
1320 fRowIndex[this->fNrows] = nr;
1330 template<
class Element>
1339 Error(
"SetSparseIndexAB",
"source matrices not compatible");
1343 if (this->GetNrows() != a.
GetNrows() || this->GetNcols() != a.
GetNcols() ||
1345 Error(
"SetSparseIndexAB",
"matrix not compatible with source matrices");
1358 const Int_t sIndexa = pRowIndexa[irowc];
1359 const Int_t eIndexa = pRowIndexa[irowc+1];
1360 const Int_t sIndexb = pRowIndexb[irowc];
1361 const Int_t eIndexb = pRowIndexb[irowc+1];
1362 nc += eIndexa-sIndexa;
1363 Int_t indexb = sIndexb;
1364 for (
Int_t indexa = sIndexa; indexa < eIndexa; indexa++) {
1365 const Int_t icola = pColIndexa[indexa];
1366 for (; indexb < eIndexb; indexb++) {
1367 if (pColIndexb[indexb] >= icola) {
1368 if (pColIndexb[indexb] == icola)
1375 while (indexb < eIndexb) {
1376 const Int_t icola = (eIndexa > sIndexa && eIndexa > 0) ? pColIndexa[eIndexa-1] : -1;
1377 if (pColIndexb[indexb++] > icola)
1383 if (this->NonZeros() != nc)
1386 Int_t *
const pRowIndexc = this->GetRowIndexArray();
1387 Int_t *
const pColIndexc = this->GetColIndexArray();
1392 const Int_t sIndexa = pRowIndexa[irowc];
1393 const Int_t eIndexa = pRowIndexa[irowc+1];
1394 const Int_t sIndexb = pRowIndexb[irowc];
1395 const Int_t eIndexb = pRowIndexb[irowc+1];
1396 Int_t indexb = sIndexb;
1397 for (
Int_t indexa = sIndexa; indexa < eIndexa; indexa++) {
1398 const Int_t icola = pColIndexa[indexa];
1399 for (; indexb < eIndexb; indexb++) {
1400 if (pColIndexb[indexb] >= icola) {
1401 if (pColIndexb[indexb] == icola)
1405 pColIndexc[nc++] = pColIndexb[indexb];
1407 pColIndexc[nc++] = pColIndexa[indexa];
1409 while (indexb < eIndexb) {
1410 const Int_t icola = (eIndexa > 0) ? pColIndexa[eIndexa-1] : -1;
1411 if (pColIndexb[indexb++] > icola)
1412 pColIndexc[nc++] = pColIndexb[indexb-1];
1414 pRowIndexc[irowc+1] = nc;
1424 template<
class Element>
1433 Error(
"SetSparseIndexAB",
"source matrices not compatible");
1437 if (this->GetNrows() != a.
GetNrows() || this->GetNcols() != a.
GetNcols() ||
1439 Error(
"SetSparseIndexAB",
"matrix not compatible with source matrices");
1451 for (
Int_t irowc = 0; irowc < this->GetNrows(); irowc++) {
1452 const Int_t sIndexb = pRowIndexb[irowc];
1453 const Int_t eIndexb = pRowIndexb[irowc+1];
1454 const Int_t off = irowc*this->GetNcols();
1455 Int_t indexb = sIndexb;
1456 for (
Int_t icolc = 0; icolc < this->GetNcols(); icolc++) {
1457 if (pDataa[off+icolc] != 0.0 || pColIndexb[indexb] > icolc)
continue;
1458 for (; indexb < eIndexb; indexb++) {
1459 if (pColIndexb[indexb] >= icolc) {
1460 if (pColIndexb[indexb] == icolc) {
1471 if (this->NonZeros() != nc)
1474 Int_t *
const pRowIndexc = this->GetRowIndexArray();
1475 Int_t *
const pColIndexc = this->GetColIndexArray();
1479 for (
Int_t irowc = 0; irowc < this->GetNrows(); irowc++) {
1480 const Int_t sIndexb = pRowIndexb[irowc];
1481 const Int_t eIndexb = pRowIndexb[irowc+1];
1482 const Int_t off = irowc*this->GetNcols();
1483 Int_t indexb = sIndexb;
1484 for (
Int_t icolc = 0; icolc < this->GetNcols(); icolc++) {
1485 if (pDataa[off+icolc] != 0.0)
1486 pColIndexc[nc++] = icolc;
1487 else if (pColIndexb[indexb] <= icolc) {
1488 for (; indexb < eIndexb; indexb++) {
1489 if (pColIndexb[indexb] >= icolc) {
1490 if (pColIndexb[indexb] == icolc)
1491 pColIndexc[nc++] = pColIndexb[indexb++];
1497 pRowIndexc[irowc+1] = nc;
1509 template<
class Element>
1513 if (!this->fIsOwner) {
1514 Error(
"ResizeTo(Int_t,Int_t,Int_t)",
"Not owner of data array,cannot resize");
1518 if (this->fNelems > 0) {
1519 if (this->fNrows == nrows && this->fNcols == ncols &&
1520 (this->fNelems == nr_nonzeros || nr_nonzeros < 0))
1522 else if (nrows == 0 || ncols == 0 || nr_nonzeros == 0) {
1523 this->fNrows = nrows; this->fNcols = ncols;
1528 const Element *elements_old = GetMatrixArray();
1529 const Int_t *rowIndex_old = GetRowIndexArray();
1530 const Int_t *colIndex_old = GetColIndexArray();
1532 Int_t nrows_old = this->fNrows;
1533 Int_t nrowIndex_old = this->fNrowIndex;
1536 if (nr_nonzeros > 0)
1537 nelems_new = nr_nonzeros;
1540 for (
Int_t irow = 0; irow < nrows_old; irow++) {
1541 if (irow >= nrows)
continue;
1542 const Int_t sIndex = rowIndex_old[irow];
1543 const Int_t eIndex = rowIndex_old[irow+1];
1544 for (
Int_t index = sIndex; index < eIndex; index++) {
1545 const Int_t icol = colIndex_old[index];
1552 Allocate(nrows,ncols,0,0,1,nelems_new);
1555 Element *elements_new = GetMatrixArray();
1556 Int_t *rowIndex_new = GetRowIndexArray();
1557 Int_t *colIndex_new = GetColIndexArray();
1559 Int_t nelems_copy = 0;
1560 rowIndex_new[0] = 0;
1562 for (
Int_t irow = 0; irow < nrows_old && cont; irow++) {
1563 if (irow >= nrows)
continue;
1564 const Int_t sIndex = rowIndex_old[irow];
1565 const Int_t eIndex = rowIndex_old[irow+1];
1566 for (
Int_t index = sIndex; index < eIndex; index++) {
1567 const Int_t icol = colIndex_old[index];
1569 rowIndex_new[irow+1] = nelems_copy+1;
1570 colIndex_new[nelems_copy] = icol;
1571 elements_new[nelems_copy] = elements_old[index];
1574 if (nelems_copy >= nelems_new) {
1581 if (rowIndex_old)
delete [] (
Int_t*) rowIndex_old;
1582 if (colIndex_old)
delete [] (
Int_t*) colIndex_old;
1583 if (elements_old)
delete [] (Element*) elements_old;
1585 if (nrowIndex_old < this->fNrowIndex) {
1586 for (
Int_t irow = nrowIndex_old; irow < this->fNrowIndex; irow++)
1587 rowIndex_new[irow] = rowIndex_new[nrowIndex_old-1];
1590 const Int_t nelems_new = (nr_nonzeros >= 0) ? nr_nonzeros : 0;
1591 Allocate(nrows,ncols,0,0,1,nelems_new);
1603 template<
class Element>
1608 if (!this->fIsOwner) {
1609 Error(
"ResizeTo(Int_t,Int_t,Int_t,Int_t,Int_t)",
"Not owner of data array,cannot resize");
1613 const Int_t new_nrows = row_upb-row_lwb+1;
1614 const Int_t new_ncols = col_upb-col_lwb+1;
1616 if (this->fNelems > 0) {
1617 if (this->fNrows == new_nrows && this->fNcols == new_ncols &&
1618 this->fRowLwb == row_lwb && this->fColLwb == col_lwb &&
1619 (this->fNelems == nr_nonzeros || nr_nonzeros < 0))
1621 else if (new_nrows == 0 || new_ncols == 0 || nr_nonzeros == 0) {
1622 this->fNrows = new_nrows; this->fNcols = new_ncols;
1623 this->fRowLwb = row_lwb; this->fColLwb = col_lwb;
1628 const Int_t *rowIndex_old = GetRowIndexArray();
1629 const Int_t *colIndex_old = GetColIndexArray();
1630 const Element *elements_old = GetMatrixArray();
1632 const Int_t nrowIndex_old = this->fNrowIndex;
1634 const Int_t nrows_old = this->fNrows;
1635 const Int_t rowLwb_old = this->fRowLwb;
1636 const Int_t colLwb_old = this->fColLwb;
1639 if (nr_nonzeros > 0)
1640 nelems_new = nr_nonzeros;
1643 for (
Int_t irow = 0; irow < nrows_old; irow++) {
1644 if (irow+rowLwb_old > row_upb || irow+rowLwb_old < row_lwb)
continue;
1645 const Int_t sIndex = rowIndex_old[irow];
1646 const Int_t eIndex = rowIndex_old[irow+1];
1647 for (
Int_t index = sIndex; index < eIndex; index++) {
1648 const Int_t icol = colIndex_old[index];
1649 if (icol+colLwb_old <= col_upb && icol+colLwb_old >= col_lwb)
1655 Allocate(new_nrows,new_ncols,row_lwb,col_lwb,1,nelems_new);
1658 Int_t *rowIndex_new = GetRowIndexArray();
1659 Int_t *colIndex_new = GetColIndexArray();
1660 Element *elements_new = GetMatrixArray();
1662 Int_t nelems_copy = 0;
1663 rowIndex_new[0] = 0;
1664 const Int_t row_off = rowLwb_old-row_lwb;
1665 const Int_t col_off = colLwb_old-col_lwb;
1666 for (
Int_t irow = 0; irow < nrows_old; irow++) {
1667 if (irow+rowLwb_old > row_upb || irow+rowLwb_old < row_lwb)
continue;
1668 const Int_t sIndex = rowIndex_old[irow];
1669 const Int_t eIndex = rowIndex_old[irow+1];
1670 for (
Int_t index = sIndex; index < eIndex; index++) {
1671 const Int_t icol = colIndex_old[index];
1672 if (icol+colLwb_old <= col_upb && icol+colLwb_old >= col_lwb) {
1673 rowIndex_new[irow+row_off+1] = nelems_copy+1;
1674 colIndex_new[nelems_copy] = icol+col_off;
1675 elements_new[nelems_copy] = elements_old[index];
1678 if (nelems_copy >= nelems_new) {
1684 if (rowIndex_old)
delete [] (
Int_t*) rowIndex_old;
1685 if (colIndex_old)
delete [] (
Int_t*) colIndex_old;
1686 if (elements_old)
delete [] (Element*) elements_old;
1688 if (nrowIndex_old < this->fNrowIndex) {
1689 for (
Int_t irow = nrowIndex_old; irow < this->fNrowIndex; irow++)
1690 rowIndex_new[irow] = rowIndex_new[nrowIndex_old-1];
1693 const Int_t nelems_new = (nr_nonzeros >= 0) ? nr_nonzeros : 0;
1694 Allocate(new_nrows,new_ncols,row_lwb,col_lwb,1,nelems_new);
1702 template<
class Element>
1707 if (row_upb < row_lwb) {
1708 Error(
"Use",
"row_upb=%d < row_lwb=%d",row_upb,row_lwb);
1711 if (col_upb < col_lwb) {
1712 Error(
"Use",
"col_upb=%d < col_lwb=%d",col_upb,col_lwb);
1719 this->fNrows = row_upb-row_lwb+1;
1720 this->fNcols = col_upb-col_lwb+1;
1721 this->fRowLwb = row_lwb;
1722 this->fColLwb = col_lwb;
1723 this->fNrowIndex = this->fNrows+1;
1724 this->fNelems = nr_nonzeros;
1729 fRowIndex = pRowIndex;
1730 fColIndex = pColIndex;
1742 template<
class Element>
1748 if (row_lwb < this->fRowLwb || row_lwb > this->fRowLwb+this->fNrows-1) {
1749 Error(
"GetSub",
"row_lwb out-of-bounds");
1752 if (col_lwb < this->fColLwb || col_lwb > this->fColLwb+this->fNcols-1) {
1753 Error(
"GetSub",
"col_lwb out-of-bounds");
1756 if (row_upb < this->fRowLwb || row_upb > this->fRowLwb+this->fNrows-1) {
1757 Error(
"GetSub",
"row_upb out-of-bounds");
1760 if (col_upb < this->fColLwb || col_upb > this->fColLwb+this->fNcols-1) {
1761 Error(
"GetSub",
"col_upb out-of-bounds");
1764 if (row_upb < row_lwb || col_upb < col_lwb) {
1765 Error(
"GetSub",
"row_upb < row_lwb || col_upb < col_lwb");
1774 const Int_t row_lwb_sub = (shift) ? 0 : row_lwb;
1775 const Int_t row_upb_sub = (shift) ? row_upb-row_lwb : row_upb;
1776 const Int_t col_lwb_sub = (shift) ? 0 : col_lwb;
1777 const Int_t col_upb_sub = (shift) ? col_upb-col_lwb : col_upb;
1779 Int_t nr_nonzeros = 0;
1780 for (
Int_t irow = 0; irow < this->fNrows; irow++) {
1781 if (irow+this->fRowLwb > row_upb || irow+this->fRowLwb < row_lwb)
continue;
1782 const Int_t sIndex = fRowIndex[irow];
1783 const Int_t eIndex = fRowIndex[irow+1];
1784 for (
Int_t index = sIndex; index < eIndex; index++) {
1785 const Int_t icol = fColIndex[index];
1786 if (icol+this->fColLwb <= col_upb && icol+this->fColLwb >= col_lwb)
1791 target.
ResizeTo(row_lwb_sub,row_upb_sub,col_lwb_sub,col_upb_sub,nr_nonzeros);
1793 const Element *ep = this->GetMatrixArray();
1800 Int_t nelems_copy = 0;
1801 rowIndex_sub[0] = 0;
1802 const Int_t row_off = this->fRowLwb-row_lwb;
1803 const Int_t col_off = this->fColLwb-col_lwb;
1804 for (
Int_t irow = 0; irow < this->fNrows; irow++) {
1805 if (irow+this->fRowLwb > row_upb || irow+this->fRowLwb < row_lwb)
continue;
1806 const Int_t sIndex = fRowIndex[irow];
1807 const Int_t eIndex = fRowIndex[irow+1];
1808 for (
Int_t index = sIndex; index < eIndex; index++) {
1809 const Int_t icol = fColIndex[index];
1810 if (icol+this->fColLwb <= col_upb && icol+this->fColLwb >= col_lwb) {
1811 rowIndex_sub[irow+row_off+1] = nelems_copy+1;
1812 colIndex_sub[nelems_copy] = icol+col_off;
1813 ep_sub[nelems_copy] = ep[index];
1819 const Int_t row_off = this->fRowLwb-row_lwb;
1820 const Int_t col_off = this->fColLwb-col_lwb;
1821 const Int_t ncols_sub = col_upb_sub-col_lwb_sub+1;
1822 for (
Int_t irow = 0; irow < this->fNrows; irow++) {
1823 if (irow+this->fRowLwb > row_upb || irow+this->fRowLwb < row_lwb)
continue;
1824 const Int_t sIndex = fRowIndex[irow];
1825 const Int_t eIndex = fRowIndex[irow+1];
1826 const Int_t off = (irow+row_off)*ncols_sub;
1827 for (
Int_t index = sIndex; index < eIndex; index++) {
1828 const Int_t icol = fColIndex[index];
1829 if (icol+this->fColLwb <= col_upb && icol+this->fColLwb >= col_lwb)
1830 ep_sub[off+icol+col_off] = ep[index];
1842 template<
class Element>
1849 if (row_lwb < this->fRowLwb || row_lwb > this->fRowLwb+this->fNrows-1) {
1850 Error(
"SetSub",
"row_lwb out-of-bounds");
1853 if (col_lwb < this->fColLwb || col_lwb > this->fColLwb+this->fNcols-1) {
1854 Error(
"SetSub",
"col_lwb out-of-bounds");
1857 if (row_lwb+source.
GetNrows() > this->fRowLwb+this->fNrows || col_lwb+source.
GetNcols() > this->fColLwb+this->fNcols) {
1858 Error(
"SetSub",
"source matrix too large");
1868 Int_t nr_nonzeros = 0;
1869 for (
Int_t irow = 0; irow < this->fNrows; irow++) {
1870 if (irow+this->fRowLwb >= row_lwb+nRows_source || irow+this->fRowLwb < row_lwb)
continue;
1871 const Int_t sIndex = fRowIndex[irow];
1872 const Int_t eIndex = fRowIndex[irow+1];
1873 for (
Int_t index = sIndex; index < eIndex; index++) {
1874 const Int_t icol = fColIndex[index];
1875 if (icol+this->fColLwb < col_lwb+nCols_source && icol+this->fColLwb >= col_lwb)
1884 const Int_t nelems_old = this->fNelems;
1885 const Int_t *rowIndex_old = GetRowIndexArray();
1886 const Int_t *colIndex_old = GetColIndexArray();
1887 const Element *elements_old = GetMatrixArray();
1889 const Int_t nelems_new = nelems_old+source.
NonZeros()-nr_nonzeros;
1890 fRowIndex =
new Int_t[this->fNrowIndex];
1891 fColIndex =
new Int_t[nelems_new];
1892 fElements =
new Element[nelems_new];
1893 this->fNelems = nelems_new;
1895 Int_t *rowIndex_new = GetRowIndexArray();
1896 Int_t *colIndex_new = GetColIndexArray();
1897 Element *elements_new = GetMatrixArray();
1899 const Int_t row_off = row_lwb-this->fRowLwb;
1900 const Int_t col_off = col_lwb-this->fColLwb;
1903 rowIndex_new[0] = 0;
1904 for (
Int_t irow = 0; irow < this->fNrows; irow++) {
1905 rowIndex_new[irow+1] = rowIndex_new[irow];
1907 if (irow+this->fRowLwb < row_lwb+nRows_source && irow+this->fRowLwb >= row_lwb)
1910 const Int_t sIndex_o = rowIndex_old[irow];
1911 const Int_t eIndex_o = rowIndex_old[irow+1];
1914 const Int_t icol_left = col_off-1;
1916 for (
Int_t index = sIndex_o; index <= left; index++) {
1917 rowIndex_new[irow+1]++;
1918 colIndex_new[nr] = colIndex_old[index];
1919 elements_new[nr] = elements_old[index];
1923 if (rowIndex_s && colIndex_s) {
1924 const Int_t sIndex_s = rowIndex_s[irow-row_off];
1925 const Int_t eIndex_s = rowIndex_s[irow-row_off+1];
1926 for (
Int_t index = sIndex_s; index < eIndex_s; index++) {
1927 rowIndex_new[irow+1]++;
1928 colIndex_new[nr] = colIndex_s[index]+col_off;
1929 elements_new[nr] = elements_s[index];
1933 const Int_t off = (irow-row_off)*nCols_source;
1934 for (
Int_t icol = 0; icol < nCols_source; icol++) {
1935 rowIndex_new[irow+1]++;
1936 colIndex_new[nr] = icol+col_off;
1937 elements_new[nr] = elements_s[off+icol];
1942 const Int_t icol_right = col_off+nCols_source-1;
1945 while (right < eIndex_o-1 && colIndex_old[right+1] <= icol_right)
1949 for (
Int_t index = right; index < eIndex_o; index++) {
1950 rowIndex_new[irow+1]++;
1951 colIndex_new[nr] = colIndex_old[index];
1952 elements_new[nr] = elements_old[index];
1957 for (
Int_t index = sIndex_o; index < eIndex_o; index++) {
1958 rowIndex_new[irow+1]++;
1959 colIndex_new[nr] = colIndex_old[index];
1960 elements_new[nr] = elements_old[index];
1966 R__ASSERT(this->fNelems == fRowIndex[this->fNrowIndex-1]);
1968 if (rowIndex_old)
delete [] (
Int_t*) rowIndex_old;
1969 if (colIndex_old)
delete [] (
Int_t*) colIndex_old;
1970 if (elements_old)
delete [] (Element*) elements_old;
1978 template<
class Element>
1985 if (this->fNrows != source.
GetNcols() || this->fNcols != source.
GetNrows() ||
1987 Error(
"Transpose",
"matrix has wrong shape");
2003 Element *pData_t =
new Element[nr_nonzeros];
2006 for (
Int_t irow_s = 0; irow_s < source.
GetNrows(); irow_s++) {
2007 const Int_t sIndex = pRowIndex_s[irow_s];
2008 const Int_t eIndex = pRowIndex_s[irow_s+1];
2009 for (
Int_t index = sIndex; index < eIndex; index++) {
2010 rownr[ielem] = pColIndex_s[index]+this->fRowLwb;
2011 colnr[ielem] = irow_s+this->fColLwb;
2012 pData_t[ielem] = pData_s[index];
2020 SetMatrixArray(nr_nonzeros,rownr,colnr,pData_t);
2022 R__ASSERT(this->fNelems == fRowIndex[this->fNrowIndex-1]);
2024 if (pData_t)
delete [] pData_t;
2025 if (rownr)
delete [] rownr;
2026 if (colnr)
delete [] colnr;
2033 template<
class Element>
2038 if (fElements) {
delete [] fElements; fElements = 0; }
2039 if (fColIndex) {
delete [] fColIndex; fColIndex = 0; }
2041 memset(this->GetRowIndexArray(),0,this->fNrowIndex*
sizeof(
Int_t));
2049 template<
class Element>
2056 Int_t nr_nonzeros = 0;
2057 for (i = this->fRowLwb; i <= this->fRowLwb+this->fNrows-1; i++)
2058 for (
Int_t j = this->fColLwb; j <= this->fColLwb+this->fNcols-1; j++)
2059 if (i == j) nr_nonzeros++;
2061 if (nr_nonzeros != this->fNelems) {
2062 this->fNelems = nr_nonzeros;
2063 Int_t *oIp = fColIndex;
2064 fColIndex =
new Int_t[nr_nonzeros];
2065 if (oIp)
delete [] oIp;
2066 Element *oDp = fElements;
2067 fElements =
new Element[nr_nonzeros];
2068 if (oDp)
delete [] oDp;
2073 for (i = this->fRowLwb; i <= this->fRowLwb+this->fNrows-1; i++) {
2074 for (
Int_t j = this->fColLwb; j <= this->fColLwb+this->fNcols-1; j++) {
2076 const Int_t irow = i-this->fRowLwb;
2077 fRowIndex[irow+1] = ielem+1;
2078 fElements[ielem] = 1.0;
2079 fColIndex[ielem++] = j-this->fColLwb;
2091 template<
class Element>
2096 const Element * ep = GetMatrixArray();
2097 const Element *
const fp = ep+this->fNelems;
2098 const Int_t *
const pR = GetRowIndexArray();
2102 for (
Int_t irow = 0; irow < this->fNrows; irow++) {
2103 const Int_t sIndex = pR[irow];
2104 const Int_t eIndex = pR[irow+1];
2106 for (
Int_t index = sIndex; index < eIndex; index++)
2120 template<
class Element>
2128 const Element *
const fp = ep+this->fNcols;
2135 for (
Int_t i = 0; i < this->fNrows; i++,ep += this->fNcols)
2137 ep -= this->fNelems-1;
2148 template<
class Element>
2153 const Int_t arown = rown-this->fRowLwb;
2154 const Int_t acoln = coln-this->fColLwb;
2155 if (arown >= this->fNrows || arown < 0) {
2156 Error(
"operator()",
"Request row(%d) outside matrix range of %d - %d",rown,this->fRowLwb,this->fRowLwb+this->fNrows);
2157 return fElements[0];
2159 if (acoln >= this->fNcols || acoln < 0) {
2160 Error(
"operator()",
"Request column(%d) outside matrix range of %d - %d",coln,this->fColLwb,this->fColLwb+this->fNcols);
2161 return fElements[0];
2167 if (this->fNrowIndex > 0 && fRowIndex[this->fNrowIndex-1] != 0) {
2168 sIndex = fRowIndex[arown];
2169 eIndex = fRowIndex[arown+1];
2173 if (index >= sIndex && fColIndex[index] == acoln)
2174 return fElements[index];
2177 InsertRow(rown,coln,&val,1);
2178 sIndex = fRowIndex[arown];
2179 eIndex = fRowIndex[arown+1];
2181 if (index >= sIndex && fColIndex[index] == acoln)
2182 return fElements[index];
2184 Error(
"operator()(Int_t,Int_t",
"Insert row failed");
2185 return fElements[0];
2192 template <
class Element>
2196 if (this->fNrowIndex > 0 && this->fRowIndex[this->fNrowIndex-1] == 0) {
2197 Error(
"operator()(Int_t,Int_t) const",
"row/col indices are not set");
2198 Info(
"operator()",
"fNrowIndex = %d fRowIndex[fNrowIndex-1] = %d\n",this->fNrowIndex,this->fRowIndex[this->fNrowIndex-1]);
2201 const Int_t arown = rown-this->fRowLwb;
2202 const Int_t acoln = coln-this->fColLwb;
2204 if (arown >= this->fNrows || arown < 0) {
2205 Error(
"operator()",
"Request row(%d) outside matrix range of %d - %d",rown,this->fRowLwb,this->fRowLwb+this->fNrows);
2208 if (acoln >= this->fNcols || acoln < 0) {
2209 Error(
"operator()",
"Request column(%d) outside matrix range of %d - %d",coln,this->fColLwb,this->fColLwb+this->fNcols);
2213 const Int_t sIndex = fRowIndex[arown];
2214 const Int_t eIndex = fRowIndex[arown+1];
2216 if (index < sIndex || fColIndex[index] != acoln)
return 0.0;
2217 else return fElements[index];
2224 template<
class Element>
2228 Error(
"operator=(const TMatrixTSparse &)",
"matrices not compatible");
2236 Element *
const tp = this->GetMatrixArray();
2237 memcpy(tp,sp,this->fNelems*
sizeof(Element));
2238 this->fTol = source.
GetTol();
2247 template<
class Element>
2251 Error(
"operator=(const TMatrixT &)",
"matrices not compatible");
2259 Element *
const tp = this->GetMatrixArray();
2260 for (
Int_t irow = 0; irow < this->fNrows; irow++) {
2261 const Int_t sIndex = fRowIndex[irow];
2262 const Int_t eIndex = fRowIndex[irow+1];
2263 const Int_t off = irow*this->fNcols;
2264 for (
Int_t index = sIndex; index < eIndex; index++) {
2265 const Int_t icol = fColIndex[index];
2266 tp[index] = sp[off+icol];
2269 this->fTol = source.
GetTol();
2278 template<
class Element>
2283 if (fRowIndex[this->fNrowIndex-1] == 0) {
2284 Error(
"operator=(Element",
"row/col indices are not set");
2288 Element *ep = this->GetMatrixArray();
2289 const Element *
const ep_last = ep+this->fNelems;
2290 while (ep < ep_last)
2299 template<
class Element>
2304 Element *ep = this->GetMatrixArray();
2305 const Element *
const ep_last = ep+this->fNelems;
2306 while (ep < ep_last)
2315 template<
class Element>
2320 Element *ep = this->GetMatrixArray();
2321 const Element *
const ep_last = ep+this->fNelems;
2322 while (ep < ep_last)
2331 template<
class Element>
2336 Element *ep = this->GetMatrixArray();
2337 const Element *
const ep_last = ep+this->fNelems;
2338 while (ep < ep_last)
2347 template<
class Element>
2352 const Element scale = beta-alpha;
2353 const Element shift = alpha/scale;
2355 Int_t *
const pRowIndex = GetRowIndexArray();
2356 Int_t *
const pColIndex = GetColIndexArray();
2357 Element *
const ep = GetMatrixArray();
2359 const Int_t m = this->GetNrows();
2360 const Int_t n = this->GetNcols();
2363 const Int_t nn = this->GetNrows()*this->GetNcols();
2364 const Int_t length = (this->GetNoElements() <= nn) ? this->GetNoElements() : nn;
2368 for (
Int_t k = 0; k < nn; k++) {
2369 const Element
r =
Drand(seed);
2371 if ((nn-k)*r < length-chosen) {
2372 pColIndex[chosen] = k%
n;
2375 if (irow > icurrent) {
2376 for ( ; icurrent < irow; icurrent++)
2377 pRowIndex[icurrent+1] = chosen;
2379 ep[chosen] = scale*(
Drand(seed)+shift);
2383 for ( ; icurrent <
m; icurrent++)
2384 pRowIndex[icurrent+1] = length;
2394 template<
class Element>
2397 const Element scale = beta-alpha;
2398 const Element shift = alpha/scale;
2403 if (this->fNrows != this->fNcols || this->fRowLwb != this->fColLwb) {
2404 Error(
"RandomizePD(Element &",
"matrix should be square");
2409 const Int_t n = this->fNcols;
2411 Int_t *
const pRowIndex = this->GetRowIndexArray();
2412 Int_t *
const pColIndex = this->GetColIndexArray();
2413 Element *
const ep = GetMatrixArray();
2420 ep[0] = 1e-8+scale*(
Drand(seed)+shift);
2424 const Int_t nn = n*(n-1)/2;
2430 length = (length <= nn) ? length : nn;
2440 for (
Int_t k = 0; k < nn; k++ ) {
2441 const Element
r =
Drand(seed);
2443 if( (nn-k)*r < length-chosen) {
2450 Int_t col = k-row*(row+1)/2;
2455 if (row > icurrent) {
2459 for ( ; icurrent < row; icurrent++) {
2462 for (
Int_t ll = pRowIndex[icurrent]; ll < nnz; ll++)
2464 ep[nnz] += 1e-8+scale*(
Drand(seed)+shift);
2465 pColIndex[nnz] = icurrent;
2468 pRowIndex[icurrent+1] = nnz;
2471 ep[nnz] = scale*(
Drand(seed)+shift);
2472 pColIndex[nnz] = col;
2475 ep[pRowIndex[col+1]-1] +=
TMath::Abs(ep[nnz]);
2485 for ( ; icurrent <
n; icurrent++) {
2488 for(
Int_t ll = pRowIndex[icurrent]; ll < nnz; ll++)
2490 ep[nnz] += 1e-8+scale*(
Drand(seed)+shift);
2491 pColIndex[nnz] = icurrent;
2494 pRowIndex[icurrent+1] = nnz;
2496 this->fNelems = nnz;
2504 const Int_t *
const pR = this->GetRowIndexArray();
2505 const Int_t *
const pC = this->GetColIndexArray();
2506 Element *
const pD = GetMatrixArray();
2507 for (
Int_t irow = 0; irow < this->fNrows+1; irow++) {
2508 const Int_t sIndex = pR[irow];
2509 const Int_t eIndex = pR[irow+1];
2510 for (
Int_t index = sIndex; index < eIndex; index++) {
2511 const Int_t icol = pC[index];
2523 template<
class Element>
2532 template<
class Element>
2541 template<
class Element>
2550 template<
class Element>
2560 template<
class Element>
2570 template<
class Element>
2579 template<
class Element>
2588 template<
class Element>
2597 template<
class Element>
2607 template<
class Element>
2617 template<
class Element>
2626 template<
class Element>
2635 template<
class Element>
2644 template<
class Element>
2654 template<
class Element>
2665 template<
class Element>
2668 target += scalar * source;
2675 template<
class Element>
2679 ::Error(
"ElementMult(TMatrixTSparse &,const TMatrixTSparse &)",
"matrices not compatible");
2695 template<
class Element>
2699 ::Error(
"ElementDiv(TMatrixT &,const TMatrixT &)",
"matrices not compatible");
2706 while ( tp < ftp ) {
2710 Error(
"ElementDiv",
"source element is zero");
2720 template<
class Element>
2725 ::Error(
"AreCompatible",
"matrix 1 not valid");
2730 ::Error(
"AreCompatible",
"matrix 2 not valid");
2737 ::Error(
"AreCompatible",
"matrices 1 and 2 not compatible");
2744 if (memcmp(pR1,pR2,(nRows+1)*
sizeof(
Int_t))) {
2746 ::Error(
"AreCompatible",
"matrices 1 and 2 have different rowIndex");
2747 for (
Int_t i = 0; i < nRows+1; i++)
2748 printf(
"%d: %d %d\n",i,pR1[i],pR2[i]);
2754 if (memcmp(pD1,pD2,nData*
sizeof(
Int_t))) {
2756 ::Error(
"AreCompatible",
"matrices 1 and 2 have different colIndex");
2757 for (
Int_t i = 0; i < nData; i++)
2758 printf(
"%d: %d %d\n",i,pD1[i],pD2[i]);
2768 template<
class Element>
2776 if (this->fNelems < 0)
2785 #ifndef ROOT_TMatrixFSparsefwd
2788 #ifndef ROOT_TMatrixFfwd
2814 #ifndef ROOT_TMatrixDSparsefwd
2817 #ifndef ROOT_TMatrixDfwd
2821 template class TMatrixTSparse<Double_t>;
void AMinusB(const TMatrixTSparse< Element > &a, const TMatrixTSparse< Element > &b, Int_t constr=0)
General matrix subtraction.
virtual const Element * GetMatrixArray() const =0
virtual TMatrixTBase< Element > & InsertRow(Int_t row, Int_t col, const Element *v, Int_t n=-1)
Insert in row rown, n elements of array v at column coln.
virtual void GetMatrix2Array(Element *data, Option_t *="") const
Copy matrix data to array . It is assumed that array is of size >= fNelems.
virtual Int_t WriteClassBuffer(const TClass *cl, void *pointer)=0
Double_t Floor(Double_t x)
Small helper to encapsulate whether to return the value pointed to by the iterator or its address...
Long64_t LocMax(Long64_t n, const T *a)
TMatrixTSparse< Element > & Transpose(const TMatrixTSparse< Element > &source)
Transpose a matrix.
virtual Int_t NonZeros() const
Compute the number of elements != 0.0.
virtual const Element * GetMatrixArray() const
virtual TMatrixTBase< Element > & UnitMatrix()
Make a unit matrix (matrix need not be a square one).
template TMatrixDSparse & Add< Double_t >(TMatrixDSparse &target, Double_t scalar, const TMatrixDSparse &source)
void ToUpper()
Change string to upper case.
Buffer base class used for serializing objects.
template TMatrixDSparse & ElementMult< Double_t >(TMatrixDSparse &target, const TMatrixDSparse &source)
Int_t GetNoElements() const
Expr< TransposeOp< SMatrix< T, D1, D2, R >, T, D1, D2 >, T, D2, D1, typename TranspPolicy< T, D1, D2, R >::RepType > Transpose(const SMatrix< T, D1, D2, R > &rhs)
Matrix Transpose B(i,j) = A(j,i) returning a matrix expression.
template TMatrixFSparse & ElementDiv< Float_t >(TMatrixFSparse &target, const TMatrixFSparse &source)
virtual TMatrixTBase< Element > & SetMatrixArray(const Element *data, Option_t *="")
Copy array data to matrix .
Short_t Min(Short_t a, Short_t b)
virtual Int_t NonZeros() const
Compute the number of elements != 0.0.
virtual TMatrixTBase< Element > & ResizeTo(Int_t nrows, Int_t ncols, Int_t nr_nonzeros=-1)=0
template TMatrixFSparse & Add< Float_t >(TMatrixFSparse &target, Float_t scalar, const TMatrixFSparse &source)
virtual Element RowNorm() const
Row matrix norm, MAX{ SUM{ |M(i,j)|, over j}, over i}.
double beta(double x, double y)
Calculates the beta function.
template TMatrixFSparse & ElementMult< Float_t >(TMatrixFSparse &target, const TMatrixFSparse &source)
template Bool_t AreCompatible< Float_t >(const TMatrixFSparse &m1, const TMatrixFSparse &m2, Int_t verbose)
void AMultB(int n, int m, int k, const double *A, const double *B, double *C)
void Info(const char *location, const char *msgfmt,...)
Bool_t AreCompatible(const TMatrixTSparse< Element > &m1, const TMatrixTSparse< Element > &m2, Int_t verbose)
virtual TMatrixTBase< Element > & Randomize(Element alpha, Element beta, Double_t &seed)
randomize matrix element values
void AMultBt(const TMatrixTSparse< Element > &a, const TMatrixTSparse< Element > &b, Int_t constr=0)
General matrix multiplication.
TMatrixTSparse< Element > & ElementMult(TMatrixTSparse< Element > &target, const TMatrixTSparse< Element > &source)
Multiply target by the source, element-by-element.
TObject & operator=(const TObject &rhs)
TObject assignment operator.
virtual TMatrixTSparse< Element > & RandomizePD(Element alpha, Element beta, Double_t &seed)
randomize matrix element values but keep matrix symmetric positive definite
TMatrixTSparse< Element > & Add(TMatrixTSparse< Element > &target, Element scalar, const TMatrixTSparse< Element > &source)
Modify addition: target += scalar * source.
void Error(const char *location, const char *msgfmt,...)
TMatrixTSparse< Element > & operator-=(Element val)
Subtract val from every element of the matrix.
virtual TMatrixTBase< Element > & Zero()
Set matrix elements to zero.
virtual const Int_t * GetRowIndexArray() const
Double_t length(const TVector2 &v)
static void DoubleLexSort(Int_t n, Int_t *first, Int_t *second, Element *data)
default kTRUE, when Use array kFALSE
virtual Element ColNorm() const
Column matrix norm, MAX{ SUM{ |M(i,j)|, over i}, over j}.
template TMatrixDSparse & ElementDiv< Double_t >(TMatrixDSparse &target, const TMatrixDSparse &source)
TMatrixTSparse< Element > & operator*=(Element val)
Multiply every element of the matrix with val.
TMatrixTSparse< Element > & SetSparseIndex(Int_t nelem_new)
Increase/decrease the number of non-zero elements to nelems_new.
TMatrixTSparse< Element > operator+(const TMatrixTSparse< Element > &source1, const TMatrixTSparse< Element > &source2)
virtual const Element * GetMatrixArray() const
R__EXTERN Int_t gMatrixCheck
void Allocate(Int_t nrows, Int_t ncols, Int_t row_lwb=0, Int_t col_lwb=0, Int_t init=0, Int_t nr_nonzeros=0)
Allocate new matrix.
virtual Int_t ReadClassBuffer(const TClass *cl, void *pointer, const TClass *onfile_class=0)=0
TMatrixTSparse< Element > & ElementDiv(TMatrixTSparse< Element > &target, const TMatrixTSparse< Element > &source)
Divide target by the source, element-by-element.
templateClassImp(TMatrixTSparse) template< class Element > TMatrixTSparse< Element >
Space is allocated for row/column indices and data, but the sparse structure information has still to...
TMatrixTSparse< Element > & Use(Int_t row_lwb, Int_t row_upb, Int_t col_lwb, Int_t col_upb, Int_t nr_nonzeros, Int_t *pRowIndex, Int_t *pColIndex, Element *pData)
Element operator()(Int_t rown, Int_t coln) const
TMatrixTSparse< Element > & SetSparseIndexAB(const TMatrixTSparse< Element > &a, const TMatrixTSparse< Element > &b)
Set the row/column indices to the "sum" of matrices a and b It is checked that enough space has been ...
ClassImp(TMCParticle) void TMCParticle printf(": p=(%7.3f,%7.3f,%9.3f) ;", fPx, fPy, fPz)
template Bool_t AreCompatible< Double_t >(const TMatrixDSparse &m1, const TMatrixDSparse &m2, Int_t verbose)
TCppObject_t Allocate(TCppType_t type)
virtual const Int_t * GetColIndexArray() const
virtual TMatrixTBase< Element > & ResizeTo(Int_t nrows, Int_t ncols, Int_t nr_nonzeros=-1)
Set size of the matrix to nrows x ncols with nr_nonzeros non-zero entries if nr_nonzeros > 0 ...
TMatrixTSparse< Element > & operator+=(Element val)
Add val to every element of the matrix.
Short_t Max(Short_t a, Short_t b)
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
void AMultBt(const Element *const ap, Int_t na, Int_t ncolsa, const Element *const bp, Int_t nb, Int_t ncolsb, Element *cp)
Elementary routine to calculate matrix multiplication A*B^T.
virtual void ExtractRow(Int_t row, Int_t col, Element *v, Int_t n=-1) const
Store in array v, n matrix elements of row rown starting at column coln.
virtual TMatrixTBase< Element > & GetSub(Int_t row_lwb, Int_t row_upb, Int_t col_lwb, Int_t col_upb, TMatrixTBase< Element > &target, Option_t *option="S") const
Get submatrix [row_lwb..row_upb][col_lwb..col_upb]; The indexing range of the returned matrix depends...
TMatrixTSparse< Element > & operator=(const TMatrixT< Element > &source)
Notice that the sparsity of the matrix is NOT changed : its fRowIndex/fColIndex are used ! ...
Double_t Sqrt(Double_t x)
Long64_t LocMin(Long64_t n, const T *a)
TMatrixTSparse< Element > operator-(const TMatrixTSparse< Element > &source1, const TMatrixTSparse< Element > &source2)
void APlusB(const TMatrixTSparse< Element > &a, const TMatrixTSparse< Element > &b, Int_t constr=0)
General matrix addition.
double norm(double *x, double *p)
Double_t Drand(Double_t &ix)
Random number generator [0....1] with seed ix.
TMatrixTSparse< Element > operator*(const TMatrixTSparse< Element > &source1, const TMatrixTSparse< Element > &source2)
Long64_t BinarySearch(Long64_t n, const T *array, T value)
virtual TMatrixTBase< Element > & SetSub(Int_t row_lwb, Int_t col_lwb, const TMatrixTBase< Element > &source)
Insert matrix source starting at [row_lwb][col_lwb], thereby overwriting the part [row_lwb...
virtual const Int_t * GetColIndexArray() const =0
virtual Version_t ReadVersion(UInt_t *start=0, UInt_t *bcnt=0, const TClass *cl=0)=0
virtual const Int_t * GetRowIndexArray() const =0