13#include "CLHEP/Matrix/defs.h"
14#include "CLHEP/Random/Random.h"
15#include "CLHEP/Matrix/Matrix.h"
16#include "CLHEP/Matrix/SymMatrix.h"
17#include "CLHEP/Matrix/DiagMatrix.h"
18#include "CLHEP/Matrix/Vector.h"
19#include "CLHEP/Utility/thread_local.h"
21#ifdef HEP_DEBUG_INLINE
22#include "CLHEP/Matrix/Matrix.icc"
29#define SIMPLE_UOP(OPER) \
32 for(;a!=e; a++) (*a) OPER t;
34#define SIMPLE_BOP(OPER) \
35 HepMatrix::mIter a=m.begin(); \
36 HepMatrix::mcIter b=hm2.m.begin(); \
37 HepMatrix::mIter e=m.end(); \
38 for(;a!=e; a++, b++) (*a) OPER (*b);
40#define SIMPLE_TOP(OPER) \
41 HepMatrix::mcIter a=hm1.m.begin(); \
42 HepMatrix::mcIter b=hm2.m.begin(); \
43 HepMatrix::mIter t=mret.m.begin(); \
44 HepMatrix::mcIter e=hm1.m.end(); \
45 for(;a!=e; a++, b++, t++) (*t) = (*a) OPER (*b);
49#define CHK_DIM_2(r1,r2,c1,c2,fun) \
50 if (r1!=r2 || c1!=c2) { \
51 HepGenMatrix::error("Range error in Matrix function " #fun "(1)."); \
54#define CHK_DIM_1(c1,r2,fun) \
56 HepGenMatrix::error("Range error in Matrix function " #fun "(2)."); \
62 : m(p*q), nrow(p), ncol(q)
68 : m(p*q), nrow(p), ncol(q)
82 for(
int step=0; step < size_; step+=(ncol+1) ) *(a+step) = 1.0;
84 error(
"Invalid dimension in HepMatrix(int,int,1).");
89 error(
"Matrix: initialization must be either 0 or 1.");
95 : m(p*q), nrow(p), ncol(q)
101 for(; a<b; a++) *a = r();
110 :
HepGenMatrix(hm1), m(hm1.size_), nrow(hm1.nrow), ncol(hm1.ncol), size_(hm1.size_)
128#ifdef MATRIX_BOUND_CHECK
130 error(
"Range error in HepMatrix::operator()");
132 return *(m.begin()+(row-1)*ncol+col-1);
137#ifdef MATRIX_BOUND_CHECK
139 error(
"Range error in HepMatrix::operator()");
141 return *(m.begin()+(row-1)*ncol+col-1);
146 : m(hm1.nrow*hm1.nrow), nrow(hm1.nrow), ncol(hm1.nrow)
150 mcIter sjk = hm1.m.begin();
152 for(
int j=0; j!=nrow; ++j) {
153 for(
int k=0; k<=j; ++k) {
158 if(k!=j) m[k*nrow+j] = *sjk;
165 : m(hm1.nrow*hm1.nrow), nrow(hm1.nrow), ncol(hm1.nrow)
171 mcIter mr = hm1.m.begin();
172 for(
int r=0;r<n;r++) {
173 mrr = m.begin()+(n+1)*r;
179 : m(hm1.nrow), nrow(hm1.nrow), ncol(1)
194 int min_col,
int max_col)
const
195#ifdef HEP_GNU_OPTIMIZED_RETURN
196return mret(max_row-min_row+1,max_col-min_col+1);
200 HepMatrix mret(max_row-min_row+1,max_col-min_col+1);
202 if(max_row > num_row() || max_col >num_col())
203 error(
"HepMatrix::sub: Index out of range");
204 mIter a = mret.m.begin();
206 mcIter b1 = m.begin() + (min_row - 1) * nc + min_col - 1;
207 int rowsize = mret.num_row();
208 for(
int irow=1; irow<=rowsize; ++irow) {
210 for(
int icol=0; icol<mret.num_col(); ++icol) {
213 if(irow<rowsize) b1 += nc;
222 error(
"HepMatrix::sub: Index out of range");
225 mIter b1 = m.begin() + (row - 1) * nc + col - 1;
227 for(
int irow=1; irow<=rowsize; ++irow) {
229 for(
int icol=0; icol<hm1.
num_col(); ++icol) {
232 if(irow<rowsize) b1 += nc;
241#ifdef HEP_GNU_OPTIMIZED_RETURN
260#ifdef HEP_GNU_OPTIMIZED_RETURN
261 return hm2(nrow, ncol);
268 mIter b=hm2.m.begin();
270 for(;a<e; a++, b++) (*b) = -(*a);
277#ifdef HEP_GNU_OPTIMIZED_RETURN
278 return mret(hm1.nrow, hm1.ncol);
294#ifdef HEP_GNU_OPTIMIZED_RETURN
295 return mret(hm1.num_row(), hm1.num_col());
299 HepMatrix mret(hm1.num_row(), hm1.num_col());
302 hm1.num_col(),hm2.
num_col(),-);
314#ifdef HEP_GNU_OPTIMIZED_RETURN
326#ifdef HEP_GNU_OPTIMIZED_RETURN
338#ifdef HEP_GNU_OPTIMIZED_RETURN
350#ifdef HEP_GNU_OPTIMIZED_RETURN
351 return mret(hm1.nrow,hm2.ncol,0);
360 int m1cols = hm1.ncol;
361 int m2cols = hm2.ncol;
363 for (
int i=0; i<hm1.nrow; i++)
365 for (
int j=0; j<m1cols; j++)
367 double temp = hm1.m[i*m1cols+j];
375 (*pt) += temp * (*pb);
417 if(hm1.nrow*hm1.ncol != size_)
419 size_ = hm1.nrow * hm1.ncol;
438 if(os.flags() & std::ios::fixed)
439 width = os.precision()+3;
441 width = os.precision()+7;
442 for(
int irow = 1; irow<= q.
num_row(); irow++)
444 for(
int icol = 1; icol <= q.
num_col(); icol++)
447 os << q(irow,icol) <<
" ";
455#ifdef HEP_GNU_OPTIMIZED_RETURN
456return mret(ncol,nrow);
463 mIter pt = mret.m.begin();
464 for(
int nr=0; nr<nrow; ++nr) {
465 for(
int nc=0; nc<ncol; ++nc) {
466 pt = mret.m.begin() + nr + nrow*nc;
475#ifdef HEP_GNU_OPTIMIZED_RETURN
476return mret(num_row(),num_col());
483 mIter b = mret.m.begin();
484 for(
int ir=1;ir<=num_row();ir++) {
485 for(
int ic=1;ic<=num_col();ic++) {
486 *(b++) = (*
f)(*(a++), ir, ic);
492int HepMatrix::dfinv_matrix(
int *ir) {
494 error(
"dfinv_matrix: Matrix is not NxN");
501 mIter hm11 = m.begin();
502 mIter hm12 = hm11 + 1;
505 *hm21 = -(*hm22) * (*hm11) * (*hm21);
508 mIter mimim = hm11 +
n + 1;
509 for (
int i=3;i<=
n;i++) {
511 mIter mi = hm11 + (i-1) * n;
512 mIter mii= hm11 + (i-1) * n + i - 1;
515 mIter mji = mj + i - 1;
517 for (
int j=1;j<=ihm2;j++) {
520 mIter mkj = mj + j - 1;
521 mIter mik = mi + j - 1;
523 mIter mkpi = mj +
n + i - 1;
524 for (
int k=j;k<=ihm2;k++) {
525 s31 += (*mkj) * (*(mik++));
526 s32 += (*(mjkp++)) * (*mkpi);
530 *mij = -(*mii) * (((*(mij-
n)))*( (*(mii-1)))+(s31));
536 *(mii-1) = -(*mii) * (*mimim) * (*(mii-1));
537 *(mimim+1) = -(*(mimim+1));
543 for (
int i=1;i<
n;i++) {
547 for (j=1; j<=i;j++) {
550 mIter mikj = mi + j - 1;
551 mIter miik = mii + 1;
553 for (;miik<min_end;) {
556 s33 += (*mikj) * (*(miik++));
560 for (j=1;j<=ni;j++) {
562 mIter miik = mii + j;
563 for (
int k=j;k<=ni;k++) {
565 mIter mikij = mii + k *
n + j;
566 s34 += *mikij * (*(miik++));
574 if (nxch==0)
return 0;
575 for (
int hmm=1;hmm<=nxch;hmm++) {
576 int k = nxch - hmm + 1;
580 for (k=1; k<=
n;k++) {
582 mIter mki = hm11 + (k-1)*n + i - 1;
583 mIter mkj = hm11 + (k-1)*n + j - 1;
594int HepMatrix::dfact_matrix(
double &det,
int *ir) {
596 error(
"dfact_matrix: Matrix is not NxN");
602 double g1 = 1.0e-19, g2 = 1.0e19;
607 double epsilon = 8*DBL_EPSILON;
612 int normal = 0, imposs = -1;
613 int jrange = 0, jover = 1, junder = -1;
618 mIter mj = m.begin();
620 for (
int j=1;j<=
n;j++) {
625 for (
int i=j+1;i<
n;i++) {
626 q = (fabs(*(mj + n*(i-j) + j - 1)));
643 mIter mkl = m.begin() + (k-1)*n;
644 for (
int l=1;l<=
n;l++) {
650 ir[nxch] = (((j)<<12)+(k));
664 if (jfail == jrange) jfail = junder;
667 if (jfail==jrange) jfail = jover;
672 for (k=j+1;k<=
n;k++) {
678 mIter mik = m.begin() + k - 1;
679 mIter mijp = m.begin() + j;
682 for (
int i=1;i<j;i++) {
683 s11 += (*mik) * (*(mji++));
684 s12 += (*mijp) * (*(mki++));
689 *(mjk++) = -s11 * (*mjj);
690 *(mkjp) = -(((*(mjj+1)))*((*(mkjp-1)))+(s12));
699 if (nxch%2==1) det = -det;
700 if (jfail !=jrange) det = 0.0;
707 error(
"HepMatrix::invert: Matrix is not NxN");
709 static CLHEP_THREAD_LOCAL
int max_array = 20;
710 static CLHEP_THREAD_LOCAL
int *ir =
new int [max_array+1];
712 if (ncol > max_array) {
715 ir =
new int [max_array+1];
718 double det, temp, sd;
722 double c11,c12,c13,c21,c22,c23,c31,c32,c33;
724 c11 = (*(m.begin()+4)) * (*(m.begin()+8)) - (*(m.begin()+5)) * (*(m.begin()+7));
725 c12 = (*(m.begin()+5)) * (*(m.begin()+6)) - (*(m.begin()+3)) * (*(m.begin()+8));
726 c13 = (*(m.begin()+3)) * (*(m.begin()+7)) - (*(m.begin()+4)) * (*(m.begin()+6));
727 c21 = (*(m.begin()+7)) * (*(m.begin()+2)) - (*(m.begin()+8)) * (*(m.begin()+1));
728 c22 = (*(m.begin()+8)) * (*m.begin()) - (*(m.begin()+6)) * (*(m.begin()+2));
729 c23 = (*(m.begin()+6)) * (*(m.begin()+1)) - (*(m.begin()+7)) * (*m.begin());
730 c31 = (*(m.begin()+1)) * (*(m.begin()+5)) - (*(m.begin()+2)) * (*(m.begin()+4));
731 c32 = (*(m.begin()+2)) * (*(m.begin()+3)) - (*m.begin()) * (*(m.begin()+5));
732 c33 = (*m.begin()) * (*(m.begin()+4)) - (*(m.begin()+1)) * (*(m.begin()+3));
733 t1 = fabs(*m.begin());
734 t2 = fabs(*(m.begin()+3));
735 t3 = fabs(*(m.begin()+6));
738 temp = *(m.begin()+6);
739 det = c23*c12-c22*c13;
742 det = c22*c33-c23*c32;
744 }
else if (t3 >= t2) {
745 temp = *(m.begin()+6);
746 det = c23*c12-c22*c13;
748 temp = *(m.begin()+3);
749 det = c13*c32-c12*c33;
756 double s1 = temp/det;
757 mIter hmm = m.begin();
771 det = (*m.begin())*(*(m.begin()+3)) - (*(m.begin()+1))*(*(m.begin()+2));
777 temp = sd*(*(m.begin()+3));
778 *(m.begin()+1) *= -sd;
779 *(m.begin()+2) *= -sd;
780 *(m.begin()+3) = sd*(*m.begin());
785 if ((*(m.begin()))==0) {
789 *(m.begin()) = 1.0/(*(m.begin()));
801 ifail = dfact_matrix(det, ir);
814 static CLHEP_THREAD_LOCAL
int max_array = 20;
815 static CLHEP_THREAD_LOCAL
int *ir =
new int [max_array+1];
817 error(
"HepMatrix::determinant: Matrix is not NxN");
818 if (ncol > max_array) {
821 ir =
new int [max_array+1];
825 int i = mt.dfact_matrix(det, ir);
832 for (
mcIter d = m.begin(); d < m.end(); d += (ncol+1) )
#define CHK_DIM_2(r1, r2, c1, c2, fun)
#define CHK_DIM_1(c1, r2, fun)
std::vector< double, Alloc< double, 25 > >::const_iterator mcIter
std::vector< double, Alloc< double, 25 > >::iterator mIter
static void error(const char *s)
virtual void invertHaywood4(int &ierr)
HepMatrix sub(int min_row, int max_row, int min_col, int max_col) const
HepMatrix & operator=(const HepMatrix &)
HepMatrix apply(double(*f)(double, int, int)) const
virtual int num_col() const
virtual const double & operator()(int row, int col) const
HepMatrix & operator/=(double t)
virtual int num_row() const
HepMatrix & operator+=(const HepMatrix &)
HepMatrix operator-() const
HepMatrix & operator*=(double t)
HepMatrix & operator-=(const HepMatrix &)
double determinant() const
virtual void invertHaywood6(int &ierr)
virtual int num_size() const
virtual void invertHaywood5(int &ierr)
std::ostream & operator<<(std::ostream &s, const HepDiagMatrix &q)
HepMatrix operator+(const HepMatrix &hm1, const HepDiagMatrix &d2)
HepMatrix operator*(const HepMatrix &hm1, const HepDiagMatrix &hm2)
HepDiagMatrix dsum(const HepDiagMatrix &s1, const HepDiagMatrix &s2)