223 const int incs[] = {1,5,19,41,109,209,505,929,2161,3905,8929,16001,INT_MAX};
226 while (incs[kinc] <= n/2)
233 for( ; kinc >= 0; kinc--) {
234 const Int_t inc = incs[kinc];
236 for (
Int_t k = inc; k <
n; k++) {
237 const Element tmp = data[k];
238 const Int_t fi = first [k];
239 const Int_t se = second[k];
241 for (j = k; j >= inc; j -= inc) {
242 if ( fi < first[j-inc] || (fi == first[j-inc] && se < second[j-inc]) ) {
243 data [j] = data [j-inc];
244 first [j] = first [j-inc];
245 second[j] = second[j-inc];
259 template<
class Element>
263 const int incs[] = {1,5,19,41,109,209,505,929,2161,3905,8929,16001,INT_MAX};
266 while (incs[kinc] <= n/2)
273 for( ; kinc >= 0; kinc--) {
274 const Int_t inc = incs[kinc];
276 if ( !swapFirst && !swapSecond ) {
277 for (
Int_t k = inc; k <
n; k++) {
279 const Int_t ktemp = index[k];
280 const Int_t fi = first [ktemp];
281 const Int_t se = second[ktemp];
284 for (j = k; j >= inc; j -= inc) {
286 if (fi < first[index[j-inc]] || (fi == first[index[j-inc]] && se < second[index[j-inc]])) {
290 index[j] = index[j-inc];
301 }
else if ( swapSecond && !swapFirst ) {
302 for (
Int_t k = inc; k <
n; k++) {
303 const Int_t ktemp = index[k];
304 const Int_t fi = first [ktemp];
305 const Int_t se = second[k];
307 for (j = k; j >= inc; j -= inc) {
308 if (fi < first[index[j-inc]] || (fi == first[index[j-inc]] && se < second[j-inc])) {
309 index [j] = index[j-inc];
310 second[j] = second[j-inc];
318 }
else if (swapFirst && !swapSecond) {
319 for (
Int_t k = inc; k <
n; k++ ) {
320 const Int_t ktemp = index[k];
321 const Int_t fi = first[k];
322 const Int_t se = second[ktemp];
324 for (j = k; j >= inc; j -= inc) {
325 if ( fi < first[j-inc] || (fi == first[j-inc] && se < second[ index[j-inc]])) {
326 index[j] = index[j-inc];
327 first[j] = first[j-inc];
336 for (
Int_t k = inc; k <
n; k++ ) {
337 const Int_t ktemp = index[k];
338 const Int_t fi = first [k];
339 const Int_t se = second[k];
341 for (j = k; j >= inc; j -= inc) {
342 if ( fi < first[j-inc] || (fi == first[j-inc] && se < second[j-inc])) {
343 index [j] = index [j-inc];
344 first [j] = first [j-inc];
345 second[j] = second[j-inc];
366 template<
class Element>
374 Element *elem = GetMatrixArray();
376 for (
Int_t irow = 0; irow < fNrows; irow++) {
377 const Int_t off1 = irow*fNcols;
379 for (
Int_t icol = 0; icol < fNcols; icol++) {
380 elem[off1+icol] = data[off2+irow];
386 memcpy(elem,data,fNelems*
sizeof(Element));
394 template<
class Element>
399 if ((fNrows != fNcols) || (fRowLwb != fColLwb))
402 const Element *
const elem = GetMatrixArray();
403 for (
Int_t irow = 0; irow < fNrows; irow++) {
404 const Int_t rowOff = irow*fNcols;
406 for (
Int_t icol = 0; icol < irow; icol++) {
407 if (elem[rowOff+icol] != elem[colOff+irow])
423 template<
class Element>
431 const Element *
const elem = GetMatrixArray();
433 for (
Int_t irow = 0; irow < fNrows; irow++) {
434 const Int_t off1 = irow*fNcols;
436 for (
Int_t icol = 0; icol < fNcols; icol++) {
437 data[off2+irow] = elem[off1+icol];
443 memcpy(data,elem,fNelems*
sizeof(Element));
449 template<
class Element>
452 const Int_t arown = rown-fRowLwb;
453 const Int_t acoln = coln-fColLwb;
454 const Int_t nr = (n > 0) ? n : fNcols;
457 if (arown >= fNrows || arown < 0) {
458 Error(
"InsertRow",
"row %d out of matrix range",rown);
462 if (acoln >= fNcols || acoln < 0) {
463 Error(
"InsertRow",
"column %d out of matrix range",coln);
467 if (acoln+nr > fNcols || nr < 0) {
468 Error(
"InsertRow",
"row length %d out of range",nr);
473 const Int_t off = arown*fNcols+acoln;
474 Element *
const elem = GetMatrixArray()+off;
475 memcpy(elem,v,nr*
sizeof(Element));
483 template<
class Element>
486 const Int_t arown = rown-fRowLwb;
487 const Int_t acoln = coln-fColLwb;
488 const Int_t nr = (n > 0) ? n : fNcols;
491 if (arown >= fNrows || arown < 0) {
492 Error(
"ExtractRow",
"row %d out of matrix range",rown);
496 if (acoln >= fNcols || acoln < 0) {
497 Error(
"ExtractRow",
"column %d out of matrix range",coln);
501 if (acoln+n >= fNcols || nr < 0) {
502 Error(
"ExtractRow",
"row length %d out of range",nr);
507 const Int_t off = arown*fNcols+acoln;
508 const Element *
const elem = GetMatrixArray()+off;
509 memcpy(v,elem,nr*
sizeof(Element));
517 template<
class Element>
520 fRowLwb += row_shift;
521 fColLwb += col_shift;
529 template<
class Element>
533 memset(this->GetMatrixArray(),0,fNelems*
sizeof(Element));
541 template<
class Element>
546 Element *ep = this->GetMatrixArray();
547 const Element *
const fp = ep+fNelems;
559 template<
class Element>
564 Element *ep = this->GetMatrixArray();
565 const Element *
const fp = ep+fNelems;
577 template<
class Element>
582 Element *ep = this->GetMatrixArray();
583 const Element *
const fp = ep+fNelems;
595 template<
class Element>
600 Element *ep = this->GetMatrixArray();
601 memset(ep,0,fNelems*
sizeof(Element));
602 for (
Int_t i = fRowLwb; i <= fRowLwb+fNrows-1; i++)
603 for (
Int_t j = fColLwb; j <= fColLwb+fNcols-1; j++)
604 *ep++ = (i==j ? 1.0 : 0.0);
614 template<
class Element>
623 Error(
"NormByDiag",
"vector shorter than matrix diagonal");
633 Element *mp = this->GetMatrixArray();
636 for (
Int_t irow = 0; irow < fNrows; irow++) {
637 if (pV[irow] != 0.0) {
638 for (
Int_t icol = 0; icol < fNcols; icol++) {
639 if (pV[icol] != 0.0) {
643 Error(
"NormbyDiag",
"vector element %d is zero",icol);
648 Error(
"NormbyDiag",
"vector element %d is zero",irow);
653 for (
Int_t irow = 0; irow < fNrows; irow++) {
654 for (
Int_t icol = 0; icol < fNcols; icol++) {
668 template<
class Element>
673 const Element * ep = GetMatrixArray();
674 const Element *
const fp = ep+fNelems;
681 for (
Int_t j = 0; j < fNcols; j++)
695 template<
class Element>
700 const Element * ep = GetMatrixArray();
701 const Element *
const fp = ep+fNcols;
708 for (
Int_t i = 0; i < fNrows; i++,ep += fNcols)
722 template<
class Element>
727 const Element * ep = GetMatrixArray();
728 const Element *
const fp = ep+fNelems;
731 for ( ; ep < fp; ep++)
732 sum += (*ep) * (*ep);
740 template<
class Element>
745 Int_t nr_nonzeros = 0;
746 const Element *ep = this->GetMatrixArray();
747 const Element *
const fp = ep+fNelems;
749 if (*ep++ != 0.0) nr_nonzeros++;
757 template<
class Element>
763 const Element *ep = this->GetMatrixArray();
764 const Element *
const fp = ep+fNelems;
774 template<
class Element>
779 const Element *
const ep = this->GetMatrixArray();
787 template<
class Element>
792 const Element *
const ep = this->GetMatrixArray();
801 template<
class Element>
804 gROOT->ProcessLine(
Form(
"THistPainter::PaintSpecialObjects((TObject*)0x%lx,\"%s\");",
814 template<
class Element>
818 Error(
"Print",
"Matrix is invalid");
823 const char *
format =
"%11.4g ";
825 const char *
f = strstr(option,
"f=");
829 snprintf(topbar,100,format,123.456789);
830 Int_t nch = strlen(topbar)+1;
831 if (nch > 18) nch = 18;
833 for (
Int_t i = 0; i < nch; i++) ftopbar[i] =
' ';
835 snprintf(ftopbar+nch/2,20-nch/2,
"%s%dd",
"%",nk);
836 Int_t nch2 = strlen(ftopbar);
837 for (
Int_t i = nch2; i < nch; i++) ftopbar[i] =
' ';
841 printf(
"\n%dx%d matrix is as follows",fNrows,fNcols);
843 Int_t cols_per_sheet = 5;
844 if (nch <= 8) cols_per_sheet =10;
845 const Int_t ncols = fNcols;
846 const Int_t nrows = fNrows;
847 const Int_t collwb = fColLwb;
848 const Int_t rowlwb = fRowLwb;
850 for (
Int_t i = 0; i < nk; i++) topbar[i] =
'-';
852 for (
Int_t sheet_counter = 1; sheet_counter <= ncols; sheet_counter += cols_per_sheet) {
854 for (
Int_t j = sheet_counter; j < sheet_counter+cols_per_sheet && j <= ncols; j++)
855 printf(ftopbar,j+collwb-1);
857 if (fNelems <= 0)
continue;
858 for (
Int_t i = 1; i <= nrows; i++) {
859 printf(
"%4d |",i+rowlwb-1);
860 for (
Int_t j = sheet_counter; j < sheet_counter+cols_per_sheet && j <= ncols; j++)
861 printf(format,(*
this)(i+rowlwb-1,j+collwb-1));
871 template<
class Element>
876 if (val == 0. && fNelems == 0)
879 const Element * ep = GetMatrixArray();
880 const Element *
const fp = ep+fNelems;
881 for (; ep < fp; ep++)
891 template<
class Element>
896 if (val == 0. && fNelems == 0)
899 const Element * ep = GetMatrixArray();
900 const Element *
const fp = ep+fNelems;
901 for (; ep < fp; ep++)
911 template<
class Element>
916 const Element * ep = GetMatrixArray();
917 const Element *
const fp = ep+fNelems;
918 for (; ep < fp; ep++)
928 template<
class Element>
933 const Element * ep = GetMatrixArray();
934 const Element *
const fp = ep+fNelems;
935 for (; ep < fp; ep++)
945 template<
class Element>
950 const Element * ep = GetMatrixArray();
951 const Element *
const fp = ep+fNelems;
952 for (; ep < fp; ep++)
962 template<
class Element>
967 const Element * ep = GetMatrixArray();
968 const Element *
const fp = ep+fNelems;
969 for (; ep < fp; ep++)
979 template<
class Element>
984 Element *ep = this->GetMatrixArray();
985 const Element *
const ep_last = ep+fNelems;
996 template<
class Element>
1001 Element *ep = this->GetMatrixArray();
1002 for (action.
fI = fRowLwb; action.
fI < fRowLwb+fNrows; action.
fI++)
1003 for (action.
fJ = fColLwb; action.
fJ < fColLwb+fNcols; action.
fJ++)
1006 R__ASSERT(ep == this->GetMatrixArray()+fNelems);
1014 template<
class Element>
1019 const Element scale = beta-alpha;
1020 const Element shift = alpha/scale;
1022 Element * ep = GetMatrixArray();
1023 const Element *
const fp = ep+fNelems;
1025 *ep++ = scale*(
Drand(seed)+shift);
1033 template<
class Element>
1044 template<
class Element>
1048 ::Error(
"E2Norm",
"matrices not compatible");
1057 for (; mp1 < fmp1; mp1++, mp2++)
1058 sum += (*mp1 - *mp2)*(*mp1 - *mp2);
1066 template<
class Element1,
class Element2>
1071 ::Error(
"AreCompatible",
"matrix 1 not valid");
1076 ::Error(
"AreCompatible",
"matrix 2 not valid");
1083 ::Error(
"AreCompatible",
"matrices 1 and 2 not compatible");
1093 template<
class Element>
1097 Error(
"Compare(const TMatrixTBase<Element> &,const TMatrixTBase<Element> &)",
"matrices are incompatible");
1101 printf(
"\n\nComparison of two TMatrices:\n");
1108 Element difmax = -1;
1112 const Element mv1 = m1(i,j);
1113 const Element mv2 = m2(i,j);
1116 if (diff > difmax) {
1127 printf(
"\nMaximal discrepancy \t\t%g", difmax);
1128 printf(
"\n occured at the point\t\t(%d,%d)",imax,jmax);
1129 const Element mv1 = m1(imax,jmax);
1130 const Element mv2 = m2(imax,jmax);
1131 printf(
"\n Matrix 1 element is \t\t%g", mv1);
1132 printf(
"\n Matrix 2 element is \t\t%g", mv2);
1133 printf(
"\n Absolute error v2[i]-v1[i]\t\t%g", mv2-mv1);
1134 printf(
"\n Relative error\t\t\t\t%g\n",
1137 printf(
"\n||Matrix 1|| \t\t\t%g", norm1);
1138 printf(
"\n||Matrix 2|| \t\t\t%g", norm2);
1139 printf(
"\n||Matrix1-Matrix2||\t\t\t\t%g", ndiff);
1140 printf(
"\n||Matrix1-Matrix2||/sqrt(||Matrix1|| ||Matrix2||)\t%g\n\n",
1147 template<
class Element>
1157 Element maxDevObs = 0;
1165 if (dev > maxDevObs) {
1177 printf(
"Largest dev for (%d,%d); dev = |%g - %g| = %g\n",imax,jmax,
m(imax,jmax),val,maxDevObs);
1178 if(maxDevObs > maxDevAllow)
1179 Error(
"VerifyElementValue",
"Deviation > %g\n",maxDevAllow);
1182 if(maxDevObs > maxDevAllow)
1190 template<
class Element>
1192 Element maxDevAllow)
1197 if (m1 == 0 && m2 == 0)
1202 Element maxDevObs = 0;
1209 const Element dev =
TMath::Abs(m1(i,j)-m2(i,j));
1210 if (dev > maxDevObs) {
1222 printf(
"Largest dev for (%d,%d); dev = |%g - %g| = %g\n",
1223 imax,jmax,m1(imax,jmax),m2(imax,jmax),maxDevObs);
1224 if (maxDevObs > maxDevAllow)
1225 Error(
"VerifyMatrixValue",
"Deviation > %g\n",maxDevAllow);
1228 if (maxDevObs > maxDevAllow)
1236 template<
class Element>
1245 Error(
"TMatrixTBase<Element>::Streamer",
"Unknown version number: %d",R__v);
1248 if (R__v < 4) MakeValid();
1255 template<
class Element>
1256 struct nan_value_t {
1257 static Element gNanValue;
1260 Double_t nan_value_t<Double_t>::gNanValue = std::numeric_limits<Double_t>::quiet_NaN();
1262 Float_t nan_value_t<Float_t>::gNanValue = std::numeric_limits<Float_t>::quiet_NaN();
1264 template<
class Element>
1267 return nan_value_t<Element>::gNanValue;
1286 template class TMatrixTBase<Double_t>;
Bool_t VerifyMatrixIdentity(const TMatrixTBase< Element > &m1, const TMatrixTBase< Element > &m2, Int_t verbose, Element maxDevAllow)
Verify that elements of the two matrices are equal within MaxDevAllow .
virtual TMatrixTBase< Element > & UnitMatrix()
Make a unit matrix (matrix need not be a square one).
virtual const Element * GetMatrixArray() const =0
virtual TMatrixTBase< Element > & Sqrt()
Take square root of all elements.
virtual TMatrixTBase< Element > & Shift(Int_t row_shift, Int_t col_shift)
Shift the row index by adding row_shift and the column index by adding col_shift, respectively...
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 Int_t WriteClassBuffer(const TClass *cl, void *pointer)=0
template Bool_t AreCompatible< Float_t, Float_t >(const TMatrixFBase &m1, const TMatrixFBase &m2, Int_t verbose)
templateClassImp(TMatrixTBase) template< class Element > void TMatrixTBase< Element >
Lexical sort on array data using indices first and second.
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)
virtual TMatrixTBase< Element > & SetMatrixArray(const Element *data, Option_t *option="")
Copy array data to matrix .
virtual TMatrixTBase< Element > & Zero()
Set matrix elements to zero.
virtual Element RowNorm() const
Row matrix norm, MAX{ SUM{ |M(i,j)|, over j}, over i}.
virtual Element E2Norm() const
Square of the Euclidian norm, SUM{ m(i,j)^2 }.
Bool_t operator<=(Element val) const
Are all matrix elements <= val?
void ToUpper()
Change string to upper case.
Buffer base class used for serializing objects.
Int_t GetNoElements() const
Short_t Min(Short_t a, Short_t b)
virtual TMatrixTBase< Element > & Sqr()
Square each element of the matrix.
void Compare(const TMatrixTBase< Element > &m1, const TMatrixTBase< Element > &m2)
Compare two matrices and print out the result of the comparison.
virtual Int_t NonZeros() const
Compute the number of elements != 0.0.
static std::string format(double x, double y, int digits, int width)
double beta(double x, double y)
Calculates the beta function.
template Double_t E2Norm< Double_t >(const TMatrixDBase &m1, const TMatrixDBase &m2)
template Bool_t VerifyMatrixValue< Float_t >(const TMatrixFBase &m, Float_t val, Int_t verbose, Float_t maxDevAllow)
virtual Bool_t IsSymmetric() const
Check whether matrix is symmetric.
Bool_t operator<(Element val) const
Are all matrix elements < val?
virtual void Operation(Element &element) const =0
Element E2Norm(const TMatrixTBase< Element > &m1, const TMatrixTBase< Element > &m2)
Square of the Euclidian norm of the difference between two matrices.
Double_t Log10(Double_t x)
virtual Element Sum() const
Compute sum of elements.
template Bool_t AreCompatible< Double_t, Float_t >(const TMatrixDBase &m1, const TMatrixFBase &m2, Int_t verbose)
void Error(const char *location, const char *msgfmt,...)
Bool_t operator==(const TMatrixTBase< Element > &m1, const TMatrixTBase< Element > &m2)
Check to see if two matrices are identical.
template Bool_t VerifyMatrixIdentity< Double_t >(const TMatrixDBase &m1, const TMatrixDBase &m2, Int_t verbose, Double_t maxDevAllow)
Element * GetMatrixArray()
virtual TMatrixTBase< Element > & Apply(const TElementActionT< Element > &action)
Apply action to each matrix element.
void Print(Option_t *name="") const
Print the matrix as a table of elements.
virtual TMatrixTBase< Element > & NormByDiag(const TVectorT< Element > &v, Option_t *option="D")
option: "D" : b(i,j) = a(i,j)/sqrt(abs*(v(i)*v(j))) (default) else : b(i,j) = a(i,j)*sqrt(abs*(v(i)*v(j))) (default)
template Bool_t AreCompatible< Double_t, Double_t >(const TMatrixDBase &m1, const TMatrixDBase &m2, Int_t verbose)
virtual TMatrixTBase< Element > & InsertRow(Int_t row, Int_t col, const Element *v, Int_t n=-1)
Copy n elements from array v to row rown starting at column coln.
virtual Element ColNorm() const
Column matrix norm, MAX{ SUM{ |M(i,j)|, over i}, over j}.
Bool_t operator==(Element val) const
Are all matrix elements equal to val?
char * Form(const char *fmt,...)
virtual TMatrixTBase< Element > & Randomize(Element alpha, Element beta, Double_t &seed)
Randomize matrix element values.
template Bool_t AreCompatible< Float_t, Double_t >(const TMatrixFBase &m1, const TMatrixDBase &m2, Int_t verbose)
virtual Element Min() const
return minimum matrix element value
virtual Int_t ReadClassBuffer(const TClass *cl, void *pointer, const TClass *onfile_class=0)=0
template Bool_t VerifyMatrixIdentity< Float_t >(const TMatrixFBase &m1, const TMatrixFBase &m2, Int_t verbose, Float_t maxDevAllowN)
template void Compare< Float_t >(const TMatrixFBase &m1, const TMatrixFBase &m2)
Bool_t operator>=(Element val) const
Are all matrix elements >= val?
virtual Element Max() const
return maximum vector element value
template void Compare< Double_t >(const TMatrixDBase &m1, const TMatrixDBase &m2)
ClassImp(TMCParticle) void TMCParticle printf(": p=(%7.3f,%7.3f,%9.3f) ;", fPx, fPy, fPz)
Bool_t AreCompatible(const TMatrixTBase< Element1 > &m1, const TMatrixTBase< Element2 > &m2, Int_t verbose)
Check that matrice sm1 and m2 areboth valid and have identical shapes .
virtual void Operation(Element &element) const =0
void Draw(Option_t *option="")
Draw this matrix The histogram is named "TMatrixT" by default and no title.
Int_t GetNoElements() const
Bool_t operator>(Element val) const
Are all matrix elements > val?
static void IndexedLexSort(Int_t n, Int_t *first, Int_t swapFirst, Int_t *second, Int_t swapSecond, Int_t *index)
Lexical sort on array data using indices first and second.
Short_t Max(Short_t a, Short_t b)
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
static Element & NaNValue()
Bool_t operator!=(Element val) const
Are all matrix elements not equal to val?
Double_t Sqrt(Double_t x)
Long64_t LocMin(Long64_t n, const T *a)
double norm(double *x, double *p)
template Bool_t VerifyMatrixValue< Double_t >(const TMatrixDBase &m, Double_t val, Int_t verbose, Double_t maxDevAllow)
Double_t Drand(Double_t &ix)
Random number generator [0....1] with seed ix.
Bool_t VerifyMatrixValue(const TMatrixTBase< Element > &m, Element val, Int_t verbose, Element maxDevAllow)
Validate that all elements of matrix have value val within maxDevAllow.
template Float_t E2Norm< Float_t >(const TMatrixFBase &m1, const TMatrixFBase &m2)
virtual Version_t ReadVersion(UInt_t *start=0, UInt_t *bcnt=0, const TClass *cl=0)=0
virtual TMatrixTBase< Element > & Abs()
Take an absolute value of a matrix, i.e. apply Abs() to each element.
virtual void GetMatrix2Array(Element *data, Option_t *option="") const
Copy matrix data to array .