45 #ifndef __BelosTsqrOrthoManagerImpl_hpp
46 #define __BelosTsqrOrthoManagerImpl_hpp
55 #include "Teuchos_ParameterListAcceptorDefaultBase.hpp"
56 #ifdef BELOS_TEUCHOS_TIME_MONITOR
58 #endif // BELOS_TEUCHOS_TIME_MONITOR
130 template<
class Scalar>
132 public std::binary_function<Teuchos::ArrayView<typename Teuchos::ScalarTraits<Scalar>::magnitudeType>,
133 Teuchos::ArrayView<typename Teuchos::ScalarTraits<Scalar>::magnitudeType>,
157 template<
class Scalar>
191 template<
class Scalar,
class MV>
207 typedef typename MVT::tsqr_adaptor_type tsqr_adaptor_type;
251 const std::string& label);
281 reorthogCallback_ = callback;
292 if (label != label_) {
295 #ifdef BELOS_TEUCHOS_TIME_MONITOR
296 clearTimer (label,
"All orthogonalization");
297 clearTimer (label,
"Projection");
298 clearTimer (label,
"Normalization");
300 timerOrtho_ = makeTimer (label,
"All orthogonalization");
301 timerProject_ = makeTimer (label,
"Projection");
302 timerNormalize_ = makeTimer (label,
"Normalization");
303 #endif // BELOS_TEUCHOS_TIME_MONITOR
308 const std::string&
getLabel ()
const {
return label_; }
342 norm (
const MV& X, std::vector<magnitude_type>& normVec)
const;
415 return projectAndNormalizeImpl (X, X,
false, C, B, Q);
446 return projectAndNormalizeImpl (X_in, X_out,
true, C, B, Q);
460 for (
int k = 0; k < ncols; ++k) {
473 mat_type X1_T_X2 (ncols_X1, ncols_X2);
498 tsqr_adaptor_type tsqrAdaptor_;
517 bool randomizeNullSpace_;
524 bool reorthogonalizeBlocks_;
529 bool throwOnReorthogFault_;
543 bool forceNonnegativeDiagonal_;
545 #ifdef BELOS_TEUCHOS_TIME_MONITOR
554 #endif // BELOS_TEUCHOS_TIME_MONITOR
559 #ifdef BELOS_TEUCHOS_TIME_MONITOR
569 makeTimer (
const std::string& prefix,
570 const std::string& timerName)
572 const std::string timerLabel =
573 prefix.empty() ? timerName : (prefix +
": " + timerName);
583 clearTimer (
const std::string& prefix,
584 const std::string& timerName)
586 const std::string timerLabel =
587 prefix.empty() ? timerName : (prefix +
": " + timerName);
590 #endif // BELOS_TEUCHOS_TIME_MONITOR
594 raiseReorthogFault (
const std::vector<magnitude_type>& normsAfterFirstPass,
595 const std::vector<magnitude_type>& normsAfterSecondPass,
596 const std::vector<int>& faultIndices);
608 checkProjectionDims (
int& ncols_X,
628 const bool attemptToRecycle =
true)
const;
639 projectAndNormalizeImpl (MV& X_in,
641 const bool outOfPlace,
688 int rawNormalize (MV& X, MV& Q,
mat_type& B);
707 int normalizeOne (MV& X,
mat_ptr B)
const;
736 int normalizeImpl (MV& X, MV& Q,
mat_ptr B,
const bool outOfPlace);
739 template<
class Scalar,
class MV>
745 using Teuchos::parameterList;
747 using Teuchos::sublist;
750 RCP<const ParameterList> defaultParams = getValidParameters ();
752 RCP<ParameterList> tsqrParams;
754 RCP<ParameterList> theParams;
756 theParams = parameterList (*defaultParams);
764 randomizeNullSpace_ =
765 theParams->
get<
bool> (
"randomizeNullSpace",
766 defaultParams->get<
bool> (
"randomizeNullSpace"));
767 reorthogonalizeBlocks_ =
768 theParams->get<
bool> (
"reorthogonalizeBlocks",
769 defaultParams->get<
bool> (
"reorthogonalizeBlocks"));
770 throwOnReorthogFault_ =
771 theParams->get<
bool> (
"throwOnReorthogFault",
772 defaultParams->get<
bool> (
"throwOnReorthogFault"));
773 blockReorthogThreshold_ =
774 theParams->get<M> (
"blockReorthogThreshold",
775 defaultParams->get<M> (
"blockReorthogThreshold"));
776 relativeRankTolerance_ =
777 theParams->get<M> (
"relativeRankTolerance",
778 defaultParams->get<M> (
"relativeRankTolerance"));
779 forceNonnegativeDiagonal_ =
780 theParams->get<
bool> (
"forceNonnegativeDiagonal",
781 defaultParams->get<
bool> (
"forceNonnegativeDiagonal"));
785 if (! theParams->isSublist (
"TSQR implementation")) {
786 theParams->set (
"TSQR implementation",
787 defaultParams->sublist (
"TSQR implementation"));
789 tsqrParams = sublist (theParams,
"TSQR implementation",
true);
793 tsqrAdaptor_.setParameterList (tsqrParams);
796 setMyParamList (theParams);
799 template<
class Scalar,
class MV>
802 const std::string& label) :
806 randomizeNullSpace_ (true),
807 reorthogonalizeBlocks_ (true),
808 throwOnReorthogFault_ (false),
809 blockReorthogThreshold_ (0),
810 relativeRankTolerance_ (0),
811 forceNonnegativeDiagonal_ (false)
815 #ifdef BELOS_TEUCHOS_TIME_MONITOR
816 timerOrtho_ = makeTimer (label,
"All orthogonalization");
817 timerProject_ = makeTimer (label,
"Projection");
818 timerNormalize_ = makeTimer (label,
"Normalization");
819 #endif // BELOS_TEUCHOS_TIME_MONITOR
822 template<
class Scalar,
class MV>
828 randomizeNullSpace_ (true),
829 reorthogonalizeBlocks_ (true),
830 throwOnReorthogFault_ (false),
831 blockReorthogThreshold_ (0),
832 relativeRankTolerance_ (0),
833 forceNonnegativeDiagonal_ (false)
837 #ifdef BELOS_TEUCHOS_TIME_MONITOR
838 timerOrtho_ = makeTimer (label,
"All orthogonalization");
839 timerProject_ = makeTimer (label,
"Projection");
840 timerNormalize_ = makeTimer (label,
"Normalization");
841 #endif // BELOS_TEUCHOS_TIME_MONITOR
844 template<
class Scalar,
class MV>
847 norm (
const MV& X, std::vector<magnitude_type>& normVec)
const
849 const int numCols = MVT::GetNumberVecs (X);
852 if (normVec.size() <
static_cast<size_t>(numCols))
853 normVec.resize (numCols);
854 MVT::MvNorm (X, normVec);
857 template<
class Scalar,
class MV>
863 #ifdef BELOS_TEUCHOS_TIME_MONITOR
872 #endif // BELOS_TEUCHOS_TIME_MONITOR
874 int ncols_X, num_Q_blocks, ncols_Q_total;
875 checkProjectionDims (ncols_X, num_Q_blocks, ncols_Q_total, X, Q);
879 if (ncols_X == 0 || num_Q_blocks == 0 || ncols_Q_total == 0)
883 allocateProjectionCoefficients (C, Q, X,
true);
887 std::vector<magnitude_type> columnNormsBefore (ncols_X,
magnitude_type(0));
888 if (reorthogonalizeBlocks_)
889 MVT::MvNorm (X, columnNormsBefore);
893 rawProject (X, Q, C);
897 if (reorthogonalizeBlocks_) {
898 std::vector<magnitude_type> columnNormsAfter (ncols_X,
magnitude_type(0));
899 MVT::MvNorm (X, columnNormsAfter);
907 bool reorthogonalize =
false;
908 for (
int j = 0; j < ncols_X; ++j) {
909 if (columnNormsAfter[j] < relThres * columnNormsBefore[j]) {
910 reorthogonalize =
true;
914 if (reorthogonalize) {
917 if (! reorthogCallback_.is_null()) {
918 reorthogCallback_->operator() (Teuchos::arrayViewFromVector (columnNormsBefore),
919 Teuchos::arrayViewFromVector (columnNormsAfter));
923 allocateProjectionCoefficients (C2, Q, X,
false);
927 rawProject (X, Q, C2);
929 for (
int k = 0; k < num_Q_blocks; ++k)
936 template<
class Scalar,
class MV>
943 #ifdef BELOS_TEUCHOS_TIME_MONITOR
950 #endif // BELOS_TEUCHOS_TIME_MONITOR
955 const int numCols = MVT::GetNumberVecs (X);
964 return normalizeOne (X, B);
994 MVText::GetGlobalLength(*Q_) != MVText::GetGlobalLength(X) ||
995 numCols > MVT::GetNumberVecs (*Q_)) {
996 Q_ = MVT::Clone (X, numCols);
1005 if (MVT::GetNumberVecs(*Q_) == numCols) {
1006 return normalizeImpl (X, *Q_, B,
false);
1008 RCP<MV> Q_view = MVT::CloneViewNonConst (*Q_, Range1D(0, numCols-1));
1009 return normalizeImpl (X, *Q_view, B,
false);
1013 template<
class Scalar,
class MV>
1019 const bool attemptToRecycle)
const
1021 const int num_Q_blocks = Q.size();
1022 const int ncols_X = MVT::GetNumberVecs (X);
1024 if (attemptToRecycle)
1026 for (
int i = 0; i < num_Q_blocks; ++i)
1028 const int ncols_Qi = MVT::GetNumberVecs (*Q[i]);
1032 C[i] =
rcp (
new mat_type (ncols_Qi, ncols_X));
1035 mat_type& Ci = *C[i];
1036 if (Ci.numRows() != ncols_Qi || Ci.numCols() != ncols_X)
1037 Ci.shape (ncols_Qi, ncols_X);
1039 Ci.putScalar (SCT::zero());
1045 for (
int i = 0; i < num_Q_blocks; ++i)
1047 const int ncols_Qi = MVT::GetNumberVecs (*Q[i]);
1048 C[i] =
rcp (
new mat_type (ncols_Qi, ncols_X));
1053 template<
class Scalar,
class MV>
1058 #ifdef BELOS_TEUCHOS_TIME_MONITOR
1061 #endif // BELOS_TEUCHOS_TIME_MONITOR
1063 const int numVecs = MVT::GetNumberVecs(X);
1066 }
else if (numVecs == 1) {
1073 const int rank = normalizeOne (X, B);
1075 RCP<MV> Q_0 = MVT::CloneViewNonConst (Q, Range1D(0,0));
1076 MVT::Assign (X, *Q_0);
1082 return normalizeImpl (X, Q, B,
true);
1086 template<
class Scalar,
class MV>
1091 const bool outOfPlace,
1100 #ifdef BELOS_TEUCHOS_TIME_MONITOR
1104 #endif // BELOS_TEUCHOS_TIME_MONITOR
1109 std::invalid_argument,
1110 "Belos::TsqrOrthoManagerImpl::"
1111 "projectAndNormalizeImpl(..., outOfPlace=true, ...):"
1112 "X_out has " << MVT::GetNumberVecs(X_out)
1113 <<
" columns, but X_in has "
1114 << MVT::GetNumberVecs(X_in) <<
" columns.");
1118 int ncols_X, num_Q_blocks, ncols_Q_total;
1119 checkProjectionDims (ncols_X, num_Q_blocks, ncols_Q_total, X_in, Q);
1126 if (num_Q_blocks == 0 || ncols_Q_total == 0) {
1128 return normalizeOutOfPlace (X_in, X_out, B);
1130 return normalize (X_in, B);
1137 allocateProjectionCoefficients (C, Q, X_in,
true);
1142 std::vector<magnitude_type> normsBeforeFirstPass (ncols_X, SCTM::zero());
1143 if (reorthogonalizeBlocks_) {
1144 MVT::MvNorm (X_in, normsBeforeFirstPass);
1148 rawProject (X_in, Q, C);
1161 B_out =
rcp (
new mat_type (ncols_X, ncols_X));
1165 std::invalid_argument,
1166 "normalizeOne: Input matrix B must be at "
1167 "least " << ncols_X <<
" x " << ncols_X
1168 <<
", but is instead " << B->numRows()
1169 <<
" x " << B->numCols() <<
".");
1179 const int firstPassRank = outOfPlace ?
1180 normalizeOutOfPlace (X_in, X_out, B_out) :
1181 normalize (X_in, B_out);
1190 int rank = firstPassRank;
1206 if (firstPassRank < ncols_X && randomizeNullSpace_) {
1207 const int numNullSpaceCols = ncols_X - firstPassRank;
1208 const Range1D nullSpaceIndices (firstPassRank, ncols_X - 1);
1212 for (
int k = 0; k < num_Q_blocks; ++k) {
1213 const int numColsQk = MVT::GetNumberVecs(*Q[k]);
1214 C_null[k] =
rcp (
new mat_type (numColsQk, numNullSpaceCols));
1217 RCP<mat_type> B_null (
new mat_type (numNullSpaceCols, numNullSpaceCols));
1219 int randomVectorsRank;
1223 RCP<MV> X_out_null = MVT::CloneViewNonConst (X_out, nullSpaceIndices);
1228 RCP<MV> X_in_null = MVT::CloneViewNonConst (X_in, nullSpaceIndices);
1229 MVT::Assign (*X_out_null, *X_in_null);
1232 rawProject (*X_in_null, Q, C_null);
1233 randomVectorsRank = normalizeOutOfPlace (*X_in_null, *X_out_null, B_null);
1237 RCP<MV> X_null = MVT::CloneViewNonConst (X_in, nullSpaceIndices);
1240 rawProject (*X_null, Q, C_null);
1241 randomVectorsRank = normalize (*X_null, B_null);
1249 "Belos::TsqrOrthoManagerImpl::projectAndNormalize"
1250 "OutOfPlace(): After projecting and normalizing the "
1251 "random vectors (used to replace the null space "
1252 "basis vectors from normalizing X), they have rank "
1253 << randomVectorsRank <<
", but should have full "
1254 "rank " << numNullSpaceCols <<
".");
1259 if (reorthogonalizeBlocks_) {
1262 std::vector<magnitude_type>
1263 normsAfterFirstPass (firstPassRank, SCTM::zero());
1264 std::vector<magnitude_type>
1265 normsAfterSecondPass (firstPassRank, SCTM::zero());
1280 for (
int j = 0; j < firstPassRank; ++j) {
1281 const Scalar*
const B_j = &(*B_out)(0,j);
1284 normsAfterFirstPass[j] = blas.
NRM2 (firstPassRank, B_j, 1);
1288 bool reorthogonalize =
false;
1289 for (
int j = 0; j < firstPassRank; ++j) {
1296 const magnitude_type curThreshold =
1297 blockReorthogThreshold() * normsBeforeFirstPass[j];
1298 if (normsAfterFirstPass[j] < curThreshold) {
1299 reorthogonalize =
true;
1306 if (! reorthogCallback_.is_null()) {
1307 using Teuchos::arrayViewFromVector;
1308 (*reorthogCallback_) (arrayViewFromVector (normsBeforeFirstPass),
1309 arrayViewFromVector (normsAfterFirstPass));
1323 bool reorthogFault =
false;
1325 std::vector<int> faultIndices;
1326 if (reorthogonalize) {
1334 MVT::Assign (X_out, X_in);
1341 allocateProjectionCoefficients (C2, Q, X_in,
false);
1346 rawProject (X_in, Q, C2);
1349 RCP<mat_type> B2 (
new mat_type (ncols_X, ncols_X));
1352 const int secondPassRank = outOfPlace ?
1353 normalizeOutOfPlace (X_in, X_out, B2) :
1354 normalize (X_in, B2);
1355 rank = secondPassRank;
1360 mat_type B_copy (Copy, *B_out, B_out->numRows(), B_out->numCols());
1362 const int err = B_out->multiply (NO_TRANS, NO_TRANS, SCT::one(),
1363 *B2, B_copy, SCT::zero());
1365 "Teuchos::SerialDenseMatrix::multiply "
1366 "returned err = " << err <<
" != 0");
1370 for (
int k = 0; k < num_Q_blocks; ++k) {
1371 mat_type C_k_copy (Copy, *C[k], C[k]->numRows(), C[k]->numCols());
1374 const int err1 = C[k]->multiply (NO_TRANS, NO_TRANS, SCT::one(),
1375 *C2[k], B_copy, SCT::one());
1377 "Teuchos::SerialDenseMatrix::multiply "
1378 "returned err = " << err1 <<
" != 0");
1383 for (
int j = 0; j < rank; ++j) {
1384 const Scalar*
const B2_j = &(*B2)(0,j);
1385 normsAfterSecondPass[j] = blas.
NRM2 (rank, B2_j, 1);
1390 reorthogFault =
false;
1391 for (
int j = 0; j < rank; ++j) {
1392 const magnitude_type relativeLowerBound =
1393 blockReorthogThreshold() * normsAfterFirstPass[j];
1394 if (normsAfterSecondPass[j] < relativeLowerBound) {
1395 reorthogFault =
true;
1396 faultIndices.push_back (j);
1401 if (reorthogFault) {
1402 if (throwOnReorthogFault_) {
1403 raiseReorthogFault (normsAfterFirstPass,
1404 normsAfterSecondPass,
1413 "TsqrOrthoManagerImpl has not yet implemented"
1414 " recovery from an orthogonalization fault.");
1422 template<
class Scalar,
class MV>
1424 TsqrOrthoManagerImpl<Scalar, MV>::
1425 raiseReorthogFault (
const std::vector<magnitude_type>& normsAfterFirstPass,
1426 const std::vector<magnitude_type>& normsAfterSecondPass,
1427 const std::vector<int>& faultIndices)
1430 typedef std::vector<int>::size_type size_type;
1431 std::ostringstream os;
1433 os <<
"Orthogonalization fault at the following column(s) of X:" << endl;
1434 os <<
"Column\tNorm decrease factor" << endl;
1435 for (size_type k = 0; k < faultIndices.size(); ++k) {
1436 const int index = faultIndices[k];
1437 const magnitude_type decreaseFactor =
1438 normsAfterSecondPass[index] / normsAfterFirstPass[index];
1439 os << index <<
"\t" << decreaseFactor << endl;
1441 throw TsqrOrthoFault (os.str());
1444 template<
class Scalar,
class MV>
1449 using Teuchos::parameterList;
1452 if (defaultParams_.is_null()) {
1453 RCP<ParameterList> params = parameterList (
"TsqrOrthoManagerImpl");
1457 params->set (
"TSQR implementation", *(tsqrAdaptor_.getValidParameters()),
1458 "TSQR implementation parameters.");
1462 const bool defaultRandomizeNullSpace =
true;
1463 params->set (
"randomizeNullSpace", defaultRandomizeNullSpace,
1464 "Whether to fill in null space vectors with random data.");
1466 const bool defaultReorthogonalizeBlocks =
true;
1467 params->set (
"reorthogonalizeBlocks", defaultReorthogonalizeBlocks,
1468 "Whether to do block reorthogonalization as necessary.");
1474 params->set (
"blockReorthogThreshold", defaultBlockReorthogThreshold,
1475 "If reorthogonalizeBlocks==true, and if the norm of "
1476 "any column within a block decreases by this much or "
1477 "more after orthogonalization, we reorthogonalize.");
1482 Teuchos::as<magnitude_type>(10) * SCTM::eps();
1487 params->set (
"relativeRankTolerance", defaultRelativeRankTolerance,
1488 "Relative tolerance to determine the numerical rank of a "
1489 "block when normalizing.");
1493 const bool defaultThrowOnReorthogFault =
true;
1494 params->set (
"throwOnReorthogFault", defaultThrowOnReorthogFault,
1495 "Whether to throw an exception if an orthogonalization "
1496 "fault occurs. This only matters if reorthogonalization "
1497 "is enabled (reorthogonalizeBlocks==true).");
1499 const bool defaultForceNonnegativeDiagonal =
false;
1500 params->set (
"forceNonnegativeDiagonal", defaultForceNonnegativeDiagonal,
1501 "Whether to force the R factor produced by the normalization "
1502 "step to have a nonnegative diagonal.");
1504 defaultParams_ = params;
1506 return defaultParams_;
1509 template<
class Scalar,
class MV>
1517 RCP<const ParameterList> defaultParams = getValidParameters();
1519 RCP<ParameterList> params =
rcp (
new ParameterList (*defaultParams));
1528 const bool randomizeNullSpace =
false;
1529 params->set (
"randomizeNullSpace", randomizeNullSpace);
1530 const bool reorthogonalizeBlocks =
false;
1531 params->set (
"reorthogonalizeBlocks", reorthogonalizeBlocks);
1536 template<
class Scalar,
class MV>
1548 tsqrAdaptor_.factorExplicit (X, Q, B, forceNonnegativeDiagonal_);
1551 rank = tsqrAdaptor_.revealRank (Q, B, relativeRankTolerance_);
1552 }
catch (std::exception& e) {
1553 throw TsqrOrthoError (e.what());
1558 template<
class Scalar,
class MV>
1560 TsqrOrthoManagerImpl<Scalar, MV>::
1561 normalizeOne (MV& X,
1569 B_out =
rcp (
new mat_type (1, 1));
1571 const int numRows = B->
numRows();
1572 const int numCols = B->
numCols();
1574 std::invalid_argument,
1575 "normalizeOne: Input matrix B must be at "
1576 "least 1 x 1, but is instead " << numRows
1577 <<
" x " << numCols <<
".");
1583 std::vector<magnitude_type> theNorm (1, SCTM::zero());
1584 MVT::MvNorm (X, theNorm);
1585 (*B_out)(0,0) = theNorm[0];
1599 if (theNorm[0] == SCTM::zero()) {
1602 if (randomizeNullSpace_) {
1604 MVT::MvNorm (X, theNorm);
1605 if (theNorm[0] == SCTM::zero()) {
1610 throw TsqrOrthoError(
"normalizeOne: a supposedly random "
1611 "vector has norm zero!");
1616 const Scalar alpha = SCT::one() / theNorm[0];
1617 MVT::MvScale (X, alpha);
1624 const Scalar alpha = SCT::one() / theNorm[0];
1625 MVT::MvScale (X, alpha);
1631 template<
class Scalar,
class MV>
1633 TsqrOrthoManagerImpl<Scalar, MV>::
1638 #ifdef BELOS_TEUCHOS_TIME_MONITOR
1640 #endif // BELOS_TEUCHOS_TIME_MONITOR
1643 const int num_Q_blocks = Q.size();
1644 for (
int i = 0; i < num_Q_blocks; ++i)
1652 mat_type& Ci = *C[i];
1653 const MV& Qi = *Q[i];
1654 innerProd (Qi, X, Ci);
1655 MVT::MvTimesMatAddMv (-SCT::one(), Qi, Ci, SCT::one(), X);
1660 template<
class Scalar,
class MV>
1662 TsqrOrthoManagerImpl<Scalar, MV>::
1667 #ifdef BELOS_TEUCHOS_TIME_MONITOR
1669 #endif // BELOS_TEUCHOS_TIME_MONITOR
1672 innerProd (*Q, X, *C);
1673 MVT::MvTimesMatAddMv (-SCT::one(), *Q, *C, SCT::one(), X);
1676 template<
class Scalar,
class MV>
1678 TsqrOrthoManagerImpl<Scalar, MV>::
1679 normalizeImpl (MV& X,
1682 const bool outOfPlace)
1688 using Teuchos::tuple;
1690 const int numCols = MVT::GetNumberVecs (X);
1698 MVT::GetNumberVecs (Q) < numCols, std::invalid_argument,
1699 "TsqrOrthoManagerImpl::normalizeImpl: Q has "
1700 << MVT::GetNumberVecs(Q) <<
" columns. This is too "
1701 "few, since X has " << numCols <<
" columns.");
1705 RCP<MV> Q_view = MVT::CloneViewNonConst (Q, Range1D (0, numCols-1));
1713 B_out =
rcp (
new mat_type (numCols, numCols));
1717 B->
numRows() < numCols || B->
numCols() < numCols, std::invalid_argument,
1718 "TsqrOrthoManagerImpl::normalizeImpl: Input matrix B must be at least "
1719 << numCols <<
" x " << numCols <<
", but is instead " << B->
numRows ()
1720 <<
" x " << B->
numCols() <<
".");
1735 const int rank = rawNormalize (X, *Q_view, *B_out);
1746 rank < 0 || rank > numCols, std::logic_error,
1747 "Belos::TsqrOrthoManagerImpl::normalizeImpl: rawNormalize returned rank "
1748 " = " << rank <<
" for a matrix X with " << numCols <<
" columns. "
1749 "Please report this bug to the Belos developers.");
1753 if (rank == numCols || ! randomizeNullSpace_) {
1757 MVT::Assign (*Q_view, X);
1762 if (randomizeNullSpace_ && rank < numCols) {
1769 const int nullSpaceNumCols = numCols - rank;
1772 Range1D nullSpaceIndices (rank, numCols-1);
1779 RCP<MV> Q_null = MVT::CloneViewNonConst (*Q_view, nullSpaceIndices);
1781 MVT::MvRandom (*Q_null);
1787 std::vector<magnitude_type> norms (MVT::GetNumberVecs (*Q_null));
1788 MVT::MvNorm (*Q_null, norms);
1790 bool anyZero =
false;
1791 typedef typename std::vector<magnitude_type>::const_iterator iter_type;
1792 for (iter_type it = norms.begin(); it != norms.end(); ++it) {
1793 if (*it == SCTM::zero()) {
1798 std::ostringstream os;
1799 os <<
"TsqrOrthoManagerImpl::normalizeImpl: "
1800 "We are being asked to randomize the null space, for a matrix "
1801 "with " << numCols <<
" columns and reported column rank "
1802 << rank <<
". The inclusive range of columns to fill with "
1803 "random data is [" << nullSpaceIndices.lbound() <<
","
1804 << nullSpaceIndices.ubound() <<
"]. After filling the null "
1805 "space vectors with random numbers, at least one of the vectors"
1806 " has norm zero. Here are the norms of all the null space "
1808 for (iter_type it = norms.begin(); it != norms.end(); ++it) {
1810 if (it+1 != norms.end())
1813 os <<
"].) There is a tiny probability that this could happen "
1814 "randomly, but it is likely a bug. Please report it to the "
1815 "Belos developers, especially if you are able to reproduce the "
1828 RCP<const MV> Q_col = MVT::CloneView (*Q_view, Range1D (0, rank-1));
1833 mat_ptr C_null (
new mat_type (rank, nullSpaceNumCols));
1834 rawProject (*Q_null, Q_col, C_null);
1843 RCP<MV> X_null = MVT::CloneViewNonConst (X, nullSpaceIndices);
1846 mat_type B_null (nullSpaceNumCols, nullSpaceNumCols);
1848 const int nullSpaceBasisRank = rawNormalize (*Q_null, *X_null, B_null);
1862 if (nullSpaceBasisRank < nullSpaceNumCols) {
1863 std::vector<magnitude_type> norms (MVT::GetNumberVecs(*X_null));
1864 MVT::MvNorm (*X_null, norms);
1865 std::ostringstream os;
1866 os <<
"TsqrOrthoManagerImpl::normalizeImpl: "
1867 <<
"We are being asked to randomize the null space, "
1868 <<
"for a matrix with " << numCols <<
" columns and "
1869 <<
"column rank " << rank <<
". After projecting and "
1870 <<
"normalizing the generated random vectors, they "
1871 <<
"only have rank " << nullSpaceBasisRank <<
". They"
1872 <<
" should have full rank " << nullSpaceNumCols
1873 <<
". (The inclusive range of columns to fill with "
1874 <<
"random data is [" << nullSpaceIndices.lbound()
1875 <<
"," << nullSpaceIndices.ubound() <<
"]. The "
1876 <<
"column norms of the resulting Q factor are: [";
1877 for (
typename std::vector<magnitude_type>::size_type k = 0;
1878 k < norms.size(); ++k) {
1880 if (k != norms.size()-1) {
1884 os <<
"].) There is a tiny probability that this could "
1885 <<
"happen randomly, but it is likely a bug. Please "
1886 <<
"report it to the Belos developers, especially if "
1887 <<
"you are able to reproduce the behavior.";
1890 TsqrOrthoError, os.str ());
1900 MVT::Assign (*X_null, *Q_null);
1901 }
else if (rank > 0) {
1903 RCP<const MV> Q_col = MVT::CloneView (*Q_view, Range1D (0, rank-1));
1904 RCP<MV> X_col = MVT::CloneViewNonConst (X, Range1D (0, rank-1));
1905 MVT::Assign (*Q_col, *X_col);
1912 template<
class Scalar,
class MV>
1914 TsqrOrthoManagerImpl<Scalar, MV>::
1915 checkProjectionDims (
int& ncols_X,
1927 int the_ncols_X, the_num_Q_blocks, the_ncols_Q_total;
1928 the_num_Q_blocks = Q.size();
1929 the_ncols_X = MVT::GetNumberVecs (X);
1932 the_ncols_Q_total = 0;
1937 typedef typename ArrayView<RCP<const MV> >::const_iterator iter_type;
1938 for (iter_type it = Q.begin(); it != Q.end(); ++it) {
1939 const MV& Qi = **it;
1940 the_ncols_Q_total += MVT::GetNumberVecs (Qi);
1944 ncols_X = the_ncols_X;
1945 num_Q_blocks = the_num_Q_blocks;
1946 ncols_Q_total = the_ncols_Q_total;
1951 #endif // __BelosTsqrOrthoManagerImpl_hpp
int projectAndNormalize(MV &X, Teuchos::Array< mat_ptr > C, mat_ptr B, Teuchos::ArrayView< Teuchos::RCP< const MV > > Q)
Project X against Q and normalize X.
Interface of callback invoked by TsqrOrthoManager on reorthogonalization.
bool is_null(const boost::shared_ptr< T > &p)
void project(MV &X, Teuchos::Array< mat_ptr > C, Teuchos::ArrayView< Teuchos::RCP< const MV > > Q)
Compute and .
void norm(const MV &X, std::vector< magnitude_type > &normVec) const
Compute the 2-norm of each column j of X.
TsqrOrthoManagerImpl(const Teuchos::RCP< Teuchos::ParameterList > ¶ms, const std::string &label)
Constructor (that sets user-specified parameters).
magnitude_type orthogError(const MV &X1, const MV &X2) const
Return the Frobenius norm of the inner product of X1 with itself.
Teuchos::SerialDenseMatrix< int, Scalar > mat_type
Type of the projection and normalization coefficients.
T & get(const std::string &name, T def_value)
void setParameterList(const Teuchos::RCP< Teuchos::ParameterList > ¶ms)
Set parameters from the given parameter list.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Teuchos::RCP< const Teuchos::ParameterList > getFastParameters()
Get "fast" parameters for TsqrOrthoManagerImpl.
TSQR-based OrthoManager subclass implementation.
Declaration of basic traits for the multivector type.
void setReorthogonalizationCallback(const Teuchos::RCP< ReorthogonalizationCallback< Scalar > > &callback)
Set callback to be invoked on reorthogonalization.
Error in TsqrOrthoManager or TsqrOrthoManagerImpl.
int normalize(MV &X, mat_ptr B)
Orthogonalize the columns of X in place.
void setLabel(const std::string &label)
Set the label for timers.
An extension of the MultiVecTraits class that adds a new vector length method.
Teuchos::RCP< mat_type > mat_ptr
static void MvTransMv(const ScalarType alpha, const MV &A, const MV &mv, Teuchos::SerialDenseMatrix< int, ScalarType > &B)
Compute a dense matrix B through the matrix-matrix multiply .
ScalarTraits< ScalarType >::magnitudeType normFrobenius() const
static int GetNumberVecs(const MV &mv)
Obtain the number of vectors in mv.
Teuchos::ScalarTraits< Scalar >::magnitudeType magnitude_type
The type of a norm result.
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
Default valid parameter list.
Traits class which defines basic operations on multivectors.
Scalar scalar_type
The template parameter of this class; the type of an inner product result.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
virtual void operator()(Teuchos::ArrayView< magnitude_type > normsBeforeFirstPass, Teuchos::ArrayView< magnitude_type > normsAfterFirstPass)=0
Callback invoked by TsqrOrthoManager on reorthogonalization.
const std::string & getLabel() const
Get the label for timers (if timers are enabled).
magnitude_type orthonormError(const MV &X) const
Return .
Exception thrown to signal error in an orthogonalization manager method.
Teuchos::ScalarTraits< Scalar >::magnitudeType magnitude_type
magnitude_type relativeRankTolerance() const
Relative tolerance for determining (via the SVD) whether a block is of full numerical rank...
void resize(size_type new_size, const value_type &x=value_type())
TsqrOrthoError(const std::string &what_arg)
OrdinalType numCols() const
virtual ~ReorthogonalizationCallback()
Destructor (virtual for memory safety of derived classes)
Templated virtual class for providing orthogonalization/orthonormalization methods.
TsqrOrthoFault(const std::string &what_arg)
magnitude_type blockReorthogThreshold() const
Relative tolerance for triggering a block reorthogonalization.
int projectAndNormalizeOutOfPlace(MV &X_in, MV &X_out, Teuchos::Array< mat_ptr > C, mat_ptr B, Teuchos::ArrayView< Teuchos::RCP< const MV > > Q)
Project and normalize X_in into X_out; overwrite X_in.
int normalizeOutOfPlace(MV &X, MV &Q, mat_ptr B)
Normalize X into Q*B, overwriting X.
void innerProd(const MV &X, const MV &Y, mat_type &Z) const
Euclidean inner product.
Belos header file which uses auto-configuration information to include necessary C++ headers...
OrdinalType numRows() const
ScalarTraits< ScalarType >::magnitudeType NRM2(const OrdinalType n, const ScalarType *x, const OrdinalType incx) const