Reference documentation for deal.II version 8.5.1
lapack_full_matrix.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2005 - 2017 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE at
12 // the top level of the deal.II distribution.
13 //
14 // ---------------------------------------------------------------------
15 
16 #ifndef dealii__lapack_full_matrix_h
17 #define dealii__lapack_full_matrix_h
18 
19 
20 #include <deal.II/base/config.h>
21 #include <deal.II/base/smartpointer.h>
22 #include <deal.II/base/table.h>
23 #include <deal.II/lac/lapack_support.h>
24 #include <deal.II/lac/vector_memory.h>
25 
26 #include <deal.II/base/std_cxx11/shared_ptr.h>
27 #include <vector>
28 #include <complex>
29 
30 DEAL_II_NAMESPACE_OPEN
31 
32 // forward declarations
33 template<typename number> class Vector;
34 template<typename number> class BlockVector;
35 template<typename number> class FullMatrix;
36 template<typename number> class SparseMatrix;
37 
38 
51 template <typename number>
52 class LAPACKFullMatrix : public TransposeTable<number>
53 {
54 public:
55 
60 
70  explicit LAPACKFullMatrix (const size_type size = 0);
71 
72 
76  LAPACKFullMatrix (const size_type rows,
77  const size_type cols);
78 
79 
90 
96 
103  template <typename number2>
106 
113  template <typename number2>
116 
122  operator = (const double d);
123 
128  operator*= (const number factor);
129 
134  operator/= (const number factor);
135 
142  template <typename MatrixType>
143  void copy_from (const MatrixType &);
144 
150  void reinit (const size_type size);
151 
157  void reinit (const size_type rows,
158  const size_type cols);
159 
165  unsigned int m () const;
166 
172  unsigned int n () const;
173 
187  template<typename MatrixType>
188  void fill (const MatrixType &src,
189  const size_type dst_offset_i = 0,
190  const size_type dst_offset_j = 0,
191  const size_type src_offset_i = 0,
192  const size_type src_offset_j = 0,
193  const number factor = 1.,
194  const bool transpose = false);
195 
196 
225  template <typename number2>
226  void vmult (Vector<number2> &w,
227  const Vector<number2> &v,
228  const bool adding = false) const;
229 
233  void vmult (Vector<number> &w,
234  const Vector<number> &v,
235  const bool adding = false) const;
236 
242  template <typename number2>
243  void vmult_add (Vector<number2> &w,
244  const Vector<number2> &v) const;
245 
249  void vmult_add (Vector<number> &w,
250  const Vector<number> &v) const;
251 
264  template <typename number2>
265  void Tvmult (Vector<number2> &w,
266  const Vector<number2> &v,
267  const bool adding=false) const;
268 
272  void Tvmult (Vector<number> &w,
273  const Vector<number> &v,
274  const bool adding=false) const;
275 
282  template <typename number2>
283  void Tvmult_add (Vector<number2> &w,
284  const Vector<number2> &v) const;
285 
289  void Tvmult_add (Vector<number> &w,
290  const Vector<number> &v) const;
291 
292 
309  const LAPACKFullMatrix<number> &B,
310  const bool adding=false) const;
311 
316  void mmult (FullMatrix<number> &C,
317  const LAPACKFullMatrix<number> &B,
318  const bool adding=false) const;
319 
336  const LAPACKFullMatrix<number> &B,
337  const bool adding=false) const;
338 
343  void Tmmult (FullMatrix<number> &C,
344  const LAPACKFullMatrix<number> &B,
345  const bool adding=false) const;
346 
363  const LAPACKFullMatrix<number> &B,
364  const bool adding=false) const;
365 
370  void mTmult (FullMatrix<number> &C,
371  const LAPACKFullMatrix<number> &B,
372  const bool adding=false) const;
373 
391  const LAPACKFullMatrix<number> &B,
392  const bool adding=false) const;
393 
398  void TmTmult (FullMatrix<number> &C,
399  const LAPACKFullMatrix<number> &B,
400  const bool adding=false) const;
401 
405  void compute_lu_factorization ();
406 
411  void invert ();
412 
422  const bool transposed) const;
423 
434  const bool transposed) const;
435 
454  void compute_eigenvalues (const bool right_eigenvectors = false,
455  const bool left_eigenvectors = false);
456 
475  void compute_eigenvalues_symmetric (const number lower_bound,
476  const number upper_bound,
477  const number abs_accuracy,
478  Vector<number> &eigenvalues,
479  FullMatrix<number> &eigenvectors);
480 
503  const number lower_bound,
504  const number upper_bound,
505  const number abs_accuracy,
506  Vector<number> &eigenvalues,
507  std::vector<Vector<number> > &eigenvectors,
508  const int itype = 1);
509 
527  std::vector<Vector<number> > &eigenvectors,
528  const int itype = 1);
529 
538  void compute_svd ();
539 
559  void compute_inverse_svd (const double threshold = 0.);
560 
564  std::complex<number>
565  eigenvalue (const size_type i) const;
566 
571  number
572  singular_value (const size_type i) const;
573 
600  void print_formatted (std::ostream &out,
601  const unsigned int precision = 3,
602  const bool scientific = true,
603  const unsigned int width = 0,
604  const char *zero_string = " ",
605  const double denominator = 1.,
606  const double threshold = 0.) const;
607 
608 private:
609 
614  LAPACKSupport::State state;
615 
620  LAPACKSupport::Properties properties;
621 
625  mutable std::vector<number> work;
626 
633  std::vector<int> ipiv;
634 
638  std::vector<number> inv_work;
639 
644  std::vector<number> wr;
645 
649  std::vector<number> wi;
650 
654  std::vector<number> vl;
655 
659  std::vector<number> vr;
660 
665  std_cxx11::shared_ptr<LAPACKFullMatrix<number> > svd_u;
666 
671  std_cxx11::shared_ptr<LAPACKFullMatrix<number> > svd_vt;
672 };
673 
674 
675 
682 template <typename number>
684  :
685  public Subscriptor
686 {
687 public:
688  void initialize(const LAPACKFullMatrix<number> &);
689  void initialize(const LAPACKFullMatrix<number> &,
691  void vmult(Vector<number> &, const Vector<number> &) const;
692  void Tvmult(Vector<number> &, const Vector<number> &) const;
693  void vmult(BlockVector<number> &,
694  const BlockVector<number> &) const;
695  void Tvmult(BlockVector<number> &,
696  const BlockVector<number> &) const;
697 private:
700 };
701 
702 /*---------------------- Inline functions -----------------------------------*/
703 
704 template <typename number>
705 inline
706 unsigned int
708 {
709  return this->n_rows ();
710 }
711 
712 template <typename number>
713 inline
714 unsigned int
716 {
717  return this->n_cols ();
718 }
719 
720 template <typename number>
721 template <typename MatrixType>
722 inline void
724 {
725  this->reinit (M.m(), M.n());
726 
727  // loop over the elements of the argument matrix row by row, as suggested
728  // in the documentation of the sparse matrix iterator class, and
729  // copy them into the current object
730  for (size_type row = 0; row < M.m(); ++row)
731  {
732  const typename MatrixType::const_iterator end_row = M.end(row);
733  for (typename MatrixType::const_iterator entry = M.begin(row);
734  entry != end_row; ++entry)
735  this->el(row, entry->column()) = entry->value();
736  }
737 
738  state = LAPACKSupport::matrix;
739 }
740 
741 
742 
743 template <typename number>
744 template <typename MatrixType>
745 inline void
746 LAPACKFullMatrix<number>::fill (const MatrixType &M,
747  const size_type dst_offset_i,
748  const size_type dst_offset_j,
749  const size_type src_offset_i,
750  const size_type src_offset_j,
751  const number factor,
752  const bool transpose)
753 {
754  // loop over the elements of the argument matrix row by row, as suggested
755  // in the documentation of the sparse matrix iterator class
756  for (size_type row = src_offset_i; row < M.m(); ++row)
757  {
758  const typename MatrixType::const_iterator end_row = M.end(row);
759  for (typename MatrixType::const_iterator entry = M.begin(row);
760  entry != end_row; ++entry)
761  {
762  const size_type i = transpose ? entry->column() : row;
763  const size_type j = transpose ? row : entry->column();
764 
765  const size_type dst_i=dst_offset_i+i-src_offset_i;
766  const size_type dst_j=dst_offset_j+j-src_offset_j;
767  if (dst_i<this->n_rows() && dst_j<this->n_cols())
768  (*this)(dst_i, dst_j) = factor * entry->value();
769  }
770  }
771 
772  state = LAPACKSupport::matrix;
773 }
774 
775 
776 template <typename number>
777 template <typename number2>
778 void
780  const Vector<number2> &,
781  const bool) const
782 {
783  Assert(false,
784  ExcMessage("LAPACKFullMatrix<number>::vmult must be called with a "
785  "matching Vector<double> vector type."));
786 }
787 
788 
789 template <typename number>
790 template <typename number2>
791 void
793  const Vector<number2> &) const
794 {
795  Assert(false,
796  ExcMessage("LAPACKFullMatrix<number>::vmult_add must be called with a "
797  "matching Vector<double> vector type."));
798 }
799 
800 
801 template <typename number>
802 template <typename number2>
803 void
805  const Vector<number2> &,
806  const bool) const
807 {
808  Assert(false,
809  ExcMessage("LAPACKFullMatrix<number>::Tvmult must be called with a "
810  "matching Vector<double> vector type."));
811 }
812 
813 
814 template <typename number>
815 template <typename number2>
816 void
818  const Vector<number2> &) const
819 {
820  Assert(false,
821  ExcMessage("LAPACKFullMatrix<number>::Tvmult_add must be called with a "
822  "matching Vector<double> vector type."));
823 }
824 
825 
826 template <typename number>
827 inline std::complex<number>
829 {
830  Assert (state & LAPACKSupport::eigenvalues, ExcInvalidState());
831  Assert (wr.size() == this->n_rows(), ExcInternalError());
832  Assert (wi.size() == this->n_rows(), ExcInternalError());
833  Assert (i<this->n_rows(), ExcIndexRange(i,0,this->n_rows()));
834 
835  return std::complex<number>(wr[i], wi[i]);
836 }
837 
838 
839 template <typename number>
840 inline number
842 {
843  Assert (state == LAPACKSupport::svd || state == LAPACKSupport::inverse_svd, LAPACKSupport::ExcState(state));
844  AssertIndexRange(i,wr.size());
845 
846  return wr[i];
847 }
848 
849 
850 
851 DEAL_II_NAMESPACE_CLOSE
852 
853 #endif
LAPACKFullMatrix(const size_type size=0)
std_cxx11::shared_ptr< LAPACKFullMatrix< number > > svd_u
std::vector< number > work
void TmTmult(LAPACKFullMatrix< number > &C, const LAPACKFullMatrix< number > &B, const bool adding=false) const
LAPACKFullMatrix< number > & operator/=(const number factor)
void Tvmult(Vector< number2 > &w, const Vector< number2 > &v, const bool adding=false) const
LAPACKSupport::State state
const TableIndices< N > & size() const
std::complex< number > eigenvalue(const size_type i) const
#define AssertIndexRange(index, range)
Definition: exceptions.h:1170
LAPACKSupport::Properties properties
std::vector< number > vr
void compute_eigenvalues_symmetric(const number lower_bound, const number upper_bound, const number abs_accuracy, Vector< number > &eigenvalues, FullMatrix< number > &eigenvectors)
void reinit(const size_type size)
static ::ExceptionBase & ExcIndexRange(int arg1, int arg2, int arg3)
void Tmmult(LAPACKFullMatrix< number > &C, const LAPACKFullMatrix< number > &B, const bool adding=false) const
types::global_dof_index size_type
static ::ExceptionBase & ExcState(State arg1)
static ::ExceptionBase & ExcInvalidState()
static ::ExceptionBase & ExcMessage(std::string arg1)
void mTmult(LAPACKFullMatrix< number > &C, const LAPACKFullMatrix< number > &B, const bool adding=false) const
TableBase< 2, T >::size_type size_type
Definition: table.h:1530
std::vector< int > ipiv
std::vector< number > wr
unsigned int global_dof_index
Definition: types.h:88
std_cxx11::shared_ptr< LAPACKFullMatrix< number > > svd_vt
#define Assert(cond, exc)
Definition: exceptions.h:313
void copy_from(const MatrixType &)
std::vector< number > inv_work
std::vector< number > vl
std::vector< number > wi
void vmult(Vector< number2 > &w, const Vector< number2 > &v, const bool adding=false) const
void compute_generalized_eigenvalues_symmetric(LAPACKFullMatrix< number > &B, const number lower_bound, const number upper_bound, const number abs_accuracy, Vector< number > &eigenvalues, std::vector< Vector< number > > &eigenvectors, const int itype=1)
void print_formatted(std::ostream &out, const unsigned int precision=3, const bool scientific=true, const unsigned int width=0, const char *zero_string=" ", const double denominator=1., const double threshold=0.) const
number singular_value(const size_type i) const
LAPACKFullMatrix< number > & operator*=(const number factor)
void compute_inverse_svd(const double threshold=0.)
LAPACKFullMatrix< number > & operator=(const LAPACKFullMatrix< number > &)
void fill(const MatrixType &src, const size_type dst_offset_i=0, const size_type dst_offset_j=0, const size_type src_offset_i=0, const size_type src_offset_j=0, const number factor=1., const bool transpose=false)
void Tvmult_add(Vector< number2 > &w, const Vector< number2 > &v) const
unsigned int m() const
unsigned int n() const
void compute_eigenvalues(const bool right_eigenvectors=false, const bool left_eigenvectors=false)
void apply_lu_factorization(Vector< number > &v, const bool transposed) const
DerivativeForm< 1, spacedim, dim, Number > transpose(const DerivativeForm< 1, dim, spacedim, Number > &DF)
void vmult_add(Vector< number2 > &w, const Vector< number2 > &v) const
void mmult(LAPACKFullMatrix< number > &C, const LAPACKFullMatrix< number > &B, const bool adding=false) const
static ::ExceptionBase & ExcInternalError()