42 #ifndef _TEUCHOS_SERIALQRDENSESOLVER_HPP_
43 #define _TEUCHOS_SERIALQRDENSESOLVER_HPP_
57 #ifdef HAVE_TEUCHOSNUMERICS_EIGEN
58 #include "Eigen/Dense"
131 template<
typename OrdinalType,
typename ScalarType>
133 public LAPACK<OrdinalType, ScalarType>
139 #ifdef HAVE_TEUCHOSNUMERICS_EIGEN
140 typedef typename Eigen::Matrix<ScalarType,Eigen::Dynamic,Eigen::Dynamic,Eigen::ColMajor> EigenMatrix;
141 typedef typename Eigen::Matrix<ScalarType,Eigen::Dynamic,1> EigenVector;
142 typedef typename Eigen::InnerStride<Eigen::Dynamic> EigenInnerStride;
143 typedef typename Eigen::OuterStride<Eigen::Dynamic> EigenOuterStride;
144 typedef typename Eigen::Map<EigenVector,0,EigenInnerStride > EigenVectorMap;
145 typedef typename Eigen::Map<const EigenVector,0,EigenInnerStride > EigenConstVectorMap;
146 typedef typename Eigen::Map<EigenMatrix,0,EigenOuterStride > EigenMatrixMap;
147 typedef typename Eigen::Map<const EigenMatrix,0,EigenOuterStride > EigenConstMatrixMap;
148 typedef typename Eigen::PermutationMatrix<Eigen::Dynamic,Eigen::Dynamic,OrdinalType> EigenPermutationMatrix;
149 typedef typename Eigen::Array<OrdinalType,Eigen::Dynamic,1> EigenOrdinalArray;
150 typedef typename Eigen::Map<EigenOrdinalArray> EigenOrdinalArrayMap;
151 typedef typename Eigen::Array<ScalarType,Eigen::Dynamic,1> EigenScalarArray;
152 typedef typename Eigen::Map<EigenScalarArray> EigenScalarArrayMap;
320 std::vector<ScalarType>
tau()
const {
return(
TAU_);}
329 void Print(std::ostream& os)
const;
382 #ifdef HAVE_TEUCHOSNUMERICS_EIGEN
383 Eigen::HouseholderQR<EigenMatrix> qr_;
395 using namespace details;
399 template<
typename OrdinalType,
typename ScalarType>
402 Object(
"Teuchos::SerialQRDenseSolver"),
404 shouldEquilibrate_(false),
405 equilibratedA_(false),
406 equilibratedB_(false),
407 equilibrationModeA_(0),
408 equilibrationModeB_(0),
412 matrixIsZero_(false),
436 template<
typename OrdinalType,
typename ScalarType>
442 template<
typename OrdinalType,
typename ScalarType>
449 equilibratedB_ =
false;
453 template<
typename OrdinalType,
typename ScalarType>
457 equilibratedA_ =
false;
458 equilibrationModeA_ = 0;
459 equilibrationModeB_ = 0;
461 matrixIsZero_ =
false;
482 template<
typename OrdinalType,
typename ScalarType>
486 "SerialQRDenseSolver<T>::setMatrix: the matrix A must have A.numRows() >= A.numCols()!");
509 template<
typename OrdinalType,
typename ScalarType>
515 "SerialQRDenseSolver<T>::setVectors: X and B have different numbers of columns!");
517 "SerialQRDenseSolver<T>::setVectors: B is an empty SerialDenseMatrix<T>!");
519 "SerialQRDenseSolver<T>::setVectors: X is an empty SerialDenseMatrix<T>!");
521 "SerialQRDenseSolver<T>::setVectors: B has an invalid stride!");
523 "SerialQRDenseSolver<T>::setVectors: X has an invalid stride!");
533 template<
typename OrdinalType,
typename ScalarType>
536 if (factored())
return(0);
540 if (equilibrate_) ierr = equilibrateMatrix();
541 if (ierr!=0)
return(ierr);
544 if ((
int)TAU_.size()!=Min_MN_) TAU_.resize( Min_MN_ );
549 #ifdef HAVE_TEUCHOSNUMERICS_EIGEN
550 EigenMatrixMap aMap( AF_, M_, N_, EigenOuterStride(LDAF_) );
551 EigenScalarArray tau;
552 EigenScalarArrayMap tauMap(&TAU_[0],
TEUCHOS_MIN(M_,N_));
555 for (OrdinalType i=0; i<tauMap.innerSize(); i++) {
558 EigenMatrix qrMat = qr_.matrixQR();
559 for (OrdinalType j=0; j<aMap.outerSize(); j++) {
560 for (OrdinalType i=0; i<aMap.innerSize(); i++) {
561 aMap(i,j) = qrMat(i,j);
565 this->GEQRF(M_, N_, AF_, LDAF_, &TAU_[0], &WORK_[0], LWORK_, &INFO_);
575 template<
typename OrdinalType,
typename ScalarType>
587 ierr = equilibrateRHS();
588 equilibratedB_ =
true;
590 if (ierr != 0)
return(ierr);
593 std::logic_error,
"SerialQRDenseSolver<T>::solve: Matrix and vectors must be similarly scaled!");
595 "SerialQRDenseSolver<T>::solve: No right-hand side vector (RHS) has been set for the linear system!");
597 "SerialQRDenseSolver<T>::solve: No solution vector (LHS) has been set for the linear system!");
600 "SerialQRDenseSolver<T>::solve: No transpose: must have LHS_->numRows() = N_");
603 "SerialQRDenseSolver<T>::solve: Transpose: must have LHS_->numRows() = M_");
606 if (shouldEquilibrate() && !equilibratedA_)
607 std::cout <<
"WARNING! SerialQRDenseSolver<T>::solve: System should be equilibrated!" << std::endl;
610 if (!factored()) factor();
613 for (OrdinalType j=0; j<RHS_->numCols(); j++) {
614 for (OrdinalType i=0; i<RHS_->numRows(); i++) {
615 (*TMP_)(i,j) = (*RHS_)(i,j);
632 for (OrdinalType j = 0; j < RHS_->numCols(); j++ ) {
633 for (OrdinalType i = N_; i < M_; i++ ) {
640 for (OrdinalType j = 0; j < LHS_->numCols(); j++ ) {
641 for (OrdinalType i = 0; i < LHS_->numRows(); i++ ) {
642 (*LHS_)(i, j) = (*TMP_)(i,j);
650 ierr = unequilibrateLHS();
661 template<
typename OrdinalType,
typename ScalarType>
676 for (j = 0; j < N_; j++) {
677 for (i = 0; i < M_; i++) {
684 if (ANORM_ > mZero && ANORM_ < smlnum) {
686 shouldEquilibrate_ =
true;
687 }
else if (ANORM_ > bignum) {
689 shouldEquilibrate_ =
true;
690 }
else if (ANORM_ == mZero) {
692 matrixIsZero_ =
true;
700 template<
typename OrdinalType,
typename ScalarType>
703 if (equilibratedA_)
return(0);
718 for (j = 0; j < N_; j++) {
719 for (i = 0; i < M_; i++) {
726 if (ANORM_ > mZero && ANORM_ < smlnum) {
729 equilibrationModeA_ = 1;
730 }
else if (ANORM_ > bignum) {
733 equilibrationModeA_ = 2;
734 }
else if (ANORM_ == mZero) {
736 matrixIsZero_ =
true;
739 equilibratedA_ =
true;
746 template<
typename OrdinalType,
typename ScalarType>
749 if (equilibratedB_)
return(0);
764 for (j = 0; j <RHS_->numCols(); j++) {
765 for (i = 0; i < RHS_->numRows(); i++) {
773 if (BNORM_ > mZero && BNORM_ < smlnum) {
776 RHS_->values(), RHS_->stride(), &INFO_);
777 equilibrationModeB_ = 1;
778 }
else if (BNORM_ > bignum) {
781 RHS_->values(), RHS_->stride(), &INFO_);
782 equilibrationModeB_ = 2;
785 equilibratedB_ =
true;
792 template<
typename OrdinalType,
typename ScalarType>
795 if (!equilibratedB_)
return(0);
809 if (equilibrationModeA_ == 1) {
811 LHS_->values(), LHS_->stride(), &INFO_);
812 }
else if (equilibrationModeA_ == 2) {
814 LHS_->values(), LHS_->stride(), &INFO_);
816 if (equilibrationModeB_ == 1) {
818 LHS_->values(), LHS_->stride(), &INFO_);
819 }
else if (equilibrationModeB_ == 2) {
821 LHS_->values(), LHS_->stride(), &INFO_);
829 template<
typename OrdinalType,
typename ScalarType>
833 if (!factored()) factor();
838 Q_ = FactorQ_->values();
839 LDQ_ = FactorQ_->stride();
845 #ifdef HAVE_TEUCHOSNUMERICS_EIGEN
846 EigenMatrixMap qMap( Q_, M_, N_, EigenOuterStride(LDQ_) );
847 EigenMatrix qMat = qr_.householderQ();
848 for (OrdinalType j=0; j<qMap.outerSize(); j++) {
849 for (OrdinalType i=0; i<qMap.innerSize(); i++) {
850 qMap(i,j) = qMat(i,j);
854 this->UNGQR(M_, N_, N_, Q_, LDQ_, &TAU_[0], &WORK_[0], LWORK_, &INFO_);
857 if (INFO_!=0)
return(INFO_);
866 template<
typename OrdinalType,
typename ScalarType>
870 if (!factored()) factor();
875 R_ = FactorR_->values();
876 LDR_ = FactorR_->stride();
880 for (OrdinalType j=0; j<N_; j++) {
881 for (OrdinalType i=0; i<=j; i++) {
882 (*FactorR_)(i,j) = (*Factor_)(i,j);
893 template<
typename OrdinalType,
typename ScalarType>
899 "SerialQRDenseSolver<T>::multiplyQ: C.numRows() != M_!");
901 "SerialQRDenseSolver<T>::multiplyQ: C is an empty SerialDenseMatrix<T>!");
904 if (!factored()) factor();
909 #ifdef HAVE_TEUCHOSNUMERICS_EIGEN
913 cMap = qr_.householderQ() * cMap;
916 cMap = qr_.householderQ().adjoint() * cMap;
917 for (OrdinalType j = 0; j < C.
numCols(); j++) {
918 for (OrdinalType i = N_; i < C.
numRows(); i++ ) {
932 &TAU_[0], C.
values(), C.
stride(), &WORK_[0], LWORK_, &INFO_);
938 &TAU_[0], C.
values(), C.
stride(), &WORK_[0], LWORK_, &INFO_);
940 for (OrdinalType j = 0; j < C.
numCols(); j++) {
941 for (OrdinalType i = N_; i < C.
numRows(); i++ ) {
954 template<
typename OrdinalType,
typename ScalarType>
960 "SerialQRDenseSolver<T>::solveR: must have N_ < C.numRows() < M_!");
962 "SerialQRDenseSolver<T>::solveR: C is an empty SerialDenseMatrix<T>!");
965 if (!factored()) factor();
970 #ifdef HAVE_TEUCHOSNUMERICS_EIGEN
973 for (OrdinalType j=0; j<N_; j++) {
981 qr_.matrixQR().topRows(N_).template triangularView<Eigen::Upper>().solveInPlace(cMap);
984 qr_.matrixQR().topRows(N_).template triangularView<Eigen::Upper>().adjoint().solveInPlace(cMap);
992 ScalarType* RMAT = (formedR_) ? R_ : AF_;
993 OrdinalType LDRMAT = (formedR_) ? LDR_ : LDAF_;
1016 template<
typename OrdinalType,
typename ScalarType>
1019 if (Matrix_!=
Teuchos::null) os <<
"Solver Matrix" << std::endl << *Matrix_ << std::endl;
1020 if (Factor_!=
Teuchos::null) os <<
"Solver Factored Matrix" << std::endl << *Factor_ << std::endl;
1021 if (Q_!=
Teuchos::null) os <<
"Solver Factor Q" << std::endl << *Q_ << std::endl;
1022 if (LHS_ !=
Teuchos::null) os <<
"Solver LHS" << std::endl << *LHS_ << std::endl;
1023 if (RHS_ !=
Teuchos::null) os <<
"Solver RHS" << std::endl << *RHS_ << std::endl;
ScalarType * values() const
Data array access method.
RCP< SerialDenseMatrix< OrdinalType, ScalarType > > FactorR_
int unequilibrateLHS()
Unscales the solution vectors if equilibration was used to solve the system.
RCP< SerialDenseMatrix< OrdinalType, ScalarType > > getMatrix() const
Returns pointer to current matrix.
OrdinalType numRows() const
Returns row dimension of system.
int setMatrix(const RCP< SerialDenseMatrix< OrdinalType, ScalarType > > &A)
Sets the pointers for coefficient matrix.
Templated serial dense matrix class.
OrdinalType equilibrationModeB_
int multiplyQ(ETransp transq, SerialDenseMatrix< OrdinalType, ScalarType > &C)
Left multiply the input matrix by the unitary matrix Q or its adjoint.
int solveR(ETransp transr, SerialDenseMatrix< OrdinalType, ScalarType > &C)
Solve input matrix on the left with the upper triangular matrix R or its adjoint. ...
int formQ()
Explicitly forms the unitary matrix Q.
Templated interface class to BLAS routines.
std::vector< ScalarType > WORK_
Teuchos::ScalarTraits< ScalarType >::magnitudeType MagnitudeType
bool formedQ()
Returns true if Q has been formed explicitly.
std::vector< ScalarType > TAU_
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
bool equilibratedA()
Returns true if factor is equilibrated (factor available via getFactoredMatrix()).
RCP< SerialDenseMatrix< OrdinalType, ScalarType > > getRHS() const
Returns pointer to current RHS.
RCP< SerialDenseMatrix< OrdinalType, ScalarType > > LHS_
Teuchos header file which uses auto-configuration information to include necessary C++ headers...
RCP< SerialDenseMatrix< OrdinalType, ScalarType > > TMP_
SerialQRDenseSolver & operator=(const SerialQRDenseSolver< OrdinalType, ScalarType > &Source)
Object for storing data and providing functionality that is common to all computational classes...
bool factored()
Returns true if matrix is factored (factor available via getFactoredMatrix()).
int solve()
Computes the solution X to AX = B for the this matrix and the B provided to SetVectors()..
TEUCHOSNUMERICS_LIB_DLL_EXPORT const char EDiagChar[]
static magnitudeType sfmin()
Returns safe minimum (sfmin), such that 1/sfmin does not overflow.
OrdinalType equilibrationModeA_
This structure defines some basic traits for a scalar field type.
A class for solving dense linear problems.
Templated class for solving dense linear problems.
Functionality and data that is common to all computational classes.
RCP< SerialDenseMatrix< OrdinalType, ScalarType > > Matrix_
Templated interface class to LAPACK routines.
void solveWithTransposeFlag(Teuchos::ETransp trans)
All subsequent function calls will work with the transpose-type set by this method (Teuchos::NO_TRANS...
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
static magnitudeType prec()
Returns eps*base.
RCP< SerialDenseMatrix< OrdinalType, ScalarType > > FactorQ_
TEUCHOSNUMERICS_LIB_DLL_EXPORT const char EUploChar[]
void Print(std::ostream &os) const
Print service methods; defines behavior of ostream << operator.
TEUCHOSNUMERICS_LIB_DLL_EXPORT const char ETypeChar[]
int equilibrateRHS()
Equilibrates the current RHS.
RCP< SerialDenseMatrix< OrdinalType, ScalarType > > getFactoredMatrix() const
Returns pointer to factored matrix (assuming factorization has been performed).
ScalarTraits< ScalarType >::magnitudeType MagnitudeType
The Templated LAPACK Wrapper Class.
void solveWithTranspose(bool flag)
If flag is true, causes all subsequent function calls to work with the adjoint of this matrix...
#define TEUCHOS_MAX(x, y)
bool solved()
Returns true if the current set of vectors has been solved.
RCP< SerialDenseMatrix< OrdinalType, ScalarType > > Factor_
TEUCHOSNUMERICS_LIB_DLL_EXPORT const char ESideChar[]
SerialQRDenseSolver()
Default constructor; matrix should be set using setMatrix(), LHS and RHS set with setVectors()...
RCP< SerialDenseMatrix< OrdinalType, ScalarType > > RHS_
std::vector< ScalarType > tau() const
Returns pointer to pivot vector (if factorization has been computed), zero otherwise.
RCP< SerialDenseMatrix< OrdinalType, ScalarType > > getLHS() const
Returns pointer to current LHS.
bool equilibratedB()
Returns true if RHS is equilibrated (RHS available via getRHS()).
OrdinalType numCols() const
Returns column dimension of system.
static magnitudeType magnitude(T a)
Returns the magnitudeType of the scalar type a.
OrdinalType numCols() const
Returns the column dimension of this matrix.
bool shouldEquilibrate()
Returns true if the LAPACK general rules for equilibration suggest you should equilibrate the system...
MagnitudeType ANORM() const
Returns the absolute value of the largest element of this matrix (returns -1 if not yet computed)...
void factorWithEquilibration(bool flag)
Causes equilibration to be called just before the matrix factorization as part of the call to factor...
int equilibrateMatrix()
Equilibrates the this matrix.
Defines basic traits for the scalar field type.
static T zero()
Returns representation of zero for this scalar type.
TEUCHOSNUMERICS_LIB_DLL_EXPORT const char ETranspChar[]
virtual ~SerialQRDenseSolver()
SerialQRDenseSolver destructor.
int computeEquilibrateScaling()
Determines if this matrix should be scaled.
Smart reference counting pointer class for automatic garbage collection.
int formR()
Explicitly forms the upper triangular matrix R.
RCP< SerialDenseMatrix< OrdinalType, ScalarType > > getQ() const
Returns pointer to Q (assuming factorization has been performed).
int factor()
Computes the in-place QR factorization of the matrix using the LAPACK routine _GETRF or the Eigen cla...
#define TEUCHOS_MIN(x, y)
bool transpose()
Returns true if adjoint of this matrix has and will be used.
bool formedR()
Returns true if R has been formed explicitly.
Reference-counted pointer class and non-member templated function implementations.
static T one()
Returns representation of one for this scalar type.
RCP< SerialDenseMatrix< OrdinalType, ScalarType > > getR() const
Returns pointer to R (assuming factorization has been performed).
int setVectors(const RCP< SerialDenseMatrix< OrdinalType, ScalarType > > &X, const RCP< SerialDenseMatrix< OrdinalType, ScalarType > > &B)
Sets the pointers for left and right hand side vector(s).
OrdinalType stride() const
Returns the stride between the columns of this matrix in memory.
OrdinalType numRows() const
Returns the row dimension of this matrix.
This class creates and provides basic support for dense rectangular matrix of templated type...