From 81ae6b6ffef6eadd91235caaf38b5ec1d290285c Mon Sep 17 00:00:00 2001 From: guido Date: Fri, 7 Jul 2006 09:39:32 +0000 Subject: [PATCH] new TransposeMatrix git-svn-id: https://svn.dealii.org/trunk@13349 0785d39b-7218-0410-832d-ea1e28bc413d --- deal.II/doc/news/changes.html | 10 +- deal.II/lac/include/lac/transpose_matrix.h | 228 +++++++++++++++++++++ tests/lac/Makefile | 2 +- tests/lac/pointer_matrix.cc | 70 +++++++ tests/lac/pointer_matrix/.cvsignore | 1 + tests/lac/pointer_matrix/cmp/generic | 21 ++ 6 files changed, 330 insertions(+), 2 deletions(-) create mode 100644 deal.II/lac/include/lac/transpose_matrix.h create mode 100644 tests/lac/pointer_matrix.cc create mode 100644 tests/lac/pointer_matrix/.cvsignore create mode 100644 tests/lac/pointer_matrix/cmp/generic diff --git a/deal.II/doc/news/changes.html b/deal.II/doc/news/changes.html index 1e6420d8c0..c25d9e5bc0 100644 --- a/deal.II/doc/news/changes.html +++ b/deal.II/doc/news/changes.html @@ -529,7 +529,15 @@ inconvenience this causes.

lac

    - + +
  1. New: The class TransposeMatrix + modeled after PointerMatrix swaps the + vmult functions such that its effect is + the transpose of the matrix it points to. +
    + (GK 2006/07/07) +

    +
  2. New: There is now a function FullMatrix::trace that does what its name suggests it does. diff --git a/deal.II/lac/include/lac/transpose_matrix.h b/deal.II/lac/include/lac/transpose_matrix.h new file mode 100644 index 0000000000..28fa8e018b --- /dev/null +++ b/deal.II/lac/include/lac/transpose_matrix.h @@ -0,0 +1,228 @@ +//--------------------------------------------------------------------------- +// $Id$ +// Version: $Name$ +// +// Copyright (C) 2002, 2003, 2004, 2005, 2006 by the deal.II authors +// +// This file is subject to QPL and may not be distributed +// without copyright and license information. Please refer +// to the file deal.II/doc/license.html for the text and +// further information on this license. +// +//--------------------------------------------------------------------------- +#ifndef __deal2__transpose_matrix_h +#define __deal2__transpose_matrix_h + + +#include +#include +#include + + +/** + * The transpose of a given matrix. This auxiliary class swaps the + * effect ov vmult() and Tvmult() as well as vmult_add() and + * Tvmult_add(). + * + * The implementation is analogous to the class PointerMatrix. + * + * @note The transposed matrix is never actually assembled. Instead, + * only the matrix vector multiplication is performed in a transposed + * way. + * + * @ingroup Matrix2 + * @author Guido Kanschat, 2006 + */ + +template +class +TransposeMatrix : public PointerMatrixBase +{ + public: + /** + * Constructor. The pointer in the + * argument is stored in this + * class. As usual, the lifetime of + * *M must be longer than the + * one of the PointerMatrix. + * + * If M is zero, no + * matrix is stored. + */ + TransposeMatrix (const MATRIX* M=0); + + /** + * Constructor. The name argument + * is used to identify the + * SmartPointer for this object. + */ + TransposeMatrix(const char* name); + + /** + * Constructor. M points + * to a matrix which must live + * longer than the + * TransposeMatrix. The name + * argument is used to identify + * the SmartPointer for this + * object. + */ + TransposeMatrix(const MATRIX* M, + const char* name); + + // Use doc from base class + virtual void clear(); + + /** + * Return whether the object is + * empty. + */ + bool empty () const; + + /** + * Assign a new matrix + * pointer. Deletes the old pointer + * and releases its matrix. + * @see SmartPointer + */ + const TransposeMatrix& 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 + * dst. + */ + virtual void vmult_add (VECTOR& dst, + const VECTOR& src) const; + + /** + * Tranposed matrix-vector product, + * adding to dst. + */ + virtual void Tvmult_add (VECTOR& dst, + const VECTOR& src) const; + + private: + /** + * Return the address of the + * matrix for comparison. + */ + virtual const void* get() const; + + /** + * The pointer to the actual matrix. + */ + SmartPointer m; +}; + + +//----------------------------------------------------------------------// + + +template +TransposeMatrix::TransposeMatrix (const MATRIX* M) + : m(M, typeid(*this).name()) +{} + + +template +TransposeMatrix::TransposeMatrix (const char* name) + : m(0, name) +{} + + +template +TransposeMatrix::TransposeMatrix ( + const MATRIX* M, + const char* name) + : m(M, name) +{} + + +template +inline void +TransposeMatrix::clear () +{ + m = 0; +} + + +template +inline const TransposeMatrix& +TransposeMatrix::operator= (const MATRIX* M) +{ + m = M; + return *this; +} + + +template +inline bool +TransposeMatrix::empty () const +{ + if (m == 0) + return true; + return m->empty(); +} + +template +inline void +TransposeMatrix::vmult (VECTOR& dst, + const VECTOR& src) const +{ + Assert (m != 0, ExcNotInitialized()); + m->Tvmult (dst, src); +} + + +template +inline void +TransposeMatrix::Tvmult (VECTOR& dst, + const VECTOR& src) const +{ + Assert (m != 0, ExcNotInitialized()); + m->vmult (dst, src); +} + + +template +inline void +TransposeMatrix::vmult_add (VECTOR& dst, + const VECTOR& src) const +{ + Assert (m != 0, ExcNotInitialized()); + m->Tvmult_add (dst, src); +} + + +template +inline void +TransposeMatrix::Tvmult_add (VECTOR& dst, + const VECTOR& src) const +{ + Assert (m != 0, ExcNotInitialized()); + m->vmult_add (dst, src); +} + + +template +inline const void* +TransposeMatrix::get () const +{ + return m; +} + + + +#endif diff --git a/tests/lac/Makefile b/tests/lac/Makefile index 8365ca85b1..1dd33e50ae 100644 --- a/tests/lac/Makefile +++ b/tests/lac/Makefile @@ -25,7 +25,7 @@ tests_x = vector-vector \ sparsity_pattern sparse_matrices matrices \ block_sparsity_pattern_01 \ block_vector block_vector_* block_matrices \ - matrix_lib matrix_out \ + matrix_lib matrix_out pointer_matrix \ solver eigen \ sparse_ilu sparse_ilu_t lapack lapack_fill block_minres \ identity_matrix* trace diff --git a/tests/lac/pointer_matrix.cc b/tests/lac/pointer_matrix.cc new file mode 100644 index 0000000000..4936e959a9 --- /dev/null +++ b/tests/lac/pointer_matrix.cc @@ -0,0 +1,70 @@ +//---------------------------------------------------------------------- +// $Id$ +// Version: $Name$ +// +// Copyright (C) 2006 by the deal.II authors +// +// This file is subject to QPL and may not be distributed +// without copyright and license information. Please refer +// to the file deal.II/doc/license.html for the text and +// further information on this license. +// +//---------------------------------------------------------------------- + +// Test vmult and Tvmult of PointerMatrix and TransposeMatrix + +#include +#include +#include +#include +#include + +#include + +int main() +{ + std::ofstream logfile("pointer_matrix/output"); + deallog.attach(logfile); + deallog.depth_console(0); + + FullMatrix A(5,5); + unsigned int k=0; + for (unsigned int i=0;i, Vector > P(&A, "P"); + TransposeMatrix, Vector > T(&A, "T"); + + Vector x(5); + Vector y(5); + Vector y2(5); + Vector diff(5); + + for (unsigned int i=0;i