From: Wolfgang Bangerth Date: Mon, 24 Apr 2006 23:06:59 +0000 (+0000) Subject: Copy operations from identity matrix X-Git-Tag: v8.0.0~11885 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b65da843522a618e1464f3f9ec1d2cd34e36cf03;p=dealii.git Copy operations from identity matrix git-svn-id: https://svn.dealii.org/trunk@12878 0785d39b-7218-0410-832d-ea1e28bc413d --- diff --git a/deal.II/lac/include/lac/full_matrix.h b/deal.II/lac/include/lac/full_matrix.h index a5ea36872a..bbb148ed43 100644 --- a/deal.II/lac/include/lac/full_matrix.h +++ b/deal.II/lac/include/lac/full_matrix.h @@ -17,6 +17,7 @@ #include #include #include +#include #include @@ -245,6 +246,20 @@ class FullMatrix : public Table<2,number> const unsigned int cols, const number* entries); + /** + * Construct a full matrix that + * equals the identity matrix of + * the size of the + * argument. Using this + * constructor, one can easily + * create an identity matrix of + * size n by saying + * @verbatim + * FullMatrix M(IdentityMatrix(n)); + * @endverbatim + */ + explicit FullMatrix (const IdentityMatrix &id); + /** * Assignment operator. */ @@ -271,6 +286,20 @@ class FullMatrix : public Table<2,number> FullMatrix & operator = (const double d); + /** + * Copy operator to create a full + * matrix that equals the + * identity matrix of the size of + * the argument. This way, one can easily + * create an identity matrix of + * size n by saying + * @verbatim + * M = IdentityMatrix(n); + * @endverbatim + */ + FullMatrix & + operator = (const IdentityMatrix &id); + /** * Assignment from different * matrix classes. This diff --git a/deal.II/lac/include/lac/full_matrix.templates.h b/deal.II/lac/include/lac/full_matrix.templates.h index ae08654650..c7b0e60b2a 100644 --- a/deal.II/lac/include/lac/full_matrix.templates.h +++ b/deal.II/lac/include/lac/full_matrix.templates.h @@ -57,6 +57,19 @@ FullMatrix::FullMatrix (const FullMatrix &m) {} + +template +FullMatrix::FullMatrix (const IdentityMatrix &id) + : + Table<2,number> (id.m(), id.n()) +{ + for (unsigned int i=0; i FullMatrix& FullMatrix::operator = (const FullMatrix& M) @@ -76,6 +89,20 @@ FullMatrix::operator = (const FullMatrix& M) } + +template +FullMatrix& +FullMatrix::operator = (const IdentityMatrix &id) +{ + reinit (id.m(), id.n()); + for (unsigned int i=0; i bool FullMatrix::all_zero () const diff --git a/deal.II/lac/include/lac/identity_matrix.h b/deal.II/lac/include/lac/identity_matrix.h index a244633cd4..e41cc413f6 100644 --- a/deal.II/lac/include/lac/identity_matrix.h +++ b/deal.II/lac/include/lac/identity_matrix.h @@ -42,9 +42,10 @@ @endverbatim * * This creates a $10\times 10$ matrix with ones on the diagonal and - * zeros everywhere else. All matrix types have conversion - * constructors and can therefore be filled rather easily with - * identity matrices. + * zeros everywhere else. Most matrix types, in particular FullMatrix + * and SparseMatrix, have conversion constructors and assignment + * operators for IdentityMatrix, and can therefore be filled rather + * easily with identity matrices. * * *

Preconditioning

diff --git a/deal.II/lac/include/lac/sparse_matrix.h b/deal.II/lac/include/lac/sparse_matrix.h index 4c37510981..1508743b22 100644 --- a/deal.II/lac/include/lac/sparse_matrix.h +++ b/deal.II/lac/include/lac/sparse_matrix.h @@ -18,6 +18,7 @@ #include #include #include +#include #include template class Vector; @@ -564,6 +565,21 @@ class SparseMatrix : public virtual Subscriptor * generated then. */ explicit SparseMatrix (const SparsityPattern &sparsity); + + /** + * Copy constructor: initialize + * the matrix with the identity + * matrix. This constructor will + * throw an exception if the + * sizes of the sparsity pattern + * and the identity matrix do not + * coincide, or if the sparsity + * pattern does not provide for + * nonzero entries on the entire + * diagonal. + */ + SparseMatrix (const SparsityPattern &sparsity, + const IdentityMatrix &id); /** * Destructor. Free all memory, but do not @@ -578,6 +594,21 @@ class SparseMatrix : public virtual Subscriptor */ SparseMatrix& operator = (const SparseMatrix &); + /** + * Copy operator: initialize + * the matrix with the identity + * matrix. This operator will + * throw an exception if the + * sizes of the sparsity pattern + * and the identity matrix do not + * coincide, or if the sparsity + * pattern does not provide for + * nonzero entries on the entire + * diagonal. + */ + SparseMatrix & + operator= (const IdentityMatrix &id); + /** * This operator assigns a scalar to * a matrix. Since this does usually diff --git a/deal.II/lac/include/lac/sparse_matrix.templates.h b/deal.II/lac/include/lac/sparse_matrix.templates.h index 1bf79f2d9e..dfa36e6451 100644 --- a/deal.II/lac/include/lac/sparse_matrix.templates.h +++ b/deal.II/lac/include/lac/sparse_matrix.templates.h @@ -93,6 +93,24 @@ SparseMatrix::SparseMatrix (const SparsityPattern &c) +template +SparseMatrix::SparseMatrix (const SparsityPattern &c, + const IdentityMatrix &id) + : + cols(0), + val(0), + max_len(0) +{ + Assert (c.n_rows() == id.m(), ExcDimensionMismatch (c.n_rows(), id.m())); + Assert (c.n_cols() == id.n(), ExcDimensionMismatch (c.n_cols(), id.n())); + + reinit (c); + for (unsigned int i=0; iset(i,i,1.); +} + + + template SparseMatrix::~SparseMatrix () { @@ -121,6 +139,24 @@ SparseMatrix::operator = (const double d) +template +SparseMatrix & +SparseMatrix::operator= (const IdentityMatrix &id) +{ + Assert (cols->n_rows() == id.m(), + ExcDimensionMismatch (cols->n_rows(), id.m())); + Assert (cols->n_cols() == id.n(), + ExcDimensionMismatch (cols->n_cols(), id.n())); + + *this = 0; + for (unsigned int i=0; iset(i,i,1.); + + return *this; +} + + + template void SparseMatrix::reinit (const SparsityPattern &sparsity)