From: guido Date: Thu, 3 Oct 2002 08:29:22 +0000 (+0000) Subject: additional functions in SparseMatrixEZ X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e27188a8be74e60687eec1212c4905fd47ec05de;p=dealii-svn.git additional functions in SparseMatrixEZ git-svn-id: https://svn.dealii.org/trunk@6596 0785d39b-7218-0410-832d-ea1e28bc413d --- diff --git a/deal.II/lac/include/lac/sparse_matrix_ez.h b/deal.II/lac/include/lac/sparse_matrix_ez.h index 637ccad61b..c892790bd8 100644 --- a/deal.II/lac/include/lac/sparse_matrix_ez.h +++ b/deal.II/lac/include/lac/sparse_matrix_ez.h @@ -463,9 +463,9 @@ class SparseMatrixEZ : public Subscriptor * The function returns a reference to * @p{this}. */ -// template -// SparseMatrix & -// copy_from (const SparseMatrix &source); + template + SparseMatrixEZ & + copy_from (const MATRIX &source); /** * This function is complete @@ -518,22 +518,18 @@ class SparseMatrixEZ : public Subscriptor /** * Add @p{matrix} scaled by - * @p{factor} to this matrix. The - * function throws an error if - * the sparsity patterns of the - * two involved matrices do not - * point to the same object, - * since in this case the - * operation is cheaper. + * @p{factor} to this matrix. * - * The source matrix may be a matrix - * of arbitrary type, as long as its - * data type is convertible to the - * data type of this matrix. + * The source matrix may be a + * matrix of arbitrary type, as + * long as its data type is + * convertible to the data type + * of this matrix and it has the + * standard @p{const_iterator}. */ -// template -// void add_scaled (const number factor, -// const SparseMatrix &matrix); + template + void add_scaled (const number factor, + const MATRIX &matrix); /** * Return the value of the entry @@ -658,6 +654,11 @@ class SparseMatrixEZ : public Subscriptor template somenumber matrix_scalar_product (const Vector &u, const Vector &v) const; + + /** + * Frobenius-norm of the matrix. + */ + number l2_norm () const; /** * Return the l1-norm of the matrix, that is @@ -670,7 +671,7 @@ class SparseMatrixEZ : public Subscriptor * $|Mv|_1\leq |M|_1 |v|_1$. * (cf. Haemmerlin-Hoffmann : Numerische Mathematik) */ - number l1_norm () const; +// number l1_norm () const; /** * Return the linfty-norm of the @@ -684,7 +685,7 @@ class SparseMatrixEZ : public Subscriptor * $|Mv|_infty \leq |M|_infty |v|_infty$. * (cf. Haemmerlin-Hoffmann : Numerische Mathematik) */ - number linfty_norm () const; +// number linfty_norm () const; /** * Apply the Jacobi @@ -1332,5 +1333,44 @@ SparseMatrixEZ::end (unsigned int r) const return const_iterator(this, r+1, 0); } +template +template +inline +SparseMatrixEZ& +SparseMatrixEZ::copy_from (const MATRIX& M) +{ + reinit(M.m(), M.n()); + + typename MATRIX::const_iterator start = M.begin(); + const typename MATRIX::const_iterator final = M.end(); + + while (start != final) + { + set(start->row(), start->column(), start->value()); + ++start; + } + return *this; +} + +template +template +inline +void +SparseMatrixEZ::add_scaled (number factor, + const MATRIX& M) +{ + Assert (M.m() == m(), ExcDimensionMismatch(M.m(), m())); + Assert (M.n() == n(), ExcDimensionMismatch(M.n(), n())); + + typename MATRIX::const_iterator start = M.begin(); + const typename MATRIX::const_iterator final = M.end(); + + while (start != final) + { + add(start->row(), start->column(), factor * start->value()); + ++start; + } +} + #endif /*---------------------------- sparse_matrix.h ---------------------------*/ diff --git a/deal.II/lac/include/lac/sparse_matrix_ez.templates.h b/deal.II/lac/include/lac/sparse_matrix_ez.templates.h index f54b66ec1d..87ab3794b9 100644 --- a/deal.II/lac/include/lac/sparse_matrix_ez.templates.h +++ b/deal.II/lac/include/lac/sparse_matrix_ez.templates.h @@ -113,6 +113,25 @@ SparseMatrixEZ::vmult (Vector& dst, } +template +number +SparseMatrixEZ::l2_norm () const +{ + number sum = 0.; + const_iterator start = begin(); + const_iterator final = end(); + + while (start != final) + { + const double value = start->value(); + sum += value*value; + ++start; + } + return std::sqrt(sum); +} + + + template template void diff --git a/tests/lac/sparse_matrices.cc b/tests/lac/sparse_matrices.cc index ccfab17f93..d67c37ae5f 100644 --- a/tests/lac/sparse_matrices.cc +++ b/tests/lac/sparse_matrices.cc @@ -158,6 +158,11 @@ int main() #ifdef DEBUG check_iterator(E); #endif + E.add_scaled(-1., A); + if (E.l2_norm() < 1.e-14) + deallog << "Matrices are equal" << std::endl; + else + deallog << "Matrices differ!!" << std::endl; A.clear(); deallog << "Structure" << std::endl; diff --git a/tests/lac/sparse_matrices.checked b/tests/lac/sparse_matrices.checked index dbc2116780..60784853af 100644 --- a/tests/lac/sparse_matrices.checked +++ b/tests/lac/sparse_matrices.checked @@ -202,6 +202,7 @@ DEAL:: 2 1 0 -1.00 DEAL:: 2 2 1 5.00 DEAL:: 2 3 2 -2.00 DEAL:: 2 6 3 -1.00 +DEAL::Matrices are equal DEAL::Structure DEAL::Assemble DEAL:9-SparseMatrix:Richardson::Starting