32 #ifndef __AnasaziTsqrOrthoManagerImpl_hpp
33 #define __AnasaziTsqrOrthoManagerImpl_hpp
42 #include "Teuchos_ParameterListAcceptorDefaultBase.hpp"
114 template<
class Scalar,
class MV>
118 typedef Scalar scalar_type;
120 typedef MV multivector_type;
131 typedef typename MVT::tsqr_adaptor_type tsqr_adaptor_type;
175 const std::string& label);
191 if (label != label_) {
197 const std::string&
getLabel ()
const {
return label_; }
231 norm (
const MV& X, std::vector<magnitude_type>& normvec)
const;
304 return projectAndNormalizeImpl (X, X,
false, C, B, Q);
335 return projectAndNormalizeImpl (X_in, X_out,
true, C, B, Q);
349 for (
int k = 0; k < ncols; ++k) {
362 mat_type X1_T_X2 (ncols_X1, ncols_X2);
387 tsqr_adaptor_type tsqrAdaptor_;
407 bool randomizeNullSpace_;
414 bool reorthogonalizeBlocks_;
419 bool throwOnReorthogFault_;
422 magnitude_type blockReorthogThreshold_;
425 magnitude_type relativeRankTolerance_;
433 bool forceNonnegativeDiagonal_;
437 raiseReorthogFault (
const std::vector<magnitude_type>& normsAfterFirstPass,
438 const std::vector<magnitude_type>& normsAfterSecondPass,
439 const std::vector<int>& faultIndices);
451 checkProjectionDims (
int& ncols_X,
471 const bool attemptToRecycle =
true)
const;
482 projectAndNormalizeImpl (MV& X_in,
484 const bool outOfPlace,
503 const mat_ptr& C)
const;
531 int rawNormalize (MV& X, MV& Q,
mat_type& B);
550 int normalizeOne (MV& X, mat_ptr B)
const;
579 int normalizeImpl (MV& X, MV& Q, mat_ptr B,
const bool outOfPlace);
582 template<
class Scalar,
class MV>
588 using Teuchos::parameterList;
590 using Teuchos::sublist;
591 typedef magnitude_type M;
593 RCP<const ParameterList> defaultParams = getValidParameters ();
595 RCP<ParameterList> tsqrParams;
597 RCP<ParameterList> theParams;
599 theParams = parameterList (*defaultParams);
607 randomizeNullSpace_ =
608 theParams->
get<
bool> (
"randomizeNullSpace",
609 defaultParams->get<
bool> (
"randomizeNullSpace"));
610 reorthogonalizeBlocks_ =
611 theParams->get<
bool> (
"reorthogonalizeBlocks",
612 defaultParams->get<
bool> (
"reorthogonalizeBlocks"));
613 throwOnReorthogFault_ =
614 theParams->get<
bool> (
"throwOnReorthogFault",
615 defaultParams->get<
bool> (
"throwOnReorthogFault"));
616 blockReorthogThreshold_ =
617 theParams->get<M> (
"blockReorthogThreshold",
618 defaultParams->get<M> (
"blockReorthogThreshold"));
619 relativeRankTolerance_ =
620 theParams->get<M> (
"relativeRankTolerance",
621 defaultParams->get<M> (
"relativeRankTolerance"));
622 forceNonnegativeDiagonal_ =
623 theParams->get<
bool> (
"forceNonnegativeDiagonal",
624 defaultParams->get<
bool> (
"forceNonnegativeDiagonal"));
628 if (! theParams->isSublist (
"TSQR implementation")) {
629 theParams->set (
"TSQR implementation",
630 defaultParams->sublist (
"TSQR implementation"));
632 tsqrParams = sublist (theParams,
"TSQR implementation",
true);
636 tsqrAdaptor_.setParameterList (tsqrParams);
639 setMyParamList (theParams);
642 template<
class Scalar,
class MV>
645 const std::string& label) :
649 randomizeNullSpace_ (true),
650 reorthogonalizeBlocks_ (true),
651 throwOnReorthogFault_ (false),
652 blockReorthogThreshold_ (0),
653 relativeRankTolerance_ (0),
654 forceNonnegativeDiagonal_ (false)
659 template<
class Scalar,
class MV>
665 randomizeNullSpace_ (true),
666 reorthogonalizeBlocks_ (true),
667 throwOnReorthogFault_ (false),
668 blockReorthogThreshold_ (0),
669 relativeRankTolerance_ (0),
670 forceNonnegativeDiagonal_ (false)
675 template<
class Scalar,
class MV>
678 norm (
const MV& X, std::vector<magnitude_type>& normVec)
const
680 const int numCols = MVT::GetNumberVecs (X);
683 if (normVec.size() <
static_cast<size_t>(numCols))
684 normVec.resize (numCols);
685 MVT::MvNorm (X, normVec);
688 template<
class Scalar,
class MV>
694 int ncols_X, num_Q_blocks, ncols_Q_total;
695 checkProjectionDims (ncols_X, num_Q_blocks, ncols_Q_total, X, Q);
699 if (ncols_X == 0 || num_Q_blocks == 0 || ncols_Q_total == 0)
703 allocateProjectionCoefficients (C, Q, X,
true);
707 std::vector<magnitude_type> columnNormsBefore (ncols_X, magnitude_type(0));
708 if (reorthogonalizeBlocks_)
709 MVT::MvNorm (X, columnNormsBefore);
713 rawProject (X, Q, C);
717 if (reorthogonalizeBlocks_)
719 std::vector<magnitude_type> columnNormsAfter (ncols_X, magnitude_type(0));
720 MVT::MvNorm (X, columnNormsAfter);
723 const magnitude_type relThres = blockReorthogThreshold();
729 bool reorthogonalize =
false;
730 for (
int j = 0; j < ncols_X; ++j)
731 if (columnNormsAfter[j] < relThres * columnNormsBefore[j])
733 reorthogonalize =
true;
740 allocateProjectionCoefficients (C2, Q, X,
false);
744 rawProject (X, Q, C2);
746 for (
int k = 0; k < num_Q_blocks; ++k)
754 template<
class Scalar,
class MV>
764 const int numCols = MVT::GetNumberVecs (X);
773 return normalizeOne (X, B);
803 MVText::GetGlobalLength(*Q_) != MVText::GetGlobalLength(X) ||
804 numCols > MVT::GetNumberVecs (*Q_)) {
805 Q_ = MVT::Clone (X, numCols);
814 if (MVT::GetNumberVecs(*Q_) == numCols) {
815 return normalizeImpl (X, *Q_, B,
false);
817 RCP<MV> Q_view = MVT::CloneViewNonConst (*Q_, Range1D(0, numCols-1));
818 return normalizeImpl (X, *Q_view, B,
false);
822 template<
class Scalar,
class MV>
828 const bool attemptToRecycle)
const
830 const int num_Q_blocks = Q.size();
831 const int ncols_X = MVT::GetNumberVecs (X);
833 if (attemptToRecycle)
835 for (
int i = 0; i < num_Q_blocks; ++i)
837 const int ncols_Qi = MVT::GetNumberVecs (*Q[i]);
841 C[i] =
rcp (
new mat_type (ncols_Qi, ncols_X));
844 mat_type& Ci = *C[i];
845 if (Ci.numRows() != ncols_Qi || Ci.numCols() != ncols_X)
846 Ci.shape (ncols_Qi, ncols_X);
848 Ci.putScalar (SCT::zero());
854 for (
int i = 0; i < num_Q_blocks; ++i)
856 const int ncols_Qi = MVT::GetNumberVecs (*Q[i]);
857 C[i] =
rcp (
new mat_type (ncols_Qi, ncols_X));
862 template<
class Scalar,
class MV>
867 const int numVecs = MVT::GetNumberVecs(X);
870 }
else if (numVecs == 1) {
877 const int rank = normalizeOne (X, B);
879 RCP<MV> Q_0 = MVT::CloneViewNonConst (Q, Range1D(0,0));
880 MVT::Assign (X, *Q_0);
886 return normalizeImpl (X, Q, B,
true);
890 template<
class Scalar,
class MV>
895 const bool outOfPlace,
907 std::invalid_argument,
908 "Belos::TsqrOrthoManagerImpl::"
909 "projectAndNormalizeOutOfPlace(...):"
910 "X_out has " << MVT::GetNumberVecs(X_out)
911 <<
" columns, but X_in has "
912 << MVT::GetNumberVecs(X_in) <<
" columns.");
916 int ncols_X, num_Q_blocks, ncols_Q_total;
917 checkProjectionDims (ncols_X, num_Q_blocks, ncols_Q_total, X_in, Q);
924 if (num_Q_blocks == 0 || ncols_Q_total == 0) {
926 return normalizeOutOfPlace (X_in, X_out, B);
928 return normalize (X_in, B);
935 allocateProjectionCoefficients (C, Q, X_in,
true);
940 std::vector<magnitude_type> normsBeforeFirstPass (ncols_X, SCTM::zero());
941 if (reorthogonalizeBlocks_) {
942 MVT::MvNorm (X_in, normsBeforeFirstPass);
946 rawProject (X_in, Q, C);
959 B_out =
rcp (
new mat_type (ncols_X, ncols_X));
963 std::invalid_argument,
964 "normalizeOne: Input matrix B must be at "
965 "least " << ncols_X <<
" x " << ncols_X
966 <<
", but is instead " << B->numRows()
967 <<
" x " << B->numCols() <<
".");
977 const int firstPassRank = outOfPlace ?
978 normalizeOutOfPlace (X_in, X_out, B_out) :
979 normalize (X_in, B_out);
988 int rank = firstPassRank;
1004 if (firstPassRank < ncols_X && randomizeNullSpace_) {
1005 const int numNullSpaceCols = ncols_X - firstPassRank;
1006 const Range1D nullSpaceIndices (firstPassRank, ncols_X - 1);
1010 for (
int k = 0; k < num_Q_blocks; ++k) {
1011 const int numColsQk = MVT::GetNumberVecs(*Q[k]);
1012 C_null[k] =
rcp (
new mat_type (numColsQk, numNullSpaceCols));
1015 RCP<mat_type> B_null (
new mat_type (numNullSpaceCols, numNullSpaceCols));
1017 int randomVectorsRank;
1021 RCP<MV> X_out_null = MVT::CloneViewNonConst (X_out, nullSpaceIndices);
1026 RCP<MV> X_in_null = MVT::CloneViewNonConst (X_in, nullSpaceIndices);
1027 MVT::Assign (*X_out_null, *X_in_null);
1030 rawProject (*X_in_null, Q, C_null);
1031 randomVectorsRank = normalizeOutOfPlace (*X_in_null, *X_out_null, B_null);
1035 RCP<MV> X_null = MVT::CloneViewNonConst (X_in, nullSpaceIndices);
1038 rawProject (*X_null, Q, C_null);
1039 randomVectorsRank = normalize (*X_null, B_null);
1047 "Belos::TsqrOrthoManagerImpl::projectAndNormalize"
1048 "OutOfPlace(): After projecting and normalizing the "
1049 "random vectors (used to replace the null space "
1050 "basis vectors from normalizing X), they have rank "
1051 << randomVectorsRank <<
", but should have full "
1052 "rank " << numNullSpaceCols <<
".");
1057 if (reorthogonalizeBlocks_) {
1060 std::vector<magnitude_type>
1061 normsAfterFirstPass (firstPassRank, SCTM::zero());
1062 std::vector<magnitude_type>
1063 normsAfterSecondPass (firstPassRank, SCTM::zero());
1078 for (
int j = 0; j < firstPassRank; ++j) {
1079 const Scalar*
const B_j = &(*B_out)(0,j);
1082 normsAfterFirstPass[j] = blas.
NRM2 (firstPassRank, B_j, 1);
1086 bool reorthogonalize =
false;
1087 for (
int j = 0; j < firstPassRank; ++j) {
1094 const magnitude_type curThreshold =
1095 blockReorthogThreshold() * normsBeforeFirstPass[j];
1096 if (normsAfterFirstPass[j] < curThreshold) {
1097 reorthogonalize =
true;
1112 bool reorthogFault =
false;
1114 std::vector<int> faultIndices;
1115 if (reorthogonalize) {
1123 MVT::Assign (X_out, X_in);
1129 allocateProjectionCoefficients (C2, Q, X_in,
false);
1134 rawProject (X_in, Q, C2);
1137 RCP<mat_type> B2 (
new mat_type (ncols_X, ncols_X));
1140 const int secondPassRank = outOfPlace ?
1141 normalizeOutOfPlace (X_in, X_out, B2) :
1142 normalize (X_in, B2);
1143 rank = secondPassRank;
1148 mat_type B_copy (Copy, *B_out, B_out->numRows(), B_out->numCols());
1150 const int err = B_out->multiply (NO_TRANS, NO_TRANS, SCT::one(),
1151 *B2, B_copy, SCT::zero());
1153 "Teuchos::SerialDenseMatrix::multiply "
1154 "returned err = " << err <<
" != 0");
1158 for (
int k = 0; k < num_Q_blocks; ++k) {
1159 mat_type C_k_copy (Copy, *C[k], C[k]->numRows(), C[k]->numCols());
1162 const int err2 = C[k]->multiply (NO_TRANS, NO_TRANS, SCT::one(),
1163 *C2[k], B_copy, SCT::one());
1165 "Teuchos::SerialDenseMatrix::multiply "
1166 "returned err = " << err <<
" != 0");
1171 for (
int j = 0; j < rank; ++j) {
1172 const Scalar*
const B2_j = &(*B2)(0,j);
1173 normsAfterSecondPass[j] = blas.
NRM2 (rank, B2_j, 1);
1178 reorthogFault =
false;
1179 for (
int j = 0; j < rank; ++j) {
1180 const magnitude_type relativeLowerBound =
1181 blockReorthogThreshold() * normsAfterFirstPass[j];
1182 if (normsAfterSecondPass[j] < relativeLowerBound) {
1183 reorthogFault =
true;
1184 faultIndices.push_back (j);
1189 if (reorthogFault) {
1190 if (throwOnReorthogFault_) {
1191 raiseReorthogFault (normsAfterFirstPass,
1192 normsAfterSecondPass,
1201 "TsqrOrthoManagerImpl has not yet implemented"
1202 " recovery from an orthogonalization fault.");
1210 template<
class Scalar,
class MV>
1212 TsqrOrthoManagerImpl<Scalar, MV>::
1213 raiseReorthogFault (
const std::vector<magnitude_type>& normsAfterFirstPass,
1214 const std::vector<magnitude_type>& normsAfterSecondPass,
1215 const std::vector<int>& faultIndices)
1218 typedef std::vector<int>::size_type size_type;
1219 std::ostringstream os;
1221 os <<
"Orthogonalization fault at the following column(s) of X:" << endl;
1222 os <<
"Column\tNorm decrease factor" << endl;
1223 for (size_type k = 0; k < faultIndices.size(); ++k)
1225 const int index = faultIndices[k];
1226 const magnitude_type decreaseFactor =
1227 normsAfterSecondPass[index] / normsAfterFirstPass[index];
1228 os << index <<
"\t" << decreaseFactor << endl;
1230 throw TsqrOrthoFault (os.str());
1233 template<
class Scalar,
class MV>
1238 using Teuchos::parameterList;
1241 if (defaultParams_.is_null()) {
1242 RCP<ParameterList> params = parameterList (
"TsqrOrthoManagerImpl");
1246 params->set (
"TSQR implementation", *(tsqrAdaptor_.getValidParameters()),
1247 "TSQR implementation parameters.");
1251 const bool defaultRandomizeNullSpace =
true;
1252 params->set (
"randomizeNullSpace", defaultRandomizeNullSpace,
1253 "Whether to fill in null space vectors with random data.");
1255 const bool defaultReorthogonalizeBlocks =
true;
1256 params->set (
"reorthogonalizeBlocks", defaultReorthogonalizeBlocks,
1257 "Whether to do block reorthogonalization as necessary.");
1261 const magnitude_type defaultBlockReorthogThreshold =
1262 magnitude_type(10) * SCTM::squareroot (SCTM::eps());
1263 params->set (
"blockReorthogThreshold", defaultBlockReorthogThreshold,
1264 "If reorthogonalizeBlocks==true, and if the norm of "
1265 "any column within a block decreases by this much or "
1266 "more after orthogonalization, we reorthogonalize.");
1270 const magnitude_type defaultRelativeRankTolerance =
1271 Teuchos::as<magnitude_type>(10) * SCTM::eps();
1276 params->set (
"relativeRankTolerance", defaultRelativeRankTolerance,
1277 "Relative tolerance to determine the numerical rank of a "
1278 "block when normalizing.");
1282 const bool defaultThrowOnReorthogFault =
true;
1283 params->set (
"throwOnReorthogFault", defaultThrowOnReorthogFault,
1284 "Whether to throw an exception if an orthogonalization "
1285 "fault occurs. This only matters if reorthogonalization "
1286 "is enabled (reorthogonalizeBlocks==true).");
1288 const bool defaultForceNonnegativeDiagonal =
false;
1289 params->set (
"forceNonnegativeDiagonal", defaultForceNonnegativeDiagonal,
1290 "Whether to force the R factor produced by the normalization "
1291 "step to have a nonnegative diagonal.");
1293 defaultParams_ = params;
1295 return defaultParams_;
1298 template<
class Scalar,
class MV>
1306 RCP<const ParameterList> defaultParams = getValidParameters();
1308 RCP<ParameterList> params =
rcp (
new ParameterList (*defaultParams));
1317 const bool randomizeNullSpace =
false;
1318 params->set (
"randomizeNullSpace", randomizeNullSpace);
1319 const bool reorthogonalizeBlocks =
false;
1320 params->set (
"reorthogonalizeBlocks", reorthogonalizeBlocks);
1325 template<
class Scalar,
class MV>
1337 tsqrAdaptor_.factorExplicit (X, Q, B, forceNonnegativeDiagonal_);
1340 rank = tsqrAdaptor_.revealRank (Q, B, relativeRankTolerance_);
1341 }
catch (std::exception& e) {
1342 throw TsqrOrthoError (e.what());
1347 template<
class Scalar,
class MV>
1349 TsqrOrthoManagerImpl<Scalar, MV>::
1350 normalizeOne (MV& X,
1358 B_out =
rcp (
new mat_type (1, 1));
1360 const int numRows = B->
numRows();
1361 const int numCols = B->
numCols();
1363 std::invalid_argument,
1364 "normalizeOne: Input matrix B must be at "
1365 "least 1 x 1, but is instead " << numRows
1366 <<
" x " << numCols <<
".");
1372 std::vector<magnitude_type> theNorm (1, SCTM::zero());
1373 MVT::MvNorm (X, theNorm);
1374 (*B_out)(0,0) = theNorm[0];
1388 if (theNorm[0] == SCTM::zero()) {
1391 if (randomizeNullSpace_) {
1393 MVT::MvNorm (X, theNorm);
1394 if (theNorm[0] == SCTM::zero()) {
1399 throw TsqrOrthoError(
"normalizeOne: a supposedly random "
1400 "vector has norm zero!");
1405 const Scalar alpha = SCT::one() / theNorm[0];
1406 MVT::MvScale (X, alpha);
1413 const Scalar alpha = SCT::one() / theNorm[0];
1414 MVT::MvScale (X, alpha);
1420 template<
class Scalar,
class MV>
1422 TsqrOrthoManagerImpl<Scalar, MV>::
1428 const int num_Q_blocks = Q.size();
1429 for (
int i = 0; i < num_Q_blocks; ++i)
1437 mat_type& Ci = *C[i];
1438 const MV& Qi = *Q[i];
1439 innerProd (Qi, X, Ci);
1440 MVT::MvTimesMatAddMv (-SCT::one(), Qi, Ci, SCT::one(), X);
1445 template<
class Scalar,
class MV>
1447 TsqrOrthoManagerImpl<Scalar, MV>::
1453 innerProd (*Q, X, *C);
1454 MVT::MvTimesMatAddMv (-SCT::one(), *Q, *C, SCT::one(), X);
1457 template<
class Scalar,
class MV>
1459 TsqrOrthoManagerImpl<Scalar, MV>::
1460 normalizeImpl (MV& X,
1463 const bool outOfPlace)
1469 using Teuchos::tuple;
1474 const bool extraDebug =
false;
1476 const int numCols = MVT::GetNumberVecs (X);
1484 std::invalid_argument,
1485 "TsqrOrthoManagerImpl::normalizeImpl(X,Q,B): Q has "
1486 << MVT::GetNumberVecs(Q) <<
" columns. This is too "
1487 "few, since X has " << numCols <<
" columns.");
1491 RCP<MV> Q_view = MVT::CloneViewNonConst (Q, Range1D(0, numCols-1));
1499 B_out =
rcp (
new mat_type (numCols, numCols));
1503 std::invalid_argument,
1504 "normalizeOne: Input matrix B must be at "
1505 "least " << numCols <<
" x " << numCols
1506 <<
", but is instead " << B->
numRows()
1507 <<
" x " << B->
numCols() <<
".");
1514 std::vector<magnitude_type> norms (numCols);
1515 MVT::MvNorm (X, norms);
1516 cerr <<
"Column norms of X before orthogonalization: ";
1517 typedef typename std::vector<magnitude_type>::const_iterator iter_type;
1518 for (iter_type iter = norms.begin(); iter != norms.end(); ++iter) {
1520 if (iter+1 != norms.end())
1534 const int rank = rawNormalize (X, *Q_view, *B_out);
1545 std::vector<magnitude_type> norms (numCols);
1546 MVT::MvNorm (*Q_view, norms);
1547 cerr <<
"Column norms of Q_view after orthogonalization: ";
1548 for (
typename std::vector<magnitude_type>::const_iterator iter = norms.begin();
1549 iter != norms.end(); ++iter) {
1551 if (iter+1 != norms.end())
1556 "Belos::TsqrOrthoManagerImpl::normalizeImpl: "
1557 "rawNormalize() returned rank = " << rank <<
" for a "
1558 "matrix X with " << numCols <<
" columns. Please report"
1559 " this bug to the Belos developers.");
1560 if (extraDebug && rank == 0) {
1563 const mat_type& B_ref = *B;
1564 std::vector<magnitude_type> norms (B_ref.numCols());
1565 for (
typename mat_type::ordinalType j = 0; j < B_ref.numCols(); ++j) {
1566 typedef typename mat_type::scalarType mat_scalar_type;
1567 mat_scalar_type sumOfSquares = ScalarTraits<mat_scalar_type>::zero();
1568 for (
typename mat_type::ordinalType i = 0; i <= j; ++i) {
1569 const mat_scalar_type B_ij =
1570 ScalarTraits<mat_scalar_type>::magnitude (B_ref(i,j));
1571 sumOfSquares += B_ij*B_ij;
1573 norms[j] = ScalarTraits<mat_scalar_type>::squareroot (sumOfSquares);
1577 cerr <<
"Norms of columns of B after orthogonalization: ";
1578 for (
typename mat_type::ordinalType j = 0; j < B_ref.numCols(); ++j) {
1580 if (j != B_ref.numCols() - 1)
1588 if (rank == numCols || ! randomizeNullSpace_) {
1592 MVT::Assign (*Q_view, X);
1597 if (randomizeNullSpace_ && rank < numCols) {
1604 const int nullSpaceNumCols = numCols - rank;
1607 Range1D nullSpaceIndices (rank, numCols-1);
1614 RCP<MV> Q_null = MVT::CloneViewNonConst (*Q_view, nullSpaceIndices);
1616 MVT::MvRandom (*Q_null);
1622 std::vector<magnitude_type> norms (MVT::GetNumberVecs(*Q_null));
1623 MVT::MvNorm (*Q_null, norms);
1625 bool anyZero =
false;
1626 typedef typename std::vector<magnitude_type>::const_iterator iter_type;
1627 for (iter_type it = norms.begin(); it != norms.end(); ++it) {
1628 if (*it == SCTM::zero()) {
1633 std::ostringstream os;
1634 os <<
"TsqrOrthoManagerImpl::normalizeImpl: "
1635 "We are being asked to randomize the null space, for a matrix "
1636 "with " << numCols <<
" columns and reported column rank "
1637 << rank <<
". The inclusive range of columns to fill with "
1638 "random data is [" << nullSpaceIndices.lbound() <<
","
1639 << nullSpaceIndices.ubound() <<
"]. After filling the null "
1640 "space vectors with random numbers, at least one of the vectors"
1641 " has norm zero. Here are the norms of all the null space "
1643 for (iter_type it = norms.begin(); it != norms.end(); ++it) {
1645 if (it+1 != norms.end())
1648 os <<
"].) There is a tiny probability that this could happen "
1649 "randomly, but it is likely a bug. Please report it to the "
1650 "Belos developers, especially if you are able to reproduce the "
1663 RCP<const MV> Q_col = MVT::CloneView (*Q_view, Range1D(0, rank-1));
1668 mat_ptr C_null (
new mat_type (rank, nullSpaceNumCols));
1669 rawProject (*Q_null, Q_col, C_null);
1678 RCP<MV> X_null = MVT::CloneViewNonConst (X, nullSpaceIndices);
1681 mat_type B_null (nullSpaceNumCols, nullSpaceNumCols);
1683 const int nullSpaceBasisRank = rawNormalize (*Q_null, *X_null, B_null);
1697 if (nullSpaceBasisRank < nullSpaceNumCols) {
1698 std::vector<magnitude_type> norms (MVT::GetNumberVecs(*X_null));
1699 MVT::MvNorm (*X_null, norms);
1700 std::ostringstream os;
1701 os <<
"TsqrOrthoManagerImpl::normalizeImpl: "
1702 <<
"We are being asked to randomize the null space, "
1703 <<
"for a matrix with " << numCols <<
" columns and "
1704 <<
"column rank " << rank <<
". After projecting and "
1705 <<
"normalizing the generated random vectors, they "
1706 <<
"only have rank " << nullSpaceBasisRank <<
". They"
1707 <<
" should have full rank " << nullSpaceNumCols
1708 <<
". (The inclusive range of columns to fill with "
1709 <<
"random data is [" << nullSpaceIndices.lbound()
1710 <<
"," << nullSpaceIndices.ubound() <<
"]. The "
1711 <<
"column norms of the resulting Q factor are: [";
1712 for (
typename std::vector<magnitude_type>::size_type k = 0;
1713 k < norms.size(); ++k) {
1715 if (k != norms.size()-1) {
1719 os <<
"].) There is a tiny probability that this could "
1720 <<
"happen randomly, but it is likely a bug. Please "
1721 <<
"report it to the Belos developers, especially if "
1722 <<
"you are able to reproduce the behavior.";
1725 TsqrOrthoError, os.str());
1735 MVT::Assign (*X_null, *Q_null);
1736 }
else if (rank > 0) {
1738 RCP<const MV> Q_col = MVT::CloneView (*Q_view, Range1D(0, rank-1));
1739 RCP<MV> X_col = MVT::CloneViewNonConst (X, Range1D(0, rank-1));
1740 MVT::Assign (*Q_col, *X_col);
1747 template<
class Scalar,
class MV>
1749 TsqrOrthoManagerImpl<Scalar, MV>::
1750 checkProjectionDims (
int& ncols_X,
1762 int the_ncols_X, the_num_Q_blocks, the_ncols_Q_total;
1763 the_num_Q_blocks = Q.size();
1764 the_ncols_X = MVT::GetNumberVecs (X);
1767 the_ncols_Q_total = 0;
1772 typedef typename ArrayView<RCP<const MV> >::const_iterator iter_type;
1773 for (iter_type it = Q.begin(); it != Q.end(); ++it) {
1774 const MV& Qi = **it;
1775 the_ncols_Q_total += MVT::GetNumberVecs (Qi);
1779 ncols_X = the_ncols_X;
1780 num_Q_blocks = the_num_Q_blocks;
1781 ncols_Q_total = the_ncols_Q_total;
1786 #endif // __AnasaziTsqrOrthoManagerImpl_hpp
An extension of the MultiVecTraits class that adds a new vector length method.This traits class provi...
bool is_null(const boost::shared_ptr< T > &p)
TSQR-based OrthoManager subclass implementation.
Declaration of basic traits for the multivector type.
T & get(const std::string &name, T def_value)
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.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Teuchos::SerialDenseMatrix< int, Scalar > mat_type
magnitude_type blockReorthogThreshold() const
magnitude_type orthonormError(const MV &X) const
Return .
void innerProd(const MV &X, const MV &Y, mat_type &Z) const
Euclidean inner product.
void norm(const MV &X, std::vector< magnitude_type > &normvec) const
ScalarTraits< ScalarType >::magnitudeType normFrobenius() const
void setParameterList(const Teuchos::RCP< Teuchos::ParameterList > ¶ms)
Set parameters from the given parameter list.
TsqrOrthoManagerImpl(const Teuchos::RCP< Teuchos::ParameterList > ¶ms, const std::string &label)
Constructor (that sets user-specified parameters).
TsqrOrthoManager(Impl) error.
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 .
void setLabel(const std::string &label)
Set the label for timers.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Templated virtual class for providing orthogonalization/orthonormalization methods.
Traits class which defines basic operations on multivectors.
const std::string & getLabel() const
Get the label for timers (if timers are enabled).
Teuchos::RCP< const Teuchos::ParameterList > getFastParameters()
Get "fast" parameters for TsqrOrthoManagerImpl.
void project(MV &X, Teuchos::Array< mat_ptr > C, Teuchos::ArrayView< Teuchos::RCP< const MV > > Q)
Compute and .
Anasazi header file which uses auto-configuration information to include necessary C++ headers...
void resize(size_type new_size, const value_type &x=value_type())
Exception thrown to signal error in an orthogonalization manager method.
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.
static int GetNumberVecs(const MV &mv)
Obtain the number of vectors in mv.
OrdinalType numCols() const
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
Default valid parameter list.
magnitude_type relativeRankTolerance() const
magnitude_type orthogError(const MV &X1, const MV &X2) const
Return the Frobenius norm of the inner product of X1 with itself.
int normalize(MV &X, mat_ptr B)
Orthogonalize the columns of X in place.
OrdinalType numRows() const
ScalarTraits< ScalarType >::magnitudeType NRM2(const OrdinalType n, const ScalarType *x, const OrdinalType incx) const