From 0a612ab9b5a07e05ed0d5417da2252d7cde9ec5f Mon Sep 17 00:00:00 2001 From: Wolfgang Bangerth Date: Tue, 12 Feb 2013 17:03:23 +0000 Subject: [PATCH] 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 --- .../include/deal.II/lac/sparse_matrix_ez.h | 33 +++++++++++-------- 1 file changed, 19 insertions(+), 14 deletions(-) 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()); } } -- 2.39.5