25 #include <queso/GslMatrix.h>
26 #include <queso/GslVector.h>
27 #include <queso/Defines.h>
28 #include <gsl/gsl_linalg.h>
29 #include <gsl/gsl_eigen.h>
41 m_mat (gsl_matrix_calloc(map.NumGlobalElements(),nCols)),
49 m_determinant (-INFINITY),
50 m_lnDeterminant(-INFINITY),
65 m_mat (gsl_matrix_calloc(map.NumGlobalElements(),map.NumGlobalElements())),
73 m_determinant (-INFINITY),
74 m_lnDeterminant(-INFINITY),
81 for (
unsigned int i = 0; i <
m_mat->size1; ++i) {
82 (*this)(i,i) = diagValue;
91 m_mat (gsl_matrix_calloc(v.sizeLocal(),v.sizeLocal())),
99 m_determinant (-INFINITY),
100 m_lnDeterminant(-INFINITY),
101 m_permutation (NULL),
107 for (
unsigned int i = 0; i <
m_mat->size1; ++i) {
108 (*this)(i,i) = diagValue;
115 m_mat (gsl_matrix_calloc(v.sizeLocal(),v.sizeLocal())),
123 m_determinant (-INFINITY),
124 m_lnDeterminant(-INFINITY),
125 m_permutation (NULL),
132 for (
unsigned int i = 0; i <
dim; ++i) {
141 m_mat (gsl_matrix_calloc(B.numRowsLocal(),B.numCols())),
149 m_determinant (-INFINITY),
150 m_lnDeterminant(-INFINITY),
151 m_permutation (NULL),
181 iRC = gsl_matrix_scale(
m_mat,a);
222 if ((i >=
m_mat->size1) ||
223 (j >=
m_mat->size2)) {
224 std::cerr <<
"In GslMatrix::operator(i,j)"
227 <<
", m_mat->size1 = " <<
m_mat->size1
228 <<
", m_mat->size2 = " <<
m_mat->size2
233 return *gsl_matrix_ptr(
m_mat,i,j);
241 return *gsl_matrix_const_ptr(
m_mat,i,j);
250 iRC = gsl_matrix_memcpy(this->
m_mat, src.
m_mat);
260 gsl_matrix_free(
m_LU);
323 unsigned int nCols = this->
numCols();
325 for (
unsigned int i = 0; i < nRows; i++) {
326 for (
unsigned int j = 0; j < nCols; j++) {
341 unsigned int nCols = this->
numCols();
343 for (
unsigned int i = 0; i < nRows; i++) {
344 for (
unsigned int j = 0; j < nCols; j++) {
345 aux = fabs((*
this)(i,j));
346 if (aux > value) value = aux;
356 double value = -INFINITY;
359 unsigned int nCols = this->
numCols();
361 for (
unsigned int i = 0; i < nRows; i++) {
362 for (
unsigned int j = 0; j < nCols; j++) {
364 if (aux > value) value = aux;
375 unsigned int nCols = this->
numCols();
377 for (
unsigned int row = 0; row < nRows; ++row) {
378 for (
unsigned int col = 0; col < nCols; ++col) {
379 *gsl_matrix_ptr(
m_mat,row,col) = value;
388 unsigned int initialTargetRowId,
389 unsigned int initialTargetColId,
401 for (
unsigned int j = 0; j < mat.
numCols(); ++j) {
402 (*this)(initialTargetRowId+i,initialTargetColId+j) = mat(i,j);
411 unsigned int initialTargetRowId,
412 unsigned int initialTargetColId,
424 for (
unsigned int j = 0; j < mat.
numCols(); ++j) {
425 mat(i,j) = (*this)(initialTargetRowId+i,initialTargetColId+j) ;
437 gsl_error_handler_t* oldHandler;
438 oldHandler = gsl_set_error_handler_off();
439 iRC = gsl_linalg_cholesky_decomp(
m_mat);
441 std::cerr <<
"In GslMatrix::chol()"
443 <<
", gsl error message = " << gsl_strerror(iRC)
445 std::cerr <<
"Here is the offending matrix: " << std::endl;
446 std::cerr << *
this << std::endl;
448 gsl_set_error_handler(oldHandler);
453 "matrix is not positive definite",
463 unsigned int nCols = this->
numCols();
484 unsigned int nCols = this->
numCols();
495 <<
", this->numCols() = " << this->
numCols()
501 <<
"\n rhsVec.sizeLocal() = " << rhsVec.
sizeLocal()
502 <<
"\n solVec.sizeLocal() = " << solVec.
sizeLocal()
515 unsigned int nCols = this->
numCols();
526 for (
unsigned int j = 0; j < rhsMat.
numCols(); ++j) {
528 iRC = this->
svdSolve(rhsVec, solVec);
563 unsigned int nCols = this->
numCols();
577 struct timeval timevalBegin;
578 gettimeofday(&timevalBegin, NULL);
579 gsl_error_handler_t* oldHandler;
580 oldHandler = gsl_set_error_handler_off();
588 std::cerr <<
"In GslMatrix::internalSvd()"
590 <<
", gsl error message = " << gsl_strerror(iRC)
593 gsl_set_error_handler(oldHandler);
595 struct timeval timevalNow;
596 gettimeofday(&timevalNow, NULL);
604 "GslMatrix::internalSvd()",
619 unsigned int nCols = this->
numCols();
625 if (includeDiagonal) {
626 for (
unsigned int i = 0; i < nRows; i++) {
627 for (
unsigned int j = 0; j <= i; j++) {
633 for (
unsigned int i = 0; i < nRows; i++) {
634 for (
unsigned int j = 0; j < i; j++) {
647 unsigned int nCols = this->
numCols();
653 if (includeDiagonal) {
654 for (
unsigned int i = 0; i < nRows; i++) {
655 for (
unsigned int j = i; j < nCols; j++) {
661 for (
unsigned int i = 0; i < nRows; i++) {
662 for (
unsigned int j = (i+1); j < nCols; j++) {
675 unsigned int nCols = this->
numCols();
676 for (
unsigned int i = 0; i < nRows; ++i) {
677 for (
unsigned int j = 0; j < nCols; ++j) {
678 double aux = (*this)(i,j);
681 (-thresholdValue < aux)) {
685 (thresholdValue > aux)) {
698 unsigned int nCols = this->
numCols();
699 for (
unsigned int i = 0; i < nRows; ++i) {
700 for (
unsigned int j = 0; j < nCols; ++j) {
701 double aux = (*this)(i,j);
704 (-thresholdValue > aux)) {
708 (thresholdValue < aux)) {
721 unsigned int nCols = this->
numCols();
726 for (
unsigned int row = 0; row < nRows; ++row) {
727 for (
unsigned int col = 0; col < nCols; ++col) {
728 mat(row,col) = (*this)(col,row);
739 unsigned int nCols = this->
numCols();
746 unitVector.
cwSet(0.);
748 for (
unsigned int j = 0; j < nCols; ++j) {
749 if (j > 0) unitVector[j-1] = 0.;
752 for (
unsigned int i = 0; i < nRows; ++i) {
753 (*m_inverse)(i,j) = multVector[i];
765 <<
"\n M = " << *
this
767 <<
"\n M*M^{-1} = " << (*this)*(*m_inverse)
768 <<
"\n M^{-1}*M = " << (*m_inverse)*(*this)
777 unsigned int initialTargetRowId,
778 unsigned int initialTargetColId,
779 const std::vector<const GslMatrix* >& matrices,
780 bool checkForExactNumRowsMatching,
781 bool checkForExactNumColsMatching)
783 unsigned int sumNumRowsLocals = 0;
784 unsigned int sumNumCols = 0;
785 for (
unsigned int i = 0; i < matrices.size(); ++i) {
786 sumNumRowsLocals += matrices[i]->numRowsLocal();
787 sumNumCols += matrices[i]->numCols();
794 unsigned int cumulativeRowId = 0;
795 unsigned int cumulativeColId = 0;
796 for (
unsigned int i = 0; i < matrices.size(); ++i) {
797 unsigned int nRows = matrices[i]->numRowsLocal();
798 unsigned int nCols = matrices[i]->numCols();
799 for (
unsigned int rowId = 0; rowId < nRows; ++rowId) {
800 for (
unsigned int colId = 0; colId < nCols; ++colId) {
801 (*this)(initialTargetRowId + cumulativeRowId + rowId, initialTargetColId + cumulativeColId + colId) = (*(matrices[i]))(rowId,colId);
804 cumulativeRowId += nRows;
805 cumulativeColId += nCols;
813 unsigned int initialTargetRowId,
814 unsigned int initialTargetColId,
815 const std::vector<GslMatrix* >& matrices,
816 bool checkForExactNumRowsMatching,
817 bool checkForExactNumColsMatching)
819 unsigned int sumNumRowsLocals = 0;
820 unsigned int sumNumCols = 0;
821 for (
unsigned int i = 0; i < matrices.size(); ++i) {
822 sumNumRowsLocals += matrices[i]->numRowsLocal();
823 sumNumCols += matrices[i]->numCols();
830 unsigned int cumulativeRowId = 0;
831 unsigned int cumulativeColId = 0;
832 for (
unsigned int i = 0; i < matrices.size(); ++i) {
833 unsigned int nRows = matrices[i]->numRowsLocal();
834 unsigned int nCols = matrices[i]->numCols();
835 for (
unsigned int rowId = 0; rowId < nRows; ++rowId) {
836 for (
unsigned int colId = 0; colId < nCols; ++colId) {
837 (*this)(initialTargetRowId + cumulativeRowId + rowId, initialTargetColId + cumulativeColId + colId) = (*(matrices[i]))(rowId,colId);
840 cumulativeRowId += nRows;
841 cumulativeColId += nCols;
849 unsigned int initialTargetRowId,
850 unsigned int initialTargetColId,
851 const std::vector<const GslMatrix* >& matrices,
852 bool checkForExactNumRowsMatching,
853 bool checkForExactNumColsMatching)
855 unsigned int sumNumCols = 0;
856 for (
unsigned int i = 0; i < matrices.size(); ++i) {
859 sumNumCols += matrices[i]->numCols();
864 unsigned int cumulativeColId = 0;
865 for (
unsigned int i = 0; i < matrices.size(); ++i) {
866 unsigned int nRows = matrices[i]->numRowsLocal();
867 unsigned int nCols = matrices[i]->numCols();
868 for (
unsigned int rowId = 0; rowId < nRows; ++rowId) {
869 for (
unsigned int colId = 0; colId < nCols; ++colId) {
870 (*this)(initialTargetRowId + rowId, initialTargetColId + cumulativeColId + colId) = (*(matrices[i]))(rowId,colId);
873 cumulativeColId += nCols;
881 unsigned int initialTargetRowId,
882 unsigned int initialTargetColId,
883 const std::vector<GslMatrix* >& matrices,
884 bool checkForExactNumRowsMatching,
885 bool checkForExactNumColsMatching)
887 unsigned int sumNumCols = 0;
888 for (
unsigned int i = 0; i < matrices.size(); ++i) {
891 sumNumCols += matrices[i]->numCols();
896 unsigned int cumulativeColId = 0;
897 for (
unsigned int i = 0; i < matrices.size(); ++i) {
898 unsigned int nRows = matrices[i]->numRowsLocal();
899 unsigned int nCols = matrices[i]->numCols();
900 for (
unsigned int rowId = 0; rowId < nRows; ++rowId) {
901 for (
unsigned int colId = 0; colId < nCols; ++colId) {
902 (*this)(initialTargetRowId + rowId, initialTargetColId + cumulativeColId + colId) = (*(matrices[i]))(rowId,colId);
905 cumulativeColId += nCols;
913 unsigned int initialTargetRowId,
914 unsigned int initialTargetColId,
915 const std::vector<const GslMatrix* >& matrices,
916 bool checkForExactNumRowsMatching,
917 bool checkForExactNumColsMatching)
919 unsigned int sumNumRows = 0;
920 for (
unsigned int i = 0; i < matrices.size(); ++i) {
923 sumNumRows += matrices[i]->numRowsLocal();
928 unsigned int cumulativeRowId = 0;
929 for (
unsigned int i = 0; i < matrices.size(); ++i) {
930 unsigned int nRows = matrices[i]->numRowsLocal();
931 unsigned int nCols = matrices[i]->numCols();
932 for (
unsigned int rowId = 0; rowId < nRows; ++rowId) {
933 for (
unsigned int colId = 0; colId < nCols; ++colId) {
934 (*this)(initialTargetRowId + cumulativeRowId + rowId, initialTargetColId + colId) = (*(matrices[i]))(rowId,colId);
937 cumulativeRowId += nRows;
945 unsigned int initialTargetRowId,
946 unsigned int initialTargetColId,
947 const std::vector<GslMatrix* >& matrices,
948 bool checkForExactNumRowsMatching,
949 bool checkForExactNumColsMatching)
951 unsigned int sumNumRows = 0;
952 for (
unsigned int i = 0; i < matrices.size(); ++i) {
955 sumNumRows += matrices[i]->numRowsLocal();
960 unsigned int cumulativeRowId = 0;
961 for (
unsigned int i = 0; i < matrices.size(); ++i) {
962 unsigned int nRows = matrices[i]->numRowsLocal();
963 unsigned int nCols = matrices[i]->numCols();
964 for (
unsigned int rowId = 0; rowId < nRows; ++rowId) {
965 for (
unsigned int colId = 0; colId < nCols; ++colId) {
966 (*this)(initialTargetRowId + cumulativeRowId + rowId, initialTargetColId + colId) = (*(matrices[i]))(rowId,colId);
969 cumulativeRowId += nRows;
977 unsigned int initialTargetRowId,
978 unsigned int initialTargetColId,
981 bool checkForExactNumRowsMatching,
982 bool checkForExactNumColsMatching)
989 for (
unsigned int rowId1 = 0; rowId1 < mat1.
numRowsLocal(); ++rowId1) {
990 for (
unsigned int colId1 = 0; colId1 < mat1.
numCols(); ++colId1) {
991 double multiplicativeFactor = mat1(rowId1,colId1);
992 unsigned int targetRowId = rowId1 * mat2.
numRowsLocal();
993 unsigned int targetColId = colId1 * mat2.
numCols();
994 for (
unsigned int rowId2 = 0; rowId2 < mat2.
numRowsLocal(); ++rowId2) {
995 for (
unsigned int colId2 = 0; colId2 < mat2.
numCols(); ++colId2) {
996 (*this)(initialTargetRowId + targetRowId + rowId2, initialTargetColId + targetColId + colId2) = multiplicativeFactor * mat2(rowId2,colId2);
1007 unsigned int initialTargetRowId,
1008 unsigned int initialTargetColId,
1011 bool checkForExactNumRowsMatching,
1012 bool checkForExactNumColsMatching)
1019 for (
unsigned int rowId1 = 0; rowId1 < mat1.
numRowsLocal(); ++rowId1) {
1020 for (
unsigned int colId1 = 0; colId1 < mat1.
numCols(); ++colId1) {
1021 double multiplicativeFactor = mat1(rowId1,colId1);
1022 unsigned int targetRowId = rowId1 * vec2.
sizeLocal();
1023 unsigned int targetColId = colId1 * 1;
1024 for (
unsigned int rowId2 = 0; rowId2 < vec2.
sizeLocal(); ++rowId2) {
1025 for (
unsigned int colId2 = 0; colId2 < 1; ++colId2) {
1026 (*this)(initialTargetRowId + targetRowId + rowId2, initialTargetColId + targetColId + colId2) = multiplicativeFactor * vec2[rowId2];
1038 unsigned int initialTargetRowId,
1039 unsigned int initialTargetColId,
1041 bool checkForExactNumRowsMatching,
1042 bool checkForExactNumColsMatching)
1045 unsigned int nCols = mat.
numCols();
1051 for (
unsigned int row = 0; row < nRows; ++row) {
1052 for (
unsigned int col = 0; col < nCols; ++col) {
1053 (*this)(initialTargetRowId + col, initialTargetColId + row) = mat(row,col);
1069 <<
": before 'this->invertMultiply()'"
1075 <<
": after 'this->invertMultiply()'"
1081 <<
": before 'gsl_linalg_LU_det()'"
1087 <<
": after 'gsl_linalg_LU_det()'"
1093 <<
": after 'gsl_linalg_LU_lndet()'"
1110 <<
": before 'this->invertMultiply()'"
1116 <<
": after 'this->invertMultiply()'"
1122 <<
": before 'gsl_linalg_LU_det()'"
1128 <<
": after 'gsl_linalg_LU_det()'"
1134 <<
": before 'gsl_linalg_LU_lndet()'"
1150 if (relativeVec[0] > 0.) {
1151 relativeVec = (1./relativeVec[0])*relativeVec;
1154 unsigned int rankValue = 0;
1155 for (
unsigned int i = 0; i < relativeVec.
sizeLocal(); ++i) {
1156 if (( (*
m_svdSvec)[i] >= absoluteZeroThreshold ) &&
1157 ( relativeVec [i] >= relativeZeroThreshold )) {
1165 <<
", this->numCols() = " << this->
numCols()
1166 <<
", absoluteZeroThreshold = " << absoluteZeroThreshold
1167 <<
", relativeZeroThreshold = " << relativeZeroThreshold
1168 <<
", rankValue = " << rankValue
1170 <<
", relativeVec = " << relativeVec
1198 unsigned int sizeX = this->
numCols();
1200 for (
unsigned int i = 0; i < sizeY; ++i) {
1202 for (
unsigned int j = 0; j < sizeX; ++j) {
1203 value += (*this)(i,j)*x[j];
1246 std::cout <<
"In GslMatrix::invertMultiply()"
1247 <<
": before LU decomposition, m_LU = ";
1248 gsl_matrix_fprintf(stdout,
m_LU,
"%f");
1249 std::cout << std::endl;
1252 gsl_error_handler_t* oldHandler;
1253 oldHandler = gsl_set_error_handler_off();
1256 <<
": before 'gsl_linalg_LU_decomp()'"
1261 std::cerr <<
"In GslMatrix::invertMultiply()"
1262 <<
", after gsl_linalg_LU_decomp()"
1263 <<
": iRC = " << iRC
1264 <<
", gsl error message = " << gsl_strerror(iRC)
1267 gsl_set_error_handler(oldHandler);
1270 <<
": after 'gsl_linalg_LU_decomp()'"
1271 <<
", IRC = " << iRC
1277 std::cout <<
"In GslMatrix::invertMultiply()"
1278 <<
": after LU decomposition, m_LU = ";
1279 gsl_matrix_fprintf(stdout,
m_LU,
"%f");
1280 std::cout << std::endl;
1284 gsl_error_handler_t* oldHandler;
1285 oldHandler = gsl_set_error_handler_off();
1288 <<
": before 'gsl_linalg_LU_solve()'"
1294 std::cerr <<
"In GslMatrix::invertMultiply()"
1295 <<
", after gsl_linalg_LU_solve()"
1296 <<
": iRC = " << iRC
1297 <<
", gsl error message = " << gsl_strerror(iRC)
1300 gsl_set_error_handler(oldHandler);
1303 <<
": after 'gsl_linalg_LU_solve()'"
1304 <<
", IRC = " << iRC
1315 std::cout <<
"In GslMatrix::invertMultiply()"
1316 <<
": ||b - Ax||_2 = " << tmpVec.
norm2()
1317 <<
": ||b - Ax||_2/||b||_2 = " << tmpVec.
norm2()/b.
norm2()
1338 "Matrices B and X are incompatible");
1340 "Matrices B and X are incompatible");
1342 "This and X matrices are incompatible");
1348 for(
unsigned int j = 0; j < B.
numCols(); ++j )
1385 if (
m_LU == NULL ) {
1412 unsigned int n = eigenValues.
sizeLocal();
1420 if (eigenVectors == NULL) {
1421 gsl_eigen_symm_workspace* w = gsl_eigen_symm_alloc((
size_t) n);
1422 gsl_eigen_symm(
m_mat,eigenValues.
data(),w);
1423 gsl_eigen_symm_free(w);
1426 gsl_eigen_symmv_workspace* w = gsl_eigen_symmv_alloc((
size_t) n);
1428 gsl_eigen_symmv_sort(eigenValues.
data(),eigenVectors->
m_mat,GSL_EIGEN_SORT_VAL_ASC);
1429 gsl_eigen_symmv_free(w);
1440 unsigned int n = eigenVector.
sizeLocal();
1451 const unsigned int max_num_iterations = 10000;
1452 const double tolerance = 1.0e-13;
1466 for(
unsigned int k = 0;
k < max_num_iterations; ++
k )
1473 index = (w.
abs()).getMaxValueIndex();
1477 z = (1.0/lambda) * w;
1481 residual = ( (*this)*z - lambda*z ).norm2();
1483 if( residual < tolerance )
1485 eigenValue = lambda;
1508 unsigned int n = eigenVector.
sizeLocal();
1519 const unsigned int max_num_iterations = 1000;
1520 const double tolerance = 1.0e-13;
1532 double one_over_lambda;
1535 for(
unsigned int k = 0;
k < max_num_iterations; ++
k )
1537 w = (*this).invertMultiplyForceLU( z );
1542 index = (w.
abs()).getMaxValueIndex();
1545 one_over_lambda = w[index];
1547 lambda = 1.0/one_over_lambda;
1553 residual = ( (*this)*z - lambda*z ).norm2();
1555 if( residual < tolerance )
1557 eigenValue = lambda;
1584 gsl_vector* gsl_column = gsl_vector_alloc( column.
sizeLocal() );
1586 int error_code = gsl_matrix_get_col( gsl_column,
m_mat, column_num );
1590 for(
unsigned int i = 0; i < column.
sizeLocal(); ++i )
1592 column[i] = gsl_vector_get( gsl_column, i );
1596 gsl_vector_free( gsl_column );
1610 gsl_vector* gsl_row = gsl_vector_alloc( row.
sizeLocal() );
1612 int error_code = gsl_matrix_get_row( gsl_row,
m_mat, row_num );
1616 for(
unsigned int i = 0; i < row.
sizeLocal(); ++i )
1618 row[i] = gsl_vector_get( gsl_row, i );
1622 gsl_vector_free( gsl_row );
1635 this->
getRow( row_num, row );
1662 gsl_vector* gsl_row = row.
data();
1664 int error_code = gsl_matrix_set_row(
m_mat, row_num, gsl_row );
1679 gsl_vector* gsl_column = column.
data();
1681 int error_code = gsl_matrix_set_col(
m_mat, column_num, gsl_column );
1694 "GslMatrix::mpiSum()",
1695 "local and global matrices incompatible");
1699 std::vector<double> local( size, 0.0 );
1700 std::vector<double> global( size, 0.0 );
1703 for(
unsigned int i = 0; i < this->
numRowsLocal(); i++ )
1705 for(
unsigned int j = 0; j < this->
numCols(); j++ )
1709 local[
k] = (*this)(i,j);
1714 "GslMatrix::mpiSum()",
1715 "failed MPI.Allreduce()");
1717 for(
unsigned int i = 0; i < this->
numRowsLocal(); i++ )
1719 for(
unsigned int j = 0; j < this->
numCols(); j++ )
1723 M_global(i,j) = global[
k];
1746 for (
unsigned int colId = 0; colId < y1Mat.
numCols(); ++colId) {
1765 unsigned int nCols = this->
numCols();
1768 for (
unsigned int i = 0; i < nRows; ++i) {
1769 for (
unsigned int j = 0; j < nCols; ++j) {
1773 if (i != (nRows-1)) os <<
"; ";
1778 for (
unsigned int i = 0; i < nRows; ++i) {
1779 for (
unsigned int j = 0; j < nCols; ++j) {
1792 const std::string& varNamePrefix,
1793 const std::string& fileName,
1794 const std::string& fileType,
1795 const std::set<unsigned int>& allowedSubEnvIds)
const
1808 unsigned int nCols = this->
numCols();
1815 for (
unsigned int i = 0; i < nRows; ++i) {
1816 for (
unsigned int j = 0; j < nCols; ++j) {
1817 *filePtrSet.
ofsVar << (*this)(i,j)
1820 *filePtrSet.
ofsVar <<
"\n";
1822 *filePtrSet.
ofsVar <<
"];\n";
1832 const std::string& fileName,
1833 const std::string& fileType,
1834 const std::set<unsigned int>& allowedSubEnvIds)
1850 unsigned int idOfMyFirstLine = 1;
1851 unsigned int idOfMyLastLine = nRowsLocal;
1852 unsigned int nCols = this->
numCols();
1856 std::string tmpString;
1859 *filePtrSet.
ifsVar >> tmpString;
1863 *filePtrSet.
ifsVar >> tmpString;
1868 *filePtrSet.
ifsVar >> tmpString;
1870 unsigned int posInTmpString = 6;
1873 char nRowsString[tmpString.size()-posInTmpString+1];
1874 unsigned int posInRowsString = 0;
1877 nRowsString[posInRowsString++] = tmpString[posInTmpString++];
1878 }
while (tmpString[posInTmpString] !=
',');
1879 nRowsString[posInRowsString] =
'\0';
1883 char nColsString[tmpString.size()-posInTmpString+1];
1884 unsigned int posInColsString = 0;
1887 nColsString[posInColsString++] = tmpString[posInTmpString++];
1888 }
while (tmpString[posInTmpString] !=
')');
1889 nColsString[posInColsString] =
'\0';
1892 unsigned int numRowsInFile = (
unsigned int) strtod(nRowsString,NULL);
1893 unsigned int numColsInFile = (
unsigned int) strtod(nColsString,NULL);
1897 <<
", numRowsInFile = " << numRowsInFile
1898 <<
", numColsInFile = " << numColsInFile
1899 <<
", nRowsLocal = " << nRowsLocal
1900 <<
", nCols = " << nCols
1908 queso_require_equal_to_msg(numColsInFile, nCols,
"number of parameters of vec in file is different than number of parameters in this vec object");
1911 unsigned int maxCharsPerLine = 64*nCols;
1913 unsigned int lineId = 0;
1914 while (lineId < idOfMyFirstLine) {
1915 filePtrSet.
ifsVar->ignore(maxCharsPerLine,
'\n');
1921 <<
": beginning to read input actual data"
1928 *filePtrSet.
ifsVar >> tmpString;
1932 *filePtrSet.
ifsVar >> tmpString;
1937 std::streampos tmpPos = filePtrSet.
ifsVar->tellg();
1938 filePtrSet.
ifsVar->seekg(tmpPos+(std::streampos)2);
1942 <<
": beginning to read lines with numbers only"
1943 <<
", lineId = " << lineId
1944 <<
", idOfMyFirstLine = " << idOfMyFirstLine
1945 <<
", idOfMyLastLine = " << idOfMyLastLine
1950 while (lineId <= idOfMyLastLine) {
1951 for (
unsigned int j = 0; j < nCols; ++j) {
1952 *filePtrSet.
ifsVar >> tmpRead;
1953 (*this)(lineId-idOfMyFirstLine,j) = tmpRead;
1987 unsigned int m1Cols = m1.
numCols();
1989 unsigned int m2Cols = m2.
numCols();
1997 unsigned int commonSize = m1Cols;
1998 for (
unsigned int row1 = 0; row1 < m1Rows; ++row1) {
1999 for (
unsigned int col2 = 0; col2 < m2Cols; ++col2) {
2001 for (
unsigned int k = 0;
k < commonSize; ++
k) {
2002 result += m1(row1,
k)*m2(
k,col2);
2004 mat(row1,col2) = result;
2032 for (
unsigned int i = 0; i < nRows; ++i) {
2033 double value1 = v1[i];
2034 for (
unsigned int j = 0; j < nCols; ++j) {
2035 answer(i,j) = value1*v2[j];
2046 unsigned int mCols = mat.
numCols();
2053 for (
unsigned int i = 0; i < mRows; ++i) {
2054 double vecValue = vec[i];
2055 for (
unsigned int j = 0; j < mCols; ++j) {
2056 answer(i,j) *= vecValue;
2067 unsigned int mCols = mat.
numCols();
2074 for (
unsigned int j = 0; j < mCols; ++j) {
2075 double vecValue = vec[j];
2076 for (
unsigned int i = 0; i < mRows; ++i) {
2077 answer(i,j) *= vecValue;
const GslMatrix & svdMatV() const
This function calls private member GslMatrix::internalSvd() to set a N-by-N orthogonal square matrix ...
unsigned int displayVerbosity() const
void zeroUpper(bool includeDiagonal=false)
This function sets all the entries above the main diagonal of this matrix to zero.
GslMatrix operator*(double a, const GslMatrix &mat)
double m_determinant
The determinant of this matrix.
virtual void base_copy(const Matrix &src)
Copies base data from matrix src to this matrix.
void subWriteContents(const std::string &varNamePrefix, const std::string &fileName, const std::string &fileType, const std::set< unsigned int > &allowedSubEnvIds) const
Write contents of subenvironment in file fileName.
GslMatrix & operator=(const GslMatrix &rhs)
Copies values from matrix rhs to this.
bool m_isSingular
Indicates whether or not this matrix is singular.
unsigned int numCols() const
Returns the column dimension of this matrix.
int m_signum
m_signum stores the sign of the permutation of the LU decomposition PA = LU.
GslVector invertMultiply(const GslVector &b) const
This function calculates the inverse of this matrix and multiplies it with vector b...
Class for matrix operations using GSL library.
int subRank() const
Access function for sub-rank.
GslMatrix & operator/=(double a)
Stores in this the coordinate-wise division of this by a.
GslVector abs() const
This function returns absolute value of elements in this.
void eigen(GslVector &eigenValues, GslMatrix *eigenVectors) const
This function computes the eigenvalues of a real symmetric matrix.
double determinant() const
Calculates the determinant of this matrix.
int svdSolve(const GslVector &rhsVec, GslVector &solVec) const
This function solves the system A x = b using the singular value decomposition (U, S, V) of A which must have been computed previously with GslMatrix::svd (x=solVec, b=rhsVec).
int svd(GslMatrix &matU, GslVector &vecS, GslMatrix &matVt) const
Checks for the dimension of this matrix, matU, VecS and matVt, and calls the protected routine intern...
const int UQ_MATRIX_SVD_FAILED_RC
GslMatrix leftDiagScaling(const GslVector &vec, const GslMatrix &mat)
const BaseEnvironment & m_env
QUESO environment variable.
double & operator()(unsigned int i, unsigned int j)
Element access method (non-const).
Class for matrix operations (virtual).
void mpiSum(const MpiComm &comm, GslMatrix &M_global) const
std::ofstream * ofsVar
Provides a stream interface to write data to files.
int fullRank() const
Returns the process full rank.
GslVector * m_svdSvec
m_svdSvec stores the diagonal of the N-by-N diagonal matrix of singular values S after the singular v...
#define queso_require_not_equal_to_msg(expr1, expr2, msg)
Struct for handling data input and output from files.
GslMatrix & operator*=(double a)
Stores in this the coordinate-wise multiplication of this and a.
GslMatrix()
Default Constructor.
void setColumn(const unsigned int column_num, const GslVector &column)
This function copies vector column into the column_num-th column of this matrix.
#define queso_require_less_equal_msg(expr1, expr2, msg)
unsigned int sizeLocal() const
Returns the length of this vector.
void fillWithTranspose(unsigned int rowId, unsigned int colId, const GslMatrix &mat, bool checkForExactNumRowsMatching, bool checkForExactNumColsMatching)
This function stores the transpose of this matrix into this matrix.
gsl_matrix * m_mat
GSL matrix, also referred to as this matrix.
std::ostream & operator<<(std::ostream &os, const BaseEnvironment &obj)
GslMatrix operator+(const GslMatrix &m1, const GslMatrix &m2)
const std::string & subIdString() const
Access to the attribute m_subIdString; which stores the string for the sub-environment, and it will be used, for instance, to create the output files for each sub-environment.
double max() const
Returns the maximum element value of the matrix.
The QUESO MPI Communicator Class.
gsl_permutation * m_permutation
The permutation matrix of a LU decomposition.
void cwExtract(unsigned int rowId, unsigned int colId, GslMatrix &mat) const
void closeFile(FilePtrSetStruct &filePtrSet, const std::string &fileType) const
Closes the file.
#define queso_require_equal_to_msg(expr1, expr2, msg)
void smallestEigen(double &eigenValue, GslVector &eigenVector) const
This function finds smallest eigenvalue, namely eigenValue, of this matrix and its corresponding eige...
GslVector invertMultiplyForceLU(const GslVector &b) const
This function calculates the inverse of this matrix and multiplies it with vector b...
unsigned int numRowsGlobal() const
Returns the global row dimension of this matrix.
void zeroLower(bool includeDiagonal=false)
This function sets all the entries bellow the main diagonal of this matrix to zero.
unsigned int numRowsLocal() const
Returns the local row dimension of this matrix.
void fillWithBlocksVertically(unsigned int rowId, unsigned int colId, const std::vector< const GslMatrix * > &matrices, bool checkForExactNumRowsMatching, bool checkForExactNumColsMatching)
This function fills this matrix vertically with const block matrices.
double norm2() const
Returns the 2-norm (Euclidean norm) of the vector.
const int UQ_MATRIX_IS_NOT_POS_DEFINITE_RC
#define queso_require_msg(asserted, msg)
void print(std::ostream &os) const
Print method. Defines the behavior of the ostream << operator inherited from the Object class...
gsl_matrix * data()
Returns this matrix.
const BaseEnvironment & env() const
double m_lnDeterminant
The natural logarithm of the determinant of this matrix.
Class for vector operations using GSL library.
std::ofstream * subDisplayFile() const
Access function for m_subDisplayFile (displays file on stream).
GslMatrix inverse() const
This function calculated the inverse of this matrix (square).
#define queso_require_less_msg(expr1, expr2, msg)
GslMatrix rightDiagScaling(const GslMatrix &mat, const GslVector &vec)
void cwSet(double value)
Component-wise sets all values to this with value.
#define UQ_RC_MACRO(macroIRc, givenRank, where, what, retValue)
GslMatrix & operator+=(const GslMatrix &rhs)
Stores in this the coordinate-wise addition of this and rhs.
void getRow(const unsigned int row_num, GslVector &row) const
This function gets the row_num-th column of this matrix and stores it into vector row...
GslVector multiply(const GslVector &x) const
This function multiplies this matrix by vector x and returns the resulting vector.
GslMatrix matrixProduct(const GslVector &v1, const GslVector &v2)
#define UQ_FATAL_RC_MACRO(macroIRc, givenRank, where, what)
double lnDeterminant() const
Calculates the ln(determinant) of this matrix.
This (virtual) class sets up the environment underlying the use of the QUESO library by an executable...
bool m_printHorizontally
Flag for either or not print this matrix.
int internalSvd() const
This function factorizes the M-by-N matrix A into the singular value decomposition A = U S V^T for M ...
void fillWithTensorProduct(unsigned int rowId, unsigned int colId, const GslMatrix &mat1, const GslMatrix &mat2, bool checkForExactNumRowsMatching, bool checkForExactNumColsMatching)
This function calculates the tensor product of matrices mat1 and mat2 and stores it in this matrix...
GslMatrix * m_svdVmat
m_svdVmat stores the N-by-N orthogonal square matrix V after the singular value decomposition of a ma...
void matlabLinearInterpExtrap(const GslVector &x1Vec, const GslMatrix &y1Mat, const GslVector &x2Vec)
GslMatrix * m_svdUmat
m_svdUmat stores the M-by-N orthogonal matrix U after the singular value decomposition of a matrix...
const BaseEnvironment & env() const
const Map & m_map
Mapping variable.
void copy(const GslMatrix &src)
In this function this matrix receives a copy of matrix src.
unsigned int numOfProcsForStorage() const
bool m_inDebugMode
Flag for either or not QUESO is in debug mode.
void resetLU()
In this function resets the LU decomposition of this matrix, as well as deletes the private member po...
unsigned int rank(double absoluteZeroThreshold, double relativeZeroThreshold) const
This function returns the number of singular values of this matrix (rank).
void getColumn(const unsigned int column_num, GslVector &column) const
This function gets the column_num-th column of this matrix and stores it into vector column...
gsl_matrix * m_LU
GSL matrix for the LU decomposition of m_mat.
GslMatrix * m_svdVTmat
m_svdVmatT stores the transpose of N-by-N orthogonal square matrix V, namely V^T, after the singular ...
void filterLargeValues(double thresholdValue)
This function sets to zero (filters) all entries of this matrix which are greater than thresholdValue...
void largestEigen(double &eigenValue, GslVector &eigenVector) const
This function finds largest eigenvalue, namely eigenValue, of this matrix and its corresponding eigen...
GslMatrix operator-(const GslMatrix &m1, const GslMatrix &m2)
GslMatrix * m_inverse
Inverse matrix of this.
bool openOutputFile(const std::string &fileName, const std::string &fileType, const std::set< unsigned int > &allowedSubEnvIds, bool writeOver, FilePtrSetStruct &filePtrSet) const
Opens an output file for each sub-environment that was chosen to send data to the file...
gsl_vector * data() const
A class for partitioning vectors and matrices.
#define queso_require_greater_equal_msg(expr1, expr2, msg)
std::ifstream * ifsVar
Provides a stream interface to read data from files.
void matlabLinearInterpExtrap(const GslVector &x1Vec, const GslVector &y1Vec, const GslVector &x2Vec)
unsigned int checkingLevel() const
Access function to private attribute m_checkingLevel.
GslMatrix transpose() const
This function calculated the transpose of this matrix (square).
void cwSet(double value)
Component-wise set all values to this with value.
int chol()
Computes Cholesky factorization of a real symmetric positive definite matrix this.
double normMax() const
Returns the Frobenius norm of this matrix.
void fillWithBlocksDiagonally(unsigned int rowId, unsigned int colId, const std::vector< const GslMatrix * > &matrices, bool checkForExactNumRowsMatching, bool checkForExactNumColsMatching)
This function fills this matrix diagonally with const block matrices.
int worldRank() const
Returns the process world rank.
const GslMatrix & svdMatU() const
This function calls private member GslMatrix::internalSvd() to set a M-by-N orthogonal matrix U of th...
#define queso_require_greater_msg(expr1, expr2, msg)
double normFrob() const
Returns the Frobenius norm of this matrix.
void setRow(const unsigned int row_num, const GslVector &row)
This function copies vector row into the row_num-th column of this matrix.
void subReadContents(const std::string &fileName, const std::string &fileType, const std::set< unsigned int > &allowedSubEnvIds)
Read contents of subenvironment from file fileName.
GslMatrix & operator-=(const GslMatrix &rhs)
Stores in this the coordinate-wise subtraction of this by rhs.
void filterSmallValues(double thresholdValue)
This function sets to zero (filters) all entries of this matrix which are smaller than thresholdValue...
#define RawValue_MPI_DOUBLE
void Allreduce(void *sendbuf, void *recvbuf, int count, RawType_MPI_Datatype datatype, RawType_MPI_Op op, const char *whereMsg, const char *whatMsg) const
Combines values from all processes and distributes the result back to all processes.
void fillWithBlocksHorizontally(unsigned int rowId, unsigned int colId, const std::vector< const GslMatrix * > &matrices, bool checkForExactNumRowsMatching, bool checkForExactNumColsMatching)
This function fills this matrix horizontally with const block matrices.
Map * m_svdColMap
Mapping for matrices involved in the singular value decomposition (svd) routine.
bool openInputFile(const std::string &fileName, const std::string &fileType, const std::set< unsigned int > &allowedSubEnvIds, FilePtrSetStruct &filePtrSet) const
Opens an input file.