From: bangerth Date: Tue, 12 Feb 2013 17:03:23 +0000 (+0000) Subject: Follow the documented advice of not looping over all elements of a matrix but do... X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a830d299b673ffb01558737ea4f68aa9b8343d7d;p=dealii-svn.git Follow the documented advice of not looping over all elements of a matrix but do so row by row. git-svn-id: https://svn.dealii.org/trunk@28332 0785d39b-7218-0410-832d-ea1e28bc413d --- diff --git a/deal.II/include/deal.II/lac/sparse_matrix_ez.h b/deal.II/include/deal.II/lac/sparse_matrix_ez.h index 0e0b63acb9..71b209873f 100644 --- a/deal.II/include/deal.II/lac/sparse_matrix_ez.h +++ b/deal.II/include/deal.II/lac/sparse_matrix_ez.h @@ -1611,15 +1611,18 @@ 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) + // loop over the elements of the argument matrix row by row, as suggested + // in the documentation of the sparse matrix iterator class, and + // copy them into the current object + for (unsigned int row = 0; row < M.n(); ++row) { - if (start->value() != 0.) - set(start->row(), start->column(), start->value()); - ++start; + const typename MATRIX::const_iterator end_row = M.end(row); + for (typename MATRIX::const_iterator entry = M.begin(row); + entry != end_row; ++entry) + if (entry->value() != 0) + set(row, entry->column(), entry->value()); } + return *this; } @@ -1636,14 +1639,16 @@ SparseMatrixEZ::add (const number factor, if (factor == 0.) return; - typename MATRIX::const_iterator start = M.begin(); - const typename MATRIX::const_iterator final = M.end(); - - while (start != final) + // loop over the elements of the argument matrix row by row, as suggested + // in the documentation of the sparse matrix iterator class, and + // add them into the current object + for (unsigned int row = 0; row < M.n(); ++row) { - if (start->value() != 0.) - add(start->row(), start->column(), factor * start->value()); - ++start; + const typename MATRIX::const_iterator end_row = M.end(row); + for (typename MATRIX::const_iterator entry = M.begin(row); + entry != end_row; ++entry) + if (entry->value() != 0) + add(row, entry->column(), factor * entry->value()); } }