Reference documentation for deal.II version 8.4.1
pointer_matrix.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2002 - 2015 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__pointer_matrix_h
17 #define dealii__pointer_matrix_h
18 
19 #include <deal.II/base/subscriptor.h>
20 #include <deal.II/base/smartpointer.h>
21 #include <deal.II/lac/vector.h>
22 #include <deal.II/lac/vector_memory.h>
23 
24 DEAL_II_NAMESPACE_OPEN
25 
26 template<typename VectorType> class VectorMemory;
27 
28 class IdentityMatrix;
29 template <typename number> class FullMatrix;
30 template <typename number> class LAPACKFullMatrix;
31 template <typename number> class SparseMatrix;
32 template <typename number> class BlockSparseMatrix;
33 template <typename number> class SparseMatrixEZ;
34 template <typename number> class BlockSparseMatrixEZ;
35 template <typename number> class TridiagonalMatrix;
36 template <typename number, typename BlockVectorType> class BlockMatrixArray;
37 
50 template<typename VectorType>
52 {
53 public:
62  typedef typename VectorType::value_type value_type;
63 
69  virtual ~PointerMatrixBase ();
70 
74  virtual void clear () = 0;
75 
79  virtual void vmult (VectorType &dst,
80  const VectorType &src) const = 0;
81 
85  virtual void Tvmult (VectorType &dst,
86  const VectorType &src) const = 0;
87 
91  virtual void vmult_add (VectorType &dst,
92  const VectorType &src) const = 0;
93 
97  virtual void Tvmult_add (VectorType &dst,
98  const VectorType &src) const = 0;
99 };
100 
101 
112 template<typename MatrixType, typename VectorType>
113 class PointerMatrix : public PointerMatrixBase<VectorType>
114 {
115 public:
123  PointerMatrix (const MatrixType *M=0);
124 
135  PointerMatrix(const char *name);
136 
148  PointerMatrix(const MatrixType *M,
149  const char *name);
150 
151  // Use doc from base class
152  virtual void clear();
153 
157  bool empty () const;
158 
164  const PointerMatrix &operator= (const MatrixType *M);
165 
169  virtual void vmult (VectorType &dst,
170  const VectorType &src) const;
171 
175  virtual void Tvmult (VectorType &dst,
176  const VectorType &src) const;
177 
181  virtual void vmult_add (VectorType &dst,
182  const VectorType &src) const;
183 
187  virtual void Tvmult_add (VectorType &dst,
188  const VectorType &src) const;
189 
190 private:
195 };
196 
197 
212 template<typename MatrixType, typename VectorType>
213 class PointerMatrixAux : public PointerMatrixBase<VectorType>
214 {
215 public:
226  const MatrixType *M = 0);
227 
239  const char *name);
240 
253  const MatrixType *M,
254  const char *name);
255 
256  // Use doc from base class
257  virtual void clear();
258 
262  bool empty () const;
263 
268 
274  const PointerMatrixAux &operator= (const MatrixType *M);
275 
279  virtual void vmult (VectorType &dst,
280  const VectorType &src) const;
281 
285  virtual void Tvmult (VectorType &dst,
286  const VectorType &src) const;
287 
291  virtual void vmult_add (VectorType &dst,
292  const VectorType &src) const;
293 
297  virtual void Tvmult_add (VectorType &dst,
298  const VectorType &src) const;
299 
300 private:
305 
310 
315 };
316 
317 
318 
329 template <typename number>
330 class PointerMatrixVector : public PointerMatrixBase<Vector<number> >
331 {
332 public:
340  PointerMatrixVector (const Vector<number> *M=0);
341 
352  PointerMatrixVector (const char *name);
353 
366  const char *name);
367 
368  // Use doc from base class
369  virtual void clear();
370 
374  bool empty () const;
375 
382 
390  virtual void vmult (Vector<number> &dst,
391  const Vector<number> &src) const;
392 
400  virtual void Tvmult (Vector<number> &dst,
401  const Vector<number> &src) const;
402 
409  virtual void vmult_add (Vector<number> &dst,
410  const Vector<number> &src) const;
411 
418  virtual void Tvmult_add (Vector<number> &dst,
419  const Vector<number> &src) const;
420 
421 private:
426 };
427 
428 
429 
447 template <typename VectorType, typename MatrixType>
448 inline
450 new_pointer_matrix_base(MatrixType &matrix, const VectorType &, const char *name = "PointerMatrixAux")
451 {
452  return new PointerMatrixAux<MatrixType, VectorType>(0, &matrix, name);
453 }
454 
460 template <typename numberv>
462 new_pointer_matrix_base(const IdentityMatrix &matrix, const Vector<numberv> &, const char *name = "PointerMatrix")
463 {
464  return new PointerMatrix<IdentityMatrix, Vector<numberv> >(&matrix, name);
465 }
466 
467 
473 template <typename numberv, typename numberm>
475 new_pointer_matrix_base(const FullMatrix<numberm> &matrix, const Vector<numberv> &, const char *name = "PointerMatrix")
476 {
477  return new PointerMatrix<FullMatrix<numberm>, Vector<numberv> >(&matrix, name);
478 }
479 
480 
486 template <typename numberv, typename numberm>
488 new_pointer_matrix_base(const LAPACKFullMatrix<numberm> &matrix, const Vector<numberv> &, const char *name = "PointerMatrix")
489 {
490  return new PointerMatrix<LAPACKFullMatrix<numberm>, Vector<numberv> >(&matrix, name);
491 }
492 
493 
499 template <typename numberv, typename numberm>
501 new_pointer_matrix_base(const SparseMatrix<numberm> &matrix, const Vector<numberv> &, const char *name = "PointerMatrix")
502 {
503  return new PointerMatrix<SparseMatrix<numberm>, Vector<numberv> >(&matrix, name);
504 }
505 
506 
512 template <typename VectorType, typename numberm>
514 new_pointer_matrix_base(const BlockSparseMatrix<numberm> &matrix, const VectorType &, const char *name = "PointerMatrix")
515 {
516  return new PointerMatrix<BlockSparseMatrix<numberm>, VectorType>(&matrix, name);
517 }
518 
519 
525 template <typename numberv, typename numberm>
527 new_pointer_matrix_base(const SparseMatrixEZ<numberm> &matrix, const Vector<numberv> &, const char *name = "PointerMatrix")
528 {
529  return new PointerMatrix<SparseMatrixEZ<numberm>, Vector<numberv> >(&matrix, name);
530 }
531 
532 
538 template <typename VectorType, typename numberm>
540 new_pointer_matrix_base(const BlockSparseMatrixEZ<numberm> &matrix, const VectorType &, const char *name = "PointerMatrix")
541 {
542  return new PointerMatrix<BlockSparseMatrixEZ<numberm>, VectorType>(&matrix, name);
543 }
544 
545 
551 template <typename numberv, typename numberm, typename BLOCK_VectorType>
553 new_pointer_matrix_base(const BlockMatrixArray<numberm,BLOCK_VectorType> &matrix, const BLOCK_VectorType &, const char *name = "PointerMatrix")
554 {
556 }
557 
558 
564 template <typename numberv, typename numberm>
566 new_pointer_matrix_base(const TridiagonalMatrix<numberm> &matrix, const Vector<numberv> &, const char *name = "PointerMatrix")
567 {
568  return new PointerMatrix<TridiagonalMatrix<numberm>, Vector<numberv> >(&matrix, name);
569 }
570 
571 
572 
574 //---------------------------------------------------------------------------
575 
576 template<typename VectorType>
577 inline
579 {}
580 
581 
582 
583 //----------------------------------------------------------------------//
584 
585 
586 template<typename MatrixType, typename VectorType>
588  : m(M, typeid(*this).name())
589 {}
590 
591 
592 template<typename MatrixType, typename VectorType>
594  : m(0, name)
595 {}
596 
597 
598 template<typename MatrixType, typename VectorType>
600  const char *name)
601  : m(M, name)
602 {}
603 
604 
605 template<typename MatrixType, typename VectorType>
606 inline void
608 {
609  m = 0;
610 }
611 
612 
613 template<typename MatrixType, typename VectorType>
616 {
617  m = M;
618  return *this;
619 }
620 
621 
622 template<typename MatrixType, typename VectorType>
623 inline bool
625 {
626  if (m == 0)
627  return true;
628  return m->empty();
629 }
630 
631 template<typename MatrixType, typename VectorType>
632 inline void
634  const VectorType &src) const
635 {
636  Assert (m != 0, ExcNotInitialized());
637  m->vmult (dst, src);
638 }
639 
640 
641 template<typename MatrixType, typename VectorType>
642 inline void
644  const VectorType &src) const
645 {
646  Assert (m != 0, ExcNotInitialized());
647  m->Tvmult (dst, src);
648 }
649 
650 
651 template<typename MatrixType, typename VectorType>
652 inline void
654  const VectorType &src) const
655 {
656  Assert (m != 0, ExcNotInitialized());
657  m->vmult_add (dst, src);
658 }
659 
660 
661 template<typename MatrixType, typename VectorType>
662 inline void
664  const VectorType &src) const
665 {
666  Assert (m != 0, ExcNotInitialized());
667  m->Tvmult_add (dst, src);
668 }
669 
670 
671 
672 //----------------------------------------------------------------------//
673 
674 
675 template<typename MatrixType, typename VectorType>
677  const MatrixType *M)
678  : mem(mem, typeid(*this).name()),
679  m(M, typeid(*this).name())
680 {
681  if (mem == 0) mem = &my_memory;
682 }
683 
684 
685 template<typename MatrixType, typename VectorType>
687  const char *name)
688  : mem(mem, name),
689  m(0, name)
690 {
691  if (mem == 0) mem = &my_memory;
692 }
693 
694 
695 template<typename MatrixType, typename VectorType>
697  const MatrixType *M,
698  const char *name)
699  : mem(mem, name),
700  m(M, name)
701 {
702  if (mem == 0) mem = &my_memory;
703 }
704 
705 
706 template<typename MatrixType, typename VectorType>
707 inline void
709 {
710  m = 0;
711 }
712 
713 
714 template<typename MatrixType, typename VectorType>
717 {
718  m = M;
719  return *this;
720 }
721 
722 
723 template<typename MatrixType, typename VectorType>
724 inline void
726 {
727  mem = M;
728  if (mem == 0)
729  mem = &my_memory;
730 }
731 
732 
733 template<typename MatrixType, typename VectorType>
734 inline bool
736 {
737  if (m == 0)
738  return true;
739  return m->empty();
740 }
741 
742 template<typename MatrixType, typename VectorType>
743 inline void
745  const VectorType &src) const
746 {
747  if (mem == 0)
748  mem = &my_memory;
749  Assert (mem != 0, ExcNotInitialized());
750  Assert (m != 0, ExcNotInitialized());
751  m->vmult (dst, src);
752 }
753 
754 
755 template<typename MatrixType, typename VectorType>
756 inline void
758  const VectorType &src) const
759 {
760  if (mem == 0)
761  mem = &my_memory;
762  Assert (mem != 0, ExcNotInitialized());
763  Assert (m != 0, ExcNotInitialized());
764  m->Tvmult (dst, src);
765 }
766 
767 
768 template<typename MatrixType, typename VectorType>
769 inline void
771  const VectorType &src) const
772 {
773  if (mem == 0)
774  mem = &my_memory;
775  Assert (mem != 0, ExcNotInitialized());
776  Assert (m != 0, ExcNotInitialized());
777  VectorType *v = mem->alloc();
778  v->reinit(dst);
779  m->vmult (*v, src);
780  dst += *v;
781  mem->free(v);
782 }
783 
784 
785 template<typename MatrixType, typename VectorType>
786 inline void
788  const VectorType &src) const
789 {
790  if (mem == 0)
791  mem = &my_memory;
792  Assert (mem != 0, ExcNotInitialized());
793  Assert (m != 0, ExcNotInitialized());
794  VectorType *v = mem->alloc();
795  v->reinit(dst);
796  m->Tvmult (*v, src);
797  dst += *v;
798  mem->free(v);
799 }
800 
801 
802 //----------------------------------------------------------------------//
803 
804 
805 template<typename number>
807  : m(M, typeid(*this).name())
808 {}
809 
810 
811 template<typename number>
813  : m(0, name)
814 {}
815 
816 
817 template<typename number>
819  const char *name)
820  : m(M, name)
821 {}
822 
823 
824 template<typename number>
825 inline void
827 {
828  m = 0;
829 }
830 
831 
832 template<typename number>
833 inline const PointerMatrixVector<number> &
835 {
836  m = M;
837  return *this;
838 }
839 
840 
841 template<typename number>
842 inline bool
844 {
845  if (m == 0)
846  return true;
847  return m->empty();
848 }
849 
850 template<typename number>
851 inline void
853  const Vector<number> &src) const
854 {
855  Assert (m != 0, ExcNotInitialized());
856  Assert (dst.size() == 1, ExcDimensionMismatch(dst.size(), 1));
857 
858  dst(0) = *m * src;
859 }
860 
861 
862 template<typename number>
863 inline void
865  const Vector<number> &src) const
866 {
867  Assert (m != 0, ExcNotInitialized());
868  Assert(src.size() == 1, ExcDimensionMismatch(src.size(), 1));
869 
870  dst.equ (src(0), *m);
871 }
872 
873 
874 template<typename number>
875 inline void
877  const Vector<number> &src) const
878 {
879  Assert (m != 0, ExcNotInitialized());
880  Assert (dst.size() == 1, ExcDimensionMismatch(dst.size(), 1));
881 
882  dst(0) += *m * src;
883 }
884 
885 
886 template<typename number>
887 inline void
889  const Vector<number> &src) const
890 {
891  Assert (m != 0, ExcNotInitialized());
892  Assert(src.size() == 1, ExcDimensionMismatch(src.size(), 1));
893 
894  dst.add (src(0), *m);
895 }
896 
897 
898 DEAL_II_NAMESPACE_CLOSE
899 
900 #endif
SmartPointer< const Vector< number >, PointerMatrixVector< number > > m
virtual void vmult(Vector< number > &dst, const Vector< number > &src) const
virtual void clear()
SmartPointer< const MatrixType, PointerMatrix< MatrixType, VectorType > > m
virtual ~PointerMatrixBase()
PointerMatrixBase< Vector< numberv > > * new_pointer_matrix_base(const FullMatrix< numberm > &matrix, const Vector< numberv > &, const char *name="PointerMatrix")
PointerMatrixBase< Vector< numberv > > * new_pointer_matrix_base(const IdentityMatrix &matrix, const Vector< numberv > &, const char *name="PointerMatrix")
virtual void Tvmult_add(VectorType &dst, const VectorType &src) const
void equ(const Number a, const Vector< Number > &u)
PointerMatrixBase< VectorType > * new_pointer_matrix_base(const BlockSparseMatrixEZ< numberm > &matrix, const VectorType &, const char *name="PointerMatrix")
virtual void vmult(VectorType &dst, const VectorType &src) const
virtual void Tvmult(Vector< number > &dst, const Vector< number > &src) const
bool empty() const
virtual void vmult_add(VectorType &dst, const VectorType &src) const
PointerMatrixVector(const Vector< number > *M=0)
PointerMatrixBase< Vector< numberv > > * new_pointer_matrix_base(const SparseMatrixEZ< numberm > &matrix, const Vector< numberv > &, const char *name="PointerMatrix")
const PointerMatrixAux & operator=(const MatrixType *M)
virtual void vmult_add(VectorType &dst, const VectorType &src) const =0
PointerMatrixBase< VectorType > * new_pointer_matrix_base(MatrixType &matrix, const VectorType &, const char *name="PointerMatrixAux")
virtual void vmult_add(VectorType &dst, const VectorType &src) const
void add(const std::vector< size_type > &indices, const std::vector< OtherNumber > &values)
#define Assert(cond, exc)
Definition: exceptions.h:294
virtual void vmult(VectorType &dst, const VectorType &src) const =0
virtual void clear()
bool empty() const
std::size_t size() const
const PointerMatrix & operator=(const MatrixType *M)
GrowingVectorMemory< VectorType > my_memory
virtual void vmult(VectorType &dst, const VectorType &src) const
virtual void vmult_add(Vector< number > &dst, const Vector< number > &src) const
virtual void Tvmult_add(VectorType &dst, const VectorType &src) const =0
PointerMatrixBase< VectorType > * new_pointer_matrix_base(const BlockSparseMatrix< numberm > &matrix, const VectorType &, const char *name="PointerMatrix")
virtual void clear()=0
virtual void clear()
PointerMatrixBase< Vector< numberv > > * new_pointer_matrix_base(const LAPACKFullMatrix< numberm > &matrix, const Vector< numberv > &, const char *name="PointerMatrix")
virtual void Tvmult_add(Vector< number > &dst, const Vector< number > &src) const
PointerMatrixAux(VectorMemory< VectorType > *mem=0, const MatrixType *M=0)
SmartPointer< const MatrixType, PointerMatrixAux< MatrixType, VectorType > > m
bool empty() const
SmartPointer< VectorMemory< VectorType >, PointerMatrixAux< MatrixType, VectorType > > mem
VectorType::value_type value_type
const PointerMatrixVector & operator=(const Vector< number > *M)
PointerMatrixBase< Vector< numberv > > * new_pointer_matrix_base(const SparseMatrix< numberm > &matrix, const Vector< numberv > &, const char *name="PointerMatrix")
virtual void Tvmult_add(VectorType &dst, const VectorType &src) const
void set_memory(VectorMemory< VectorType > *mem)
PointerMatrixBase< Vector< numberv > > * new_pointer_matrix_base(const TridiagonalMatrix< numberm > &matrix, const Vector< numberv > &, const char *name="PointerMatrix")
virtual void Tvmult(VectorType &dst, const VectorType &src) const =0
virtual void Tvmult(VectorType &dst, const VectorType &src) const
PointerMatrixBase< BLOCK_VectorType > * new_pointer_matrix_base(const BlockMatrixArray< numberm, BLOCK_VectorType > &matrix, const BLOCK_VectorType &, const char *name="PointerMatrix")
PointerMatrix(const MatrixType *M=0)
virtual void Tvmult(VectorType &dst, const VectorType &src) const