// Write a whole matrix to a datafile (dat, tab-separated values) bool WriteMatrix(const char *strFileName, CMatrix &mat) { FILE *file; if(! mat.Defined() ) {throw ELENotDefined; } if(! (file=fopen(strFileName, "w+")) ) { // unable to open file // printf("Unable to open file: %s\n", strFileName); return false; } // if // fprintf(file, "# File %s automatically created by linalfunc\n", strFileName); // fprintf(file, "# Written by Rutger van Haasteren\n"); for(int i=0; i<mat.m_pnDimSize[0]; i++) { for(int j=0; j<mat.m_pnDimSize[1]; j++) { if(! fprintf(file, "%.30e ", double(mat[i][j]) ) ) return false; if(j!=mat.m_pnDimSize[1]-1) { if(! fprintf(file, "\t") ) return false; } } // for j fprintf(file, "\n"); } // for i if( fclose(file) ) return false; return true; } // WriteMatrix
// For debugging purposes void PrintMatrix(CMatrix &mat) { if(! mat.Defined()) {throw ELENotDefined; } putchar('\n'); for(int i=0; i<mat.m_pnDimSize[0]; i++) { for(int j=0; j<mat.m_pnDimSize[1]; j++) { printf("%e ", double(mat[i][j])); } // for j printf("\n"); } // for i } // PrintMatrix
double Max(CMatrix &mat) { double dMax; if(! mat.Defined()) {throw ELENotDefined; } dMax = mat[0]; for(int i=0; i<mat.m_pnDimSize[0]; i++) { if(mat[i] > dMax) dMax = double(mat[i]); } // for i if(mat.Allocated()) {delete &mat; } // This should call for the destructor return dMax; } // Max
double Trace(CMatrix &mat) { double dTrace=0; if(! mat.Defined()) {throw ELENotDefined; } if(mat.m_pnDimSize[0] != mat.m_pnDimSize[1]) {throw ELEDimensionMisMatch; } for(int i=0; i<mat.m_pnDimSize[0]; i++) { dTrace += double(mat[i][i]); } // for i if(mat.Allocated()) {delete &mat; } // This should call for the destructor return dTrace; } // Trace
double Prod(CMatrix &mat) { double dProd=1; if(! mat.Defined()) {throw ELENotDefined; } for(int i=0; i<mat.m_pnDimSize[0]; i++) { for(int j=0; j<mat.m_pnDimSize[1]; j++) { dProd *= double(mat[i][j]); } // for j } // for i if(mat.Allocated()) {delete &mat; } // This should call for the destructor return dProd; } // Prod
double Det(CMatrix &mat) { double dDet=1; CVector vdEigenvalues; if(! mat.Defined()) {throw ELENotDefined; } if(mat.m_pnDimSize[0] != mat.m_pnDimSize[1]) {throw ELEDimensionMisMatch; } vdEigenvalues = mat.Eigenvalues(); for(int i=0; i<vdEigenvalues.m_pnDimSize[0]; i++) { dDet *= double(vdEigenvalues[i]); } // for i if(mat.Allocated()) {delete &mat; } // This should call for the destructor return dDet; } // Det
// Log(Det(mat)) (=Trace(Log(mat)) ) <- Use Cholesky and gmp library! double LogDetCholGmp(CMatrix &mat, int nBits){ double dLogDet=0; CVector vdFact; if(! mat.Defined()) {throw ELENotDefined; } if(mat.m_pnDimSize[0] != mat.m_pnDimSize[1]) {throw ELEDimensionMisMatch; } // Use Cholesky vdFact = CholeskyGmp(mat, nBits); for(int i =0; i<vdFact.m_pnDimSize[0]; i++) { dLogDet += log(double(vdFact[i])); } // for i dLogDet *= 2; if(mat.Allocated()) {delete &mat; } // This should call for the destructor return dLogDet; } // LogDetCholGmp
/********************************* This function is doesn't work! *********************************/ double LogDet(CMatrix &mat) { double dLogDet=0; // CVector vdEigenvalues; CMatrix mdFact; if(! mat.Defined()) {throw ELENotDefined; } if(mat.m_pnDimSize[0] != mat.m_pnDimSize[1]) {throw ELEDimensionMisMatch; } // Use Cholesky mdFact = mat.LUFact(); for(int i =0; i<mdFact.m_pnDimSize[0]; i++) { if(double(mdFact[i][i]) > 0) dLogDet += log(fabs(double(mdFact[i][i]))); } // for i dLogDet *= 2; if(mat.Allocated()) {delete &mat; } // This should call for the destructor return dLogDet; } // LogDet