* 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
* 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
/*
* 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.
*/
private:
/**
- * Actual data storage.
+ * Actual data storage. This
+ * vector contains the
+ * entries of a row ordered
+ * by column number.
*/
std::vector<Entry> values;
};
}
+template <typename number>
+inline
+unsigned int
+SparseMatrixEZ<number>::Row::size() const
+{
+ return values.size();
+}
+
+
template <typename number>
inline
std::vector<Entry>::const_iterator
#include <lac/sparse_matrix_ez.h>
+#include <algorithm>
+
template <typename number>
void
SparseMatrixEZ<number>::Row::set(unsigned int column,
const number& value)
{
- Assert(false, ExcNotImplemented());
+ // Store end of vector
+ const std::vector<Entry>::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<Entry>::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);
}
SparseMatrixEZ<number>::Row::add(unsigned int column,
const number& value)
{
- Assert(false, ExcNotImplemented());
+ const std::vector<Entry>::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<Entry>::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));
}
{
Assert(m() == dst.size(), ExcDimensionMismatch(m(),dst.size()));
Assert(n() == src.size(), ExcDimensionMismatch(n(),src.size()));
+
+ std::vector<Row>::const_iterator row = rows.begin();
+ const std::vector<Row>::const_iterator end_row = rows.end();
+ for (unsigned int i=0; row != end_row; ++i, ++row)
+ {
+ std::vector<Entry>::const_iterator col = row->begin();
+ const std::vector<Entry>::const_iterator end_col = row->end();
+
+ double s = 0.;
+ for (;col != end_col; ++col)
+ s += col->value * src(col->column);
+ dst(i) = s;
+ }
}
+
+template <typename number>
+template <typename somenumber>
+void
+SparseMatrixEZ<number>::Tvmult (Vector<somenumber>& dst,
+ const Vector<somenumber>& src) const
+{
+ dst = 0.;
+ Tvmult_add(dst, src);
+}
+
+
+template <typename number>
+template <typename somenumber>
+void
+SparseMatrixEZ<number>::vmult_add (Vector<somenumber>& dst,
+ const Vector<somenumber>& src) const
+{
+ Assert(m() == dst.size(), ExcDimensionMismatch(m(),dst.size()));
+ Assert(n() == src.size(), ExcDimensionMismatch(n(),src.size()));
+
+ std::vector<Row>::const_iterator row = rows.begin();
+ const std::vector<Row>::const_iterator end_row = rows.end();
+ for (unsigned int i=0; row != end_row; ++i, ++row)
+ {
+ std::vector<Entry>::const_iterator col = row->begin();
+ const std::vector<Entry>::const_iterator end_col = row->end();
+
+ double s = 0.;
+ for (;col != end_col; ++col)
+ s += col->value * src(col->column);
+ dst(i) += s;
+ }
+}
+
+template <typename number>
+template <typename somenumber>
+void
+SparseMatrixEZ<number>::Tvmult_add (Vector<somenumber>& dst,
+ const Vector<somenumber>& src) const
+{
+ Assert(n() == dst.size(), ExcDimensionMismatch(n(),dst.size()));
+ Assert(m() == src.size(), ExcDimensionMismatch(m(),src.size()));
+
+ std::vector<Row>::const_iterator row = rows.begin();
+ const std::vector<Row>::const_iterator end_row = rows.end();
+ for (unsigned int i=0; row != end_row; ++i, ++row)
+ {
+ std::vector<Entry>::const_iterator col = row->begin();
+ const std::vector<Entry>::const_iterator end_col = row->end();
+
+ double s = 0.;
+ for (;col != end_col; ++col)
+ dst(col->column) += col->value * src(i);
+ }
+}
+
+template <typename number>
+template <typename somenumber>
+unsigned int
+SparseMatrixEZ<number>::memory_consumption() const
+{
+ unsigned int result = sizeof (*this)
+ + sizeof(Row) * sizeof (rows);
+
+ for (std::vector<Row>::const_iterator r = rows.begin();
+ r != rows.end(); ++r)
+ result += r->size() * sizeof(Entry);
+
+ return result;
+}