From 25509d74b0d17a575f0e0368eb246d62e28bad48 Mon Sep 17 00:00:00 2001 From: guido Date: Mon, 8 Jul 2002 11:12:31 +0000 Subject: [PATCH] implementation of SparseMatrixEZ started git-svn-id: https://svn.dealii.org/trunk@6222 0785d39b-7218-0410-832d-ea1e28bc413d --- deal.II/lac/include/lac/sparse_matrix_ez.h | 25 +++- .../include/lac/sparse_matrix_ez.templates.h | 127 +++++++++++++++++- 2 files changed, 146 insertions(+), 6 deletions(-) diff --git a/deal.II/lac/include/lac/sparse_matrix_ez.h b/deal.II/lac/include/lac/sparse_matrix_ez.h index 3a4c61c82b..2bbac3b8b6 100644 --- a/deal.II/lac/include/lac/sparse_matrix_ez.h +++ b/deal.II/lac/include/lac/sparse_matrix_ez.h @@ -137,7 +137,7 @@ class SparseMatrixEZ : public Subscriptor * the entries should happen to * be zero, it is counted anyway. */ - unsigned int n_nonzero_elements () const; +// unsigned int n_nonzero_elements () const; /** * Return the number of actually @@ -151,7 +151,7 @@ class SparseMatrixEZ : public Subscriptor * sparsity pattern but only the * ones that are nonzero. */ - unsigned int n_actually_nonzero_elements () const; +// unsigned int n_actually_nonzero_elements () const; /** * Set the element @p{(i,j)} to @@ -714,13 +714,18 @@ class SparseMatrixEZ : public Subscriptor /* * Access to value. */ - number& operator() (unsigned int column); +// number& operator() (unsigned int column); /** * Read-only access to value. */ const number& operator() (unsigned int column) const; + /** + * Number of entries. + */ + unsigned int size() const; + /** * Start of entry list. */ @@ -744,7 +749,10 @@ class SparseMatrixEZ : public Subscriptor private: /** - * Actual data storage. + * Actual data storage. This + * vector contains the + * entries of a row ordered + * by column number. */ std::vector values; }; @@ -911,6 +919,15 @@ SparseMatrixEZ::Row::begin() } +template +inline +unsigned int +SparseMatrixEZ::Row::size() const +{ + return values.size(); +} + + template inline std::vector::const_iterator 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 54c3fa448b..81ca366be0 100644 --- a/deal.II/lac/include/lac/sparse_matrix_ez.templates.h +++ b/deal.II/lac/include/lac/sparse_matrix_ez.templates.h @@ -1,11 +1,32 @@ #include +#include + template void SparseMatrixEZ::Row::set(unsigned int column, const number& value) { - Assert(false, ExcNotImplemented()); + // Store end of vector + const std::vector::const_iterator end_col = end(); + + // Create Entry for inserting + const Entry e(column, value); + + // Find position for inserting + // should return first Entry with + // higher column number. + std::vector::iterator col = lower_bound(begin(), end, e); + + // Insert Entry if column did not exist. + // Edit existing entry otherwise. + if (col==end_col) + values.push_back(e); + else + if (col->column == column) + col->values = value; + else + values.insert(col, e); } @@ -14,7 +35,25 @@ void SparseMatrixEZ::Row::add(unsigned int column, const number& value) { - Assert(false, ExcNotImplemented()); + const std::vector::const_iterator end_col = end(); + + // Create Entry for inserting + const Entry e(column, value); + + // Find position for inserting + // should return first Entry with + // higher column number. + std::vector::iterator col = lower_bound(begin(), end, e); + + // Insert Entry if column did not exist. + // Edit existing entry otherwise. + if (col==end_col) + values.push_back(Entry(column, value)); + else + if (col->column == column) + col->values += value; + else + values.insert(col, Entry(column, value)); } @@ -91,5 +130,89 @@ SparseMatrixEZ::vmult (Vector& dst, { Assert(m() == dst.size(), ExcDimensionMismatch(m(),dst.size())); Assert(n() == src.size(), ExcDimensionMismatch(n(),src.size())); + + std::vector::const_iterator row = rows.begin(); + const std::vector::const_iterator end_row = rows.end(); + for (unsigned int i=0; row != end_row; ++i, ++row) + { + std::vector::const_iterator col = row->begin(); + const std::vector::const_iterator end_col = row->end(); + + double s = 0.; + for (;col != end_col; ++col) + s += col->value * src(col->column); + dst(i) = s; + } } + +template +template +void +SparseMatrixEZ::Tvmult (Vector& dst, + const Vector& src) const +{ + dst = 0.; + Tvmult_add(dst, src); +} + + +template +template +void +SparseMatrixEZ::vmult_add (Vector& dst, + const Vector& src) const +{ + Assert(m() == dst.size(), ExcDimensionMismatch(m(),dst.size())); + Assert(n() == src.size(), ExcDimensionMismatch(n(),src.size())); + + std::vector::const_iterator row = rows.begin(); + const std::vector::const_iterator end_row = rows.end(); + for (unsigned int i=0; row != end_row; ++i, ++row) + { + std::vector::const_iterator col = row->begin(); + const std::vector::const_iterator end_col = row->end(); + + double s = 0.; + for (;col != end_col; ++col) + s += col->value * src(col->column); + dst(i) += s; + } +} + +template +template +void +SparseMatrixEZ::Tvmult_add (Vector& dst, + const Vector& src) const +{ + Assert(n() == dst.size(), ExcDimensionMismatch(n(),dst.size())); + Assert(m() == src.size(), ExcDimensionMismatch(m(),src.size())); + + std::vector::const_iterator row = rows.begin(); + const std::vector::const_iterator end_row = rows.end(); + for (unsigned int i=0; row != end_row; ++i, ++row) + { + std::vector::const_iterator col = row->begin(); + const std::vector::const_iterator end_col = row->end(); + + double s = 0.; + for (;col != end_col; ++col) + dst(col->column) += col->value * src(i); + } +} + +template +template +unsigned int +SparseMatrixEZ::memory_consumption() const +{ + unsigned int result = sizeof (*this) + + sizeof(Row) * sizeof (rows); + + for (std::vector::const_iterator r = rows.begin(); + r != rows.end(); ++r) + result += r->size() * sizeof(Entry); + + return result; +} -- 2.39.5