From f9689e8cd7c52c6623fda7cf75a27f3001013c35 Mon Sep 17 00:00:00 2001 From: bangerth Date: Mon, 7 Jan 2013 14:56:43 +0000 Subject: [PATCH] Fix implementation of SparseMatrix::iterator::operator- for the general case. Add a test. git-svn-id: https://svn.dealii.org/trunk@27961 0785d39b-7218-0410-832d-ea1e28bc413d --- deal.II/include/deal.II/lac/sparse_matrix.h | 14 +-- tests/bits/sparse_matrix_iterator_13.cc | 107 ++++++++++++++++++ .../sparse_matrix_iterator_13/cmp/generic | 2 + 3 files changed, 116 insertions(+), 7 deletions(-) create mode 100644 tests/bits/sparse_matrix_iterator_13.cc create mode 100644 tests/bits/sparse_matrix_iterator_13/cmp/generic diff --git a/deal.II/include/deal.II/lac/sparse_matrix.h b/deal.II/include/deal.II/lac/sparse_matrix.h index f787951f9e..96b39d28ff 100644 --- a/deal.II/include/deal.II/lac/sparse_matrix.h +++ b/deal.II/include/deal.II/lac/sparse_matrix.h @@ -2240,13 +2240,13 @@ namespace SparseMatrixIterators return ((*this)->index() - other->index()); else { -//TODO: this shouldn't be so hard to implement. it could either be done as -// std::difference(*this, other), but for that we lack a bunch of typedefs -// in the iterator class; it is also inefficient since it has linear complexity -// in the distance. alternatively, one should be able to just add up the -// entries in all of the rows of the matrix between *this and other - Assert (false, ExcNotImplemented()); - return 0; + const SparsityPattern &sparsity = accessor.get_matrix().get_sparsity_pattern(); + const unsigned int this_position + = sparsity.get_rowstart_indices()[(*this)->row()] + (*this)->index(); + const unsigned int other_position + = sparsity.get_rowstart_indices()[other->row()] + other->index(); + + return (this_position - other_position); } } diff --git a/tests/bits/sparse_matrix_iterator_13.cc b/tests/bits/sparse_matrix_iterator_13.cc new file mode 100644 index 0000000000..94661d23ba --- /dev/null +++ b/tests/bits/sparse_matrix_iterator_13.cc @@ -0,0 +1,107 @@ +//---------------------------- sparse_matrix_iterator_13.cc --------------------------- +// $Id$ +// Version: $Name$ +// +// Copyright (C) 2004, 2005, 2013 by the deal.II authors +// +// This file is subject to QPL and may not be distributed +// without copyright and license information. Please refer +// to the file deal.II/doc/license.html for the text and +// further information on this license. +// +//---------------------------- sparse_matrix_iterator_13.cc --------------------------- + +// test SparseMatrix::iterator::operator- + +#include "../tests.h" +#include +#include +#include + + +void test () +{ + SparsityPattern sp (5,5,3); + for (unsigned int i=0; i<5; ++i) + for (unsigned int j=0; j<5; ++j) + if (((i+2*j+1) % 3 == 0) + || + (i==j)) + sp.add (i,j); + sp.compress (); + + SparseMatrix m(sp); + + for (unsigned int row=0; row