Reference documentation for deal.II version 8.5.1
chunk_sparse_matrix.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2008 - 2016 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 DEAL_II_NAMESPACE_OPEN
28 
29 template<typename number> class Vector;
30 template<typename number> class FullMatrix;
31 
41 {
42  // forward declaration
43  template <typename number, bool Constness>
44  class Iterator;
45 
56  template <typename number, bool Constness>
58  {
59  public:
63  number value() const;
64 
68  number &value();
69 
74  const ChunkSparseMatrix<number> &get_matrix () const;
75  };
76 
77 
78 
85  template <typename number>
87  {
88  public:
94 
98  Accessor (MatrixType *matrix,
99  const unsigned int row);
100 
104  Accessor (MatrixType *matrix);
105 
110 
114  number value() const;
115 
120  const MatrixType &get_matrix () const;
121 
122  private:
127 
132 
136  template <typename, bool>
137  friend class Iterator;
138  };
139 
140 
147  template <typename number>
149  {
150  private:
173  class Reference
174  {
175  public:
180  Reference (const Accessor *accessor,
181  const bool dummy);
182 
186  operator number () const;
187 
191  const Reference &operator = (const number n) const;
192 
196  const Reference &operator += (const number n) const;
197 
201  const Reference &operator -= (const number n) const;
202 
206  const Reference &operator *= (const number n) const;
207 
211  const Reference &operator /= (const number n) const;
212 
213  private:
219  };
220 
221  public:
227 
231  Accessor (MatrixType *matrix,
232  const unsigned int row);
233 
237  Accessor (MatrixType *matrix);
238 
242  Reference value() const;
243 
248  MatrixType &get_matrix () const;
249 
250  private:
255 
260 
264  template <typename, bool>
265  friend class Iterator;
266  };
267 
268 
269 
280  template <typename number, bool Constness>
281  class Iterator
282  {
283  public:
287  typedef
290 
295  typedef
297 
302  Iterator (MatrixType *matrix,
303  const unsigned int row);
304 
308  Iterator (MatrixType *matrix);
309 
315 
320 
324  Iterator operator++ (int);
325 
329  const Accessor<number,Constness> &operator* () const;
330 
335 
339  bool operator == (const Iterator &) const;
340 
344  bool operator != (const Iterator &) const;
345 
353  bool operator < (const Iterator &) const;
354 
359  bool operator > (const Iterator &) const;
360 
367  int operator - (const Iterator &p) const;
368 
372  Iterator operator + (const unsigned int n) const;
373 
374  private:
379  };
380 
381 }
382 
383 
384 
403 template <typename number>
404 class ChunkSparseMatrix : public virtual Subscriptor
405 {
406 public:
411 
416  typedef number value_type;
417 
428 
433  typedef
436 
443  typedef
446 
453  struct Traits
454  {
459  static const bool zero_addition_can_be_elided = true;
460  };
461 
477 
487 
501  explicit ChunkSparseMatrix (const ChunkSparsityPattern &sparsity);
502 
509  ChunkSparseMatrix (const ChunkSparsityPattern &sparsity,
510  const IdentityMatrix &id);
511 
516  virtual ~ChunkSparseMatrix ();
517 
528 
536  operator= (const IdentityMatrix &id);
537 
547  ChunkSparseMatrix &operator = (const double d);
548 
562  virtual void reinit (const ChunkSparsityPattern &sparsity);
563 
569  virtual void clear ();
571 
579  bool empty () const;
580 
585  size_type m () const;
586 
591  size_type n () const;
592 
598  size_type n_nonzero_elements () const;
599 
608 
618 
623  std::size_t memory_consumption () const;
624 
626 
635  void set (const size_type i,
636  const size_type j,
637  const number value);
638 
644  void add (const size_type i,
645  const size_type j,
646  const number value);
647 
657  template <typename number2>
658  void add (const size_type row,
659  const size_type n_cols,
660  const size_type *col_indices,
661  const number2 *values,
662  const bool elide_zero_values = true,
663  const bool col_indices_are_sorted = false);
664 
668  ChunkSparseMatrix &operator *= (const number factor);
669 
673  ChunkSparseMatrix &operator /= (const number factor);
674 
687  void symmetrize ();
688 
705  template <typename somenumber>
708 
726  template <typename ForwardIterator>
727  void copy_from (const ForwardIterator begin,
728  const ForwardIterator end);
729 
735  template <typename somenumber>
736  void copy_from (const FullMatrix<somenumber> &matrix);
737 
749  template <typename somenumber>
750  void add (const number factor,
751  const ChunkSparseMatrix<somenumber> &matrix);
752 
754 
758 
772  number operator () (const size_type i,
773  const size_type j) const;
774 
787  number el (const size_type i,
788  const size_type j) const;
789 
799  number diag_element (const size_type i) const;
800 
805  number &diag_element (const size_type i);
806 
816  void extract_row_copy (const size_type row,
817  const size_type array_length,
818  size_type &row_length,
819  size_type *column_indices,
820  number *values) const;
821 
823 
841  template <class OutVector, class InVector>
842  void vmult (OutVector &dst,
843  const InVector &src) const;
844 
860  template <class OutVector, class InVector>
861  void Tvmult (OutVector &dst,
862  const InVector &src) const;
863 
878  template <class OutVector, class InVector>
879  void vmult_add (OutVector &dst,
880  const InVector &src) const;
881 
897  template <class OutVector, class InVector>
898  void Tvmult_add (OutVector &dst,
899  const InVector &src) const;
900 
916  template <typename somenumber>
917  somenumber matrix_norm_square (const Vector<somenumber> &v) const;
918 
922  template <typename somenumber>
923  somenumber matrix_scalar_product (const Vector<somenumber> &u,
924  const Vector<somenumber> &v) const;
932  template <typename somenumber>
933  somenumber residual (Vector<somenumber> &dst,
934  const Vector<somenumber> &x,
935  const Vector<somenumber> &b) const;
936 
938 
942 
950  real_type l1_norm () const;
951 
959  real_type linfty_norm () const;
960 
965  real_type frobenius_norm () const;
967 
971 
977  template <typename somenumber>
979  const Vector<somenumber> &src,
980  const number omega = 1.) const;
981 
985  template <typename somenumber>
987  const Vector<somenumber> &src,
988  const number om = 1.) const;
989 
993  template <typename somenumber>
995  const Vector<somenumber> &src,
996  const number om = 1.) const;
997 
1001  template <typename somenumber>
1003  const Vector<somenumber> &src,
1004  const number om = 1.) const;
1005 
1011  template <typename somenumber>
1012  void SSOR (Vector<somenumber> &v,
1013  const number omega = 1.) const;
1014 
1019  template <typename somenumber>
1020  void SOR (Vector<somenumber> &v,
1021  const number om = 1.) const;
1022 
1027  template <typename somenumber>
1028  void TSOR (Vector<somenumber> &v,
1029  const number om = 1.) const;
1030 
1041  template <typename somenumber>
1042  void PSOR (Vector<somenumber> &v,
1043  const std::vector<size_type> &permutation,
1044  const std::vector<size_type> &inverse_permutation,
1045  const number om = 1.) const;
1046 
1057  template <typename somenumber>
1058  void TPSOR (Vector<somenumber> &v,
1059  const std::vector<size_type> &permutation,
1060  const std::vector<size_type> &inverse_permutation,
1061  const number om = 1.) const;
1062 
1067  template <typename somenumber>
1068  void SOR_step (Vector<somenumber> &v,
1069  const Vector<somenumber> &b,
1070  const number om = 1.) const;
1071 
1076  template <typename somenumber>
1077  void TSOR_step (Vector<somenumber> &v,
1078  const Vector<somenumber> &b,
1079  const number om = 1.) const;
1080 
1085  template <typename somenumber>
1086  void SSOR_step (Vector<somenumber> &v,
1087  const Vector<somenumber> &b,
1088  const number om = 1.) const;
1090 
1094 
1104  const_iterator begin () const;
1105 
1114  const_iterator end () const;
1115 
1125  iterator begin ();
1126 
1135  iterator end ();
1136 
1151  const_iterator begin (const unsigned int r) const;
1152 
1167  const_iterator end (const unsigned int r) const;
1168 
1183  iterator begin (const unsigned int r);
1184 
1199  iterator end (const unsigned int r);
1201 
1205 
1210  void print (std::ostream &out) const;
1211 
1232  void print_formatted (std::ostream &out,
1233  const unsigned int precision = 3,
1234  const bool scientific = true,
1235  const unsigned int width = 0,
1236  const char *zero_string = " ",
1237  const double denominator = 1.) const;
1238 
1244  void print_pattern(std::ostream &out,
1245  const double threshold = 0.) const;
1246 
1257  void block_write (std::ostream &out) const;
1258 
1275  void block_read (std::istream &in);
1277 
1286  int, int,
1287  << "You are trying to access the matrix entry with index <"
1288  << arg1 << ',' << arg2
1289  << ">, but this entry does not exist in the sparsity pattern"
1290  "of this matrix."
1291  "\n\n"
1292  "The most common cause for this problem is that you used "
1293  "a method to build the sparsity pattern that did not "
1294  "(completely) take into account all of the entries you "
1295  "will later try to write into. An example would be "
1296  "building a sparsity pattern that does not include "
1297  "the entries you will write into due to constraints "
1298  "on degrees of freedom such as hanging nodes or periodic "
1299  "boundary conditions. In such cases, building the "
1300  "sparsity pattern will succeed, but you will get errors "
1301  "such as the current one at one point or other when "
1302  "trying to write into the entries of the matrix.");
1311  int, int,
1312  << "The iterators denote a range of " << arg1
1313  << " elements, but the given number of rows was " << arg2);
1319 private:
1326 
1333  number *val;
1334 
1342 
1347  const size_type j) const;
1348 
1349  // make all other sparse matrices friends
1350  template <typename somenumber> friend class ChunkSparseMatrix;
1351 
1355  template <typename,bool> friend class ChunkSparseMatrixIterators::Iterator;
1356  template <typename,bool> friend class ChunkSparseMatrixIterators::Accessor;
1357 };
1358 
1361 #ifndef DOXYGEN
1362 /*---------------------- Inline functions -----------------------------------*/
1363 
1364 
1365 
1366 template <typename number>
1367 inline
1370 {
1371  Assert (cols != 0, ExcNotInitialized());
1372  return cols->rows;
1373 }
1374 
1375 
1376 template <typename number>
1377 inline
1380 {
1381  Assert (cols != 0, ExcNotInitialized());
1382  return cols->cols;
1383 }
1384 
1385 
1386 
1387 template <typename number>
1388 inline
1389 const ChunkSparsityPattern &
1391 {
1392  Assert (cols != 0, ExcNotInitialized());
1393  return *cols;
1394 }
1395 
1396 
1397 
1398 template <typename number>
1399 inline
1402  const size_type j) const
1403 {
1404  const size_type chunk_size = cols->get_chunk_size();
1405  const size_type chunk_index
1406  = cols->sparsity_pattern(i/chunk_size, j/chunk_size);
1407 
1408  if (chunk_index == ChunkSparsityPattern::invalid_entry)
1410  else
1411  {
1412  return (chunk_index * chunk_size * chunk_size
1413  +
1414  (i % chunk_size) * chunk_size
1415  +
1416  (j % chunk_size));
1417  }
1418 }
1419 
1420 
1421 template <typename number>
1422 inline
1423 void ChunkSparseMatrix<number>::set (const size_type i,
1424  const size_type j,
1425  const number value)
1426 {
1427 
1429 
1430  Assert (cols != 0, ExcNotInitialized());
1431  // it is allowed to set elements of the matrix that are not part of the
1432  // sparsity pattern, if the value to which we set it is zero
1433  const size_type index = compute_location(i,j);
1434  Assert ((index != SparsityPattern::invalid_entry) ||
1435  (value == 0.),
1436  ExcInvalidIndex(i,j));
1437 
1438  if (index != SparsityPattern::invalid_entry)
1439  val[index] = value;
1440 }
1441 
1442 
1443 
1444 template <typename number>
1445 inline
1446 void ChunkSparseMatrix<number>::add (const size_type i,
1447  const size_type j,
1448  const number value)
1449 {
1450 
1452 
1453  Assert (cols != 0, ExcNotInitialized());
1454 
1455  if (value != 0.)
1456  {
1457  const size_type index = compute_location(i,j);
1459  ExcInvalidIndex(i,j));
1460 
1461  val[index] += value;
1462  }
1463 }
1464 
1465 
1466 
1467 template <typename number>
1468 template <typename number2>
1469 inline
1470 void ChunkSparseMatrix<number>::add (const size_type row,
1471  const size_type n_cols,
1472  const size_type *col_indices,
1473  const number2 *values,
1474  const bool /*elide_zero_values*/,
1475  const bool /*col_indices_are_sorted*/)
1476 {
1477  // TODO: could be done more efficiently...
1478  for (size_type col=0; col<n_cols; ++col)
1479  add(row, col_indices[col], static_cast<number>(values[col]));
1480 }
1481 
1482 
1483 
1484 template <typename number>
1485 inline
1487 ChunkSparseMatrix<number>::operator *= (const number factor)
1488 {
1489  Assert (cols != 0, ExcNotInitialized());
1490  Assert (val != 0, ExcNotInitialized());
1491 
1492  const size_type chunk_size = cols->get_chunk_size();
1493 
1494  // multiply all elements of the matrix with the given factor. this includes
1495  // the padding elements in chunks that overlap the boundaries of the actual
1496  // matrix -- but since multiplication with a number does not violate the
1497  // invariant of keeping these elements at zero nothing can happen
1498  number *val_ptr = val;
1499  const number *const end_ptr = val +
1500  cols->sparsity_pattern.n_nonzero_elements()
1501  *
1502  chunk_size * chunk_size;
1503  while (val_ptr != end_ptr)
1504  *val_ptr++ *= factor;
1505 
1506  return *this;
1507 }
1508 
1509 
1510 
1511 template <typename number>
1512 inline
1514 ChunkSparseMatrix<number>::operator /= (const number factor)
1515 {
1516  Assert (cols != 0, ExcNotInitialized());
1517  Assert (val != 0, ExcNotInitialized());
1518  Assert (factor !=0, ExcDivideByZero());
1519 
1520  const number factor_inv = 1. / factor;
1521 
1522  const size_type chunk_size = cols->get_chunk_size();
1523 
1524  // multiply all elements of the matrix with the given factor. this includes
1525  // the padding elements in chunks that overlap the boundaries of the actual
1526  // matrix -- but since multiplication with a number does not violate the
1527  // invariant of keeping these elements at zero nothing can happen
1528  number *val_ptr = val;
1529  const number *const end_ptr = val +
1530  cols->sparsity_pattern.n_nonzero_elements()
1531  *
1532  chunk_size * chunk_size;
1533 
1534  while (val_ptr != end_ptr)
1535  *val_ptr++ *= factor_inv;
1536 
1537  return *this;
1538 }
1539 
1540 
1541 
1542 template <typename number>
1543 inline
1544 number ChunkSparseMatrix<number>::operator () (const size_type i,
1545  const size_type j) const
1546 {
1547  Assert (cols != 0, ExcNotInitialized());
1548  AssertThrow (compute_location(i,j) != SparsityPattern::invalid_entry,
1549  ExcInvalidIndex(i,j));
1550  return val[compute_location(i,j)];
1551 }
1552 
1553 
1554 
1555 template <typename number>
1556 inline
1557 number ChunkSparseMatrix<number>::el (const size_type i,
1558  const size_type j) const
1559 {
1560  Assert (cols != 0, ExcNotInitialized());
1561  const size_type index = compute_location(i,j);
1562 
1564  return val[index];
1565  else
1566  return 0;
1567 }
1568 
1569 
1570 
1571 template <typename number>
1572 inline
1573 number ChunkSparseMatrix<number>::diag_element (const size_type i) const
1574 {
1575  Assert (cols != 0, ExcNotInitialized());
1576  Assert (m() == n(), ExcNotQuadratic());
1577  AssertIndexRange(i, m());
1578 
1579  // Use that the first element in each row of a quadratic matrix is the main
1580  // diagonal of the chunk sparsity pattern
1581  const size_type chunk_size = cols->get_chunk_size();
1582  return val[cols->sparsity_pattern.rowstart[i/chunk_size]
1583  *
1584  chunk_size * chunk_size
1585  +
1586  (i % chunk_size) * chunk_size
1587  +
1588  (i % chunk_size)];
1589 }
1590 
1591 
1592 
1593 template <typename number>
1594 template <typename ForwardIterator>
1595 inline
1596 void
1597 ChunkSparseMatrix<number>::copy_from (const ForwardIterator begin,
1598  const ForwardIterator end)
1599 {
1600  Assert (static_cast<size_type >(std::distance (begin, end)) == m(),
1601  ExcIteratorRange (std::distance (begin, end), m()));
1602 
1603  // for use in the inner loop, we define a typedef to the type of the inner
1604  // iterators
1605  typedef typename std::iterator_traits<ForwardIterator>::value_type::const_iterator inner_iterator;
1606  size_type row=0;
1607  for (ForwardIterator i=begin; i!=end; ++i, ++row)
1608  {
1609  const inner_iterator end_of_row = i->end();
1610  for (inner_iterator j=i->begin(); j!=end_of_row; ++j)
1611  // write entries
1612  set (row, j->first, j->second);
1613  }
1614 }
1615 
1616 
1617 
1618 //---------------------------------------------------------------------------
1619 
1620 
1622 {
1623  template <typename number>
1624  inline
1626  Accessor (const MatrixType *matrix,
1627  const unsigned int row)
1628  :
1629  ChunkSparsityPatternIterators::Accessor (&matrix->get_sparsity_pattern(),
1630  row),
1631  matrix (matrix)
1632  {}
1633 
1634 
1635 
1636  template <typename number>
1637  inline
1638  Accessor<number,true>::
1639  Accessor (const MatrixType *matrix)
1640  :
1641  ChunkSparsityPatternIterators::Accessor (&matrix->get_sparsity_pattern()),
1642  matrix (matrix)
1643  {}
1644 
1645 
1646 
1647  template <typename number>
1648  inline
1649  Accessor<number,true>::
1651  :
1652  ChunkSparsityPatternIterators::Accessor (a),
1653  matrix (&a.get_matrix())
1654  {}
1655 
1656 
1657 
1658  template <typename number>
1659  inline
1660  number
1661  Accessor<number, true>::value () const
1662  {
1663  const unsigned int chunk_size = matrix->get_sparsity_pattern().get_chunk_size();
1664  return matrix->val[reduced_index() * chunk_size * chunk_size
1665  +
1666  chunk_row * chunk_size
1667  +
1668  chunk_col];
1669  }
1670 
1671 
1672 
1673  template <typename number>
1674  inline
1675  const typename Accessor<number, true>::MatrixType &
1676  Accessor<number, true>::get_matrix () const
1677  {
1678  return *matrix;
1679  }
1680 
1681 
1682 
1683  template <typename number>
1684  inline
1685  Accessor<number, false>::Reference::Reference (
1686  const Accessor *accessor,
1687  const bool)
1688  :
1689  accessor (accessor)
1690  {}
1691 
1692 
1693  template <typename number>
1694  inline
1695  Accessor<number, false>::Reference::operator number() const
1696  {
1697  const unsigned int chunk_size = accessor->matrix->get_sparsity_pattern().get_chunk_size();
1698  return accessor->matrix->val[accessor->reduced_index() * chunk_size * chunk_size
1699  +
1700  accessor->chunk_row * chunk_size
1701  +
1702  accessor->chunk_col];
1703  }
1704 
1705 
1706 
1707  template <typename number>
1708  inline
1709  const typename Accessor<number, false>::Reference &
1710  Accessor<number, false>::Reference::operator = (const number n) const
1711  {
1712  const unsigned int chunk_size = accessor->matrix->get_sparsity_pattern().get_chunk_size();
1713  accessor->matrix->val[accessor->reduced_index() * chunk_size * chunk_size
1714  +
1715  accessor->chunk_row * chunk_size
1716  +
1717  accessor->chunk_col] = n;
1718  return *this;
1719  }
1720 
1721 
1722 
1723  template <typename number>
1724  inline
1725  const typename Accessor<number, false>::Reference &
1726  Accessor<number, false>::Reference::operator += (const number n) const
1727  {
1728  const unsigned int chunk_size = accessor->matrix->get_sparsity_pattern().get_chunk_size();
1729  accessor->matrix->val[accessor->reduced_index() * chunk_size * chunk_size
1730  +
1731  accessor->chunk_row * chunk_size
1732  +
1733  accessor->chunk_col] += n;
1734  return *this;
1735  }
1736 
1737 
1738 
1739  template <typename number>
1740  inline
1741  const typename Accessor<number, false>::Reference &
1742  Accessor<number, false>::Reference::operator -= (const number n) const
1743  {
1744  const unsigned int chunk_size = accessor->matrix->get_sparsity_pattern().get_chunk_size();
1745  accessor->matrix->val[accessor->reduced_index() * chunk_size * chunk_size
1746  +
1747  accessor->chunk_row * chunk_size
1748  +
1749  accessor->chunk_col] -= n;
1750  return *this;
1751  }
1752 
1753 
1754 
1755  template <typename number>
1756  inline
1757  const typename Accessor<number, false>::Reference &
1758  Accessor<number, false>::Reference::operator *= (const number n) const
1759  {
1760  const unsigned int chunk_size = accessor->matrix->get_sparsity_pattern().get_chunk_size();
1761  accessor->matrix->val[accessor->reduced_index() * chunk_size * chunk_size
1762  +
1763  accessor->chunk_row * chunk_size
1764  +
1765  accessor->chunk_col] *= n;
1766  return *this;
1767  }
1768 
1769 
1770 
1771  template <typename number>
1772  inline
1773  const typename Accessor<number, false>::Reference &
1774  Accessor<number, false>::Reference::operator /= (const number n) const
1775  {
1776  const unsigned int chunk_size = accessor->matrix->get_sparsity_pattern().get_chunk_size();
1777  accessor->matrix->val[accessor->reduced_index() * chunk_size * chunk_size
1778  +
1779  accessor->chunk_row * chunk_size
1780  +
1781  accessor->chunk_col] /= n;
1782  return *this;
1783  }
1784 
1785 
1786 
1787  template <typename number>
1788  inline
1789  Accessor<number,false>::
1790  Accessor (MatrixType *matrix,
1791  const unsigned int row)
1792  :
1793  ChunkSparsityPatternIterators::Accessor (&matrix->get_sparsity_pattern(),
1794  row),
1795  matrix (matrix)
1796  {}
1797 
1798 
1799 
1800  template <typename number>
1801  inline
1802  Accessor<number,false>::
1803  Accessor (MatrixType *matrix)
1804  :
1805  ChunkSparsityPatternIterators::Accessor (&matrix->get_sparsity_pattern()),
1806  matrix (matrix)
1807  {}
1808 
1809 
1810 
1811  template <typename number>
1812  inline
1813  typename Accessor<number, false>::Reference
1814  Accessor<number, false>::value() const
1815  {
1816  return Reference(this,true);
1817  }
1818 
1819 
1820 
1821 
1822  template <typename number>
1823  inline
1824  typename Accessor<number, false>::MatrixType &
1825  Accessor<number, false>::get_matrix () const
1826  {
1827  return *matrix;
1828  }
1829 
1830 
1831 
1832  template <typename number, bool Constness>
1833  inline
1834  Iterator<number, Constness>::
1835  Iterator (MatrixType *matrix,
1836  const unsigned int row)
1837  :
1838  accessor(matrix, row)
1839  {}
1840 
1841 
1842 
1843  template <typename number, bool Constness>
1844  inline
1845  Iterator<number, Constness>::
1846  Iterator (MatrixType *matrix)
1847  :
1848  accessor(matrix)
1849  {}
1850 
1851 
1852 
1853  template <typename number, bool Constness>
1854  inline
1855  Iterator<number, Constness>::
1857  :
1858  accessor(*i)
1859  {}
1860 
1861 
1862 
1863  template <typename number, bool Constness>
1864  inline
1865  Iterator<number, Constness> &
1866  Iterator<number,Constness>::operator++ ()
1867  {
1868  accessor.advance ();
1869  return *this;
1870  }
1871 
1872 
1873  template <typename number, bool Constness>
1874  inline
1875  Iterator<number,Constness>
1876  Iterator<number,Constness>::operator++ (int)
1877  {
1878  const Iterator iter = *this;
1879  accessor.advance ();
1880  return iter;
1881  }
1882 
1883 
1884  template <typename number, bool Constness>
1885  inline
1886  const Accessor<number,Constness> &
1887  Iterator<number,Constness>::operator* () const
1888  {
1889  return accessor;
1890  }
1891 
1892 
1893  template <typename number, bool Constness>
1894  inline
1895  const Accessor<number,Constness> *
1896  Iterator<number,Constness>::operator-> () const
1897  {
1898  return &accessor;
1899  }
1900 
1901 
1902  template <typename number, bool Constness>
1903  inline
1904  bool
1905  Iterator<number,Constness>::
1906  operator == (const Iterator &other) const
1907  {
1908  return (accessor == other.accessor);
1909  }
1910 
1911 
1912  template <typename number, bool Constness>
1913  inline
1914  bool
1915  Iterator<number,Constness>::
1916  operator != (const Iterator &other) const
1917  {
1918  return ! (*this == other);
1919  }
1920 
1921 
1922  template <typename number, bool Constness>
1923  inline
1924  bool
1925  Iterator<number,Constness>::
1926  operator < (const Iterator &other) const
1927  {
1928  Assert (&accessor.get_matrix() == &other.accessor.get_matrix(),
1929  ExcInternalError());
1930 
1931  return (accessor < other.accessor);
1932  }
1933 
1934 
1935  template <typename number, bool Constness>
1936  inline
1937  bool
1938  Iterator<number,Constness>::
1939  operator > (const Iterator &other) const
1940  {
1941  return (other < *this);
1942  }
1943 
1944 
1945  template <typename number, bool Constness>
1946  inline
1947  int
1948  Iterator<number,Constness>::
1949  operator - (const Iterator &other) const
1950  {
1951  Assert (&accessor.get_matrix() == &other.accessor.get_matrix(),
1952  ExcInternalError());
1953 
1954  // TODO: can be optimized
1955  int difference = 0;
1956  if (*this < other)
1957  {
1958  Iterator copy = *this;
1959  while (copy != other)
1960  {
1961  ++copy;
1962  --difference;
1963  }
1964  }
1965  else
1966  {
1967  Iterator copy = other;
1968  while (copy != *this)
1969  {
1970  ++copy;
1971  ++difference;
1972  }
1973  }
1974  return difference;
1975  }
1976 
1977 
1978 
1979  template <typename number, bool Constness>
1980  inline
1981  Iterator<number,Constness>
1982  Iterator<number,Constness>::
1983  operator + (const unsigned int n) const
1984  {
1985  Iterator x = *this;
1986  for (unsigned int i=0; i<n; ++i)
1987  ++x;
1988 
1989  return x;
1990  }
1991 
1992 }
1993 
1994 
1995 
1996 template <typename number>
1997 inline
2000 {
2001  return const_iterator(this, 0);
2002 }
2003 
2004 
2005 template <typename number>
2006 inline
2009 {
2010  return const_iterator(this);
2011 }
2012 
2013 
2014 template <typename number>
2015 inline
2018 {
2019  return iterator(this, 0);
2020 }
2021 
2022 
2023 template <typename number>
2024 inline
2027 {
2028  return iterator(this);
2029 }
2030 
2031 
2032 template <typename number>
2033 inline
2035 ChunkSparseMatrix<number>::begin (const unsigned int r) const
2036 {
2037  Assert (r<m(), ExcIndexRange(r,0,m()));
2038  return const_iterator(this, r);
2039 }
2040 
2041 
2042 
2043 template <typename number>
2044 inline
2046 ChunkSparseMatrix<number>::end (const unsigned int r) const
2047 {
2048  Assert (r<m(), ExcIndexRange(r,0,m()));
2049  return const_iterator(this, r+1);
2050 }
2051 
2052 
2053 
2054 template <typename number>
2055 inline
2057 ChunkSparseMatrix<number>::begin (const unsigned int r)
2058 {
2059  Assert (r<m(), ExcIndexRange(r,0,m()));
2060  return iterator(this, r);
2061 }
2062 
2063 
2064 
2065 template <typename number>
2066 inline
2068 ChunkSparseMatrix<number>::end (const unsigned int r)
2069 {
2070  Assert (r<m(), ExcIndexRange(r,0,m()));
2071  return iterator(this, r+1);
2072 }
2073 
2074 
2075 
2076 
2077 #endif // DOXYGEN
2078 
2079 
2080 /*---------------------------- chunk_sparse_matrix.h ---------------------------*/
2081 
2082 DEAL_II_NAMESPACE_CLOSE
2083 
2084 #endif
2085 /*---------------------------- 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:576
somenumber residual(Vector< somenumber > &dst, const Vector< somenumber > &x, const Vector< somenumber > &b) const
bool empty() const
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:1170
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:369
static ::ExceptionBase & ExcIndexRange(int arg1, int arg2, int arg3)
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:313
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 DeclException0(Exception0)
Definition: exceptions.h:541
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
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 > &)
real_type linfty_norm() const
#define AssertIsFinite(number)
Definition: exceptions.h:1197
static ::ExceptionBase & ExcInternalError()