Reference documentation for deal.II version 9.0.0
chunk_sparse_matrix.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2008 - 2017 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE at
12 // the top level of the deal.II distribution.
13 //
14 // ---------------------------------------------------------------------
15 
16 #ifndef dealii_chunk_sparse_matrix_h
17 #define dealii_chunk_sparse_matrix_h
18 
19 
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/chunk_sparsity_pattern.h>
24 #include <deal.II/lac/identity_matrix.h>
25 #include <deal.II/lac/exceptions.h>
26 
27 #include <memory>
28 
29 
30 DEAL_II_NAMESPACE_OPEN
31 
32 template <typename number> class Vector;
33 template <typename number> class FullMatrix;
34 
44 {
45  // forward declaration
46  template <typename number, bool Constness>
47  class Iterator;
48 
59  template <typename number, bool Constness>
61  {
62  public:
66  number value() const;
67 
71  number &value();
72 
77  const ChunkSparseMatrix<number> &get_matrix () const;
78  };
79 
80 
81 
88  template <typename number>
90  {
91  public:
97 
101  Accessor (MatrixType *matrix,
102  const unsigned int row);
103 
107  Accessor (MatrixType *matrix);
108 
113 
117  number value() const;
118 
123  const MatrixType &get_matrix () const;
124 
125  private:
130 
135 
139  template <typename, bool>
140  friend class Iterator;
141  };
142 
143 
150  template <typename number>
152  {
153  private:
176  class Reference
177  {
178  public:
183  Reference (const Accessor *accessor,
184  const bool dummy);
185 
189  operator number () const;
190 
194  const Reference &operator = (const number n) const;
195 
199  const Reference &operator += (const number n) const;
200 
204  const Reference &operator -= (const number n) const;
205 
209  const Reference &operator *= (const number n) const;
210 
214  const Reference &operator /= (const number n) const;
215 
216  private:
222  };
223 
224  public:
230 
234  Accessor (MatrixType *matrix,
235  const unsigned int row);
236 
240  Accessor (MatrixType *matrix);
241 
245  Reference value() const;
246 
251  MatrixType &get_matrix () const;
252 
253  private:
258 
263 
267  template <typename, bool>
268  friend class Iterator;
269  };
270 
271 
272 
283  template <typename number, bool Constness>
284  class Iterator
285  {
286  public:
290  typedef
293 
298  typedef
300 
305  Iterator (MatrixType *matrix,
306  const unsigned int row);
307 
311  Iterator (MatrixType *matrix);
312 
318 
323 
327  Iterator operator++ (int);
328 
332  const Accessor<number,Constness> &operator* () const;
333 
338 
342  bool operator == (const Iterator &) const;
343 
347  bool operator != (const Iterator &) const;
348 
356  bool operator < (const Iterator &) const;
357 
362  bool operator > (const Iterator &) const;
363 
370  int operator - (const Iterator &p) const;
371 
375  Iterator operator + (const unsigned int n) const;
376 
377  private:
382  };
383 
384 }
385 
386 
387 
406 template <typename number>
407 class ChunkSparseMatrix : public virtual Subscriptor
408 {
409 public:
414 
419  typedef number value_type;
420 
431 
436  typedef
439 
446  typedef
449 
456  struct Traits
457  {
462  static const bool zero_addition_can_be_elided = true;
463  };
464 
480 
490 
504  explicit ChunkSparseMatrix (const ChunkSparsityPattern &sparsity);
505 
512  ChunkSparseMatrix (const ChunkSparsityPattern &sparsity,
513  const IdentityMatrix &id);
514 
519  virtual ~ChunkSparseMatrix ();
520 
531 
539  operator= (const IdentityMatrix &id);
540 
550  ChunkSparseMatrix &operator = (const double d);
551 
565  virtual void reinit (const ChunkSparsityPattern &sparsity);
566 
572  virtual void clear ();
574 
582  bool empty () const;
583 
588  size_type m () const;
589 
594  size_type n () const;
595 
601  size_type n_nonzero_elements () const;
602 
611 
621 
626  std::size_t memory_consumption () const;
627 
629 
638  void set (const size_type i,
639  const size_type j,
640  const number value);
641 
647  void add (const size_type i,
648  const size_type j,
649  const number value);
650 
660  template <typename number2>
661  void add (const size_type row,
662  const size_type n_cols,
663  const size_type *col_indices,
664  const number2 *values,
665  const bool elide_zero_values = true,
666  const bool col_indices_are_sorted = false);
667 
671  ChunkSparseMatrix &operator *= (const number factor);
672 
676  ChunkSparseMatrix &operator /= (const number factor);
677 
690  void symmetrize ();
691 
708  template <typename somenumber>
711 
729  template <typename ForwardIterator>
730  void copy_from (const ForwardIterator begin,
731  const ForwardIterator end);
732 
738  template <typename somenumber>
739  void copy_from (const FullMatrix<somenumber> &matrix);
740 
752  template <typename somenumber>
753  void add (const number factor,
754  const ChunkSparseMatrix<somenumber> &matrix);
755 
757 
761 
775  number operator () (const size_type i,
776  const size_type j) const;
777 
790  number el (const size_type i,
791  const size_type j) const;
792 
802  number diag_element (const size_type i) const;
803 
813  void extract_row_copy (const size_type row,
814  const size_type array_length,
815  size_type &row_length,
816  size_type *column_indices,
817  number *values) const;
818 
820 
838  template <class OutVector, class InVector>
839  void vmult (OutVector &dst,
840  const InVector &src) const;
841 
857  template <class OutVector, class InVector>
858  void Tvmult (OutVector &dst,
859  const InVector &src) const;
860 
875  template <class OutVector, class InVector>
876  void vmult_add (OutVector &dst,
877  const InVector &src) const;
878 
894  template <class OutVector, class InVector>
895  void Tvmult_add (OutVector &dst,
896  const InVector &src) const;
897 
913  template <typename somenumber>
914  somenumber matrix_norm_square (const Vector<somenumber> &v) const;
915 
919  template <typename somenumber>
920  somenumber matrix_scalar_product (const Vector<somenumber> &u,
921  const Vector<somenumber> &v) const;
929  template <typename somenumber>
930  somenumber residual (Vector<somenumber> &dst,
931  const Vector<somenumber> &x,
932  const Vector<somenumber> &b) const;
933 
935 
939 
947  real_type l1_norm () const;
948 
956  real_type linfty_norm () const;
957 
962  real_type frobenius_norm () const;
964 
968 
974  template <typename somenumber>
975  void precondition_Jacobi (Vector<somenumber> &dst,
976  const Vector<somenumber> &src,
977  const number omega = 1.) const;
978 
982  template <typename somenumber>
983  void precondition_SSOR (Vector<somenumber> &dst,
984  const Vector<somenumber> &src,
985  const number om = 1.) const;
986 
990  template <typename somenumber>
991  void precondition_SOR (Vector<somenumber> &dst,
992  const Vector<somenumber> &src,
993  const number om = 1.) const;
994 
998  template <typename somenumber>
999  void precondition_TSOR (Vector<somenumber> &dst,
1000  const Vector<somenumber> &src,
1001  const number om = 1.) const;
1002 
1008  template <typename somenumber>
1009  void SSOR (Vector<somenumber> &v,
1010  const number omega = 1.) const;
1011 
1016  template <typename somenumber>
1017  void SOR (Vector<somenumber> &v,
1018  const number om = 1.) const;
1019 
1024  template <typename somenumber>
1025  void TSOR (Vector<somenumber> &v,
1026  const number om = 1.) const;
1027 
1038  template <typename somenumber>
1039  void PSOR (Vector<somenumber> &v,
1040  const std::vector<size_type> &permutation,
1041  const std::vector<size_type> &inverse_permutation,
1042  const number om = 1.) const;
1043 
1054  template <typename somenumber>
1055  void TPSOR (Vector<somenumber> &v,
1056  const std::vector<size_type> &permutation,
1057  const std::vector<size_type> &inverse_permutation,
1058  const number om = 1.) const;
1059 
1064  template <typename somenumber>
1065  void SOR_step (Vector<somenumber> &v,
1066  const Vector<somenumber> &b,
1067  const number om = 1.) const;
1068 
1073  template <typename somenumber>
1074  void TSOR_step (Vector<somenumber> &v,
1075  const Vector<somenumber> &b,
1076  const number om = 1.) const;
1077 
1082  template <typename somenumber>
1083  void SSOR_step (Vector<somenumber> &v,
1084  const Vector<somenumber> &b,
1085  const number om = 1.) const;
1087 
1091 
1101  const_iterator begin () const;
1102 
1111  const_iterator end () const;
1112 
1122  iterator begin ();
1123 
1132  iterator end ();
1133 
1148  const_iterator begin (const unsigned int r) const;
1149 
1164  const_iterator end (const unsigned int r) const;
1165 
1180  iterator begin (const unsigned int r);
1181 
1196  iterator end (const unsigned int r);
1198 
1202 
1207  void print (std::ostream &out) const;
1208 
1229  void print_formatted (std::ostream &out,
1230  const unsigned int precision = 3,
1231  const bool scientific = true,
1232  const unsigned int width = 0,
1233  const char *zero_string = " ",
1234  const double denominator = 1.) const;
1235 
1241  void print_pattern(std::ostream &out,
1242  const double threshold = 0.) const;
1243 
1254  void block_write (std::ostream &out) const;
1255 
1272  void block_read (std::istream &in);
1274 
1283  int, int,
1284  << "You are trying to access the matrix entry with index <"
1285  << arg1 << ',' << arg2
1286  << ">, but this entry does not exist in the sparsity pattern"
1287  "of this matrix."
1288  "\n\n"
1289  "The most common cause for this problem is that you used "
1290  "a method to build the sparsity pattern that did not "
1291  "(completely) take into account all of the entries you "
1292  "will later try to write into. An example would be "
1293  "building a sparsity pattern that does not include "
1294  "the entries you will write into due to constraints "
1295  "on degrees of freedom such as hanging nodes or periodic "
1296  "boundary conditions. In such cases, building the "
1297  "sparsity pattern will succeed, but you will get errors "
1298  "such as the current one at one point or other when "
1299  "trying to write into the entries of the matrix.");
1308  int, int,
1309  << "The iterators denote a range of " << arg1
1310  << " elements, but the given number of rows was " << arg2);
1315  "You are attempting an operation on two matrices that "
1316  "are the same object, but the operation requires that the "
1317  "two objects are in fact different.");
1319 private:
1326 
1334  std::unique_ptr<number[]> val;
1335 
1343 
1348  const size_type j) const;
1349 
1350  // make all other sparse matrices friends
1351  template <typename somenumber> friend class ChunkSparseMatrix;
1352 
1356  template <typename,bool> friend class ChunkSparseMatrixIterators::Iterator;
1357  template <typename,bool> friend class ChunkSparseMatrixIterators::Accessor;
1358 };
1359 
1362 #ifndef DOXYGEN
1363 /*---------------------- Inline functions -----------------------------------*/
1364 
1365 
1366 
1367 template <typename number>
1368 inline
1371 {
1372  Assert (cols != nullptr, ExcNotInitialized());
1373  return cols->rows;
1374 }
1375 
1376 
1377 template <typename number>
1378 inline
1381 {
1382  Assert (cols != nullptr, ExcNotInitialized());
1383  return cols->cols;
1384 }
1385 
1386 
1387 
1388 template <typename number>
1389 inline
1390 const ChunkSparsityPattern &
1392 {
1393  Assert (cols != nullptr, ExcNotInitialized());
1394  return *cols;
1395 }
1396 
1397 
1398 
1399 template <typename number>
1400 inline
1403  const size_type j) const
1404 {
1405  const size_type chunk_size = cols->get_chunk_size();
1406  const size_type chunk_index
1407  = cols->sparsity_pattern(i/chunk_size, j/chunk_size);
1408 
1409  if (chunk_index == ChunkSparsityPattern::invalid_entry)
1411  else
1412  {
1413  return (chunk_index * chunk_size * chunk_size
1414  +
1415  (i % chunk_size) * chunk_size
1416  +
1417  (j % chunk_size));
1418  }
1419 }
1420 
1421 
1422 template <typename number>
1423 inline
1424 void ChunkSparseMatrix<number>::set (const size_type i,
1425  const size_type j,
1426  const number value)
1427 {
1428 
1430 
1431  Assert (cols != nullptr, ExcNotInitialized());
1432  // it is allowed to set elements of the matrix that are not part of the
1433  // sparsity pattern, if the value to which we set it is zero
1434  const size_type index = compute_location(i,j);
1435  Assert ((index != SparsityPattern::invalid_entry) ||
1436  (value == 0.),
1437  ExcInvalidIndex(i,j));
1438 
1439  if (index != SparsityPattern::invalid_entry)
1440  val[index] = value;
1441 }
1442 
1443 
1444 
1445 template <typename number>
1446 inline
1447 void ChunkSparseMatrix<number>::add (const size_type i,
1448  const size_type j,
1449  const number value)
1450 {
1451 
1453 
1454  Assert (cols != nullptr, ExcNotInitialized());
1455 
1456  if (value != 0.)
1457  {
1458  const size_type index = compute_location(i,j);
1460  ExcInvalidIndex(i,j));
1461 
1462  val[index] += value;
1463  }
1464 }
1465 
1466 
1467 
1468 template <typename number>
1469 template <typename number2>
1470 inline
1471 void ChunkSparseMatrix<number>::add (const size_type row,
1472  const size_type n_cols,
1473  const size_type *col_indices,
1474  const number2 *values,
1475  const bool /*elide_zero_values*/,
1476  const bool /*col_indices_are_sorted*/)
1477 {
1478  // TODO: could be done more efficiently...
1479  for (size_type col=0; col<n_cols; ++col)
1480  add(row, col_indices[col], static_cast<number>(values[col]));
1481 }
1482 
1483 
1484 
1485 template <typename number>
1486 inline
1488 ChunkSparseMatrix<number>::operator *= (const number factor)
1489 {
1490  Assert (cols != nullptr, ExcNotInitialized());
1491  Assert (val != nullptr, ExcNotInitialized());
1492 
1493  const size_type chunk_size = cols->get_chunk_size();
1494 
1495  // multiply all elements of the matrix with the given factor. this includes
1496  // the padding elements in chunks that overlap the boundaries of the actual
1497  // matrix -- but since multiplication with a number does not violate the
1498  // invariant of keeping these elements at zero nothing can happen
1499  number *val_ptr = val.get();
1500  const number *const end_ptr = val.get() +
1501  cols->sparsity_pattern.n_nonzero_elements()
1502  *
1504  while (val_ptr != end_ptr)
1505  *val_ptr++ *= factor;
1506 
1507  return *this;
1508 }
1509 
1510 
1511 
1512 template <typename number>
1513 inline
1515 ChunkSparseMatrix<number>::operator /= (const number factor)
1516 {
1517  Assert (cols != nullptr, ExcNotInitialized());
1518  Assert (val != nullptr, ExcNotInitialized());
1519  Assert (factor !=0, ExcDivideByZero());
1520 
1521  const number factor_inv = 1. / factor;
1522 
1523  const size_type chunk_size = cols->get_chunk_size();
1524 
1525  // multiply all elements of the matrix with the given factor. this includes
1526  // the padding elements in chunks that overlap the boundaries of the actual
1527  // matrix -- but since multiplication with a number does not violate the
1528  // invariant of keeping these elements at zero nothing can happen
1529  number *val_ptr = val.get();
1530  const number *const end_ptr = val.get() +
1531  cols->sparsity_pattern.n_nonzero_elements()
1532  *
1534 
1535  while (val_ptr != end_ptr)
1536  *val_ptr++ *= factor_inv;
1537 
1538  return *this;
1539 }
1540 
1541 
1542 
1543 template <typename number>
1544 inline
1545 number ChunkSparseMatrix<number>::operator () (const size_type i,
1546  const size_type j) const
1547 {
1548  Assert (cols != nullptr, ExcNotInitialized());
1549  AssertThrow (compute_location(i,j) != SparsityPattern::invalid_entry,
1550  ExcInvalidIndex(i,j));
1551  return val[compute_location(i,j)];
1552 }
1553 
1554 
1555 
1556 template <typename number>
1557 inline
1558 number ChunkSparseMatrix<number>::el (const size_type i,
1559  const size_type j) const
1560 {
1561  Assert (cols != nullptr, ExcNotInitialized());
1562  const size_type index = compute_location(i,j);
1563 
1565  return val[index];
1566  else
1567  return 0;
1568 }
1569 
1570 
1571 
1572 template <typename number>
1573 inline
1574 number ChunkSparseMatrix<number>::diag_element (const size_type i) const
1575 {
1576  Assert (cols != nullptr, ExcNotInitialized());
1577  Assert (m() == n(), ExcNotQuadratic());
1578  AssertIndexRange(i, m());
1579 
1580  // Use that the first element in each row of a quadratic matrix is the main
1581  // diagonal of the chunk sparsity pattern
1582  const size_type chunk_size = cols->get_chunk_size();
1583  return val[cols->sparsity_pattern.rowstart[i/chunk_size]
1584  *
1586  +
1587  (i % chunk_size) * chunk_size
1588  +
1589  (i % chunk_size)];
1590 }
1591 
1592 
1593 
1594 template <typename number>
1595 template <typename ForwardIterator>
1596 inline
1597 void
1598 ChunkSparseMatrix<number>::copy_from (const ForwardIterator begin,
1599  const ForwardIterator end)
1600 {
1601  Assert (static_cast<size_type >(std::distance (begin, end)) == m(),
1602  ExcIteratorRange (std::distance (begin, end), m()));
1603 
1604  // for use in the inner loop, we define a typedef to the type of the inner
1605  // iterators
1606  typedef typename std::iterator_traits<ForwardIterator>::value_type::const_iterator inner_iterator;
1607  size_type row=0;
1608  for (ForwardIterator i=begin; i!=end; ++i, ++row)
1609  {
1610  const inner_iterator end_of_row = i->end();
1611  for (inner_iterator j=i->begin(); j!=end_of_row; ++j)
1612  // write entries
1613  set (row, j->first, j->second);
1614  }
1615 }
1616 
1617 
1618 
1619 //---------------------------------------------------------------------------
1620 
1621 
1623 {
1624  template <typename number>
1625  inline
1627  Accessor (const MatrixType *matrix,
1628  const unsigned int row)
1629  :
1630  ChunkSparsityPatternIterators::Accessor (&matrix->get_sparsity_pattern(),
1631  row),
1632  matrix (matrix)
1633  {}
1634 
1635 
1636 
1637  template <typename number>
1638  inline
1639  Accessor<number,true>::
1640  Accessor (const MatrixType *matrix)
1641  :
1642  ChunkSparsityPatternIterators::Accessor (&matrix->get_sparsity_pattern()),
1643  matrix (matrix)
1644  {}
1645 
1646 
1647 
1648  template <typename number>
1649  inline
1650  Accessor<number,true>::
1652  :
1653  ChunkSparsityPatternIterators::Accessor (a),
1654  matrix (&a.get_matrix())
1655  {}
1656 
1657 
1658 
1659  template <typename number>
1660  inline
1661  number
1662  Accessor<number, true>::value () const
1663  {
1664  const unsigned int chunk_size = matrix->get_sparsity_pattern().get_chunk_size();
1665  return matrix->val[reduced_index() * chunk_size * chunk_size
1666  +
1667  chunk_row * chunk_size
1668  +
1669  chunk_col];
1670  }
1671 
1672 
1673 
1674  template <typename number>
1675  inline
1676  const typename Accessor<number, true>::MatrixType &
1677  Accessor<number, true>::get_matrix () const
1678  {
1679  return *matrix;
1680  }
1681 
1682 
1683 
1684  template <typename number>
1685  inline
1686  Accessor<number, false>::Reference::Reference (
1687  const Accessor *accessor,
1688  const bool)
1689  :
1690  accessor (accessor)
1691  {}
1692 
1693 
1694  template <typename number>
1695  inline
1696  Accessor<number, false>::Reference::operator number() const
1697  {
1698  const unsigned int chunk_size = accessor->matrix->get_sparsity_pattern().get_chunk_size();
1699  return accessor->matrix->val[accessor->reduced_index() * chunk_size * chunk_size
1700  +
1701  accessor->chunk_row * chunk_size
1702  +
1703  accessor->chunk_col];
1704  }
1705 
1706 
1707 
1708  template <typename number>
1709  inline
1710  const typename Accessor<number, false>::Reference &
1711  Accessor<number, false>::Reference::operator = (const number n) const
1712  {
1713  const unsigned int chunk_size = accessor->matrix->get_sparsity_pattern().get_chunk_size();
1714  accessor->matrix->val[accessor->reduced_index() * chunk_size * chunk_size
1715  +
1716  accessor->chunk_row * chunk_size
1717  +
1718  accessor->chunk_col] = n;
1719  return *this;
1720  }
1721 
1722 
1723 
1724  template <typename number>
1725  inline
1726  const typename Accessor<number, false>::Reference &
1727  Accessor<number, false>::Reference::operator += (const number n) const
1728  {
1729  const unsigned int chunk_size = accessor->matrix->get_sparsity_pattern().get_chunk_size();
1730  accessor->matrix->val[accessor->reduced_index() * chunk_size * chunk_size
1731  +
1732  accessor->chunk_row * chunk_size
1733  +
1734  accessor->chunk_col] += n;
1735  return *this;
1736  }
1737 
1738 
1739 
1740  template <typename number>
1741  inline
1742  const typename Accessor<number, false>::Reference &
1743  Accessor<number, false>::Reference::operator -= (const number n) const
1744  {
1745  const unsigned int chunk_size = accessor->matrix->get_sparsity_pattern().get_chunk_size();
1746  accessor->matrix->val[accessor->reduced_index() * chunk_size * chunk_size
1747  +
1748  accessor->chunk_row * chunk_size
1749  +
1750  accessor->chunk_col] -= n;
1751  return *this;
1752  }
1753 
1754 
1755 
1756  template <typename number>
1757  inline
1758  const typename Accessor<number, false>::Reference &
1759  Accessor<number, false>::Reference::operator *= (const number n) const
1760  {
1761  const unsigned int chunk_size = accessor->matrix->get_sparsity_pattern().get_chunk_size();
1762  accessor->matrix->val[accessor->reduced_index() * chunk_size * chunk_size
1763  +
1764  accessor->chunk_row * chunk_size
1765  +
1766  accessor->chunk_col] *= n;
1767  return *this;
1768  }
1769 
1770 
1771 
1772  template <typename number>
1773  inline
1774  const typename Accessor<number, false>::Reference &
1775  Accessor<number, false>::Reference::operator /= (const number n) const
1776  {
1777  const unsigned int chunk_size = accessor->matrix->get_sparsity_pattern().get_chunk_size();
1778  accessor->matrix->val[accessor->reduced_index() * chunk_size * chunk_size
1779  +
1780  accessor->chunk_row * chunk_size
1781  +
1782  accessor->chunk_col] /= n;
1783  return *this;
1784  }
1785 
1786 
1787 
1788  template <typename number>
1789  inline
1790  Accessor<number,false>::
1791  Accessor (MatrixType *matrix,
1792  const unsigned int row)
1793  :
1794  ChunkSparsityPatternIterators::Accessor (&matrix->get_sparsity_pattern(),
1795  row),
1796  matrix (matrix)
1797  {}
1798 
1799 
1800 
1801  template <typename number>
1802  inline
1803  Accessor<number,false>::
1804  Accessor (MatrixType *matrix)
1805  :
1806  ChunkSparsityPatternIterators::Accessor (&matrix->get_sparsity_pattern()),
1807  matrix (matrix)
1808  {}
1809 
1810 
1811 
1812  template <typename number>
1813  inline
1814  typename Accessor<number, false>::Reference
1815  Accessor<number, false>::value() const
1816  {
1817  return Reference(this,true);
1818  }
1819 
1820 
1821 
1822 
1823  template <typename number>
1824  inline
1825  typename Accessor<number, false>::MatrixType &
1826  Accessor<number, false>::get_matrix () const
1827  {
1828  return *matrix;
1829  }
1830 
1831 
1832 
1833  template <typename number, bool Constness>
1834  inline
1835  Iterator<number, Constness>::
1836  Iterator (MatrixType *matrix,
1837  const unsigned int row)
1838  :
1839  accessor(matrix, row)
1840  {}
1841 
1842 
1843 
1844  template <typename number, bool Constness>
1845  inline
1846  Iterator<number, Constness>::
1847  Iterator (MatrixType *matrix)
1848  :
1849  accessor(matrix)
1850  {}
1851 
1852 
1853 
1854  template <typename number, bool Constness>
1855  inline
1856  Iterator<number, Constness>::
1858  :
1859  accessor(*i)
1860  {}
1861 
1862 
1863 
1864  template <typename number, bool Constness>
1865  inline
1866  Iterator<number, Constness> &
1867  Iterator<number,Constness>::operator++ ()
1868  {
1869  accessor.advance ();
1870  return *this;
1871  }
1872 
1873 
1874  template <typename number, bool Constness>
1875  inline
1876  Iterator<number,Constness>
1877  Iterator<number,Constness>::operator++ (int)
1878  {
1879  const Iterator iter = *this;
1880  accessor.advance ();
1881  return iter;
1882  }
1883 
1884 
1885  template <typename number, bool Constness>
1886  inline
1887  const Accessor<number,Constness> &
1889  {
1890  return accessor;
1891  }
1892 
1893 
1894  template <typename number, bool Constness>
1895  inline
1896  const Accessor<number,Constness> *
1897  Iterator<number,Constness>::operator-> () const
1898  {
1899  return &accessor;
1900  }
1901 
1902 
1903  template <typename number, bool Constness>
1904  inline
1905  bool
1906  Iterator<number,Constness>::
1907  operator == (const Iterator &other) const
1908  {
1909  return (accessor == other.accessor);
1910  }
1911 
1912 
1913  template <typename number, bool Constness>
1914  inline
1915  bool
1916  Iterator<number,Constness>::
1917  operator != (const Iterator &other) const
1918  {
1919  return ! (*this == other);
1920  }
1921 
1922 
1923  template <typename number, bool Constness>
1924  inline
1925  bool
1926  Iterator<number,Constness>::
1927  operator < (const Iterator &other) const
1928  {
1929  Assert (&accessor.get_matrix() == &other.accessor.get_matrix(),
1930  ExcInternalError());
1931 
1932  return (accessor < other.accessor);
1933  }
1934 
1935 
1936  template <typename number, bool Constness>
1937  inline
1938  bool
1939  Iterator<number,Constness>::
1940  operator > (const Iterator &other) const
1941  {
1942  return (other < *this);
1943  }
1944 
1945 
1946  template <typename number, bool Constness>
1947  inline
1948  int
1950  operator - (const Iterator &other) const
1951  {
1952  Assert (&accessor.get_matrix() == &other.accessor.get_matrix(),
1953  ExcInternalError());
1954 
1955  // TODO: can be optimized
1956  int difference = 0;
1957  if (*this < other)
1958  {
1959  Iterator copy = *this;
1960  while (copy != other)
1961  {
1962  ++copy;
1963  --difference;
1964  }
1965  }
1966  else
1967  {
1968  Iterator copy = other;
1969  while (copy != *this)
1970  {
1971  ++copy;
1972  ++difference;
1973  }
1974  }
1975  return difference;
1976  }
1977 
1978 
1979 
1980  template <typename number, bool Constness>
1981  inline
1982  Iterator<number,Constness>
1984  operator + (const unsigned int n) const
1985  {
1986  Iterator x = *this;
1987  for (unsigned int i=0; i<n; ++i)
1988  ++x;
1989 
1990  return x;
1991  }
1992 
1993 }
1994 
1995 
1996 
1997 template <typename number>
1998 inline
2001 {
2002  return const_iterator(this, 0);
2003 }
2004 
2005 
2006 template <typename number>
2007 inline
2010 {
2011  return const_iterator(this);
2012 }
2013 
2014 
2015 template <typename number>
2016 inline
2019 {
2020  return iterator(this, 0);
2021 }
2022 
2023 
2024 template <typename number>
2025 inline
2028 {
2029  return iterator(this);
2030 }
2031 
2032 
2033 template <typename number>
2034 inline
2036 ChunkSparseMatrix<number>::begin (const unsigned int r) const
2037 {
2038  Assert (r<m(), ExcIndexRange(r,0,m()));
2039  return const_iterator(this, r);
2040 }
2041 
2042 
2043 
2044 template <typename number>
2045 inline
2047 ChunkSparseMatrix<number>::end (const unsigned int r) const
2048 {
2049  Assert (r<m(), ExcIndexRange(r,0,m()));
2050  return const_iterator(this, r+1);
2051 }
2052 
2053 
2054 
2055 template <typename number>
2056 inline
2058 ChunkSparseMatrix<number>::begin (const unsigned int r)
2059 {
2060  Assert (r<m(), ExcIndexRange(r,0,m()));
2061  return iterator(this, r);
2062 }
2063 
2064 
2065 
2066 template <typename number>
2067 inline
2069 ChunkSparseMatrix<number>::end (const unsigned int r)
2070 {
2071  Assert (r<m(), ExcIndexRange(r,0,m()));
2072  return iterator(this, r+1);
2073 }
2074 
2075 
2076 
2077 
2078 #endif // DOXYGEN
2079 
2080 
2081 /*---------------------------- chunk_sparse_matrix.h ---------------------------*/
2082 
2083 DEAL_II_NAMESPACE_CLOSE
2084 
2085 #endif
2086 /*---------------------------- chunk_sparse_matrix.h ---------------------------*/
void block_write(std::ostream &out) const
size_type n() const
ChunkSparseMatrixIterators::Iterator< number, false > iterator
void precondition_SOR(Vector< somenumber > &dst, const Vector< somenumber > &src, const number om=1.) const
void vmult(OutVector &dst, const InVector &src) const
#define DeclException2(Exception2, type1, type2, outsequence)
Definition: exceptions.h:358
somenumber residual(Vector< somenumber > &dst, const Vector< somenumber > &x, const Vector< somenumber > &b) const
bool empty() const
Contents is actually a matrix.
static const size_type invalid_entry
ChunkSparseMatrix< number > & copy_from(const ChunkSparseMatrix< somenumber > &source)
bool operator==(const Iterator &) const
void TPSOR(Vector< somenumber > &v, const std::vector< size_type > &permutation, const std::vector< size_type > &inverse_permutation, const number om=1.) const
void print(std::ostream &out) const
Iterator operator+(const unsigned int n) const
void print_pattern(std::ostream &out, const double threshold=0.) const
void extract_row_copy(const size_type row, const size_type array_length, size_type &row_length, size_type *column_indices, number *values) const
#define AssertIndexRange(index, range)
Definition: exceptions.h:1284
size_type m() const
ChunkSparseMatrixIterators::Iterator< number, true > const_iterator
size_type n_nonzero_elements() const
types::global_dof_index size_type
static ::ExceptionBase & ExcNotInitialized()
Iterator(MatrixType *matrix, const unsigned int row)
#define AssertThrow(cond, exc)
Definition: exceptions.h:1221
SymmetricTensor< rank_, dim, typename ProductType< Number, OtherNumber >::type > operator+(const SymmetricTensor< rank_, dim, Number > &left, const SymmetricTensor< rank_, dim, OtherNumber > &right)
static ::ExceptionBase & ExcIndexRange(int arg1, int arg2, int arg3)
SymmetricTensor< rank_, dim, Number > operator*(const SymmetricTensor< rank_, dim, Number > &t, const Number &factor)
std::unique_ptr< number[]> val
static ::ExceptionBase & ExcDivideByZero()
void TSOR_step(Vector< somenumber > &v, const Vector< somenumber > &b, const number om=1.) const
const ChunkSparseMatrix< number > & get_matrix() const
void SOR(Vector< somenumber > &v, const number om=1.) const
static ::ExceptionBase & ExcDifferentChunkSparsityPatterns()
void TSOR(Vector< somenumber > &v, const number om=1.) const
somenumber matrix_scalar_product(const Vector< somenumber > &u, const Vector< somenumber > &v) const
void precondition_SSOR(Vector< somenumber > &dst, const Vector< somenumber > &src, const number om=1.) 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
somenumber matrix_norm_square(const Vector< somenumber > &v) const
numbers::NumberTraits< number >::real_type real_type
std::size_t memory_consumption() const
const_iterator begin() const
unsigned int global_dof_index
Definition: types.h:88
number operator()(const size_type i, const size_type j) const
void SSOR(Vector< somenumber > &v, const number omega=1.) const
#define Assert(cond, exc)
Definition: exceptions.h:1142
void SOR_step(Vector< somenumber > &v, const Vector< somenumber > &b, const number om=1.) const
void SSOR_step(Vector< somenumber > &v, const Vector< somenumber > &b, const number om=1.) const
virtual ~ChunkSparseMatrix()
ChunkSparseMatrix & operator*=(const number factor)
size_type compute_location(const size_type i, const size_type j) const
#define DeclExceptionMsg(Exception, defaulttext)
Definition: exceptions.h:335
#define DeclException0(Exception0)
Definition: exceptions.h:323
void PSOR(Vector< somenumber > &v, const std::vector< size_type > &permutation, const std::vector< size_type > &inverse_permutation, const number om=1.) const
const Accessor< number, Constness > * operator->() const
virtual void reinit(const ChunkSparsityPattern &sparsity)
static ::ExceptionBase & ExcIteratorRange(int arg1, int arg2)
void precondition_Jacobi(Vector< somenumber > &dst, const Vector< somenumber > &src, const number omega=1.) const
bool operator<(const Iterator &) const
number el(const size_type i, const size_type j) const
real_type frobenius_norm() const
void Tvmult(OutVector &dst, const InVector &src) const
const Accessor< number, Constness > & value_type
Accessor(const ChunkSparsityPattern *matrix, const unsigned int row)
static ::ExceptionBase & ExcNotQuadratic()
number diag_element(const size_type i) const
const Accessor< number, Constness > & operator*() const
const_iterator end() const
void Tvmult_add(OutVector &dst, const InVector &src) const
size_type n_actually_nonzero_elements() const
Accessor< number, Constness > accessor
void vmult_add(OutVector &dst, const InVector &src) const
static const bool zero_addition_can_be_elided
Accessor< number, Constness >::MatrixType MatrixType
constexpr int chunk_size
Definition: cuda_size.h:33
const ChunkSparsityPattern & get_sparsity_pattern() const
void block_read(std::istream &in)
virtual void clear()
int operator-(const Iterator &p) const
ChunkSparseMatrix & operator/=(const number factor)
real_type l1_norm() const
bool operator>(const Iterator &) const
void set(const size_type i, const size_type j, const number value)
static const size_type invalid_entry
void add(const size_type i, const size_type j, const number value)
bool operator!=(const Iterator &) const
void precondition_TSOR(Vector< somenumber > &dst, const Vector< somenumber > &src, const number om=1.) const
static ::ExceptionBase & ExcInvalidIndex(int arg1, int arg2)
SmartPointer< const ChunkSparsityPattern, ChunkSparseMatrix< number > > cols
static ::ExceptionBase & ExcSourceEqualsDestination()
ChunkSparseMatrix< number > & operator=(const ChunkSparseMatrix< number > &)
SymmetricTensor< rank_, dim, typename ProductType< Number, OtherNumber >::type > operator-(const SymmetricTensor< rank_, dim, Number > &left, const SymmetricTensor< rank_, dim, OtherNumber > &right)
real_type linfty_norm() const
#define AssertIsFinite(number)
Definition: exceptions.h:1299
static ::ExceptionBase & ExcInternalError()