25 #include <queso/Defines.h>
27 #ifdef QUESO_HAS_TRILINOS
28 #include <queso/TeuchosMatrix.h>
29 #include <queso/TeuchosVector.h>
34 #ifdef QUESO_HAS_TRILINOS
55 m_determinant (-INFINITY),
56 m_lnDeterminant(-INFINITY),
79 m_determinant (-INFINITY),
80 m_lnDeterminant(-INFINITY),
88 for (
unsigned int i = 0; i < (
unsigned int)
m_mat.numRows(); ++i) {
89 m_mat(i,i) = diagValue;
106 m_determinant (-INFINITY),
107 m_lnDeterminant(-INFINITY),
115 for (
unsigned int i = 0; i < (
unsigned int)
m_mat.numRows(); ++i) {
116 m_mat(i,i) = diagValue;
132 m_determinant (-INFINITY),
133 m_lnDeterminant(-INFINITY),
143 for (
unsigned int i = 0; i <
dim; ++i) {
159 m_determinant (-INFINITY),
160 m_lnDeterminant(-INFINITY),
196 iRC =
m_mat.scale(a);
197 queso_require_msg(!(iRC),
"scaling failed");
218 for(i=0; i< nrows ; i++)
219 for (j = 0; j < ncols; j++)
220 (*
this)(i,j) += rhs(i,j);
233 for(i=0; i< nrows ; i++)
234 for (j = 0; j < ncols; j++)
235 (*
this)(i,j) -= rhs(i,j);
247 if ((i >= (
unsigned int)
m_mat.numRows()) ||
248 (j >= (
unsigned int)
m_mat.numCols())) {
249 std::cerr <<
"In TeuchosMatrix::operator(i,j) (non-const)"
252 <<
", m_mat.numRows() = " << (
unsigned int)
m_mat.numRows()
253 <<
", m_mat.numCols() = " << (
unsigned int)
m_mat.numCols()
255 queso_require_less_msg(i, (
unsigned int)
m_mat.numRows(),
"i is too large");
256 queso_require_less_msg(j, (
unsigned int)
m_mat.numCols(),
"j is too large");
264 queso_require_less_msg(i, (
unsigned int)
m_mat.numRows(),
"i is too large");
265 queso_require_less_msg(j, (
unsigned int)
m_mat.numCols(),
"j is too large");
276 return (
unsigned int)
m_mat.numRows();
284 return (
unsigned int)
m_mat.numRows();
292 return (
unsigned int)
m_mat.numCols();
300 return m_mat.values();
308 return m_mat.stride();
317 double value = -INFINITY;
320 unsigned int nCols = this->
numCols();
322 for (
unsigned int i = 0; i < nRows; i++) {
323 for (
unsigned int j = 0; j < nCols; j++) {
325 if (aux > value) value = aux;
342 if (relativeVec[0] > 0.) {
343 relativeVec = (1./relativeVec[0])*relativeVec;
346 unsigned int rankValue = 0;
347 for (
unsigned int i = 0; i < relativeVec.
sizeLocal(); ++i) {
348 if (( (*
m_svdSvec)[i] >= absoluteZeroThreshold ) &&
349 ( relativeVec [i] >= relativeZeroThreshold )) {
357 <<
", this->numCols() = " << this->
numCols()
358 <<
", absoluteZeroThreshold = " << absoluteZeroThreshold
359 <<
", relativeZeroThreshold = " << relativeZeroThreshold
360 <<
", rankValue = " << rankValue
362 <<
", relativeVec = " << relativeVec
375 unsigned int nCols = this->
numCols();
380 for (
unsigned int row = 0; row < nRows; ++row) {
381 for (
unsigned int col = 0; col < nCols; ++col) {
382 mat(row,col) = (*this)(col,row);
395 unsigned int nCols = this->
numCols();
402 unitVector.
cwSet(0.);
404 for (
unsigned int j = 0; j < nCols; ++j) {
405 if (j > 0) unitVector[j-1] = 0.;
408 for (
unsigned int i = 0; i < nRows; ++i) {
409 (*m_inverse)(i,j) = multVector[i];
421 <<
"\n M = " << *
this
423 <<
"\n M*M^{-1} = " << (*this)*(*m_inverse)
424 <<
"\n M^{-1}*M = " << (*m_inverse)*(*this)
446 if(
m_LU.numRows() ==0 &&
m_LU.numCols() ==0)
452 <<
": before 'this->invertMultiply()'"
458 <<
": after 'this->invertMultiply()'"
464 <<
": before computing det"
470 for (
int i=0;i<
m_LU.numCols();i++) {
472 lnDet += std::log(
m_LU(i,i));
480 <<
": after computing det"
495 if(
m_LU.numRows() ==0 &&
m_LU.numCols() ==0)
501 <<
": before 'this->invertMultiply()'"
507 <<
": after 'this->invertMultiply()'"
513 <<
": before computing lnDet"
519 for (
int i=0;i<
m_LU.numCols();i++) {
521 lnDet += std::log(
m_LU(i,i));
529 <<
": after computing lnDet"
543 return m_mat.normFrobenius ();
553 unsigned int nCols = this->
numCols();
555 for (
unsigned int i = 0; i < nRows; i++) {
556 for (
unsigned int j = 0; j < nCols; j++) {
557 aux = fabs((*
this)(i,j));
558 if (aux > value) value = aux;
570 int return_success =0 ;
576 Teuchos::LAPACK<int, double> lapack;
586 lapack.POTRF (UPLO,
m_mat.numRows(),
m_mat.values(),
m_mat.stride(), &info);
591 for (
int i=0;i<
m_mat.numRows();i++){
592 for (
int j=i+1;j<
m_mat.numCols();j++)
597 std::cerr <<
"In TeuchosMtrix::chol()"
598 <<
": INFO = " << info
599 <<
",\n INFO < 0: if INFO = -i, the i-th argument had an illegal value."
600 <<
"\n INFO > 0: if INFO = i, the leading minor of order i is not "
601 <<
" positive definite, and the factorization could not be completed."
608 "TeuchosMatrix::chol()",
609 "matrix is not positive definite",
612 return return_success;
620 unsigned int nCols = this->
numCols();
622 queso_require_msg(!((matU.
numRowsLocal() != nRows) || (matU.
numCols() != nCols)),
"invalid matU");
626 queso_require_msg(!((matVt.
numRowsLocal() != nCols) || (matVt.
numCols() != nCols)),
"invalid matVt");
670 unsigned int nCols = this->
numCols();
682 <<
", this->numCols() = " << this->
numCols()
688 <<
"\n rhsVec.sizeLocal() = " << rhsVec.
sizeLocal()
689 <<
"\n solVec.sizeLocal() = " << solVec.
sizeLocal()
698 for (i=0; i<nRows; i++){
709 solVec = auxMatrix*rhsVec;
717 Teuchos::LAPACK<int, double> lapack;
718 lapack.GESV(nCols, 1, aux_m_svdVTmat->
values(), aux_m_svdVTmat->
stride(), ipiv, &solVec[0], solVec.
sizeLocal(), &info );
727 delete aux_m_svdVTmat;
738 unsigned int nCols = this->
numCols();
749 for (
unsigned int j = 0; j < rhsMat.
numCols(); ++j) {
751 iRC = this->
svdSolve(rhsVec, solVec);
797 if (
m_LU.numCols() == 0 &&
m_LU.numRows() == 0)
799 queso_require_msg(!(
v_pivoting),
"v_pivoting should be NULL");
807 queso_require_msg(
v_pivoting,
"malloc() failed");
810 std::cout <<
"In TeuchosMatrix::invertMultiply()"
811 <<
": before LU decomposition, m_LU = ";
812 for (
int i=0;i<3;i++){
813 for (
int j=0;j<3;j++)
814 cout <<
m_LU(i,j) <<
"\t" ;
820 Teuchos::LAPACK<int, double> lapack;
826 std::cerr <<
"In TeuchosMatrix::invertMultiply()"
827 <<
", after lapack.GETRF"
828 <<
": INFO = " << info
829 <<
",\nINFO < 0: if INFO = -i, the i-th argument had an illegal value.\n"
830 <<
"INFO > 0: if INFO = i, U(i,i) is exactly zero. The factorization \n"
831 <<
"has been completed, but the factor U is exactly singular, and division \n"
832 <<
"by zero will occur if it is used to solve a system of equations."
835 queso_require_msg(!(info),
"GETRF() failed");
841 std::cout <<
"In TeuchosMatrix::invertMultiply()"
842 <<
": after LU decomposition, m_LU = ";
843 for (
int i=0;i<3;i++){
844 for (
int j=0;j<3;j++)
845 cout <<
m_LU(i,j) <<
"\t" ;
848 std::cout << std::endl;
853 <<
": before 'lapack.GETRS()'"
858 Teuchos::LAPACK<int, double> lapack;
872 std::cerr <<
"In TeuchosMatrix::invertMultiply()"
873 <<
", after lapack.GETRS - solve LU system"
874 <<
": INFO = " << info02
875 <<
",\nINFO < 0: if INFO = -i, the i-th argument had an illegal value.\n"
878 queso_require_msg(!(info02),
"GETRS() failed");
881 <<
", after lapack.GETRS() - solve LU system."
886 std::cout <<
"In TeuchosMatrix::invertMultiply()"
887 <<
": ||b - Ax||_2 = " << tmpVec.
norm2()
888 <<
": ||b - Ax||_2/||b||_2 = " << tmpVec.
norm2()/b.
norm2()
917 for(
unsigned int j = 0; j < B.
numCols(); ++j ) {
947 if (
m_LU.numCols() == 0 &&
m_LU.numRows() == 0) {
948 queso_require_msg(!(
v_pivoting),
"v_pivoting should be NULL");
960 queso_require_msg(
v_pivoting,
"malloc() for v_pivoting failed");
963 Teuchos::LAPACK<int, double> lapack;
969 std::cerr <<
"In TeuchosMatrix::invertMultiply()"
970 <<
", after lapack.GETRF"
971 <<
": INFO = " << info
972 <<
",\nINFO < 0: if INFO = -i, the i-th argument had an illegal value.\n"
973 <<
"INFO > 0: if INFO = i, U(i,i) is exactly zero. The factorization \n"
974 <<
"has been completed, but the factor U is exactly singular, and division \n"
975 <<
"by zero will occur if it is used to solve a system of equations."
979 queso_require_msg(!(info),
"GETRF() failed");
998 std::cerr <<
"In TeuchosMatrix::invertMultiply()"
999 <<
", after lapack.GETRS - solve LU system"
1000 <<
": INFO = " << info02
1001 <<
",\nINFO < 0: if INFO = -i, the i-th argument had an illegal value.\n"
1005 queso_require_msg(!(info02),
"GETRS() failed");
1041 unsigned int n = eigenValues.
sizeLocal();
1051 Teuchos::SerialDenseMatrix<int,double> copy_m_mat;
1054 Teuchos::LAPACK<int, double> lapack;
1061 WORK =
new double[lwork];
1063 if (eigenVectors == NULL) {
1066 lapack.SYEV(JOBZ, UPLO, copy_m_mat.numRows(), copy_m_mat.values(), copy_m_mat.stride(), &W[0], &WORK[0], lwork, &info);
1068 for (
unsigned int i=0; i< n; i++)
1069 eigenValues[i] = W[i];
1076 lapack.SYEV(JOBZ, UPLO, copy_m_mat.numRows(), copy_m_mat.values(), copy_m_mat.stride(), &W[0], &WORK[0], lwork, &info);
1078 for (
unsigned int i=0; i< n; i++)
1079 eigenValues[i] = W[i];
1081 eigenVectors->
m_mat = copy_m_mat;
1085 std::cerr <<
"In TeuchosMtrix::eigen()"
1086 <<
": INFO = " << info
1087 <<
",\n INFO < 0: if INFO = -i, the i-th argument had an illegal value."
1088 <<
"\n INFO > 0: if INFO = i, the algorithm failed to converge; i off-diagonal "
1089 <<
" elements of an intermediate tridiagonal form did not converge to zero."
1099 unsigned int n = eigenVector.
sizeLocal();
1102 Teuchos::LAPACK<int, double> lapack;
1105 Teuchos::SerialDenseMatrix<int,double> copy_m_mat;
1115 WORK =
new double[lwork];
1117 lapack.SYEV(JOBZ, UPLO, copy_m_mat.numRows(), copy_m_mat.values(), copy_m_mat.stride(), &W[0], &WORK[0], lwork, &info);
1120 std::cerr <<
"In TeuchosMtrix::largestEigen()"
1121 <<
": INFO = " << info
1122 <<
",\n INFO < 0: if INFO = -i, the i-th argument had an illegal value."
1123 <<
"\n INFO > 0: if INFO = i, the algorithm failed to converge; i off-diagonal "
1124 <<
" elements of an intermediate tridiagonal form did not converge to zero."
1129 eigenValue = W[n-1];
1133 for (
int i=0; i< copy_m_mat.numRows(); i++)
1134 eigenVector[i] = copy_m_mat(i,n-1);
1143 unsigned int n = eigenVector.
sizeLocal();
1146 Teuchos::LAPACK<int, double> lapack;
1149 Teuchos::SerialDenseMatrix<int,double> copy_m_mat;
1159 WORK =
new double[lwork];
1161 lapack.SYEV(JOBZ, UPLO, copy_m_mat.numRows(), copy_m_mat.values(), copy_m_mat.stride(), &W[0], &WORK[0], lwork, &info);
1164 std::cerr <<
"In TeuchosMtrix::smallestEigen()"
1165 <<
": INFO = " << info
1166 <<
",\n INFO < 0: if INFO = -i, the i-th argument had an illegal value."
1167 <<
"\n INFO > 0: if INFO = i, the algorithm failed to converge; i off-diagonal "
1168 <<
" elements of an intermediate tridiagonal form did not converge to zero."
1178 for (
int i=0; i< copy_m_mat.numRows(); i++)
1179 eigenVector[i] = copy_m_mat(i,0);
1191 m_mat.putScalar(value);
1200 queso_require_less_msg(rowId, this->
numRowsLocal(),
"invalid rowId");
1204 queso_require_less_msg(colId, this->
numCols(),
"invalid colId");
1206 queso_require_less_equal_msg((colId + mat.
numCols()), this->
numCols(),
"invalid vec.numCols()");
1208 for (
unsigned int i = 0; i < mat.
numRowsLocal(); ++i) {
1209 for (
unsigned int j = 0; j < mat.
numCols(); ++j) {
1210 (*this)(rowId+i,colId+j) = mat(i,j);
1223 queso_require_less_msg(column_num, this->
numCols(),
"Specified row number not within range");
1228 const double* temp_ptr ;
1231 temp_ptr =
m_mat[column_num];
1234 for (
unsigned int i=0; i< column.
sizeLocal();i++)
1235 column[i] = temp_ptr[i];
1259 queso_require_less_msg(column_num, this->
numCols(),
"Specified column number not within range");
1263 for (
unsigned int i =0; i < column.
sizeLocal(); i++)
1264 m_mat(i,column_num) = column[i];
1275 queso_require_less_msg(row_num, this->
numRowsLocal(),
"Specified row number not within range");
1280 for (
unsigned int i=0; i< row.
sizeLocal();i++)
1281 row[i] =
m_mat(row_num,i);
1294 this->
getRow( row_num, row );
1304 queso_require_less_msg(row_num, this->
numRowsLocal(),
"Specified row number not within range");
1309 for (
unsigned int i=0; i< row.
sizeLocal();i++)
1310 m_mat(row_num,i) = row[i] ;
1321 unsigned int nCols = this->
numCols();
1326 if (includeDiagonal) {
1327 for (
unsigned int i = 0; i < nRows; i++) {
1328 for (
unsigned int j = 0; j <= i; j++) {
1334 for (
unsigned int i = 0; i < nRows; i++) {
1335 for (
unsigned int j = 0; j < i; j++) {
1350 unsigned int nCols = this->
numCols();
1355 if (includeDiagonal) {
1356 for (
unsigned int i = 0; i < nRows; i++) {
1357 for (
unsigned int j = i; j < nCols; j++) {
1363 for (
unsigned int i = 0; i < nRows; i++) {
1364 for (
unsigned int j = (i+1); j < nCols; j++) {
1378 unsigned int nCols = this->
numCols();
1380 for (
unsigned int i = 0; i < nRows; ++i) {
1381 for (
unsigned int j = 0; j < nCols; ++j) {
1382 double aux = (*this)(i,j);
1384 if ((aux < 0. ) && (-thresholdValue < aux)) {
1387 if ((aux > 0. ) && (thresholdValue > aux)) {
1400 unsigned int nCols = this->
numCols();
1402 for (
unsigned int i = 0; i < nRows; ++i) {
1403 for (
unsigned int j = 0; j < nCols; ++j) {
1404 double aux = (*this)(i,j);
1406 if ( (aux < 0. ) && (-thresholdValue > aux))
1409 if ((aux > 0. ) && (thresholdValue < aux))
1423 unsigned int nCols = mat.
numCols();
1427 for (
unsigned int row = 0; row < nRows; ++row) {
1428 for (
unsigned int col = 0; col < nCols; ++col) {
1429 (*this)(col,row) = mat(row,col);
1441 unsigned int sumNumRowsLocals = 0;
1442 unsigned int sumNumCols = 0;
1443 for (
unsigned int i = 0; i < matrices.size(); ++i) {
1444 sumNumRowsLocals += matrices[i]->numRowsLocal();
1445 sumNumCols += matrices[i]->numCols();
1450 unsigned int cumulativeRowId = 0;
1451 unsigned int cumulativeColId = 0;
1452 for (
unsigned int i = 0; i < matrices.size(); ++i) {
1453 unsigned int nRows = matrices[i]->numRowsLocal();
1454 unsigned int nCols = matrices[i]->numCols();
1455 for (
unsigned int rowId = 0; rowId < nRows; ++rowId) {
1456 for (
unsigned int colId = 0; colId < nCols; ++colId) {
1457 (*this)(cumulativeRowId + rowId, cumulativeColId + colId) = (*(matrices[i]))(rowId,colId);
1460 cumulativeRowId += nRows;
1461 cumulativeColId += nCols;
1471 unsigned int sumNumRowsLocals = 0;
1472 unsigned int sumNumCols = 0;
1473 for (
unsigned int i = 0; i < matrices.size(); ++i) {
1474 sumNumRowsLocals += matrices[i]->numRowsLocal();
1475 sumNumCols += matrices[i]->numCols();
1480 unsigned int cumulativeRowId = 0;
1481 unsigned int cumulativeColId = 0;
1482 for (
unsigned int i = 0; i < matrices.size(); ++i) {
1483 unsigned int nRows = matrices[i]->numRowsLocal();
1484 unsigned int nCols = matrices[i]->numCols();
1485 for (
unsigned int rowId = 0; rowId < nRows; ++rowId) {
1486 for (
unsigned int colId = 0; colId < nCols; ++colId) {
1487 (*this)(cumulativeRowId + rowId, cumulativeColId + colId) = (*(matrices[i]))(rowId,colId);
1490 cumulativeRowId += nRows;
1491 cumulativeColId += nCols;
1501 unsigned int sumNumCols = 0;
1502 for (
unsigned int i = 0; i < matrices.size(); ++i) {
1504 sumNumCols += matrices[i]->numCols();
1508 unsigned int cumulativeColId = 0;
1509 for (
unsigned int i = 0; i < matrices.size(); ++i) {
1510 unsigned int nRows = matrices[i]->numRowsLocal();
1511 unsigned int nCols = matrices[i]->numCols();
1512 for (
unsigned int rowId = 0; rowId < nRows; ++rowId) {
1513 for (
unsigned int colId = 0; colId < nCols; ++colId) {
1514 (*this)(rowId, cumulativeColId + colId) = (*(matrices[i]))(rowId,colId);
1517 cumulativeColId += nCols;
1528 unsigned int sumNumCols = 0;
1529 for (
unsigned int i = 0; i < matrices.size(); ++i) {
1531 sumNumCols += matrices[i]->numCols();
1535 unsigned int cumulativeColId = 0;
1536 for (
unsigned int i = 0; i < matrices.size(); ++i) {
1537 unsigned int nRows = matrices[i]->numRowsLocal();
1538 unsigned int nCols = matrices[i]->numCols();
1539 for (
unsigned int rowId = 0; rowId < nRows; ++rowId) {
1540 for (
unsigned int colId = 0; colId < nCols; ++colId) {
1541 (*this)(rowId, cumulativeColId + colId) = (*(matrices[i]))(rowId,colId);
1544 cumulativeColId += nCols;
1555 unsigned int sumNumRows = 0;
1556 for (
unsigned int i = 0; i < matrices.size(); ++i) {
1558 sumNumRows += matrices[i]->numRowsLocal();
1562 unsigned int cumulativeRowId = 0;
1563 for (
unsigned int i = 0; i < matrices.size(); ++i) {
1564 unsigned int nRows = matrices[i]->numRowsLocal();
1565 unsigned int nCols = matrices[i]->numCols();
1566 for (
unsigned int rowId = 0; rowId < nRows; ++rowId) {
1567 for (
unsigned int colId = 0; colId < nCols; ++colId) {
1568 (*this)(cumulativeRowId + rowId, colId) = (*(matrices[i]))(rowId,colId);
1571 cumulativeRowId += nRows;
1581 unsigned int sumNumRows = 0;
1582 for (
unsigned int i = 0; i < matrices.size(); ++i) {
1584 sumNumRows += matrices[i]->numRowsLocal();
1588 unsigned int cumulativeRowId = 0;
1589 for (
unsigned int i = 0; i < matrices.size(); ++i) {
1590 unsigned int nRows = matrices[i]->numRowsLocal();
1591 unsigned int nCols = matrices[i]->numCols();
1592 for (
unsigned int rowId = 0; rowId < nRows; ++rowId) {
1593 for (
unsigned int colId = 0; colId < nCols; ++colId) {
1594 (*this)(cumulativeRowId + rowId, colId) = (*(matrices[i]))(rowId,colId);
1597 cumulativeRowId += nRows;
1610 for (
unsigned int rowId1 = 0; rowId1 < mat1.
numRowsLocal(); ++rowId1) {
1611 for (
unsigned int colId1 = 0; colId1 < mat1.
numCols(); ++colId1) {
1612 double multiplicativeFactor = mat1(rowId1,colId1);
1613 unsigned int targetRowId = rowId1 * mat2.
numRowsLocal();
1614 unsigned int targetColId = colId1 * mat2.
numCols();
1615 for (
unsigned int rowId2 = 0; rowId2 < mat2.
numRowsLocal(); ++rowId2) {
1616 for (
unsigned int colId2 = 0; colId2 < mat2.
numCols(); ++colId2) {
1617 (*this)(targetRowId + rowId2, targetColId + colId2) = multiplicativeFactor * mat2(rowId2,colId2);
1633 for (
unsigned int rowId1 = 0; rowId1 < mat1.
numRowsLocal(); ++rowId1) {
1634 for (
unsigned int colId1 = 0; colId1 < mat1.
numCols(); ++colId1) {
1635 double multiplicativeFactor = mat1(rowId1,colId1);
1636 unsigned int targetRowId = rowId1 * vec2.
sizeLocal();
1637 unsigned int targetColId = colId1 * 1;
1638 for (
unsigned int rowId2 = 0; rowId2 < vec2.
sizeLocal(); ++rowId2) {
1639 for (
unsigned int colId2 = 0; colId2 < 1; ++colId2) {
1640 (*this)(targetRowId + rowId2, targetColId + colId2) = multiplicativeFactor * vec2[rowId2];
1658 "local and global matrices incompatible");
1661 "local and global matrices incompatible");
1665 std::vector<double> local( size, 0.0 );
1666 std::vector<double> global( size, 0.0 );
1669 for(
unsigned int i = 0; i < this->
numRowsLocal(); i++ ) {
1670 for(
unsigned int j = 0; j < this->
numCols(); j++ ) {
1672 local[k] = (*this)(i,j);
1676 comm.
Allreduce<
double>(&local[0], &global[0],
size, RawValue_MPI_SUM,
1677 "TeuchosMatrix::mpiSum()",
1678 "failed MPI.Allreduce()");
1680 for(
unsigned int i = 0; i < this->
numRowsLocal(); i++ ) {
1681 for(
unsigned int j = 0; j < this->
numCols(); j++ ) {
1683 M_global(i,j) = global[k];
1698 queso_require_greater_msg(x1Vec.
sizeLocal(), 1,
"invalid 'x1' size");
1708 for (
unsigned int colId = 0; colId < y1Mat.
numCols(); ++colId) {
1724 unsigned int nCols = this->
numCols();
1727 for (
unsigned int i = 0; i < nRows; ++i) {
1728 for (
unsigned int j = 0; j < nCols; ++j) {
1732 if (i != (nRows-1)) os <<
"; ";
1737 for (
unsigned int i = 0; i < nRows; ++i) {
1738 for (
unsigned int j = 0; j < nCols; ++j) {
1752 const std::string& fileName,
1753 const std::string& fileType,
1754 const std::set<unsigned int>& allowedSubEnvIds)
1756 queso_require_greater_equal_msg(
m_env.
subRank(), 0,
"unexpected subRank");
1758 queso_require_less_equal_msg(this->
numOfProcsForStorage(), 1,
"implemented just for sequential vectors for now");
1770 unsigned int idOfMyFirstLine = 1;
1771 unsigned int idOfMyLastLine = nRowsLocal;
1772 unsigned int nCols = this->
numCols();
1776 std::string tmpString;
1779 *filePtrSet.
ifsVar >> tmpString;
1782 *filePtrSet.
ifsVar >> tmpString;
1787 *filePtrSet.
ifsVar >> tmpString;
1789 unsigned int posInTmpString = 6;
1792 char nRowsString[tmpString.size()-posInTmpString+1];
1793 unsigned int posInRowsString = 0;
1795 queso_require_less_msg(posInTmpString, tmpString.size(),
"symbol ',' not found in first line of file");
1796 nRowsString[posInRowsString++] = tmpString[posInTmpString++];
1797 }
while (tmpString[posInTmpString] !=
',');
1798 nRowsString[posInRowsString] =
'\0';
1802 char nColsString[tmpString.size()-posInTmpString+1];
1803 unsigned int posInColsString = 0;
1805 queso_require_less_msg(posInTmpString, tmpString.size(),
"symbol ')' not found in first line of file");
1806 nColsString[posInColsString++] = tmpString[posInTmpString++];
1807 }
while (tmpString[posInTmpString] !=
')');
1808 nColsString[posInColsString] =
'\0';
1811 unsigned int numRowsInFile = (
unsigned int) strtod(nRowsString,NULL);
1812 unsigned int numColsInFile = (
unsigned int) strtod(nColsString,NULL);
1816 <<
", numRowsInFile = " << numRowsInFile
1817 <<
", numColsInFile = " << numColsInFile
1818 <<
", nRowsLocal = " << nRowsLocal
1819 <<
", nCols = " << nCols
1827 queso_require_equal_to_msg(numColsInFile, nCols,
"number of parameters of vec in file is different than number of parameters in this vec object");
1830 unsigned int maxCharsPerLine = 64*nCols;
1832 unsigned int lineId = 0;
1833 while (lineId < idOfMyFirstLine) {
1834 filePtrSet.
ifsVar->ignore(maxCharsPerLine,
'\n');
1840 <<
": beginning to read input actual data"
1847 *filePtrSet.
ifsVar >> tmpString;
1851 *filePtrSet.
ifsVar >> tmpString;
1856 std::streampos tmpPos = filePtrSet.
ifsVar->tellg();
1857 filePtrSet.
ifsVar->seekg(tmpPos+(std::streampos)2);
1861 <<
": beginning to read lines with numbers only"
1862 <<
", lineId = " << lineId
1863 <<
", idOfMyFirstLine = " << idOfMyFirstLine
1864 <<
", idOfMyLastLine = " << idOfMyLastLine
1869 while (lineId <= idOfMyLastLine) {
1870 for (
unsigned int j = 0; j < nCols; ++j) {
1871 *filePtrSet.
ifsVar >> tmpRead;
1872 (*this)(lineId-idOfMyFirstLine,j) = tmpRead;
1886 const std::string& varNamePrefix,
1887 const std::string& fileName,
1888 const std::string& fileType,
1889 const std::set<unsigned int>& allowedSubEnvIds)
const
1891 queso_require_greater_equal_msg(
m_env.
subRank(), 0,
"unexpected subRank");
1893 queso_require_less_equal_msg(this->
numOfProcsForStorage(), 1,
"implemented just for sequential vectors for now");
1902 unsigned int nCols = this->
numCols();
1909 for (
unsigned int i = 0; i < nRows; ++i) {
1910 for (
unsigned int j = 0; j < nCols; ++j) {
1911 *filePtrSet.
ofsVar << (*this)(i,j)
1914 *filePtrSet.
ofsVar <<
"\n";
1916 *filePtrSet.
ofsVar <<
"];\n";
1937 for(i=0; i< nrows ; i++)
1938 for (j = 0; j < ncols; j++)
1939 m_mat(i,j) = src(i,j);
1948 if (
m_LU.numCols() >0 ||
m_LU.numRows() > 0) {
1999 unsigned int sizeX = this->
numCols();
2001 for (
unsigned int i = 0; i < sizeY; ++i) {
2003 for (
unsigned int j = 0; j < sizeX; ++j) {
2004 value += (*this)(i,j)*x[j];
2019 int nCols = (int) this->
numCols();
2020 queso_require_greater_equal_msg(nRows, nCols,
"LAPACK/Teuchos only supports cases where nRows >= nCols");
2029 int minRowsCols, maxRowsCols;
2031 if (nRows>=nCols) { minRowsCols = nCols; maxRowsCols = nRows; }
else { minRowsCols = nRows; maxRowsCols = nCols; }
2035 double *work, *rwork;
2040 lwork = 15*maxRowsCols;
2041 work =
new double[lwork];
2043 int aux1= 5*minRowsCols+7, aux2= 2*maxRowsCols+2*minRowsCols+1;
2046 if (aux1>=aux2) { aux_dim = minRowsCols*aux1; }
else {aux_dim = minRowsCols*aux2; }
2048 rwork =
new double[aux_dim];
2050 Teuchos::LAPACK<int, double> lapack;
2081 unsigned int m1Cols = m1.
numCols();
2083 unsigned int m2Cols = m2.
numCols();
2088 unsigned int commonSize = m1Cols;
2090 for (
unsigned int row1 = 0; row1 < m1Rows; ++row1) {
2091 for (
unsigned int col2 = 0; col2 < m2Cols; ++col2) {
2093 for (
unsigned int k = 0; k < commonSize; ++k) {
2094 result += m1(row1,k)*m2(k,col2);
2096 mat(row1,col2) = result;
2126 for (
unsigned int i = 0; i < nRows; ++i) {
2127 double value1 = v1[i];
2128 for (
unsigned int j = 0; j < nCols; ++j) {
2129 answer(i,j) = value1*v2[j];
2141 unsigned int mCols = mat.
numCols();
2148 for (
unsigned int i = 0; i < mRows; ++i) {
2149 double vecValue = vec[i];
2150 for (
unsigned int j = 0; j < mCols; ++j) {
2151 answer(i,j) *= vecValue;
2163 unsigned int mCols = mat.
numCols();
2170 for (
unsigned int j = 0; j < mCols; ++j) {
2171 double vecValue = vec[j];
2172 for (
unsigned int i = 0; i < mRows; ++i) {
2173 answer(i,j) *= vecValue;
2191 #endif // ifdef QUESO_HAS_TRILINOS
void cwSet(double value)
Component-wise set all values to this with value.
TeuchosMatrix & operator-=(const TeuchosMatrix &rhs)
Stores in this the coordinate-wise subtraction of this by rhs.
unsigned int numRowsLocal() const
Returns the local row dimension of this matrix.
void fillWithBlocksHorizontally(const std::vector< const TeuchosMatrix * > &matrices)
This function fills this matrix horizontally with const block matrices.
TeuchosMatrix()
Default Constructor.
double normFrob() const
Returns the Frobenius norm of this matrix.
int svd(TeuchosMatrix &matU, TeuchosVector &vecS, TeuchosMatrix &matVt) const
Checks for the dimension of this matrix, matU, VecS and matVt, and calls the protected routine intern...
int stride()
Returns the stride between the columns of this matrix in memory.
const Map m_map
Mapping variable.
A class for partitioning vectors and matrices.
const BaseEnvironment & env() const
const TeuchosMatrix & svdMatV() const
This function calls private member TeuchosMatrix::internalSvd() to set a N-by-N orthogonal square mat...
Map * m_svdColMap
Mapping for matrices involved in the singular value decomposition (svd) routine.
void invertMultiplyForceLU(const TeuchosVector &b, TeuchosVector &x) const
This function calculates the inverse of this matrix, multiplies it with vector b and stores the resul...
unsigned int sizeLocal() const
Returns the length of this vector.
void eigen(TeuchosVector &eigenValues, TeuchosMatrix *eigenVectors) const
This function computes the eigenvalues of a real symmetric matrix.
This (virtual) class sets up the environment underlying the use of the QUESO library by an executable...
Teuchos::SerialDenseMatrix< int, double > m_LU
Teuchos matrix for the LU decomposition of m_mat.
TeuchosMatrix * m_svdVmat
m_svdVmat stores the N-by-N orthogonal square matrix V after the singular value decomposition of a ma...
void matlabLinearInterpExtrap(const TeuchosVector &xVec, const TeuchosVector &yVec, const TeuchosVector &xiVec)
Reproduces MATLAB linear inter/extra-polation.
TeuchosMatrix * m_svdUmat
m_svdUmat stores the M-by-N orthogonal matrix U after the singular value decomposition of a matrix...
double m_lnDeterminant
The natural logarithm of the determinant of this matrix.
~TeuchosMatrix()
Destructor.
const TeuchosMatrix & svdMatU() const
This function calls private member TeuchosMatrix::internalSvd() to set a M-by-N orthogonal matrix U o...
int chol()
Computes Cholesky factorization of this, a real symmetric positive definite matrix.
void setColumn(const unsigned int column_num, const TeuchosVector &column)
This function copies vector column into the column_num-th column of this matrix.
TeuchosMatrix & operator*=(double a)
Stores in this the coordinate-wise multiplication of this and a.
unsigned int numOfProcsForStorage() const
std::ostream & operator<<(std::ostream &os, const SequenceStatisticalOptions &obj)
void closeFile(FilePtrSetStruct &filePtrSet, const std::string &fileType) const
Closes the file.
MonteCarloSGOptions::MonteCarloSGOptions(const BaseEnvironment &env, const char *prefix queso_require_not_equal_to_msg)(m_env.optionsInputFileName(), std::string(""), std::string("this constructor is incompatible with the absence of an options input file"))
TeuchosMatrix inverse() const
This function calculates the inverse of this matrix (square).
int subRank() const
Returns the rank of the MPI process in the sub-communicator subComm()
unsigned int numRowsGlobal() const
Returns the global row dimension of this matrix.
GslMatrix operator-(const GslMatrix &m1, const GslMatrix &m2)
bool m_printHorizontally
Flag for either or not print 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.
void fillWithTranspose(const TeuchosMatrix &mat)
This function stores the transpose of this matrix into this matrix.
TeuchosMatrix * m_inverse
Stores the inverse of this matrix.
unsigned int checkingLevel() const
Access function to private attribute m_checkingLevel.
const int UQ_MATRIX_IS_NOT_POS_DEFINITE_RC
void setRow(const unsigned int row_num, const TeuchosVector &row)
This function copies vector row into the row_num-th column of this matrix.
void zeroUpper(bool includeDiagonal=false)
This function sets all the entries bellow the main diagonal (inclusive or not) of this matrix to zero...
void largestEigen(double &eigenValue, TeuchosVector &eigenVector) const
This function finds largest eigenvalue, namely eigenValue, of this matrix and its corresponding eigen...
TeuchosVector multiply(const TeuchosVector &x) const
This function multiplies this matrix by vector x and returns a vector.
void getRow(const unsigned int row_num, TeuchosVector &row) const
This function gets the row_num-th column of this matrix and stores it into vector row...
int worldRank() const
Returns the same thing as fullRank()
double determinant() const
Calculates the determinant of this matrix.
void getColumn(const unsigned int column_num, TeuchosVector &column) const
This function gets the column_num-th column of this matrix and stores it into vector column...
int fullRank() const
Returns the rank of the MPI process in QUESO's full communicator.
double normMax() const
Returns the Frobenius norm of 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 ...
bool openInputFile(const std::string &fileName, const std::string &fileType, const std::set< unsigned int > &allowedSubEnvIds, FilePtrSetStruct &filePtrSet) const
Opens an input file.
void zeroLower(bool includeDiagonal=false)
This function sets all the entries above the main diagonal (inclusive or not) of this matrix to zero...
double * values()
Returns a pointer to the first element of this matrix.
TeuchosMatrix & operator+=(const TeuchosMatrix &rhs)
Stores in this the coordinate-wise addition of this and rhs.
int * v_pivoting
The pivoting vector of a LU decomposition.
Class for matrix operations (virtual).
TeuchosVector * m_svdSvec
m_svdSvec stores the diagonal of the N-by-N diagonal matrix of singular values S after the singular v...
GslMatrix operator*(double a, const GslMatrix &mat)
void matlabLinearInterpExtrap(const TeuchosVector &x1Vec, const TeuchosMatrix &y1Mat, const TeuchosVector &x2Vec)
void filterSmallValues(double thresholdValue)
This function sets to zero (filters) all entries of this matrix which are smaller than thresholdValue...
Teuchos::SerialDenseMatrix< int, double > m_mat
Teuchos matrix, also referred to as this matrix.
TeuchosMatrix & operator=(const TeuchosMatrix &rhs)
Copies values from matrix rhs to this.
TeuchosMatrix transpose() const
This function calculates the transpose of this matrix (square).
Class for vector operations using Teuchos (Trilinos).
const BaseEnvironment & env() const
void resetLU()
In this function resets the LU decomposition of this matrix, as well as deletes the private member po...
TeuchosMatrix * m_svdVTmat
m_svdVmatT stores the transpose of N-by-N orthogonal square matrix V, namely V^T, after the singular ...
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.
int NumGlobalElements() const
Returns the total number of elements across all processors.
bool m_isSingular
Indicates whether or not this matrix is singular.
std::ifstream * ifsVar
Provides a stream interface to read data from files.
void fillWithBlocksVertically(const std::vector< const TeuchosMatrix * > &matrices)
This function fills this matrix vertically with const block matrices.
double m_determinant
The determinant of this matrix.
void cwSet(double value)
Component-wise sets all values to this with value.
GslMatrix matrixProduct(const GslVector &v1, const GslVector &v2)
void fillWithTensorProduct(const TeuchosMatrix &mat1, const TeuchosMatrix &mat2)
This function calculates the tensor product of matrices mat1 and mat2 and stores it in this matrix...
double max() const
Returns the maximum element value of the matrix.
void mpiSum(const MpiComm &comm, TeuchosMatrix &M_global) const
MonteCarloSGOptions::MonteCarloSGOptions(const BaseEnvironment &env, const char *prefix, const McOptionsValues &alternativeOptionsValues queso_require_equal_to_msg)(m_env.optionsInputFileName(), std::string(""), std::string("this constructor is incompatible with the existence of an options input file"))
double lnDeterminant() const
Calculates the ln(determinant) of this matrix.
double norm2() const
Returns the 2-norm (Euclidean norm) of the vector.
virtual void base_copy(const Matrix &src)
Copies base data from matrix src to this matrix.
unsigned int numCols() const
Returns the column dimension of this matrix.
The QUESO MPI Communicator Class.
void print(std::ostream &os) const
Print method. Defines the behavior of the ostream << operator inherited from the Object class...
void subReadContents(const std::string &fileName, const std::string &fileType, const std::set< unsigned int > &allowedSubEnvIds)
Read contents of subenvironment from file fileName.
TeuchosMatrix & operator/=(double a)
Stores in this the coordinate-wise division of this by a.
void copy(const TeuchosMatrix &src)
In this function this matrix receives a copy of matrix src.
double & operator()(unsigned int i, unsigned int j)
Element access method (non-const).
unsigned int displayVerbosity() const
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.
std::ofstream * ofsVar
Provides a stream interface to write data to files.
GslMatrix leftDiagScaling(const GslVector &vec, const GslMatrix &mat)
const BaseEnvironment & m_env
QUESO environment variable.
int svdSolve(const TeuchosVector &rhsVec, TeuchosVector &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 TeuchosMatrix::svd (x=solVec, b=rhsVec).
Struct for handling data input and output from files.
unsigned int rank(double absoluteZeroThreshold, double relativeZeroThreshold) const
This function returns the number of singular values of this matrix (rank).
GslMatrix rightDiagScaling(const GslMatrix &mat, const GslVector &vec)
void smallestEigen(double &eigenValue, TeuchosVector &eigenVector) const
This function finds smallest eigenvalue, namely eigenValue, of this matrix and its corresponding eige...
void invertMultiply(const TeuchosVector &b, TeuchosVector &x) const
This function calculates the inverse of this matrix, multiplies it with vector b and stores the resul...
bool m_inDebugMode
Flag for either or not QUESO is in debug mode.
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...
Class for matrix operations using Teuchos (Trilinos).
GslMatrix operator+(const GslMatrix &m1, const GslMatrix &m2)
std::ofstream * subDisplayFile() const
Access function for m_subDisplayFile (displays file on stream).
void filterLargeValues(double thresholdValue)
This function sets to zero (filters) all entries of this matrix which are greater than thresholdValue...
void fillWithBlocksDiagonally(const std::vector< const TeuchosMatrix * > &matrices)
This function fills this matrix diagonally with const block matrices.