16 #ifndef dealii__sparse_matrix_ez_h 17 #define dealii__sparse_matrix_ez_h 20 #include <deal.II/base/config.h> 21 #include <deal.II/base/subscriptor.h> 22 #include <deal.II/base/smartpointer.h> 23 #include <deal.II/lac/exceptions.h> 27 DEAL_II_NAMESPACE_OPEN
29 template<
typename number>
class Vector;
97 template <
typename number>
121 const number &
value);
165 static const unsigned short 166 invalid_diagonal =
static_cast<unsigned short>(-1);
189 const unsigned short index);
194 size_type row()
const;
199 unsigned short index()
const;
209 number
value()
const;
239 const unsigned short index);
259 const Accessor *operator-> ()
const;
323 const size_type default_row_length = 0,
324 const unsigned int default_increment = 1);
354 void reinit (
const size_type n_rows,
356 size_type default_row_length = 0,
357 unsigned int default_increment = 1,
358 size_type reserve = 0);
380 size_type
m ()
const;
386 size_type
n ()
const;
409 template <
class StreamType>
422 size_type &allocated,
424 std::vector<size_type> &used_by_line,
425 const bool compute_by_line)
const;
447 void set (
const size_type i,
const size_type j,
448 const number
value,
const bool elide_zero_values =
true);
455 void add (
const size_type i,
473 template <
typename number2>
474 void add (
const std::vector<size_type> &indices,
476 const bool elide_zero_values =
true);
483 template <
typename number2>
484 void add (
const std::vector<size_type> &row_indices,
485 const std::vector<size_type> &col_indices,
487 const bool elide_zero_values =
true);
498 template <
typename number2>
499 void add (
const size_type row,
500 const std::vector<size_type> &col_indices,
501 const std::vector<number2> &values,
502 const bool elide_zero_values =
true);
513 template <
typename number2>
514 void add (
const size_type row,
515 const size_type n_cols,
516 const size_type *col_indices,
517 const number2 *values,
518 const bool elide_zero_values =
true,
519 const bool col_indices_are_sorted =
false);
542 template <
typename MatrixType>
544 copy_from (
const MatrixType &source,
const bool elide_zero_values =
true);
553 template <
typename MatrixType>
554 void add (
const number factor,
555 const MatrixType &matrix);
571 const size_type j)
const;
577 number
el (
const size_type i,
578 const size_type j)
const;
588 template <
typename somenumber>
597 template <
typename somenumber>
605 template <
typename somenumber>
614 template <
typename somenumber>
636 template <
typename somenumber>
639 const number omega = 1.)
const;
644 template <
typename somenumber>
647 const number om = 1.,
648 const std::vector<std::size_t> &pos_right_of_diagonal = std::vector<std::size_t>())
const;
654 template <
typename somenumber>
657 const number om = 1.)
const;
663 template <
typename somenumber>
666 const number om = 1.)
const;
676 template <
typename MatrixTypeA,
typename MatrixTypeB>
678 const MatrixTypeB &B,
679 const bool transpose =
false);
715 void print (std::ostream &out)
const;
738 const unsigned int precision = 3,
739 const bool scientific =
true,
740 const unsigned int width = 0,
741 const char *zero_string =
" ",
742 const double denominator = 1.)
const;
779 <<
"The entry with index (" << arg1 <<
',' << arg2
780 <<
") does not exist.");
784 <<
"An entry with index (" << arg1 <<
',' << arg2
785 <<
") cannot be allocated.");
793 const size_type col)
const;
800 const size_type col);
806 const size_type col);
813 template <
typename somenumber>
816 const size_type begin_row,
817 const size_type end_row)
const;
824 template <
typename somenumber>
826 const size_type begin_row,
827 const size_type end_row,
828 somenumber *partial_sum)
const;
835 template <
typename somenumber>
838 const size_type begin_row,
839 const size_type end_row,
840 somenumber *partial_sum)
const;
873 template <
typename number>
884 template <
typename number>
893 template <
typename number>
899 diagonal(invalid_diagonal)
904 template <
typename number>
909 const unsigned short i)
917 template <
typename number>
926 template <
typename number>
935 template <
typename number>
945 template <
typename number>
954 template <
typename number>
959 const unsigned short i)
989 template <
typename number>
1017 template <
typename number>
1026 template <
typename number>
1035 template <
typename number>
1046 template <
typename number>
1052 return ! (*
this == other);
1056 template <
typename number>
1069 template <
typename number>
1077 template <
typename number>
1085 template <
typename number>
1089 const size_type col)
1091 Assert (row<
m(), ExcIndexRange(row,0,
m()));
1092 Assert (col<
n(), ExcIndexRange(col,0,
n()));
1096 for (size_type i=r.
start; i<end; ++i)
1099 if (entry->
column == col)
1109 template <
typename number>
1113 const size_type col)
const 1116 return t->
locate(row,col);
1120 template <
typename number>
1124 const size_type col)
1126 Assert (row<
m(), ExcIndexRange(row,0,
m()));
1127 Assert (col<
n(), ExcIndexRange(col,0,
n()));
1132 size_type i = r.
start;
1139 while (i<end &&
data[i].column < col) ++i;
1142 if (i != end &&
data[i].column == col)
1164 for (size_type rn=row+1; rn<
row_info.size(); ++rn)
1170 if (end >=
data.size())
1181 Entry temp = *entry;
1200 for (size_type j = i+1; j <
end; ++j)
1207 std::swap (
data[j], temp);
1218 template <
typename number>
1223 const bool elide_zero_values)
1227 Assert (i<
m(), ExcIndexRange(i,0,
m()));
1228 Assert (j<
n(), ExcIndexRange(j,0,
n()));
1230 if (elide_zero_values && value == 0.)
1239 entry->
value = value;
1245 template <
typename number>
1254 Assert (i<
m(), ExcIndexRange(i,0,
m()));
1255 Assert (j<
n(), ExcIndexRange(j,0,
n()));
1262 entry->
value += value;
1266 template <
typename number>
1267 template <
typename number2>
1270 const bool elide_zero_values)
1273 for (size_type i=0; i<indices.size(); ++i)
1274 for (size_type j=0; j<indices.size(); ++j)
1275 if ((full_matrix(i,j) != 0) || (elide_zero_values ==
false))
1276 add (indices[i], indices[j], full_matrix(i,j));
1281 template <
typename number>
1282 template <
typename number2>
1284 const std::vector<size_type> &col_indices,
1286 const bool elide_zero_values)
1289 for (size_type i=0; i<row_indices.size(); ++i)
1290 for (size_type j=0; j<col_indices.size(); ++j)
1291 if ((full_matrix(i,j) != 0) || (elide_zero_values ==
false))
1292 add (row_indices[i], col_indices[j], full_matrix(i,j));
1298 template <
typename number>
1299 template <
typename number2>
1301 const std::vector<size_type> &col_indices,
1302 const std::vector<number2> &values,
1303 const bool elide_zero_values)
1306 for (size_type j=0; j<col_indices.size(); ++j)
1307 if ((values[j] != 0) || (elide_zero_values ==
false))
1308 add (row, col_indices[j], values[j]);
1313 template <
typename number>
1314 template <
typename number2>
1316 const size_type n_cols,
1317 const size_type *col_indices,
1318 const number2 *values,
1319 const bool elide_zero_values,
1323 for (size_type j=0; j<n_cols; ++j)
1324 if ((values[j] != 0) || (elide_zero_values ==
false))
1325 add (row, col_indices[j], values[j]);
1331 template <
typename number>
1334 const size_type j)
const 1338 return entry->
value;
1344 template <
typename number>
1347 const size_type j)
const 1351 return entry->
value;
1352 Assert(
false, ExcInvalidEntry(i,j));
1357 template <
typename number>
1366 template <
typename number>
1374 template <
typename number>
1379 Assert (r<
m(), ExcIndexRange(r,0,
m()));
1384 template <
typename number>
1389 Assert (r<
m(), ExcIndexRange(r,0,
m()));
1394 template<
typename number>
1395 template <
typename MatrixType>
1408 for (size_type row = 0; row < M.m(); ++row)
1410 const typename MatrixType::const_iterator end_row = M.end(row);
1411 for (
typename MatrixType::const_iterator entry = M.begin(row);
1412 entry != end_row; ++entry)
1413 set(row, entry->column(), entry->value(), elide_zero_values);
1419 template<
typename number>
1420 template <
typename MatrixType>
1424 const MatrixType &M)
1426 Assert (M.m() ==
m(), ExcDimensionMismatch(M.m(),
m()));
1427 Assert (M.n() ==
n(), ExcDimensionMismatch(M.n(),
n()));
1435 for (size_type row = 0; row < M.m(); ++row)
1437 const typename MatrixType::const_iterator end_row = M.end(row);
1438 for (
typename MatrixType::const_iterator entry = M.begin(row);
1439 entry != end_row; ++entry)
1440 if (entry->value() != 0)
1441 add(row, entry->column(), factor * entry->value());
1447 template<
typename number>
1448 template <
typename MatrixTypeA,
typename MatrixTypeB>
1451 const MatrixTypeB &B,
1452 const bool transpose)
1470 typename MatrixTypeB::const_iterator b1 = B.begin();
1471 const typename MatrixTypeB::const_iterator b_final = B.end();
1473 while (b1 != b_final)
1475 const size_type i = b1->column();
1476 const size_type k = b1->row();
1477 typename MatrixTypeB::const_iterator b2 = B.begin();
1478 while (b2 != b_final)
1480 const size_type j = b2->column();
1481 const size_type l = b2->row();
1483 const typename MatrixTypeA::value_type a = A.el(k,l);
1486 add (i, j, a * b1->value() * b2->value());
1497 std::vector<size_type> minrow(B.n(), B.m());
1498 std::vector<size_type> maxrow(B.n(), 0);
1499 while (b1 != b_final)
1501 const size_type r = b1->row();
1502 if (r < minrow[b1->column()])
1503 minrow[b1->column()] = r;
1504 if (r > maxrow[b1->column()])
1505 maxrow[b1->column()] = r;
1509 typename MatrixTypeA::const_iterator ai = A.begin();
1510 const typename MatrixTypeA::const_iterator ae = A.end();
1514 const typename MatrixTypeA::value_type a = ai->value();
1517 if (a == 0.)
continue;
1523 b1 = B.begin(minrow[ai->row()]);
1524 const typename MatrixTypeB::const_iterator
1525 be1 = B.end(maxrow[ai->row()]);
1526 const typename MatrixTypeB::const_iterator
1527 be2 = B.end(maxrow[ai->column()]);
1531 const double b1v = b1->value();
1536 if (b1->column() == ai->row() && (b1v != 0.))
1538 const size_type i = b1->row();
1540 typename MatrixTypeB::const_iterator
1541 b2 = B.begin(minrow[ai->column()]);
1544 if (b2->column() == ai->column())
1546 const size_type j = b2->row();
1547 add (i, j, a * b1v * b2->value());
1560 template <
typename number>
1561 template <
class StreamType>
1567 size_type allocated;
1569 std::vector<size_type> used_by_line;
1573 out <<
"SparseMatrixEZ:used entries:" << used << std::endl
1574 <<
"SparseMatrixEZ:allocated entries:" << allocated << std::endl
1575 <<
"SparseMatrixEZ:reserved entries:" << reserved << std::endl;
1579 for (size_type i=0; i< used_by_line.size(); ++i)
1580 if (used_by_line[i] != 0)
1581 out <<
"SparseMatrixEZ:entries\t" << i
1582 <<
"\trows\t" << used_by_line[i]
1589 DEAL_II_NAMESPACE_CLOSE
size_type get_row_length(const size_type row) const
const types::global_dof_index invalid_size_type
void Tvmult_add(Vector< somenumber > &dst, const Vector< somenumber > &src) const
const_iterator & operator++()
void add(const size_type i, const size_type j, const number value)
DeclException2(ExcInvalidEntry, int, int,<< "The entry with index ("<< arg1<< ','<< arg2<< ") does not exist.")
void compute_statistics(size_type &used, size_type &allocated, size_type &reserved, std::vector< size_type > &used_by_line, const bool compute_by_line) const
number operator()(const size_type i, const size_type j) const
SparseMatrixEZ< number > & copy_from(const MatrixType &source, const bool elide_zero_values=true)
void set(const size_type i, const size_type j, const number value, const bool elide_zero_values=true)
void conjugate_add(const MatrixTypeA &A, const MatrixTypeB &B, const bool transpose=false)
void block_read(std::istream &in)
bool operator<(const const_iterator &) const
void vmult_add(Vector< somenumber > &dst, const Vector< somenumber > &src) const
void vmult(Vector< somenumber > &dst, const Vector< somenumber > &src) const
static const size_type invalid
const Accessor & operator*() const
std::size_t memory_consumption() const
void threaded_matrix_norm_square(const Vector< somenumber > &v, const size_type begin_row, const size_type end_row, somenumber *partial_sum) const
void threaded_matrix_scalar_product(const Vector< somenumber > &u, const Vector< somenumber > &v, const size_type begin_row, const size_type end_row, somenumber *partial_sum) const
DeclException0(ExcNoDiagonal)
bool operator==(const const_iterator &) const
void print(std::ostream &out) const
void precondition_SOR(Vector< somenumber > &dst, const Vector< somenumber > &src, const number om=1.) const
SparseMatrixEZ< number > & operator=(const SparseMatrixEZ< number > &)
Accessor(const SparseMatrixEZ< number > *matrix, const size_type row, const unsigned short index)
unsigned int global_dof_index
#define Assert(cond, exc)
void reinit(const size_type n_rows, const size_type n_columns, size_type default_row_length=0, unsigned int default_increment=1, size_type reserve=0)
bool operator!=(const const_iterator &) const
const_iterator(const SparseMatrixEZ< number > *matrix, const size_type row, const unsigned short index)
const SparseMatrixEZ< number > * matrix
std::vector< Entry > data
Entry * allocate(const size_type row, const size_type col)
void print_statistics(StreamType &s, bool full=false)
RowInfo(size_type start=Entry::invalid)
void block_write(std::ostream &out) const
types::global_dof_index size_type
const Accessor * operator->() const
void precondition_TSOR(Vector< somenumber > &dst, const Vector< somenumber > &src, const number om=1.) const
unsigned short index() const
number el(const size_type i, const size_type j) const
const Entry * locate(const size_type row, const size_type col) const
const_iterator begin() const
void precondition_Jacobi(Vector< somenumber > &dst, const Vector< somenumber > &src, const number omega=1.) const
void threaded_vmult(Vector< somenumber > &dst, const Vector< somenumber > &src, const size_type begin_row, const size_type end_row) const
void Tvmult(Vector< somenumber > &dst, const Vector< somenumber > &src) const
static const unsigned short invalid_diagonal
void precondition_SSOR(Vector< somenumber > &dst, const Vector< somenumber > &src, const number om=1., const std::vector< std::size_t > &pos_right_of_diagonal=std::vector< std::size_t >()) const
const_iterator end() const
#define AssertIsFinite(number)
unsigned int saved_default_row_length
std::vector< RowInfo > row_info
size_type n_nonzero_elements() const
void print_formatted(std::ostream &out, const unsigned int precision=3, const bool scientific=true, const unsigned int width=0, const char *zero_string=" ", const double denominator=1.) const