]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Follow the documented advice of not looping over all elements of a matrix but do...
authorWolfgang Bangerth <bangerth@math.tamu.edu>
Tue, 12 Feb 2013 17:03:23 +0000 (17:03 +0000)
committerWolfgang Bangerth <bangerth@math.tamu.edu>
Tue, 12 Feb 2013 17:03:23 +0000 (17:03 +0000)
git-svn-id: https://svn.dealii.org/trunk@28332 0785d39b-7218-0410-832d-ea1e28bc413d

deal.II/include/deal.II/lac/sparse_matrix_ez.h

index 0e0b63acb9d0707f6c96469e31ac5eec9bbc1292..71b209873f5828289dc6bdac26f4b5ce904160cb 100644 (file)
@@ -1611,15 +1611,18 @@ SparseMatrixEZ<number>::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<number>::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());
     }
 }
 

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.