Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
Teuchos_SerialQRDenseSolver.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Teuchos: Common Tools Package
5 // Copyright (2004) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #ifndef _TEUCHOS_SERIALQRDENSESOLVER_HPP_
43 #define _TEUCHOS_SERIALQRDENSESOLVER_HPP_
44 
48 #include "Teuchos_CompObject.hpp"
49 #include "Teuchos_BLAS.hpp"
50 #include "Teuchos_LAPACK.hpp"
51 #include "Teuchos_RCP.hpp"
52 #include "Teuchos_ConfigDefs.hpp"
55 #include "Teuchos_ScalarTraits.hpp"
56 
57 #ifdef HAVE_TEUCHOSNUMERICS_EIGEN
58 #include "Eigen/Dense"
59 #endif
60 
129 namespace Teuchos {
130 
131  template<typename OrdinalType, typename ScalarType>
132  class SerialQRDenseSolver : public CompObject, public Object, public BLAS<OrdinalType, ScalarType>,
133  public LAPACK<OrdinalType, ScalarType>
134  {
135 
136  public:
137 
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;
153 #endif
154 
156 
159 
161  virtual ~SerialQRDenseSolver();
163 
165 
166 
168 
171 
173 
179 
181 
182 
184 
186  void factorWithEquilibration(bool flag) {equilibrate_ = flag; return;}
187 
189  void solveWithTranspose(bool flag) {transpose_ = flag; if (flag) TRANS_ = Teuchos::CONJ_TRANS; else TRANS_ = Teuchos::NO_TRANS; return;}
190 
192  void solveWithTransposeFlag(Teuchos::ETransp trans) {TRANS_ = trans; if (trans != Teuchos::NO_TRANS) { transpose_ = true; } }
193 
195 
197 
198 
200 
203  int factor();
204 
206 
209  int solve();
210 
212 
216 
218 
222  int equilibrateMatrix();
223 
225 
229  int equilibrateRHS();
230 
232 
236  int unequilibrateLHS();
237 
239 
242  int formQ();
243 
245 
248  int formR();
249 
251 
255 
257 
262 
264 
265 
267  bool transpose() {return(transpose_);}
268 
270  bool factored() {return(factored_);}
271 
273  bool equilibratedA() {return(equilibratedA_);}
274 
276  bool equilibratedB() {return(equilibratedB_);}
277 
280 
282  bool solved() {return(solved_);}
283 
285  bool formedQ() {return(formedQ_);}
286 
288  bool formedR() {return(formedR_);}
289 
291 
293 
294 
297 
300 
303 
306 
309 
312 
314  OrdinalType numRows() const {return(M_);}
315 
317  OrdinalType numCols() const {return(N_);}
318 
320  std::vector<ScalarType> tau() const {return(TAU_);}
321 
323  MagnitudeType ANORM() const {return(ANORM_);}
324 
326 
328 
329  void Print(std::ostream& os) const;
332  protected:
333 
334  void allocateWORK() { LWORK_ = 4*N_; WORK_.resize( LWORK_ ); return;}
335  void resetMatrix();
336  void resetVectors();
337 
338 
343  OrdinalType equilibrationModeA_;
344  OrdinalType equilibrationModeB_;
346  bool factored_;
347  bool solved_;
349  bool formedQ_;
350  bool formedR_;
351 
353 
354  OrdinalType M_;
355  OrdinalType N_;
356  OrdinalType Min_MN_;
357  OrdinalType LDA_;
358  OrdinalType LDAF_;
359  OrdinalType LDQ_;
360  OrdinalType LDR_;
361  OrdinalType INFO_;
362  OrdinalType LWORK_;
363 
364  std::vector<ScalarType> TAU_;
365 
366  MagnitudeType ANORM_;
367  MagnitudeType BNORM_;
368 
376 
377  ScalarType * A_;
378  ScalarType * AF_;
379  ScalarType * Q_;
380  ScalarType * R_;
381  std::vector<ScalarType> WORK_;
382 #ifdef HAVE_TEUCHOSNUMERICS_EIGEN
383  Eigen::HouseholderQR<EigenMatrix> qr_;
384 #endif
385 
386  private:
387  // SerialQRDenseSolver copy constructor (put here because we don't want user access)
388 
391 
392  };
393 
394  // Helper traits to distinguish work arrays for real and complex-valued datatypes.
395  using namespace details;
396 
397 //=============================================================================
398 
399 template<typename OrdinalType, typename ScalarType>
401  : CompObject(),
402  Object("Teuchos::SerialQRDenseSolver"),
403  equilibrate_(false),
404  shouldEquilibrate_(false),
405  equilibratedA_(false),
406  equilibratedB_(false),
409  transpose_(false),
410  factored_(false),
411  solved_(false),
412  matrixIsZero_(false),
413  formedQ_(false),
414  formedR_(false),
416  M_(0),
417  N_(0),
418  Min_MN_(0),
419  LDA_(0),
420  LDAF_(0),
421  LDQ_(0),
422  LDR_(0),
423  INFO_(0),
424  LWORK_(0),
427  A_(0),
428  AF_(0),
429  Q_(0),
430  R_(0)
431 {
432  resetMatrix();
433 }
434 //=============================================================================
435 
436 template<typename OrdinalType, typename ScalarType>
438 {}
439 
440 //=============================================================================
441 
442 template<typename OrdinalType, typename ScalarType>
444 {
448  solved_ = false;
449  equilibratedB_ = false;
450 }
451 //=============================================================================
452 
453 template<typename OrdinalType, typename ScalarType>
455 {
456  resetVectors();
457  equilibratedA_ = false;
460  factored_ = false;
461  matrixIsZero_ = false;
462  formedQ_ = false;
463  formedR_ = false;
464  M_ = 0;
465  N_ = 0;
466  Min_MN_ = 0;
467  LDA_ = 0;
468  LDAF_ = 0;
469  LDQ_ = 0;
470  LDR_ = 0;
473  A_ = 0;
474  AF_ = 0;
475  Q_ = 0;
476  R_ = 0;
477  INFO_ = 0;
478  LWORK_ = 0;
479 }
480 //=============================================================================
481 
482 template<typename OrdinalType, typename ScalarType>
484 {
485  TEUCHOS_TEST_FOR_EXCEPTION(A->numRows() < A->numCols(), std::invalid_argument,
486  "SerialQRDenseSolver<T>::setMatrix: the matrix A must have A.numRows() >= A.numCols()!");
487 
488  resetMatrix();
489  Matrix_ = A;
490  Factor_ = A;
491  FactorQ_ = A;
492  FactorR_ = A;
493  M_ = A->numRows();
494  N_ = A->numCols();
496  LDA_ = A->stride();
497  LDAF_ = LDA_;
498  LDQ_ = LDA_;
499  LDR_ = N_;
500  A_ = A->values();
501  AF_ = A->values();
502  Q_ = A->values();
503  R_ = A->values();
504 
505  return(0);
506 }
507 //=============================================================================
508 
509 template<typename OrdinalType, typename ScalarType>
512 {
513  // Check that these new vectors are consistent
514  TEUCHOS_TEST_FOR_EXCEPTION(B->numCols() != X->numCols(), std::invalid_argument,
515  "SerialQRDenseSolver<T>::setVectors: X and B have different numbers of columns!");
516  TEUCHOS_TEST_FOR_EXCEPTION(B->values()==0, std::invalid_argument,
517  "SerialQRDenseSolver<T>::setVectors: B is an empty SerialDenseMatrix<T>!");
518  TEUCHOS_TEST_FOR_EXCEPTION(X->values()==0, std::invalid_argument,
519  "SerialQRDenseSolver<T>::setVectors: X is an empty SerialDenseMatrix<T>!");
520  TEUCHOS_TEST_FOR_EXCEPTION(B->stride()<1, std::invalid_argument,
521  "SerialQRDenseSolver<T>::setVectors: B has an invalid stride!");
522  TEUCHOS_TEST_FOR_EXCEPTION(X->stride()<1, std::invalid_argument,
523  "SerialQRDenseSolver<T>::setVectors: X has an invalid stride!");
524 
525  resetVectors();
526  LHS_ = X;
527  RHS_ = B;
528 
529  return(0);
530 }
531 //=============================================================================
532 
533 template<typename OrdinalType, typename ScalarType>
535 
536  if (factored()) return(0);
537 
538  // Equilibrate matrix if necessary
539  int ierr = 0;
540  if (equilibrate_) ierr = equilibrateMatrix();
541  if (ierr!=0) return(ierr);
542 
543  allocateWORK();
544  if ((int)TAU_.size()!=Min_MN_) TAU_.resize( Min_MN_ );
545 
546  INFO_ = 0;
547 
548  // Factor
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_));
553  qr_.compute(aMap);
554  tau = qr_.hCoeffs();
555  for (OrdinalType i=0; i<tauMap.innerSize(); i++) {
556  tauMap(i) = tau(i);
557  }
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);
562  }
563  }
564 #else
565  this->GEQRF(M_, N_, AF_, LDAF_, &TAU_[0], &WORK_[0], LWORK_, &INFO_);
566 #endif
567 
568  factored_ = true;
569 
570  return(INFO_);
571 }
572 
573 //=============================================================================
574 
575 template<typename OrdinalType, typename ScalarType>
577 
578  // Check if the matrix is zero
579  if (matrixIsZero_) {
580  LHS_->putScalar(ScalarTraits<ScalarType>::zero());
581  return(0);
582  }
583 
584  // Equilibrate RHS if necessary
585  int ierr = 0;
586  if (equilibrate_) {
587  ierr = equilibrateRHS();
588  equilibratedB_ = true;
589  }
590  if (ierr != 0) return(ierr);
591 
593  std::logic_error, "SerialQRDenseSolver<T>::solve: Matrix and vectors must be similarly scaled!");
594  TEUCHOS_TEST_FOR_EXCEPTION( RHS_==Teuchos::null, std::invalid_argument,
595  "SerialQRDenseSolver<T>::solve: No right-hand side vector (RHS) has been set for the linear system!");
596  TEUCHOS_TEST_FOR_EXCEPTION( LHS_==Teuchos::null, std::invalid_argument,
597  "SerialQRDenseSolver<T>::solve: No solution vector (LHS) has been set for the linear system!");
598  if ( TRANS_ == Teuchos::NO_TRANS ) {
599  TEUCHOS_TEST_FOR_EXCEPTION( LHS_->numRows() != N_, std::invalid_argument,
600  "SerialQRDenseSolver<T>::solve: No transpose: must have LHS_->numRows() = N_");
601  } else {
602  TEUCHOS_TEST_FOR_EXCEPTION( LHS_->numRows() != M_, std::invalid_argument,
603  "SerialQRDenseSolver<T>::solve: Transpose: must have LHS_->numRows() = M_");
604  }
605 
607  std::cout << "WARNING! SerialQRDenseSolver<T>::solve: System should be equilibrated!" << std::endl;
608 
609  // Matrix must be factored
610  if (!factored()) factor();
611 
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);
616  }
617  }
618 
619  INFO_ = 0;
620 
621  // Solve
622  if ( TRANS_ == Teuchos::NO_TRANS ) {
623 
624  // Solve A lhs = rhs
626  this->solveR( Teuchos::NO_TRANS, *TMP_ );
627 
628  } else {
629 
630  // Solve A**H lhs = rhs
631  this->solveR( Teuchos::CONJ_TRANS, *TMP_ );
632  for (OrdinalType j = 0; j < RHS_->numCols(); j++ ) {
633  for (OrdinalType i = N_; i < M_; i++ ) {
634  (*TMP_)(i, j) = ScalarTraits<ScalarType>::zero();
635  }
636  }
637  this->multiplyQ( Teuchos::NO_TRANS, *TMP_ );
638 
639  }
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);
643  }
644  }
645 
646  solved_ = true;
647 
648  // Unequilibrate LHS if necessary
649  if (equilibrate_) {
650  ierr = unequilibrateLHS();
651  }
652  if (ierr != 0) {
653  return ierr;
654  }
655 
656  return INFO_;
657 }
658 
659 //=============================================================================
660 
661 template<typename OrdinalType, typename ScalarType>
663 {
666  ScalarType sZero = ScalarTraits<ScalarType>::zero();
667  ScalarType sOne = ScalarTraits<ScalarType>::one();
669 
670  MagnitudeType smlnum = ScalarTraits<ScalarType>::magnitude(safeMin/prec);
672 
673  // Compute maximum absolute value of matrix entries
674  OrdinalType i, j;
676  for (j = 0; j < N_; j++) {
677  for (i = 0; i < M_; i++) {
678  anorm = TEUCHOS_MAX( anorm, ScalarTraits<ScalarType>::magnitude((*Matrix_)(i,j)) );
679  }
680  }
681 
682  ANORM_ = anorm;
683 
684  if (ANORM_ > mZero && ANORM_ < smlnum) {
685  // Scale matrix norm up to smlnum
686  shouldEquilibrate_ = true;
687  } else if (ANORM_ > bignum) {
688  // Scale matrix norm down to bignum
689  shouldEquilibrate_ = true;
690  } else if (ANORM_ == mZero) {
691  // Matrix all zero. Return zero solution
692  matrixIsZero_ = true;
693  }
694 
695  return(0);
696 }
697 
698 //=============================================================================
699 
700 template<typename OrdinalType, typename ScalarType>
702 {
703  if (equilibratedA_) return(0);
704 
707  ScalarType sZero = ScalarTraits<ScalarType>::zero();
708  ScalarType sOne = ScalarTraits<ScalarType>::one();
710 
711  MagnitudeType smlnum = ScalarTraits<ScalarType>::magnitude(safeMin/prec);
713  OrdinalType BW = 0;
714 
715  // Compute maximum absolute value of matrix entries
716  OrdinalType i, j;
718  for (j = 0; j < N_; j++) {
719  for (i = 0; i < M_; i++) {
720  anorm = TEUCHOS_MAX( anorm, ScalarTraits<ScalarType>::magnitude((*Matrix_)(i,j)) );
721  }
722  }
723 
724  ANORM_ = anorm;
725  int ierr1 = 0;
726  if (ANORM_ > mZero && ANORM_ < smlnum) {
727  // Scale matrix norm up to smlnum
728  this->LASCL(Teuchos::ETypeChar[Teuchos::FULL], BW, BW, ANORM_, smlnum, M_, N_, A_, LDA_, &INFO_);
730  } else if (ANORM_ > bignum) {
731  // Scale matrix norm down to bignum
732  this->LASCL(Teuchos::ETypeChar[Teuchos::FULL], BW, BW, ANORM_, bignum, M_, N_, A_, LDA_, &INFO_);
734  } else if (ANORM_ == mZero) {
735  // Matrix all zero. Return zero solution
736  matrixIsZero_ = true;
737  }
738 
739  equilibratedA_ = true;
740 
741  return(ierr1);
742 }
743 
744 //=============================================================================
745 
746 template<typename OrdinalType, typename ScalarType>
748 {
749  if (equilibratedB_) return(0);
750 
753  ScalarType sZero = ScalarTraits<ScalarType>::zero();
754  ScalarType sOne = ScalarTraits<ScalarType>::one();
756 
757  MagnitudeType smlnum = ScalarTraits<ScalarType>::magnitude(safeMin/prec);
759  OrdinalType BW = 0;
760 
761  // Compute maximum absolute value of rhs entries
762  OrdinalType i, j;
764  for (j = 0; j <RHS_->numCols(); j++) {
765  for (i = 0; i < RHS_->numRows(); i++) {
766  bnorm = TEUCHOS_MAX( bnorm, ScalarTraits<ScalarType>::magnitude((*RHS_)(i,j)) );
767  }
768  }
769 
770  BNORM_ = bnorm;
771 
772  int ierr1 = 0;
773  if (BNORM_ > mZero && BNORM_ < smlnum) {
774  // Scale RHS norm up to smlnum
775  this->LASCL(Teuchos::ETypeChar[Teuchos::FULL], BW, BW, BNORM_, smlnum, RHS_->numRows(), RHS_->numCols(),
776  RHS_->values(), RHS_->stride(), &INFO_);
778  } else if (BNORM_ > bignum) {
779  // Scale RHS norm down to bignum
780  this->LASCL(Teuchos::ETypeChar[Teuchos::FULL], BW, BW, BNORM_, bignum, RHS_->numRows(), RHS_->numCols(),
781  RHS_->values(), RHS_->stride(), &INFO_);
783  }
784 
785  equilibratedB_ = true;
786 
787  return(ierr1);
788 }
789 
790 //=============================================================================
791 
792 template<typename OrdinalType, typename ScalarType>
794 {
795  if (!equilibratedB_) return(0);
796 
799  ScalarType sZero = ScalarTraits<ScalarType>::zero();
800  ScalarType sOne = ScalarTraits<ScalarType>::one();
802  (void) mZero; // Silence "unused variable" compiler warning.
803 
804  MagnitudeType smlnum = ScalarTraits<ScalarType>::magnitude(safeMin/prec);
806  OrdinalType BW = 0;
807 
808  int ierr1 = 0;
809  if (equilibrationModeA_ == 1) {
810  this->LASCL(Teuchos::ETypeChar[Teuchos::FULL], BW, BW, ANORM_, smlnum, LHS_->numRows(), LHS_->numCols(),
811  LHS_->values(), LHS_->stride(), &INFO_);
812  } else if (equilibrationModeA_ == 2) {
813  this->LASCL(Teuchos::ETypeChar[Teuchos::FULL], BW, BW, ANORM_, bignum, LHS_->numRows(), LHS_->numCols(),
814  LHS_->values(), LHS_->stride(), &INFO_);
815  }
816  if (equilibrationModeB_ == 1) {
817  this->LASCL(Teuchos::ETypeChar[Teuchos::FULL], BW, BW, smlnum, BNORM_, LHS_->numRows(), LHS_->numCols(),
818  LHS_->values(), LHS_->stride(), &INFO_);
819  } else if (equilibrationModeB_ == 2) {
820  this->LASCL(Teuchos::ETypeChar[Teuchos::FULL], BW, BW, bignum, BNORM_, LHS_->numRows(), LHS_->numCols(),
821  LHS_->values(), LHS_->stride(), &INFO_);
822  }
823 
824  return(ierr1);
825 }
826 
827 //=============================================================================
828 
829 template<typename OrdinalType, typename ScalarType>
831 
832  // Matrix must be factored first
833  if (!factored()) factor();
834 
835  // Store Q separately from factored matrix
836  if (AF_ == Q_) {
838  Q_ = FactorQ_->values();
839  LDQ_ = FactorQ_->stride();
840  }
841 
842  INFO_ = 0;
843 
844  // Form Q
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);
851  }
852  }
853 #else
854  this->UNGQR(M_, N_, N_, Q_, LDQ_, &TAU_[0], &WORK_[0], LWORK_, &INFO_);
855 #endif
856 
857  if (INFO_!=0) return(INFO_);
858 
859  formedQ_ = true;
860 
861  return(INFO_);
862 }
863 
864 //=============================================================================
865 
866 template<typename OrdinalType, typename ScalarType>
868 
869  // Matrix must be factored first
870  if (!factored()) factor();
871 
872  // Store R separately from factored matrix
873  if (AF_ == R_) {
875  R_ = FactorR_->values();
876  LDR_ = FactorR_->stride();
877  }
878 
879  // Form R
880  for (OrdinalType j=0; j<N_; j++) {
881  for (OrdinalType i=0; i<=j; i++) {
882  (*FactorR_)(i,j) = (*Factor_)(i,j);
883  }
884  }
885 
886  formedR_ = true;
887 
888  return(0);
889 }
890 
891 //=============================================================================
892 
893 template<typename OrdinalType, typename ScalarType>
895 {
896 
897  // Check that the matrices are consistent.
898  TEUCHOS_TEST_FOR_EXCEPTION(C.numRows()!=M_, std::invalid_argument,
899  "SerialQRDenseSolver<T>::multiplyQ: C.numRows() != M_!");
900  TEUCHOS_TEST_FOR_EXCEPTION(C.values()==0, std::invalid_argument,
901  "SerialQRDenseSolver<T>::multiplyQ: C is an empty SerialDenseMatrix<T>!");
902 
903  // Matrix must be factored
904  if (!factored()) factor();
905 
906  INFO_ = 0;
907 
908  // Multiply
909 #ifdef HAVE_TEUCHOSNUMERICS_EIGEN
910  EigenMatrixMap cMap( C.values(), C.numRows(), C.numCols(), EigenOuterStride(C.stride()) );
911  if ( transq == Teuchos::NO_TRANS ) {
912  // C = Q * C
913  cMap = qr_.householderQ() * cMap;
914  } else {
915  // C = Q**H * C
916  cMap = qr_.householderQ().adjoint() * cMap;
917  for (OrdinalType j = 0; j < C.numCols(); j++) {
918  for (OrdinalType i = N_; i < C.numRows(); i++ ) {
919  cMap(i, j) = ScalarTraits<ScalarType>::zero();
920  }
921  }
922  }
923 #else
927 
928  if ( transq == Teuchos::NO_TRANS ) {
929 
930  // C = Q * C
931  this->UNMQR(ESideChar[SIDE], ETranspChar[NO_TRANS], M_, C.numCols(), N_, AF_, LDAF_,
932  &TAU_[0], C.values(), C.stride(), &WORK_[0], LWORK_, &INFO_);
933 
934  } else {
935 
936  // C = Q**H * C
937  this->UNMQR(ESideChar[SIDE], ETranspChar[TRANS], M_, C.numCols(), N_, AF_, LDAF_,
938  &TAU_[0], C.values(), C.stride(), &WORK_[0], LWORK_, &INFO_);
939 
940  for (OrdinalType j = 0; j < C.numCols(); j++) {
941  for (OrdinalType i = N_; i < C.numRows(); i++ ) {
942  C(i, j) = ScalarTraits<ScalarType>::zero();
943  }
944  }
945  }
946 #endif
947 
948  return(INFO_);
949 
950 }
951 
952 //=============================================================================
953 
954 template<typename OrdinalType, typename ScalarType>
956 {
957 
958  // Check that the matrices are consistent.
959  TEUCHOS_TEST_FOR_EXCEPTION(C.numRows()<N_ || C.numRows()>M_, std::invalid_argument,
960  "SerialQRDenseSolver<T>::solveR: must have N_ < C.numRows() < M_!");
961  TEUCHOS_TEST_FOR_EXCEPTION(C.values()==0, std::invalid_argument,
962  "SerialQRDenseSolver<T>::solveR: C is an empty SerialDenseMatrix<T>!");
963 
964  // Matrix must be factored
965  if (!factored()) factor();
966 
967  INFO_ = 0;
968 
969  // Solve
970 #ifdef HAVE_TEUCHOSNUMERICS_EIGEN
971  EigenMatrixMap cMap( C.values(), N_, C.numCols(), EigenOuterStride(C.stride()) );
972  // Check for singularity first like TRTRS
973  for (OrdinalType j=0; j<N_; j++) {
974  if ((qr_.matrixQR())(j,j) == ScalarTraits<ScalarType>::zero()) {
975  INFO_ = j+1;
976  return(INFO_);
977  }
978  }
979  if ( transr == Teuchos::NO_TRANS ) {
980  // C = R \ C
981  qr_.matrixQR().topRows(N_).template triangularView<Eigen::Upper>().solveInPlace(cMap);
982  } else {
983  // C = R**H \ C
984  qr_.matrixQR().topRows(N_).template triangularView<Eigen::Upper>().adjoint().solveInPlace(cMap);
985  }
986 #else
991 
992  ScalarType* RMAT = (formedR_) ? R_ : AF_;
993  OrdinalType LDRMAT = (formedR_) ? LDR_ : LDAF_;
994 
995  if ( transr == Teuchos::NO_TRANS ) {
996 
997  // C = R \ C
998  this->TRTRS(EUploChar[UPLO], ETranspChar[NO_TRANS], EDiagChar[DIAG], N_, C.numCols(),
999  RMAT, LDRMAT, C.values(), C.stride(), &INFO_);
1000 
1001  } else {
1002 
1003  // C = R**H \ C
1004  this->TRTRS(EUploChar[UPLO], ETranspChar[TRANS], EDiagChar[DIAG], N_, C.numCols(),
1005  RMAT, LDRMAT, C.values(), C.stride(), &INFO_);
1006 
1007  }
1008 #endif
1009 
1010  return(INFO_);
1011 
1012 }
1013 
1014 //=============================================================================
1015 
1016 template<typename OrdinalType, typename ScalarType>
1018 
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;
1024 
1025 }
1026 
1027 } // namespace Teuchos
1028 
1029 #endif /* _TEUCHOS_SERIALQRDENSESOLVER_HPP_ */
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.
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.
Teuchos::ScalarTraits< ScalarType >::magnitudeType MagnitudeType
bool formedQ()
Returns true if Q has been formed explicitly.
void GEQRF(const OrdinalType m, const OrdinalType n, ScalarType *A, const OrdinalType lda, ScalarType *TAU, ScalarType *WORK, const OrdinalType lwork, OrdinalType *info) const
Computes a QR factorization of a general m by n matrix A.
#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_
void UNGQR(const OrdinalType m, const OrdinalType n, const OrdinalType k, ScalarType *A, const OrdinalType lda, const ScalarType *TAU, ScalarType *WORK, const OrdinalType lwork, OrdinalType *info) const
Compute explicit QR factor from QR factorization (GEQRF) (complex case).
Teuchos header file which uses auto-configuration information to include necessary C++ headers...
Templated BLAS wrapper.
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.
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.
Teuchos implementation details.
RCP< SerialDenseMatrix< OrdinalType, ScalarType > > FactorQ_
TEUCHOSNUMERICS_LIB_DLL_EXPORT const char EUploChar[]
The base Teuchos class.
void UNMQR(const char SIDE, const char TRANS, const OrdinalType m, const OrdinalType n, const OrdinalType k, ScalarType *A, const OrdinalType lda, const ScalarType *TAU, ScalarType *C, const OrdinalType ldc, ScalarType *WORK, const OrdinalType lwork, OrdinalType *info) const
Apply Householder reflectors (complex case).
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.
void LASCL(const char TYPE, const OrdinalType kl, const OrdinalType ku, const MagnitudeType cfrom, const MagnitudeType cto, const OrdinalType m, const OrdinalType n, ScalarType *A, const OrdinalType lda, OrdinalType *info) const
Multiplies the m by n matrix A by the real scalar cto/cfrom.
bool equilibratedB()
Returns true if RHS is equilibrated (RHS available via getRHS()).
OrdinalType numCols() const
Returns column dimension of system.
void TRTRS(const char UPLO, const char TRANS, const char DIAG, const OrdinalType n, const OrdinalType nrhs, const ScalarType *A, const OrdinalType lda, ScalarType *B, const OrdinalType ldb, OrdinalType *info) const
Solves a triangular linear system of the form A*X=B or A**T*X=B, where A is a triangular matrix...
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...