From: guido Date: Wed, 17 Jul 2002 14:02:24 +0000 (+0000) Subject: Pointer matrices moved from private directory X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=371f1b0175d9459fffd5c670de82bc7371892e5b;p=dealii-svn.git Pointer matrices moved from private directory git-svn-id: https://svn.dealii.org/trunk@6258 0785d39b-7218-0410-832d-ea1e28bc413d --- diff --git a/deal.II/lac/include/lac/pointer_matrix.h b/deal.II/lac/include/lac/pointer_matrix.h new file mode 100644 index 0000000000..2f95805ae0 --- /dev/null +++ b/deal.II/lac/include/lac/pointer_matrix.h @@ -0,0 +1,318 @@ +// $Id$ + +#ifndef __deal2__pointer_matrix_h +#define __deal2__pointer_matrix_h + +#include +#include + +/** + * Abstract class for use in iterations. This class provides the + * interface required by LAC solver classes. It allows to use + * different concrete matrix classes in the same context, as long as + * they apply to the same vector class. + * + * @author Guido Kanschat, 2000, 2001, 2002 + */ +template +class PointerMatrixBase : public Subscriptor +{ +public: + /** + * Virtual destructor. Does + * nothing except making sure that + * the destructor of the derived + * class is called. + */ + virtual ~PointerMatrixBase (); + + /** + * Matrix-vector product. + */ + virtual void vmult (VECTOR& dst, + const VECTOR& src) const = 0; + + /** + * Tranposed matrix-vector product. + */ + virtual void Tvmult (VECTOR& dst, + const VECTOR& src) const = 0; + + /** + * Matrix-vector product, adding to + * @p{dst}. + */ + virtual void vmult_add (VECTOR& dst, + const VECTOR& src) const = 0; + + /** + * Tranposed matrix-vector product, + * adding to @p{dst}. + */ + virtual void Tvmult_add (VECTOR& dst, + const VECTOR& src) const = 0; +}; + +/** + * A pointer to be used as a matrix. This class stores a pointer to a + * matrix and can be used as a matrix itself in iterative methods. + * + * The main purpose for the existence of this class is its base class, + * which only has a vector as template argument. Therefore, this + * interface provides an abstract base class for matrices. + * + * @author Guido Kanschat 2000, 2001, 2002 + */ +template +class PointerMatrix : public PointerMatrixBase +{ +public: + /** + * Constructor. The pointer in the + * argument is stored in this + * class. As usual, the lifetime of + * @p{*M} must be longer than the + * one of the @p{PointerMatrix}. + * + * If @p{M} is zero, no matrix is stored. + */ + PointerMatrix (const MATRIX* M=0); + + /** + * Assign a new matrix + * pointer. Deletes the old pointer + * and releases its matrix. + * @see SmartPointer + */ + const PointerMatrix& operator= (const MATRIX* M); + + + /** + * Matrix-vector product. + */ + virtual void vmult (VECTOR& dst, + const VECTOR& src) const; + + /** + * Tranposed matrix-vector product. + */ + virtual void Tvmult (VECTOR& dst, + const VECTOR& src) const; + + /** + * Matrix-vector product, adding to + * @p{dst}. + */ + virtual void vmult_add (VECTOR& dst, + const VECTOR& src) const; + + /** + * Tranposed matrix-vector product, + * adding to @p{dst}. + */ + virtual void Tvmult_add (VECTOR& dst, + const VECTOR& src) const; + +private: + /** + * The pointer to the actual matrix. + */ + SmartPointer m; +}; + + +/** + * Poor man's matrix product. Stores two matrices $m_1$ and $m_2$ and + * implements matrix-vector multiplications for the product $m_1 m_2$ + * by performing multiplication with both factors consecutively. + * + * @author Guido Kanschat, 2000, 2001, 2002 + */ +template +class ProductMatrix : public PointerMatrixBase +{ + public: + /** + * Constructor. Additionally to + * the two constituting matrices, a + * memory pool for the auxiliary + * vector must be provided. + */ + ProductMatrix(const MATRIX1& m1, + const MATRIX2& m2, + VectorMemory& mem); + + /** + * Matrix-vector product. + */ + virtual void vmult (VECTOR& dst, + const VECTOR& src) const; + + /** + * Tranposed matrix-vector product. + */ + virtual void Tvmult (VECTOR& dst, + const VECTOR& src) const; + + /** + * Matrix-vector product, adding to + * @p{dst}. + */ + virtual void vmult_add (VECTOR& dst, + const VECTOR& src) const; + + /** + * Tranposed matrix-vector product, + * adding to @p{dst}. + */ + virtual void Tvmult_add (VECTOR& dst, + const VECTOR& src) const; + + private: + /** + * The left matrix of the product. + */ + SmartPointer m1; + + /** + * The right matrix of the product. + */ + SmartPointer m2; + + /** + * Memory for auxiliary vector. + */ + SmartPointer > mem; +}; + + +//----------------------------------------------------------------------// + +template +inline +PointerMatrixBase::~PointerMatrixBase () +{} + + +template +PointerMatrix::PointerMatrix (const MATRIX* M) + : + m(M) +{} + +template +const PointerMatrix& +PointerMatrix::operator= (const MATRIX* M) +{ + m = M; + return *this; +} + +template +void +PointerMatrix::vmult (VECTOR& dst, + const VECTOR& src) const +{ + Assert (m != 0, ExcNotInitialized()); + m->vmult (dst, src); +} + + +template +void +PointerMatrix::Tvmult (VECTOR& dst, + const VECTOR& src) const +{ + Assert (m != 0, ExcNotInitialized()); + m->Tvmult (dst, src); +} + + +template +void +PointerMatrix::vmult_add (VECTOR& dst, + const VECTOR& src) const +{ + Assert (m != 0, ExcNotInitialized()); + m->vmult_add (dst, src); +} + + +template +void +PointerMatrix::Tvmult_add (VECTOR& dst, + const VECTOR& src) const +{ + Assert (m != 0, ExcNotInitialized()); + m->Tvmult_add (dst, src); +} + + +//----------------------------------------------------------------------// + +template +ProductMatrix::ProductMatrix ( + const MATRIX1& mat1, + const MATRIX2& mat2, + VectorMemory& m) + : m1(&mat1), + m2(&mat2), + mem(&m) +{} + + +template +void +ProductMatrix::vmult (VECTOR& dst, + const VECTOR& src) const +{ + VECTOR* v = mem->alloc(); + v->reinit(dst); + m2->vmult (*v, src); + m1->vmult (dst, *v); + mem->free(v); +} + + +template +void +ProductMatrix::vmult_add (VECTOR& dst, + const VECTOR& src) const +{ + VECTOR* v = mem->alloc(); + v->reinit(dst); + m2->vmult (*v, src); + m1->vmult_add (dst, *v); + mem->free(v); +} + + +template +void +ProductMatrix::Tvmult ( + VECTOR& dst, + const VECTOR& src) const +{ + VECTOR* v = mem->alloc(); + v->reinit(dst); + m1->Tvmult (*v, src); + m2->Tvmult (dst, *v); + mem->free(v); +} + + +template +void +ProductMatrix::Tvmult_add ( + VECTOR& dst, + const VECTOR& src) const +{ + VECTOR* v = mem->alloc(); + v->reinit(dst); + m1->Tvmult (*v, src); + m2->Tvmult_add (dst, *v); + mem->free(v); +} + + +#endif