13 #include <gsl/gsl_blas.h>
14 #include <gsl/gsl_linalg.h>
29 const UInt_t Ncol,
double *dest,
30 UInt_t origTda,UInt_t destTda){
33 if(origTda<Ncol) origTda=Ncol;
34 if(destTda<Ncol) destTda=origTda;
36 if(origTda==Ncol && destTda==Ncol)
37 memcpy(dest,orig,Nrow*Ncol*
sizeof(
double));
39 for(UInt_t i=0;i<Nrow;i++)
40 memcpy(dest+i*destTda,orig+i*origTda,Ncol*
sizeof(
double));
42 else if (!orig && !dest){
47 "Can not copy QMatrix from origin to destination "
48 "before memory allocation!"));
110 Resize(mat->size1,mat->size2);
119 fAllocSize(nrow*ncol),
160 "Attempt to resize a QMatrix which does not own "
167 for(UInt_t i=0;i<(UInt_t)
GetNRow(); i++)
168 for(UInt_t j=0;j<(UInt_t)
GetNCol(); j++)
176 gsl_matrix_set_identity(
fMat);
207 map<int,int>::const_iterator iter;
208 int n_ele = p.size();
215 for(iter = p.begin(); iter!= p.end(); iter++) {
216 gsl_matrix_set(
fMat,i,0,iter->first);
217 gsl_matrix_set(
fMat,i,1,iter->second);
253 for(UInt_t i=0;i<
GetNCol();i++)
260 for(UInt_t i = 0; i <
GetNRow(); i++) {
261 if(lround(
operator()(i,col)) == val)
272 std::stringstream stream;
273 stream<<
"N row: "<<
GetNRow()<<
" Index: "<<i;
274 stream<<
"N col: "<<
GetNCol()<<
" Index: "<<j;
284 std::stringstream stream;
285 stream<<
"N row: "<<
GetNRow()<<
" Index: "<<i;
286 stream<<
"N col: "<<
GetNCol()<<
" Index: "<<j;
312 gsl_matrix_scale(
fMat,t);
326 gsl_blas_dgemm(CblasNoTrans,CblasNoTrans,1.0,tmp.
fMat,other.
fMat,0.0,
fMat);
355 gsl_matrix_mul_elements(
fMat,other.
fMat);
363 gsl_matrix_div_elements(
fMat,other.
fMat);
406 v = ((*this)*m).
GetCol(0);
421 "Can not transpose matrix if not data owner or square!"));
423 gsl_matrix_transpose(
fMat);
441 gsl_permutation * p = gsl_permutation_calloc(
GetNRow());
443 int * signum = &sign;
444 gsl_matrix_memcpy(LU,
fMat);
445 err=gsl_linalg_LU_decomp(LU, p, signum);
446 if(
err)cout<<
" ERROR: failed to decompose matrix "<<
err<<endl;
447 err=gsl_linalg_LU_invert(LU, p,
fMat);
448 if(
err)std::cout<<
" ERROR: failed to invert LU matrix "<<
err<<std::endl;
449 gsl_permutation_free(p);
459 gsl_permutation * p = gsl_permutation_calloc(
GetNRow());
460 gsl_matrix * tmp_ptr = gsl_matrix_calloc(
GetNRow(),
fNCol);
461 int * signum = &sign;
462 gsl_matrix_memcpy(tmp_ptr,
fMat);
463 err=gsl_linalg_LU_decomp(tmp_ptr, p, signum);
464 if(
err)cout<<
" ERROR: failed to compute matrix determinant "<<
err<<endl;
465 double res = gsl_linalg_LU_det(tmp_ptr, *signum);
466 gsl_permutation_free(p);
467 gsl_matrix_free(tmp_ptr);
472 int err=gsl_linalg_cholesky_decomp(
fMat);
473 if(
err) std::cout <<
" ERROR: Failed to Cholesky decompose the Matrix "
485 "Vector and matrix dimensions do not match "
486 "in CholeskySolveX");
492 if(
err) std::cout <<
" ERROR: Failed to cholesky solve for X!" <<
err
499 Form(
"Cannot extract a submatrix of size %ix%i starting from"
500 " element %i,%i from a matrix of size %ix%i",
511 Form(
"Cannot extract a submatrix of size %ix%i starting from"
512 " element %i,%i from a matrix of size %ix%i",
521 for(
size_t i=0;i<
GetNRow();i++) {
522 for(
size_t j=0;j<
GetNCol();j++) {
523 if(i>0) Ret(i,j) += Ret(i-1,j);
524 if(j>0) Ret(i,j) += Ret(i,j-1);
525 if(i>0 && j>0) Ret(i,j) -= Ret(i-1,j-1);
534 fMat = (gsl_matrix*) malloc(
sizeof(gsl_matrix));
550 Diana::QMatrix
operator*(
double t,
const Diana::QMatrix&mat){
551 Diana::QMatrix tmp(mat);
557 for (UInt_t i=0;i<mat.GetNRow();i++){
558 for (UInt_t j=0;j<mat.GetNCol();j++) s<<mat(i,j)<<
" ";
#define Q_BEGIN_NAMESPACE
Q_END_NAMESPACE Diana::QMatrix operator*(double t, const Diana::QMatrix &mat)
scalar times QMatrix
ostream & operator<<(ostream &s, const Diana::QMatrix &mat)
QObjectImp(Diana::QMatrix)
error class with error type and description
Interface for matrices in Diana analysis.
QMatrix GetSubMatrix(UInt_t nI, UInt_t nJ, UInt_t startI=0, UInt_t startJ=0)
Get a sub matrix of the matrix.
QVector GetDiaganol()
Get the diaganol.
void Initialize(double val=0)
initialize all elements (default to 0)
double & GetElement(UInt_t i, UInt_t j)
const QMatrix & operator+=(const QMatrix &mat)
add a QMatrix
void Set(const std::map< int, int > &p)
get a QMatrix from a map
const QMatrix & operator=(const QMatrix &orig)
copy the content of another matrix
double Det() const
Determinant.
void Resize(UInt_t nrow, UInt_t ncol)
resize a QMatrix
QVector GetCol(UInt_t ncol)
get column
const QMatrix & operator-=(const QMatrix &mat)
subtract a QMatrix
const QMatrix & Mult(const QMatrix &mat)
Multiply element by element.
void CholeskySolveX(const QVector &Y, QVector &X) const
Solve for X using Cholesky decomposition.
UInt_t MemRowElements() const
void Clear()
reset members to default values
QVector GetRowByColumnIntValue(UInt_t col, int val) const
get row matching the integer value val in column col if more than one value is found in column the fi...
const QMatrix & Div(const QMatrix &mat)
Divide element by element.
void SetCol(UInt_t ncol, const QVector &vec)
set column to specific vector
static void MatrixCopy(const double *orig, const UInt_t Nrow, const UInt_t Ncol, double *dest, UInt_t origTda=0, UInt_t destTda=0)
Static method to copy a matrix from one data location to another.
QMatrix operator-()
revert sign to all components
void SetRow(UInt_t nrow, const QVector &vec)
set row to specific vectors
QMatrix operator/(double t) const
Division by scalar.
QMatrix GetSummedAreaMatrix() const
Create a summed area matrix.
QMatrix T() const
transpose returns a different matrix transposed wrt the original
QMatrix operator+(const QMatrix &mat) const
sum matrix
Bool_t fDataOwner
This owns the memory.
QMatrix()
default constructor
UInt_t GetNRow() const
get number rows
QVector GetRow(UInt_t nrow)
get row
const QMatrix & CholeskyDecompose()
Cholesky Decomposition.
const QMatrix & Transpose()
transpose
const QMatrix & operator*=(double t)
multiplication by scalar
const QMatrix & Invert()
inverse inverts the matrix itself
const QMatrix & operator/=(double t)
division by scalar
void SetToIdentity()
initialize to identity
QMatrix operator*(const QMatrix &mat) const
Matrix product.
QMatrix GetCholeskyDecomposition() const
Cholesky Decomposition.
QMatrix Inv() const
inverse returns a different matrix inverted wrt the original
virtual ~QMatrix()
destructor
UInt_t GetNCol() const
get number of columns
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)
void Resize(const UInt_t newsize)
resize a QVector
gsl_vector * fMathVec
temporary gsl_vector to use the gsl library