14 #include <gsl/gsl_blas.h>
15 #include <gsl/gsl_linalg.h>
16 #include <gsl/gsl_eigen.h>
27 double *dest,UInt_t origCTda,UInt_t destCTda)
29 if(nrow == ncol && ncol == 0)
return;
32 if(origCTda<ncol) origCTda=ncol;
33 if(destCTda<ncol) destCTda=origCTda;
35 if(origCTda==ncol && destCTda==ncol)
36 memcpy(dest,orig,2*nrow*ncol*
sizeof(
double));
38 for(UInt_t i=0;i<nrow;i++)
39 memcpy(dest+2*i*destCTda,orig+2*i*origCTda,2*ncol*
sizeof(
double));
41 else if (!orig && !dest){
46 "Can not copy QMatrixC from origin to destination "
47 "before memory allocation!"));
87 for(UInt_t i=0;i<
vec.
Size();i++)
88 this->
operator()(i,0)=
vec[i];
116 for(UInt_t i = 0; i < orig.
GetNRow(); i++) {
117 for(UInt_t j = 0; j < orig.
GetNCol(); j++) {
132 Resize(mat->size1,mat->size2);
142 fAllocSize(2*ncol*nrow),
143 fData((double *)data),
184 "Attempt to resize a QMatrixC which does not own "
189 for(UInt_t i=0;i<
GetNRow();i++) {
190 for(UInt_t j=0;j<(UInt_t)(
fNCol);j++) {
198 gsl_matrix_complex_set_identity(
fMat);
228 gsl_matrix_complex_get_col(res.fMathVec,
fMat,n);
237 gsl_matrix_complex_get_row(res.fMathVec,
fMat,n);
248 for(UInt_t i = 0; i <
GetNCol(); i++) {
275 return (*
this)*(-1.);
305 gsl_blas_zgemm(CblasNoTrans,CblasNoTrans,uno,tmp.
fMat,other.
fMat,zero,
fMat);
322 gsl_matrix_complex_add(
fMat,other.
fMat);
330 gsl_matrix_complex_sub(
fMat,other.
fMat);
338 gsl_matrix_complex_mul_elements(
fMat,other.
fMat);
346 gsl_matrix_complex_div_elements(
fMat,other.
fMat);
407 gsl_matrix_complex_transpose(
fMat);
423 for(UInt_t i=0;i<
GetNRow();i++){
424 for(UInt_t j=0;j<
GetNCol();j++){
440 for(UInt_t i = 0; i <
GetNRow(); i++) {
441 for(UInt_t j = 0; j <
GetNCol(); j++) {
458 gsl_permutation * p = gsl_permutation_calloc(
GetNRow());
459 gsl_matrix_complex *LU = gsl_matrix_complex_calloc(
GetNRow(),
fNCol);
460 gsl_matrix_complex_memcpy(LU,
fMat);
461 int * signum = &sign;
462 err=gsl_linalg_complex_LU_decomp(LU, p, signum);
463 if(
err)std::cout<<
" ERROR: failed to decompose matrix "<<
err<<std::endl;
464 err=gsl_linalg_complex_LU_invert(LU, p,
fMat);
465 if(
err)std::cout<<
" ERROR: failed to decompose matrix "<<
err<<std::endl;
466 gsl_permutation_free(p);
467 gsl_matrix_complex_free(LU);
475 gsl_permutation * p = gsl_permutation_calloc(
GetNRow());
476 gsl_matrix_complex * tmp_ptr = gsl_matrix_complex_calloc(
GetNRow(),
fNCol);
477 int * signum = &sign;
478 gsl_matrix_complex_memcpy(tmp_ptr,
fMat);
479 err=gsl_linalg_complex_LU_decomp(tmp_ptr, p, signum);
480 if(
err)std::cout<<
" ERROR: failed to compute matrix determinant "<<
err<<std::endl;
481 gsl_complex res = gsl_linalg_complex_LU_det(tmp_ptr, *signum);
482 gsl_permutation_free(p);
483 gsl_matrix_complex_free(tmp_ptr);
490 "Can not calculate eigen values of non square matrix"
491 " using HermitianEigenValues()."));
496 gsl_eigen_herm_workspace *wSpace = gsl_eigen_herm_alloc(
GetNCol());
499 std::cout <<
" ERROR: Could not calculate eigenvalues of hermitian "
500 "matrix. " <<
err << std::endl;
501 for(UInt_t i=0;i<
GetNRow();i++){
502 for(UInt_t j=0;j<i;j++){
503 (*this)(i,j)=(*
this)(j,i).Conj();
505 (*this)(i,i)=diag[i];
507 gsl_eigen_herm_free(wSpace);
515 "Can not calculate eigensystem of non square matrix"
516 " using HermitianEigenSystem()."));
523 gsl_eigen_hermv_workspace *wSpace = gsl_eigen_hermv_alloc(
GetNCol());
526 std::cout <<
" ERROR: Could not calculate eigenvalues of hermitian "
527 "matrix. " <<
err << std::endl;
528 for(UInt_t i=0;i<
GetNRow();i++){
529 for(UInt_t j=0;j<i;j++){
530 (*this)(i,j)=(*
this)(j,i).Conj();
532 (*this)(i,i)=diag[i];
534 gsl_eigen_hermv_sort(Val.
fMathVec,Vec.
fMat,GSL_EIGEN_SORT_ABS_DESC);
535 gsl_eigen_hermv_free(wSpace);
540 "Can not Cholsky decompose non-square matrix!"));
542 int err=gsl_linalg_complex_cholesky_decomp(
fMat);
543 if(
err) std::cout <<
" ERROR: Failed to Cholesky decompose matrix! " <<
err
555 "Vector and matrix dimensions do not match "
556 "in CholeskySolveX");
561 int err = gsl_linalg_complex_cholesky_solve(
fMat,Y.fMathVec,X.fMathVec);
562 if(
err) std::cout <<
" ERROR: Failed to cholesky solve for X!" <<
err
568 fMat = (gsl_matrix_complex*) malloc(
sizeof(gsl_matrix_complex));
584 Form(
"Cannot extract a submatrix of size %ix%i starting from"
585 " element %i,%i from a matrix of size %ix%i",
587 gsl_complex *firstElem =
597 Form(
"Cannot extract a submatrix of size %ix%i starting from"
598 " element %i,%i from a matrix of size %ix%i",
600 gsl_complex *firstElem =
608 for(
size_t i=0;i<
GetNRow();i++) {
609 for(
size_t j=0;j<
GetNCol();j++) {
610 if(i>0) Ret(i,j) += Ret(i-1,j);
611 if(j>0) Ret(i,j) += Ret(i,j-1);
612 if(i>0 && j>0) Ret(i,j) -= Ret(i-1,j-1);
619 std::cout <<
"fSize " <<
fSize << std::endl;
620 std::cout <<
"fNRow " <<
GetNRow() << std::endl;
621 std::cout <<
"fNCol " <<
fNCol << std::endl;
622 std::cout <<
"fTda " <<
fTda << std::endl;
623 std::cout <<
"fData " <<
fData << std::endl;
624 std::cout <<
"fData ";
625 for(UInt_t i=0;i<TMath::Min(
GetNRow(),(UInt_t)3);i++){
626 for(UInt_t j=0;j<TMath::Min(
GetNCol(),(UInt_t)3);j++){
627 std::cout << (*this)(i,j) <<
"\t";
629 std::cout << std::endl;
631 std::cout << std::endl;
640 Diana::QMatrixC
operator*(
double t,
const Diana::QMatrixC&mat){
641 Diana::QMatrixC tmp(mat);
646 std::ostream&
operator<<(std::ostream&s,
const Diana::QMatrixC&mat){
647 for (UInt_t i=0;i<mat.GetNRow();i++){
648 for (UInt_t j=0;j<mat.GetNCol();j++) s<<mat(i,j)<<
" ";
#define Q_BEGIN_NAMESPACE
QObjectImp(Diana::QMatrixC)
std::ostream & operator<<(std::ostream &s, const Diana::QMatrixC &mat)
scalar times QMatrix
Q_END_NAMESPACE Diana::QMatrixC operator*(double t, const Diana::QMatrixC &mat)
void SetIm(const double Im)
gsl_complex Get_gsl_complex() const
void SetRe(const double Re)
double GetMagnitude() const
error class with error type and description
Interface for complex matrices in Diana analysis.
UInt_t GetNRow() const
get number rows
void SetMat() const
Fill the GSL matrix data.
const QMatrixC & operator-=(const QMatrixC &mat)
subtract a QMatrixC
const QMatrixC & operator/=(const QComplex &t)
division by scalar
QComplex GetElement(const UInt_t i, const UInt_t j)
QMatrixC GetSubMatrix(UInt_t nI, UInt_t nJ, UInt_t startI=0, UInt_t startJ=0)
Get a sub matrix of the matrix.
void Initialize(const QComplex &val)
initialize all elements (default to 0)
virtual ~QMatrixC()
destructor
const QMatrixC & operator+=(const QMatrixC &mat)
add a QMatrixC
virtual const QMatrixC & CholeskyDecompose()
Cholesky Decomposition.
QVectorC GetRow(const UInt_t nrow) const
get row
QMatrixC GetSummedAreaMatrix() const
Create a summed area matrix.
gsl_matrix_complex * fMat
QVectorC GetDiagonal() const
get matrix diagonal
void Resize(const UInt_t nrow, const UInt_t ncol)
resize a QMatrixC
UInt_t GetNCol() const
get number of columns
QVector HermitianEigenValues()
Get a vector of the eigenvalues.
QMatrix Magnitude() const
return magnitude of each element
void SetRow(const UInt_t nrow, const QVectorC &vec)
set row to specific vectors
QMatrixC GetConjugate() const
return conjugate matrix
UInt_t MemRowCElements() const
QMatrixC()
default constructor
static void MatrixCopy(double *orig, UInt_t nrow, UInt_t ncol, double *dest, UInt_t origTda=0, UInt_t destTda=0)
QMatrixC Inv() const
inverse
QMatrixC operator/(const QComplex &t) const
Division by scalar.
void Clear()
reset members to default values
const QMatrixC & operator*=(const QComplex &t)
multiplication by scalar
const QMatrixC & Mult(const QMatrixC &mat)
Multiply element by element.
QMatrixC operator*(const QMatrixC &mat) const
MatrixC product.
void SetToIdentity()
initialize to identity
QMatrixC operator+(const QMatrixC &mat) const
sum matrix
const QMatrixC & operator=(const QMatrixC &orig)
copy the content of another matrix
QVectorC GetCol(const UInt_t ncol) const
get a QMatrix from a map
QMatrixC GetH() const
compute hermitian conjugate
QMatrixC T() const
transpose
virtual void CholeskySolveX(const QVectorC &Y, QVectorC &X) const
Return the inverse, using Cholesky decomposition.
virtual QMatrixC GetCholeskyDecomposition() const
Cholesky Decomposition.
QComplex operator()(const UInt_t i, const UInt_t j)
get row matching the integer value val in column col if more than one value is found in column the fi...
const QMatrixC & H()
compute hermitian conjugate
void HermitianEigenSystem(QVector &Val, QMatrixC &Vec)
Get the eigen values and vectors.
QComplex Det() const
Determinant.
const QMatrixC & Transpose()
transpose
const QMatrixC & Div(const QMatrixC &mat)
Divide element by element.
const QMatrixC & Invert()
inverse
const QMatrixC & Conjugate()
conjugate this matrix
QMatrixC operator-() const
revert sign to alla components
void SetCol(const UInt_t ncol, const QVectorC &vec)
set column to specific vector
Interface for matrices in Diana analysis.
UInt_t GetNRow() const
get number rows
UInt_t GetNCol() const
get number of columns
virtual void Print() const
print content on screen
Interface for complex vectors in Diana analysis.
void Resize(const UInt_t newsize)
UInt_t Size() const
size of QVector
Interface for vectors in Diana analysis.
static void ArrayCopy(const double *orig, const UInt_t origStride, double *dest, const UInt_t destStride, const UInt_t size)
UInt_t Size() const
size of QVector
static void ArrayFree(double *array)
void SetMathVector() const
UInt_t fStride
data stride
static double * ArrayAlloc(const UInt_t size)
const QVector & Sort(const bool asc=true)
sort this vector
void Resize(const UInt_t newsize)
resize a QVector
gsl_vector * fMathVec
temporary gsl_vector to use the gsl library