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
42 TeuchosMatrix::TeuchosMatrix()
49 "TeuchosMatrix::constructor(), default",
50 "should not be used by user");
55 TeuchosMatrix::TeuchosMatrix(
56 const BaseEnvironment& env,
67 m_determinant (-INFINITY),
68 m_lnDeterminant(-INFINITY),
73 m_mat.shape(map.NumGlobalElements(),nCols);
79 TeuchosMatrix::TeuchosMatrix(
80 const BaseEnvironment& env,
91 m_determinant (-INFINITY),
92 m_lnDeterminant(-INFINITY),
97 m_mat.shape (map.NumGlobalElements(),map.NumGlobalElements());
100 for (
unsigned int i = 0; i < (
unsigned int) m_mat.numRows(); ++i) {
101 m_mat(i,i) = diagValue;
107 TeuchosMatrix::TeuchosMatrix(
108 const TeuchosVector& v,
111 Matrix (v.env(),v.map()),
118 m_determinant (-INFINITY),
119 m_lnDeterminant(-INFINITY),
124 m_mat.shape (v.sizeLocal(),v.sizeLocal());
127 for (
unsigned int i = 0; i < (
unsigned int) m_mat.numRows(); ++i) {
128 m_mat(i,i) = diagValue;
135 TeuchosMatrix::TeuchosMatrix(
const TeuchosVector& v)
137 Matrix (v.env(),v.map()),
144 m_determinant (-INFINITY),
145 m_lnDeterminant(-INFINITY),
150 m_mat.shape (v.sizeLocal(),v.sizeLocal());
153 unsigned int dim = v.sizeLocal();
155 for (
unsigned int i = 0; i < dim; ++i) {
162 TeuchosMatrix::TeuchosMatrix(
const TeuchosMatrix& B)
164 Matrix (B.env(),B.map()),
171 m_determinant (-INFINITY),
172 m_lnDeterminant(-INFINITY),
177 m_mat.shape (B.numRowsLocal(),B.numCols());
186 TeuchosMatrix::~TeuchosMatrix()
195 TeuchosMatrix& TeuchosMatrix::operator=(
const TeuchosMatrix& obj)
204 TeuchosMatrix& TeuchosMatrix::operator*=(
double a)
208 iRC = m_mat.scale(a);
211 "TeuchosMatrix::operator*=()",
218 TeuchosMatrix& TeuchosMatrix::operator/=(
double a)
227 TeuchosMatrix& TeuchosMatrix::operator+=(
const TeuchosMatrix& rhs)
231 unsigned int i,j, nrows=rhs.numRowsLocal(), ncols=rhs.numCols();
233 for(i=0; i< nrows ; i++)
234 for (j = 0; j < ncols; j++)
235 (*
this)(i,j) += rhs(i,j);
242 TeuchosMatrix::operator-=(
const TeuchosMatrix& rhs)
246 unsigned int i,j, nrows=rhs.numRowsLocal(), ncols=rhs.numCols();
248 for(i=0; i< nrows ; i++)
249 for (j = 0; j < ncols; j++)
250 (*
this)(i,j) -= rhs(i,j);
259 double& TeuchosMatrix::operator()(
unsigned int i,
unsigned int j)
262 if ((i >= (
unsigned int) m_mat.numRows()) ||
263 (j >= (
unsigned int) m_mat.numCols())) {
264 std::cerr <<
"In TeuchosMatrix::operator(i,j) (non-const)"
267 <<
", m_mat.numRows() = " << (
unsigned int) m_mat.numRows()
268 <<
", m_mat.numCols() = " << (
unsigned int) m_mat.numCols()
272 "TeuchosMatrix::operator(i,j)",
276 "TeuchosMatrix::operator(i,j)",
283 const double& TeuchosMatrix::operator()(
unsigned int i,
unsigned int j)
const
287 "TeuchosMatrix::operator(i,j) const",
291 "TeuchosMatrix::operator(i,j) const",
301 TeuchosMatrix::numRowsLocal()
const
303 return (
unsigned int) m_mat.numRows();
309 TeuchosMatrix::numRowsGlobal()
const
311 return (
unsigned int) m_mat.numRows();
317 TeuchosMatrix::numCols()
const
319 return (
unsigned int) m_mat.numCols();
325 TeuchosMatrix::values()
327 return m_mat.values();
333 TeuchosMatrix::stride()
335 return m_mat.stride();
342 TeuchosMatrix::max()
const
344 double value = -INFINITY;
346 unsigned int nRows = this->numRowsLocal();
347 unsigned int nCols = this->numCols();
349 for (
unsigned int i = 0; i < nRows; i++) {
350 for (
unsigned int j = 0; j < nCols; j++) {
352 if (aux > value) value = aux;
362 TeuchosMatrix::rank(
double absoluteZeroThreshold,
double relativeZeroThreshold)
const
368 TeuchosVector relativeVec(*m_svdSvec);
369 if (relativeVec[0] > 0.) {
370 relativeVec = (1./relativeVec[0])*relativeVec;
373 unsigned int rankValue = 0;
374 for (
unsigned int i = 0; i < relativeVec.sizeLocal(); ++i) {
375 if (( (*m_svdSvec)[i] >= absoluteZeroThreshold ) &&
376 ( relativeVec [i] >= relativeZeroThreshold )) {
381 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 3)) {
382 *m_env.subDisplayFile() <<
"In TeuchosMatrix::rank()"
383 <<
": this->numRowsLocal() = " << this->numRowsLocal()
384 <<
", this->numCols() = " << this->numCols()
385 <<
", absoluteZeroThreshold = " << absoluteZeroThreshold
386 <<
", relativeZeroThreshold = " << relativeZeroThreshold
387 <<
", rankValue = " << rankValue
388 <<
", m_svdSvec = " << *m_svdSvec
389 <<
", relativeVec = " << relativeVec
399 TeuchosMatrix::transpose()
const
401 unsigned int nRows = this->numRowsLocal();
402 unsigned int nCols = this->numCols();
406 "TeuchosMatrix::transpose()",
407 "routine works only for square matrices");
409 TeuchosMatrix mat(m_env,m_map,nCols);
410 for (
unsigned int row = 0; row < nRows; ++row) {
411 for (
unsigned int col = 0; col < nCols; ++col) {
412 mat(row,col) = (*this)(col,row);
422 TeuchosMatrix::inverse()
const
424 unsigned int nRows = this->numRowsLocal();
425 unsigned int nCols = this->numCols();
429 "TeuchosMatrix::inverse()",
430 "matrix is not square");
432 if (m_inverse == NULL) {
433 m_inverse =
new TeuchosMatrix(m_env,m_map,nCols);
434 TeuchosVector unitVector(m_env,m_map);
435 unitVector.cwSet(0.);
436 TeuchosVector multVector(m_env,m_map);
437 for (
unsigned int j = 0; j < nCols; ++j) {
438 if (j > 0) unitVector[j-1] = 0.;
440 this->invertMultiply(unitVector, multVector);
441 for (
unsigned int i = 0; i < nRows; ++i) {
442 (*m_inverse)(i,j) = multVector[i];
446 if (m_env.checkingLevel() >= 1) {
447 *m_env.subDisplayFile() <<
"CHECKING In TeuchosMatrix::inverse()"
448 <<
": M.lnDet = " << this->lnDeterminant()
449 <<
", M^{-1}.lnDet = " << m_inverse->lnDeterminant()
452 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
453 *m_env.subDisplayFile() <<
"In TeuchosMatrix::inverse():"
454 <<
"\n M = " << *
this
455 <<
"\n M^{-1} = " << *m_inverse
456 <<
"\n M*M^{-1} = " << (*this)*(*m_inverse)
457 <<
"\n M^{-1}*M = " << (*m_inverse)*(*this)
475 TeuchosMatrix::determinant()
const
477 if (m_determinant == -INFINITY)
479 if(m_LU.numRows() ==0 && m_LU.numCols() ==0)
481 TeuchosVector tmpB(m_env,m_map);
482 TeuchosVector tmpX(m_env,m_map);
483 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 99)) {
484 *m_env.subDisplayFile() <<
"In TeuchosMatrix::lnDeterminant()"
485 <<
": before 'this->invertMultiply()'"
488 this->invertMultiply(tmpB,tmpX);
489 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 99)) {
490 *m_env.subDisplayFile() <<
"In TeuchosMatrix::lnDeterminant()"
491 <<
": after 'this->invertMultiply()'"
495 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 99)) {
496 *m_env.subDisplayFile() <<
"In TeuchosMatrix::lnDeterminant()"
497 <<
": before computing det"
503 for (
int i=0;i<m_LU.numCols();i++) {
505 lnDet += std::log(m_LU(i,i));
509 m_lnDeterminant = lnDet;
511 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 99)) {
512 *m_env.subDisplayFile() <<
"In TeuchosMatrix::lnDeterminant()"
513 <<
": after computing det"
518 return m_determinant;
524 TeuchosMatrix::lnDeterminant()
const
526 if (m_lnDeterminant == -INFINITY)
528 if(m_LU.numRows() ==0 && m_LU.numCols() ==0)
530 TeuchosVector tmpB(m_env,m_map);
531 TeuchosVector tmpX(m_env,m_map);
532 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 99)) {
533 *m_env.subDisplayFile() <<
"In TeuchosMatrix::lnDeterminant()"
534 <<
": before 'this->invertMultiply()'"
537 this->invertMultiply(tmpB,tmpX);
538 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 99)) {
539 *m_env.subDisplayFile() <<
"In TeuchosMatrix::lnDeterminant()"
540 <<
": after 'this->invertMultiply()'"
544 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 99)) {
545 *m_env.subDisplayFile() <<
"In TeuchosMatrix::lnDeterminant()"
546 <<
": before computing lnDet"
552 for (
int i=0;i<m_LU.numCols();i++) {
554 lnDet += std::log(m_LU(i,i));
558 m_lnDeterminant = lnDet;
560 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 99)) {
561 *m_env.subDisplayFile() <<
"In TeuchosMatrix::lnDeterminant()"
562 <<
": after computing lnDet"
567 return m_lnDeterminant;
574 TeuchosMatrix::normFrob()
const
576 return m_mat.normFrobenius ();
581 TeuchosMatrix::normMax()
const
585 unsigned int nRows = this->numRowsLocal();
586 unsigned int nCols = this->numCols();
588 for (
unsigned int i = 0; i < nRows; i++) {
589 for (
unsigned int j = 0; j < nCols; j++) {
590 aux = fabs((*
this)(i,j));
591 if (aux > value) value = aux;
601 int TeuchosMatrix::chol()
603 int return_success =0 ;
609 Teuchos::LAPACK<int, double> lapack;
619 lapack.POTRF (UPLO, m_mat.numRows(), m_mat.values(), m_mat.stride(), &info);
624 for (
int i=0;i<m_mat.numRows();i++){
625 for (
int j=i+1;j<m_mat.numCols();j++)
626 m_mat(i,j) = m_mat(j,i) ;
630 std::cerr <<
"In TeuchosMtrix::chol()"
631 <<
": INFO = " << info
632 <<
",\n INFO < 0: if INFO = -i, the i-th argument had an illegal value."
633 <<
"\n INFO > 0: if INFO = i, the leading minor of order i is not "
634 <<
" positive definite, and the factorization could not be completed."
641 "TeuchosMatrix::chol()",
642 "matrix is not positive definite",
645 return return_success;
650 TeuchosMatrix::svd(TeuchosMatrix& matU, TeuchosVector& vecS, TeuchosMatrix& matVt)
const
652 unsigned int nRows = this->numRowsLocal();
653 unsigned int nCols = this->numCols();
657 "TeuchosMatrix::svd()",
662 "TeuchosMatrix::svd()",
667 "TeuchosMatrix::svd()",
670 int iRC = internalSvd();
681 const TeuchosMatrix& TeuchosMatrix::svdMatU()
const
691 const TeuchosMatrix& TeuchosMatrix::svdMatV()
const
709 TeuchosMatrix::svdSolve(
const TeuchosVector& rhsVec, TeuchosVector& solVec)
const
711 unsigned int nRows = this->numRowsLocal();
712 unsigned int nCols = this->numCols();
717 "TeuchosMatrix::svdSolve()",
722 "TeuchosMatrix::svdSolve()",
725 int iRC = internalSvd();
727 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
728 *m_env.subDisplayFile() <<
"In TeuchosMatrix::svdSolve():"
729 <<
"\n this->numRowsLocal() = " << this->numRowsLocal()
730 <<
", this->numCols() = " << this->numCols()
731 <<
"\n m_svdUmat->numRowsLocal() = " << m_svdUmat->numRowsLocal()
732 <<
", m_svdUmat->numCols() = " << m_svdUmat->numCols()
733 <<
"\n m_svdVmat->numRowsLocal() = " << m_svdVmat->numRowsLocal()
734 <<
", m_svdVmat->numCols() = " << m_svdVmat->numCols()
735 <<
"\n m_svdSvec->sizeLocal() = " << m_svdSvec->sizeLocal()
736 <<
"\n rhsVec.sizeLocal() = " << rhsVec.sizeLocal()
737 <<
"\n solVec.sizeLocal() = " << solVec.sizeLocal()
743 TeuchosMatrix invD = TeuchosMatrix(solVec);
744 TeuchosMatrix auxMatrix = TeuchosMatrix(solVec);
746 for (i=0; i<nRows; i++){
747 invD(i,i) = 1./(m_svdSvec->values()[i]);
756 auxMatrix = invD * svdMatU().transpose();
757 solVec = auxMatrix*rhsVec;
762 TeuchosMatrix* aux_m_svdVTmat =
new TeuchosMatrix(*m_svdVTmat);
765 Teuchos::LAPACK<int, double> lapack;
766 lapack.GESV(nCols, 1, aux_m_svdVTmat->values(), aux_m_svdVTmat->stride(), ipiv, &solVec[0], solVec.sizeLocal(), &info );
775 delete aux_m_svdVTmat;
783 TeuchosMatrix::svdSolve(
const TeuchosMatrix& rhsMat, TeuchosMatrix& solMat)
const
785 unsigned int nRows = this->numRowsLocal();
786 unsigned int nCols = this->numCols();
790 "TeuchosMatrix::svdSolve()",
795 "TeuchosMatrix::svdSolve()",
800 "TeuchosMatrix::svdSolve()",
801 "rhsMat and solMat are not compatible");
803 TeuchosVector rhsVec(m_env,rhsMat.map());
804 TeuchosVector solVec(m_env,solMat.map());
806 for (
unsigned int j = 0; j < rhsMat.numCols(); ++j) {
807 rhsVec = rhsMat.getColumn(j);
808 iRC = this->svdSolve(rhsVec, solVec);
810 solMat.setColumn(j,solVec);
822 TeuchosMatrix::multiply(
const TeuchosVector& x)
const
826 "TeuchosMatrix::multiply(), return vector",
827 "matrix and vector have incompatible sizes");
829 TeuchosVector y(m_env,m_map);
838 TeuchosMatrix::invertMultiply(
const TeuchosVector& b)
const
842 "TeuchosMatrix::invertMultiply(), return vector",
843 "matrix and rhs have incompatible sizes");
844 TeuchosVector x(m_env,m_map);
846 this->invertMultiply(b,x);
854 TeuchosMatrix::invertMultiply(
const TeuchosVector& b, TeuchosVector& x)
const
858 "TeuchosMatrix::invertMultiply(), return void",
859 "matrix and rhs have incompatible sizes");
863 "TeuchosMatrix::invertMultiply(), return void",
864 "solution and rhs have incompatible sizes");
866 if (m_LU.numCols() == 0 && m_LU.numRows() == 0)
870 "TeuchosMatrix::invertMultiply()",
871 "v_pivoting should be NULL");
875 v_pivoting =(
int *) malloc(
sizeof(
int)*m_LU.numCols() );
879 "TeuchosMatrix::invertMultiply()",
884 "TeuchosMatrix::invertMultiply()",
888 std::cout <<
"In TeuchosMatrix::invertMultiply()"
889 <<
": before LU decomposition, m_LU = ";
890 for (
int i=0;i<3;i++){
891 for (
int j=0;j<3;j++)
892 cout << m_LU(i,j) <<
"\t" ;
898 Teuchos::LAPACK<int, double> lapack;
901 lapack.GETRF( m_LU.numRows(), m_LU.numCols(), m_LU.values(), m_LU.stride(), v_pivoting, &info );
904 std::cerr <<
"In TeuchosMatrix::invertMultiply()"
905 <<
", after lapack.GETRF"
906 <<
": INFO = " << info
907 <<
",\nINFO < 0: if INFO = -i, the i-th argument had an illegal value.\n"
908 <<
"INFO > 0: if INFO = i, U(i,i) is exactly zero. The factorization \n"
909 <<
"has been completed, but the factor U is exactly singular, and division \n"
910 <<
"by zero will occur if it is used to solve a system of equations."
915 "TeuchosMatrix::invertMultiplyForceLU()",
922 std::cout <<
"In TeuchosMatrix::invertMultiply()"
923 <<
": after LU decomposition, m_LU = ";
924 for (
int i=0;i<3;i++){
925 for (
int j=0;j<3;j++)
926 cout << m_LU(i,j) <<
"\t" ;
929 std::cout << std::endl;
932 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 99)) {
933 *m_env.subDisplayFile() <<
"In TeuchosMatrix::invertMultiply()"
934 <<
": before 'lapack.GETRS()'"
939 Teuchos::LAPACK<int, double> lapack;
950 lapack.GETRS(TRANS, m_LU.numRows(), NRHS, m_LU.values(), m_LU.stride(), v_pivoting, &x[0],x.sizeLocal(), &info02 );
953 std::cerr <<
"In TeuchosMatrix::invertMultiply()"
954 <<
", after lapack.GETRS - solve LU system"
955 <<
": INFO = " << info02
956 <<
",\nINFO < 0: if INFO = -i, the i-th argument had an illegal value.\n"
961 "TeuchosMatrix::invertMultiplyForceLU()",
963 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 99)) {
964 *m_env.subDisplayFile() <<
"In TeuchosMatrix::invertMultiply()"
965 <<
", after lapack.GETRS() - solve LU system."
969 TeuchosVector tmpVec(b - (*
this)*x);
970 std::cout <<
"In TeuchosMatrix::invertMultiply()"
971 <<
": ||b - Ax||_2 = " << tmpVec.norm2()
972 <<
": ||b - Ax||_2/||b||_2 = " << tmpVec.norm2()/b.norm2()
980 TeuchosMatrix::invertMultiply(
const TeuchosMatrix& B)
const
982 TeuchosMatrix X(m_env,m_map,B.numCols());
983 this->invertMultiply(B,X);
990 TeuchosMatrix::invertMultiply(
const TeuchosMatrix& B, TeuchosMatrix& X)
const
993 UQ_FATAL_RC_MACRO(((B.numRowsLocal() != X.numRowsLocal()) || (B.numCols() != X.numCols())),
995 "TeuchosMatrix::invertMultiply()",
996 "Matrices B and X are incompatible");
1000 "TeuchosMatrix::invertMultiply()",
1001 "This and X matrices are incompatible");
1004 TeuchosVector b(m_env, m_map);
1005 TeuchosVector x(m_env, m_map);
1007 for(
unsigned int j = 0; j < B.numCols(); ++j ) {
1010 x = this->invertMultiply(b);
1018 TeuchosMatrix::invertMultiplyForceLU(
const TeuchosVector& b)
const
1022 "TeuchosMatrix::invertMultiply(), return vector",
1023 "matrix and rhs have incompatible sizes");
1025 TeuchosVector x(m_env,m_map);
1026 this->invertMultiplyForceLU(b,x);
1034 TeuchosMatrix::invertMultiplyForceLU(
const TeuchosVector& b, TeuchosVector& x)
const
1038 "TeuchosMatrix::invertMultiply(), return void",
1039 "matrix and rhs have incompatible sizes");
1043 "TeuchosMatrix::invertMultiply(), return void",
1044 "solution and rhs have incompatible sizes");
1046 if (m_LU.numCols() == 0 && m_LU.numRows() == 0) {
1049 "TeuchosMatrix::invertMultiply()",
1050 "v_pivoting should be NULL");
1058 "TeuchosMatrix::invertMultiply()",
1059 "Teuchos atttribuition m_LU = m_mat failed");
1062 if ( v_pivoting == NULL )
1063 v_pivoting =(
int *) malloc(
sizeof(
int)*m_LU.numCols() );
1067 "TeuchosMatrix::invertMultiply()",
1068 "malloc() for v_pivoting failed");
1071 Teuchos::LAPACK<int, double> lapack;
1074 lapack.GETRF( m_LU.numRows(), m_LU.numCols(), m_LU.values(), m_LU.stride(), v_pivoting, &info );
1077 std::cerr <<
"In TeuchosMatrix::invertMultiply()"
1078 <<
", after lapack.GETRF"
1079 <<
": INFO = " << info
1080 <<
",\nINFO < 0: if INFO = -i, the i-th argument had an illegal value.\n"
1081 <<
"INFO > 0: if INFO = i, U(i,i) is exactly zero. The factorization \n"
1082 <<
"has been completed, but the factor U is exactly singular, and division \n"
1083 <<
"by zero will occur if it is used to solve a system of equations."
1089 "TeuchosMatrix::invertMultiplyForceLU()",
1093 m_isSingular =
true;
1106 lapack.GETRS(TRANS, m_LU.numRows(), NRHS, m_LU.values(), m_LU.stride(), v_pivoting, &x[0],x.sizeLocal(), &info02 );
1109 std::cerr <<
"In TeuchosMatrix::invertMultiply()"
1110 <<
", after lapack.GETRS - solve LU system"
1111 <<
": INFO = " << info02
1112 <<
",\nINFO < 0: if INFO = -i, the i-th argument had an illegal value.\n"
1118 "TeuchosMatrix::invertMultiplyForceLU()",
1153 TeuchosMatrix::eigen(TeuchosVector& eigenValues, TeuchosMatrix* eigenVectors)
const
1155 unsigned int n = eigenValues.sizeLocal();
1159 "TeuchosMatrix::eigen()",
1160 "invalid input vector size");
1165 "TeuchosVector::eigen()",
1166 "different input vector sizes");
1171 Teuchos::SerialDenseMatrix<int,double> copy_m_mat;
1174 Teuchos::LAPACK<int, double> lapack;
1181 WORK =
new double[lwork];
1183 if (eigenVectors == NULL) {
1186 lapack.SYEV(JOBZ, UPLO, copy_m_mat.numRows(), copy_m_mat.values(), copy_m_mat.stride(), &W[0], &WORK[0], lwork, &info);
1188 for (
unsigned int i=0; i< n; i++)
1189 eigenValues[i] = W[i];
1193 "TeuchosMatrix::eigen()",
1194 "invalid input vector size");
1199 lapack.SYEV(JOBZ, UPLO, copy_m_mat.numRows(), copy_m_mat.values(), copy_m_mat.stride(), &W[0], &WORK[0], lwork, &info);
1201 for (
unsigned int i=0; i< n; i++)
1202 eigenValues[i] = W[i];
1204 eigenVectors->m_mat = copy_m_mat;
1208 std::cerr <<
"In TeuchosMtrix::eigen()"
1209 <<
": INFO = " << info
1210 <<
",\n INFO < 0: if INFO = -i, the i-th argument had an illegal value."
1211 <<
"\n INFO > 0: if INFO = i, the algorithm failed to converge; i off-diagonal "
1212 <<
" elements of an intermediate tridiagonal form did not converge to zero."
1220 TeuchosMatrix::largestEigen(
double& eigenValue, TeuchosVector& eigenVector)
const
1222 unsigned int n = eigenVector.sizeLocal();
1226 "TeuchosMatrix::largestEigen()",
1227 "invalid input vector size");
1228 Teuchos::LAPACK<int, double> lapack;
1231 Teuchos::SerialDenseMatrix<int,double> copy_m_mat;
1241 WORK =
new double[lwork];
1243 lapack.SYEV(JOBZ, UPLO, copy_m_mat.numRows(), copy_m_mat.values(), copy_m_mat.stride(), &W[0], &WORK[0], lwork, &info);
1246 std::cerr <<
"In TeuchosMtrix::largestEigen()"
1247 <<
": INFO = " << info
1248 <<
",\n INFO < 0: if INFO = -i, the i-th argument had an illegal value."
1249 <<
"\n INFO > 0: if INFO = i, the algorithm failed to converge; i off-diagonal "
1250 <<
" elements of an intermediate tridiagonal form did not converge to zero."
1255 eigenValue = W[n-1];
1259 for (
int i=0; i< copy_m_mat.numRows(); i++)
1260 eigenVector[i] = copy_m_mat(i,n-1);
1267 TeuchosMatrix::smallestEigen(
double& eigenValue, TeuchosVector& eigenVector)
const
1269 unsigned int n = eigenVector.sizeLocal();
1273 "TeuchosMatrix::smallestEigen()",
1274 "invalid input vector size");
1275 Teuchos::LAPACK<int, double> lapack;
1278 Teuchos::SerialDenseMatrix<int,double> copy_m_mat;
1288 WORK =
new double[lwork];
1290 lapack.SYEV(JOBZ, UPLO, copy_m_mat.numRows(), copy_m_mat.values(), copy_m_mat.stride(), &W[0], &WORK[0], lwork, &info);
1293 std::cerr <<
"In TeuchosMtrix::smallestEigen()"
1294 <<
": INFO = " << info
1295 <<
",\n INFO < 0: if INFO = -i, the i-th argument had an illegal value."
1296 <<
"\n INFO > 0: if INFO = i, the algorithm failed to converge; i off-diagonal "
1297 <<
" elements of an intermediate tridiagonal form did not converge to zero."
1307 for (
int i=0; i< copy_m_mat.numRows(); i++)
1308 eigenVector[i] = copy_m_mat(i,0);
1318 TeuchosMatrix::cwSet(
double value)
1320 m_mat.putScalar(value);
1327 TeuchosMatrix::cwSet(
unsigned int rowId,
unsigned int colId,
const TeuchosMatrix& mat)
1331 "TeuchosMatrix::cwSet()",
1336 "TeuchosMatrix::cwSet()",
1337 "invalid vec.numRowsLocal()");
1341 "TeuchosMatrix::cwSet()",
1346 "TeuchosMatrix::cwSet()",
1347 "invalid vec.numCols()");
1349 for (
unsigned int i = 0; i < mat.numRowsLocal(); ++i) {
1350 for (
unsigned int j = 0; j < mat.numCols(); ++j) {
1351 (*this)(rowId+i,colId+j) = mat(i,j);
1361 TeuchosMatrix::getColumn(
unsigned int column_num, TeuchosVector& column)
const
1366 "TeuchosMatrix::getColumn",
1367 "Specified row number not within range");
1371 "TeuchosMatrix::getColumn",
1372 "column vector not same size as this matrix");
1375 const double* temp_ptr ;
1378 temp_ptr = m_mat[column_num];
1381 for (
unsigned int i=0; i< column.sizeLocal();i++)
1382 column[i] = temp_ptr[i];
1389 TeuchosMatrix::getColumn(
unsigned int column_num )
const
1393 TeuchosVector column(m_env, m_map);
1394 this->getColumn( column_num, column );
1402 TeuchosMatrix::setColumn(
unsigned int column_num,
const TeuchosVector& column)
1408 "TeuchosMatrix::setColumn",
1409 "Specified column number not within range");
1413 "TeuchosMatrix::setColumn",
1414 "column vector not same size as this matrix");
1416 for (
unsigned int i =0; i < column.sizeLocal(); i++)
1417 m_mat(i,column_num) = column[i];
1425 TeuchosMatrix::getRow(
unsigned int row_num, TeuchosVector& row)
const
1430 "TeuchosMatrix::getRow",
1431 "Specified row number not within range");
1435 "TeuchosMatrix::getRow",
1436 "row vector not same size as this matrix");
1439 for (
unsigned int i=0; i< row.sizeLocal();i++)
1440 row[i] = m_mat(row_num,i);
1448 TeuchosMatrix::getRow(
unsigned int row_num )
const
1452 TeuchosVector row(m_env, m_map);
1453 this->getRow( row_num, row );
1460 TeuchosMatrix::setRow (
const unsigned int row_num,
const TeuchosVector& row)
1465 "TeuchosMatrix::getRow",
1466 "Specified row number not within range");
1470 "TeuchosMatrix::getRow",
1471 "row vector not same size as this matrix");
1474 for (
unsigned int i=0; i< row.sizeLocal();i++)
1475 m_mat(row_num,i) = row[i] ;
1483 TeuchosMatrix::zeroLower(
bool includeDiagonal)
1485 unsigned int nRows = this->numRowsLocal();
1486 unsigned int nCols = this->numCols();
1490 "TeuchosMatrix::zeroLower()",
1491 "routine works only for square matrices");
1494 if (includeDiagonal) {
1495 for (
unsigned int i = 0; i < nRows; i++) {
1496 for (
unsigned int j = 0; j <= i; j++) {
1502 for (
unsigned int i = 0; i < nRows; i++) {
1503 for (
unsigned int j = 0; j < i; j++) {
1515 TeuchosMatrix::zeroUpper(
bool includeDiagonal)
1517 unsigned int nRows = this->numRowsLocal();
1518 unsigned int nCols = this->numCols();
1522 "TeuchosMatrix::zeroUpper()",
1523 "routine works only for square matrices");
1526 if (includeDiagonal) {
1527 for (
unsigned int i = 0; i < nRows; i++) {
1528 for (
unsigned int j = i; j < nCols; j++) {
1534 for (
unsigned int i = 0; i < nRows; i++) {
1535 for (
unsigned int j = (i+1); j < nCols; j++) {
1546 TeuchosMatrix::filterSmallValues(
double thresholdValue)
1548 unsigned int nRows = this->numRowsLocal();
1549 unsigned int nCols = this->numCols();
1551 for (
unsigned int i = 0; i < nRows; ++i) {
1552 for (
unsigned int j = 0; j < nCols; ++j) {
1553 double aux = (*this)(i,j);
1555 if ((aux < 0. ) && (-thresholdValue < aux)) {
1558 if ((aux > 0. ) && (thresholdValue > aux)) {
1568 TeuchosMatrix::filterLargeValues(
double thresholdValue)
1570 unsigned int nRows = this->numRowsLocal();
1571 unsigned int nCols = this->numCols();
1573 for (
unsigned int i = 0; i < nRows; ++i) {
1574 for (
unsigned int j = 0; j < nCols; ++j) {
1575 double aux = (*this)(i,j);
1577 if ( (aux < 0. ) && (-thresholdValue > aux))
1580 if ((aux > 0. ) && (thresholdValue < aux))
1591 TeuchosMatrix::fillWithTranspose(
const TeuchosMatrix& mat)
1593 unsigned int nRows = mat.numRowsLocal();
1594 unsigned int nCols = mat.numCols();
1597 "TeuchosMatrix::fillWithTranspose()",
1598 "inconsistent local number of rows");
1601 "TeuchosMatrix::fillWithTranspose()",
1602 "inconsistent number of cols");
1604 for (
unsigned int row = 0; row < nRows; ++row) {
1605 for (
unsigned int col = 0; col < nCols; ++col) {
1606 (*this)(col,row) = mat(row,col);
1616 TeuchosMatrix::fillWithBlocksDiagonally(
const std::vector<const TeuchosMatrix* >& matrices)
1618 unsigned int sumNumRowsLocals = 0;
1619 unsigned int sumNumCols = 0;
1620 for (
unsigned int i = 0; i < matrices.size(); ++i) {
1621 sumNumRowsLocals += matrices[i]->numRowsLocal();
1622 sumNumCols += matrices[i]->numCols();
1626 "TeuchosMatrix::fillWithBlocksDiagonally(const)",
1627 "inconsistent local number of rows");
1630 "TeuchosMatrix::fillWithBlocksDiagonally(const)",
1631 "inconsistent number of cols");
1633 unsigned int cumulativeRowId = 0;
1634 unsigned int cumulativeColId = 0;
1635 for (
unsigned int i = 0; i < matrices.size(); ++i) {
1636 unsigned int nRows = matrices[i]->numRowsLocal();
1637 unsigned int nCols = matrices[i]->numCols();
1638 for (
unsigned int rowId = 0; rowId < nRows; ++rowId) {
1639 for (
unsigned int colId = 0; colId < nCols; ++colId) {
1640 (*this)(cumulativeRowId + rowId, cumulativeColId + colId) = (*(matrices[i]))(rowId,colId);
1643 cumulativeRowId += nRows;
1644 cumulativeColId += nCols;
1652 TeuchosMatrix::fillWithBlocksDiagonally(
const std::vector<TeuchosMatrix* >& matrices)
1654 unsigned int sumNumRowsLocals = 0;
1655 unsigned int sumNumCols = 0;
1656 for (
unsigned int i = 0; i < matrices.size(); ++i) {
1657 sumNumRowsLocals += matrices[i]->numRowsLocal();
1658 sumNumCols += matrices[i]->numCols();
1662 "TeuchosMatrix::fillWithBlocksDiagonally()",
1663 "inconsistent local number of rows");
1666 "TeuchosMatrix::fillWithBlocksDiagonally()",
1667 "inconsistent number of cols");
1669 unsigned int cumulativeRowId = 0;
1670 unsigned int cumulativeColId = 0;
1671 for (
unsigned int i = 0; i < matrices.size(); ++i) {
1672 unsigned int nRows = matrices[i]->numRowsLocal();
1673 unsigned int nCols = matrices[i]->numCols();
1674 for (
unsigned int rowId = 0; rowId < nRows; ++rowId) {
1675 for (
unsigned int colId = 0; colId < nCols; ++colId) {
1676 (*this)(cumulativeRowId + rowId, cumulativeColId + colId) = (*(matrices[i]))(rowId,colId);
1679 cumulativeRowId += nRows;
1680 cumulativeColId += nCols;
1688 TeuchosMatrix::fillWithBlocksHorizontally(
const std::vector<const TeuchosMatrix* >& matrices)
1690 unsigned int sumNumCols = 0;
1691 for (
unsigned int i = 0; i < matrices.size(); ++i) {
1694 "TeuchosMatrix::fillWithBlocksHorizontally(const)",
1695 "inconsistent local number of rows");
1696 sumNumCols += matrices[i]->numCols();
1700 "TeuchosMatrix::fillWithBlocksHorizontally(const)",
1701 "inconsistent number of cols");
1703 unsigned int cumulativeColId = 0;
1704 for (
unsigned int i = 0; i < matrices.size(); ++i) {
1705 unsigned int nRows = matrices[i]->numRowsLocal();
1706 unsigned int nCols = matrices[i]->numCols();
1707 for (
unsigned int rowId = 0; rowId < nRows; ++rowId) {
1708 for (
unsigned int colId = 0; colId < nCols; ++colId) {
1709 (*this)(rowId, cumulativeColId + colId) = (*(matrices[i]))(rowId,colId);
1712 cumulativeColId += nCols;
1721 TeuchosMatrix::fillWithBlocksHorizontally(
const std::vector<TeuchosMatrix* >& matrices)
1723 unsigned int sumNumCols = 0;
1724 for (
unsigned int i = 0; i < matrices.size(); ++i) {
1727 "TeuchosMatrix::fillWithBlocksHorizontally()",
1728 "inconsistent local number of rows");
1729 sumNumCols += matrices[i]->numCols();
1733 "TeuchosMatrix::fillWithBlocksHorizontally()",
1734 "inconsistent number of cols");
1736 unsigned int cumulativeColId = 0;
1737 for (
unsigned int i = 0; i < matrices.size(); ++i) {
1738 unsigned int nRows = matrices[i]->numRowsLocal();
1739 unsigned int nCols = matrices[i]->numCols();
1740 for (
unsigned int rowId = 0; rowId < nRows; ++rowId) {
1741 for (
unsigned int colId = 0; colId < nCols; ++colId) {
1742 (*this)(rowId, cumulativeColId + colId) = (*(matrices[i]))(rowId,colId);
1745 cumulativeColId += nCols;
1754 TeuchosMatrix::fillWithBlocksVertically(
const std::vector<const TeuchosMatrix* >& matrices)
1756 unsigned int sumNumRows = 0;
1757 for (
unsigned int i = 0; i < matrices.size(); ++i) {
1760 "TeuchosMatrix::fillWithBlocksVertically(const)",
1761 "inconsistent local number of cols");
1762 sumNumRows += matrices[i]->numRowsLocal();
1766 "TeuchosMatrix::fillWithBlocksVertically(const)",
1767 "inconsistent number of rows");
1769 unsigned int cumulativeRowId = 0;
1770 for (
unsigned int i = 0; i < matrices.size(); ++i) {
1771 unsigned int nRows = matrices[i]->numRowsLocal();
1772 unsigned int nCols = matrices[i]->numCols();
1773 for (
unsigned int rowId = 0; rowId < nRows; ++rowId) {
1774 for (
unsigned int colId = 0; colId < nCols; ++colId) {
1775 (*this)(cumulativeRowId + rowId, colId) = (*(matrices[i]))(rowId,colId);
1778 cumulativeRowId += nRows;
1786 TeuchosMatrix::fillWithBlocksVertically(
const std::vector<TeuchosMatrix* >& matrices)
1788 unsigned int sumNumRows = 0;
1789 for (
unsigned int i = 0; i < matrices.size(); ++i) {
1792 "TeuchosMatrix::fillWithBlocksVertically()",
1793 "inconsistent local number of cols");
1794 sumNumRows += matrices[i]->numRowsLocal();
1798 "TeuchosMatrix::fillWithBlocksVertically()",
1799 "inconsistent number of rows");
1801 unsigned int cumulativeRowId = 0;
1802 for (
unsigned int i = 0; i < matrices.size(); ++i) {
1803 unsigned int nRows = matrices[i]->numRowsLocal();
1804 unsigned int nCols = matrices[i]->numCols();
1805 for (
unsigned int rowId = 0; rowId < nRows; ++rowId) {
1806 for (
unsigned int colId = 0; colId < nCols; ++colId) {
1807 (*this)(cumulativeRowId + rowId, colId) = (*(matrices[i]))(rowId,colId);
1810 cumulativeRowId += nRows;
1818 TeuchosMatrix::fillWithTensorProduct(
const TeuchosMatrix& mat1,
const TeuchosMatrix& mat2)
1822 "TeuchosMatrix::fillTensorProduct(mat and mat)",
1823 "inconsistent local number of rows");
1826 "TeuchosMatrix::fillTensorProduct(mat and mat)",
1827 "inconsistent number of columns");
1829 for (
unsigned int rowId1 = 0; rowId1 < mat1.numRowsLocal(); ++rowId1) {
1830 for (
unsigned int colId1 = 0; colId1 < mat1.numCols(); ++colId1) {
1831 double multiplicativeFactor = mat1(rowId1,colId1);
1832 unsigned int targetRowId = rowId1 * mat2.numRowsLocal();
1833 unsigned int targetColId = colId1 * mat2.numCols();
1834 for (
unsigned int rowId2 = 0; rowId2 < mat2.numRowsLocal(); ++rowId2) {
1835 for (
unsigned int colId2 = 0; colId2 < mat2.numCols(); ++colId2) {
1836 (*this)(targetRowId + rowId2, targetColId + colId2) = multiplicativeFactor * mat2(rowId2,colId2);
1847 TeuchosMatrix::fillWithTensorProduct(
const TeuchosMatrix& mat1,
const TeuchosVector& vec2)
1851 "TeuchosMatrix::fillTensorProduct(mat and vec)",
1852 "inconsistent local number of rows");
1855 "TeuchosMatrix::fillTensorProduct(mat and vec)",
1856 "inconsistent number of columns");
1858 for (
unsigned int rowId1 = 0; rowId1 < mat1.numRowsLocal(); ++rowId1) {
1859 for (
unsigned int colId1 = 0; colId1 < mat1.numCols(); ++colId1) {
1860 double multiplicativeFactor = mat1(rowId1,colId1);
1861 unsigned int targetRowId = rowId1 * vec2.sizeLocal();
1862 unsigned int targetColId = colId1 * 1;
1863 for (
unsigned int rowId2 = 0; rowId2 < vec2.sizeLocal(); ++rowId2) {
1864 for (
unsigned int colId2 = 0; colId2 < 1; ++colId2) {
1865 (*this)(targetRowId + rowId2, targetColId + colId2) = multiplicativeFactor * vec2[rowId2];
1878 TeuchosMatrix::mpiSum(
const MpiComm& comm, TeuchosMatrix& M_global )
const
1882 (this->numCols() != M_global.numCols() )),
1884 "TeuchosMatrix::mpiSum()",
1885 "local and global matrices incompatible");
1888 int size = M_global.numRowsLocal()*M_global.numCols();
1889 std::vector<double> local( size, 0.0 );
1890 std::vector<double> global( size, 0.0 );
1893 for(
unsigned int i = 0; i < this->numRowsLocal(); i++ ) {
1894 for(
unsigned int j = 0; j < this->numCols(); j++ ) {
1895 k = i + j*M_global.numCols();
1896 local[k] = (*this)(i,j);
1901 "TeuchosMatrix::mpiSum()",
1902 "failed MPI.Allreduce()");
1904 for(
unsigned int i = 0; i < this->numRowsLocal(); i++ ) {
1905 for(
unsigned int j = 0; j < this->numCols(); j++ ) {
1906 k = i + j*M_global.numCols();
1907 M_global(i,j) = global[k];
1917 TeuchosMatrix::matlabLinearInterpExtrap(
1918 const TeuchosVector& x1Vec,
1919 const TeuchosMatrix& y1Mat,
1920 const TeuchosVector& x2Vec)
1924 "TeuchosMatrix::matlabLinearInterpExtrap()",
1925 "invalid 'x1' size");
1929 "TeuchosMatrix::matlabLinearInterpExtrap()",
1930 "invalid 'x1' and 'y1' sizes");
1934 "TeuchosMatrix::matlabLinearInterpExtrap()",
1935 "invalid 'x2' and 'this' sizes");
1939 "TeuchosMatrix::matlabLinearInterpExtrap()",
1940 "invalid 'y1' and 'this' sizes");
1942 TeuchosVector y1Vec(x1Vec);
1943 TeuchosVector y2Vec(x2Vec);
1944 for (
unsigned int colId = 0; colId < y1Mat.numCols(); ++colId) {
1945 y1Mat.getColumn(colId,y1Vec);
1946 y2Vec.matlabLinearInterpExtrap(x1Vec,y1Vec,x2Vec);
1947 this->setColumn(colId,y2Vec);
1957 TeuchosMatrix::print(std::ostream& os)
const
1959 unsigned int nRows = this->numRowsLocal();
1960 unsigned int nCols = this->numCols();
1962 if (m_printHorizontally) {
1963 for (
unsigned int i = 0; i < nRows; ++i) {
1964 for (
unsigned int j = 0; j < nCols; ++j) {
1968 if (i != (nRows-1)) os <<
"; ";
1973 for (
unsigned int i = 0; i < nRows; ++i) {
1974 for (
unsigned int j = 0; j < nCols; ++j) {
1987 TeuchosMatrix::subReadContents(
1988 const std::string& fileName,
1989 const std::string& fileType,
1990 const std::set<unsigned int>& allowedSubEnvIds)
1994 "TeuchosMatrix::subReadContents()",
1995 "unexpected subRank");
1999 "TeuchosMatrix::subReadContents()",
2000 "implemented just for sequential vectors for now");
2002 FilePtrSetStruct filePtrSet;
2003 if (m_env.openInputFile(fileName,
2009 unsigned int nRowsLocal = this->numRowsLocal();
2012 unsigned int idOfMyFirstLine = 1;
2013 unsigned int idOfMyLastLine = nRowsLocal;
2014 unsigned int nCols = this->numCols();
2018 std::string tmpString;
2021 *filePtrSet.ifsVar >> tmpString;
2024 *filePtrSet.ifsVar >> tmpString;
2028 "TeuchosMatrix::subReadContents()",
2029 "string should be the '=' sign");
2032 *filePtrSet.ifsVar >> tmpString;
2034 unsigned int posInTmpString = 6;
2037 char nRowsString[tmpString.size()-posInTmpString+1];
2038 unsigned int posInRowsString = 0;
2042 "TeuchosMatrix::subReadContents()",
2043 "symbol ',' not found in first line of file");
2044 nRowsString[posInRowsString++] = tmpString[posInTmpString++];
2045 }
while (tmpString[posInTmpString] !=
',');
2046 nRowsString[posInRowsString] =
'\0';
2050 char nColsString[tmpString.size()-posInTmpString+1];
2051 unsigned int posInColsString = 0;
2055 "TeuchosMatrix::subReadContents()",
2056 "symbol ')' not found in first line of file");
2057 nColsString[posInColsString++] = tmpString[posInTmpString++];
2058 }
while (tmpString[posInTmpString] !=
')');
2059 nColsString[posInColsString] =
'\0';
2062 unsigned int numRowsInFile = (
unsigned int) strtod(nRowsString,NULL);
2063 unsigned int numColsInFile = (
unsigned int) strtod(nColsString,NULL);
2064 if (m_env.subDisplayFile()) {
2065 *m_env.subDisplayFile() <<
"In TeuchosMatrix::subReadContents()"
2066 <<
": fullRank " << m_env.fullRank()
2067 <<
", numRowsInFile = " << numRowsInFile
2068 <<
", numColsInFile = " << numColsInFile
2069 <<
", nRowsLocal = " << nRowsLocal
2070 <<
", nCols = " << nCols
2077 "TeuchosMatrix::subReadContents()",
2078 "size of vec in file is not big enough");
2083 "TeuchosMatrix::subReadContents()",
2084 "number of parameters of vec in file is different than number of parameters in this vec object");
2087 unsigned int maxCharsPerLine = 64*nCols;
2089 unsigned int lineId = 0;
2090 while (lineId < idOfMyFirstLine) {
2091 filePtrSet.ifsVar->ignore(maxCharsPerLine,
'\n');
2095 if (m_env.subDisplayFile()) {
2096 *m_env.subDisplayFile() <<
"In TeuchosMatrix::subReadContents()"
2097 <<
": beginning to read input actual data"
2104 *filePtrSet.ifsVar >> tmpString;
2108 *filePtrSet.ifsVar >> tmpString;
2112 "TeuchosMatrix::subReadContents()",
2113 "in core 0, string should be the '=' sign");
2116 std::streampos tmpPos = filePtrSet.ifsVar->tellg();
2117 filePtrSet.ifsVar->seekg(tmpPos+(std::streampos)2);
2119 if (m_env.subDisplayFile()) {
2120 *m_env.subDisplayFile() <<
"In TeuchosMatrix::subReadContents()"
2121 <<
": beginning to read lines with numbers only"
2122 <<
", lineId = " << lineId
2123 <<
", idOfMyFirstLine = " << idOfMyFirstLine
2124 <<
", idOfMyLastLine = " << idOfMyLastLine
2129 while (lineId <= idOfMyLastLine) {
2130 for (
unsigned int j = 0; j < nCols; ++j) {
2131 *filePtrSet.ifsVar >> tmpRead;
2132 (*this)(lineId-idOfMyFirstLine,j) = tmpRead;
2137 m_env.closeFile(filePtrSet,fileType);
2145 TeuchosMatrix::subWriteContents(
2146 const std::string& varNamePrefix,
2147 const std::string& fileName,
2148 const std::string& fileType,
2149 const std::set<unsigned int>& allowedSubEnvIds)
const
2153 "TeuchosMatrix::subWriteContents()",
2154 "unexpected subRank");
2158 "TeuchosMatrix::subWriteContents()",
2159 "implemented just for sequential vectors for now");
2161 FilePtrSetStruct filePtrSet;
2162 if (m_env.openOutputFile(fileName,
2167 unsigned int nRows = this->numRowsLocal();
2168 unsigned int nCols = this->numCols();
2169 *filePtrSet.ofsVar << varNamePrefix <<
"_sub" << m_env.subIdString() <<
" = zeros(" << nRows
2173 *filePtrSet.ofsVar << varNamePrefix <<
"_sub" << m_env.subIdString() <<
" = [";
2175 for (
unsigned int i = 0; i < nRows; ++i) {
2176 for (
unsigned int j = 0; j < nCols; ++j) {
2177 *filePtrSet.ofsVar << (*this)(i,j)
2180 *filePtrSet.ofsVar <<
"\n";
2182 *filePtrSet.ofsVar <<
"];\n";
2184 m_env.closeFile(filePtrSet,fileType);
2197 TeuchosMatrix::copy(
const TeuchosMatrix& src)
2200 unsigned int i,j, nrows=src.numRowsLocal(), ncols=src.numCols();
2202 for(i=0; i< nrows ; i++)
2203 for (j = 0; j < ncols; j++)
2204 m_mat(i,j) = src(i,j);
2211 TeuchosMatrix::resetLU()
2213 if (m_LU.numCols() >0 || m_LU.numRows() > 0) {
2240 m_determinant = -INFINITY;
2241 m_lnDeterminant = -INFINITY;
2249 m_isSingular =
false;
2258 TeuchosMatrix::multiply(
const TeuchosVector& x, TeuchosVector& y)
const
2262 "TeuchosMatrix::multiply(), vector return void",
2263 "matrix and x have incompatible sizes");
2267 "TeuchosMatrix::multiply(), vector return void",
2268 "matrix and y have incompatible sizes");
2270 unsigned int sizeX = this->numCols();
2271 unsigned int sizeY = this->numRowsLocal();
2272 for (
unsigned int i = 0; i < sizeY; ++i) {
2274 for (
unsigned int j = 0; j < sizeX; ++j) {
2275 value += (*this)(i,j)*x[j];
2286 TeuchosMatrix::internalSvd()
const
2288 if (m_svdColMap == NULL) {
2289 int nRows = (int) this->numRowsLocal();
2290 int nCols = (int) this->numCols();
2293 "TeuchosMatrix::internalSvd()",
2294 "LAPACK/Teuchos only supports cases where nRows >= nCols");
2296 m_svdColMap =
new Map(this->numCols(),0,this->map().Comm());
2298 m_svdUmat =
new TeuchosMatrix(*
this);
2299 m_svdSvec =
new TeuchosVector(m_env,*m_svdColMap);
2300 m_svdVmat =
new TeuchosMatrix(*m_svdSvec);
2301 m_svdVTmat =
new TeuchosMatrix(*m_svdSvec);
2303 int minRowsCols, maxRowsCols;
2305 if (nRows>=nCols) { minRowsCols = nCols; maxRowsCols = nRows; }
else { minRowsCols = nRows; maxRowsCols = nCols; }
2309 double *work, *rwork;
2314 lwork = 15*maxRowsCols;
2315 work =
new double[lwork];
2317 int aux1= 5*minRowsCols+7, aux2= 2*maxRowsCols+2*minRowsCols+1;
2320 if (aux1>=aux2) { aux_dim = minRowsCols*aux1; }
else {aux_dim = minRowsCols*aux2; }
2322 rwork =
new double[aux_dim];
2324 Teuchos::LAPACK<int, double> lapack;
2326 lapack.GESVD(jobu,jobvt,m_mat.numRows(),m_mat.numCols(),m_mat.values(),m_mat.stride(),
2327 m_svdSvec->values(),m_svdUmat->values(),m_svdUmat->stride(),m_svdVTmat->values(),
2328 m_svdVTmat->stride(),&work[0],lwork,&rwork[0],&info);
2338 TeuchosMatrix
operator*(
double a,
const TeuchosMatrix& mat)
2340 TeuchosMatrix answer(mat);
2346 TeuchosVector
operator*(
const TeuchosMatrix& mat,
const TeuchosVector& vec)
2352 TeuchosMatrix
operator*(
const TeuchosMatrix& m1,
const TeuchosMatrix& m2)
2355 unsigned int m1Cols = m1.numCols();
2356 unsigned int m2Rows = m2.numRowsLocal();
2357 unsigned int m2Cols = m2.numCols();
2360 m1.env().worldRank(),
2361 "TeuchosMatrix operator*(matrix,matrix)",
2362 "different sizes m1Cols and m2Rows");
2364 TeuchosMatrix mat(m1.env(),m1.map(),m2Cols);
2365 unsigned int commonSize = m1Cols;
2367 for (
unsigned int row1 = 0; row1 < m1Rows; ++row1) {
2368 for (
unsigned int col2 = 0; col2 < m2Cols; ++col2) {
2370 for (
unsigned int k = 0; k < commonSize; ++k) {
2371 result += m1(row1,k)*m2(k,col2);
2373 mat(row1,col2) = result;
2381 TeuchosMatrix
operator+(
const TeuchosMatrix& m1,
const TeuchosMatrix& m2)
2383 TeuchosMatrix answer(m1);
2389 TeuchosMatrix
operator-(
const TeuchosMatrix& m1,
const TeuchosMatrix& m2)
2391 TeuchosMatrix answer(m1);
2397 TeuchosMatrix
matrixProduct(
const TeuchosVector& v1,
const TeuchosVector& v2)
2399 unsigned int nRows = v1.sizeLocal();
2400 unsigned int nCols = v2.sizeLocal();
2401 TeuchosMatrix answer(v1.env(),v1.map(),nCols);
2403 for (
unsigned int i = 0; i < nRows; ++i) {
2404 double value1 = v1[i];
2405 for (
unsigned int j = 0; j < nCols; ++j) {
2406 answer(i,j) = value1*v2[j];
2414 TeuchosMatrix
leftDiagScaling(
const TeuchosVector& vec,
const TeuchosMatrix& mat)
2416 unsigned int vSize = vec.sizeLocal();
2418 unsigned int mCols = mat.numCols();
2421 mat.env().worldRank(),
2422 "TeuchosMatrix leftDiagScaling(vector,matrix)",
2423 "size of vector is different from the number of rows in matrix");
2426 mat.env().worldRank(),
2427 "TeuchosMatrix leftDiagScaling(vector,matrix)",
2428 "routine currently works for square matrices only");
2430 TeuchosMatrix answer(mat);
2431 for (
unsigned int i = 0; i < mRows; ++i) {
2432 double vecValue = vec[i];
2433 for (
unsigned int j = 0; j < mCols; ++j) {
2434 answer(i,j) *= vecValue;
2442 TeuchosMatrix
rightDiagScaling(
const TeuchosMatrix& mat,
const TeuchosVector& vec)
2444 unsigned int vSize = vec.sizeLocal();
2446 unsigned int mCols = mat.numCols();
2449 mat.env().worldRank(),
2450 "TeuchosMatrix rightDiagScaling(matrix,vector)",
2451 "size of vector is different from the number of cols in matrix");
2454 mat.env().worldRank(),
2455 "TeuchosMatrix rightDiagScaling(matrix,vector)",
2456 "routine currently works for square matrices only");
2458 TeuchosMatrix answer(mat);
2459 for (
unsigned int j = 0; j < mCols; ++j) {
2460 double vecValue = vec[j];
2461 for (
unsigned int i = 0; i < mRows; ++i) {
2462 answer(i,j) *= vecValue;
2471 operator<<(std::ostream& os,
const TeuchosMatrix& obj)
2480 #endif // ifdef QUESO_HAS_TRILINOS
GslVector multiply(const GslVector &x) const
This function multiplies this matrix by vector x and returns the resulting vector.
#define UQ_RC_MACRO(macroIRc, givenRank, where, what, retValue)
Macros.
#define RawValue_MPI_DOUBLE
GslMatrix operator+(const GslMatrix &m1, const GslMatrix &m2)
GslMatrix operator-(const GslMatrix &m1, const GslMatrix &m2)
virtual void copy(const Matrix &src)
Copies matrix src to this matrix.
unsigned int numRowsLocal() const
Returns the local row dimension of this matrix.
std::ostream & operator<<(std::ostream &os, const BaseEnvironment &obj)
#define UQ_FATAL_RC_MACRO(macroIRc, givenRank, where, what)
GslMatrix rightDiagScaling(const GslMatrix &mat, const GslVector &vec)
const int UQ_MATRIX_IS_NOT_POS_DEFINITE_RC
GslMatrix operator*(double a, const GslMatrix &mat)
#define UQ_FATAL_TEST_MACRO(test, givenRank, where, what)
GslMatrix matrixProduct(const GslVector &v1, const GslVector &v2)
GslMatrix leftDiagScaling(const GslVector &vec, const GslMatrix &mat)