16#ifndef dealii_sparse_matrix_h
17#define dealii_sparse_matrix_h
38template <
typename number>
40template <
typename number>
42template <
typename Matrix>
44template <
typename number>
46# ifdef DEAL_II_WITH_MPI
51 template <
typename Number>
58# ifdef DEAL_II_WITH_TRILINOS
83 template <
typename number,
bool Constness>
96 template <
typename number,
bool Constness>
128 template <
typename number>
178 template <
typename,
bool>
189 template <
typename number>
229 operator number()
const;
311 template <
typename,
bool>
346 template <
typename number,
bool Constness>
471 template <
typename number,
bool Constness>
477 typename ::SparseMatrixIterators::Iterator<number,
478 Constness>::value_type;
480 Iterator<number, Constness>::difference_type;
519template <
typename number>
702 template <
typename number2>
816 template <
typename number2>
818 set(
const std::vector<size_type> &indices,
820 const bool elide_zero_values =
false);
827 template <
typename number2>
829 set(
const std::vector<size_type> &row_indices,
830 const std::vector<size_type> &col_indices,
832 const bool elide_zero_values =
false);
844 template <
typename number2>
847 const std::vector<size_type> &col_indices,
848 const std::vector<number2> & values,
849 const bool elide_zero_values =
false);
860 template <
typename number2>
865 const number2 * values,
866 const bool elide_zero_values =
false);
890 template <
typename number2>
892 add(
const std::vector<size_type> &indices,
894 const bool elide_zero_values =
true);
901 template <
typename number2>
903 add(
const std::vector<size_type> &row_indices,
904 const std::vector<size_type> &col_indices,
906 const bool elide_zero_values =
true);
917 template <
typename number2>
920 const std::vector<size_type> &col_indices,
921 const std::vector<number2> & values,
922 const bool elide_zero_values =
true);
933 template <
typename number2>
938 const number2 * values,
939 const bool elide_zero_values =
true,
940 const bool col_indices_are_sorted =
false);
985 template <
typename somenumber>
1005 template <
typename ForwardIterator>
1018 template <
typename somenumber>
1022#ifdef DEAL_II_WITH_TRILINOS
1047 template <
typename somenumber>
1134 template <
class OutVector,
class InVector>
1136 vmult(OutVector &dst,
const InVector &src)
const;
1153 template <
class OutVector,
class InVector>
1155 Tvmult(OutVector &dst,
const InVector &src)
const;
1173 template <
class OutVector,
class InVector>
1192 template <
class OutVector,
class InVector>
1213 template <
typename somenumber>
1222 template <
typename somenumber>
1236 template <
typename somenumber>
1277 template <
typename numberB,
typename numberC>
1282 const bool rebuild_sparsity_pattern =
true)
const;
1308 template <
typename numberB,
typename numberC>
1313 const bool rebuild_sparsity_pattern =
true)
const;
1358 template <
typename somenumber>
1362 const number omega = 1.)
const;
1370 template <
typename somenumber>
1374 const number omega = 1.,
1375 const std::vector<std::size_t> &pos_right_of_diagonal =
1376 std::vector<std::size_t>())
const;
1381 template <
typename somenumber>
1385 const number omega = 1.)
const;
1390 template <
typename somenumber>
1394 const number omega = 1.)
const;
1401 template <
typename somenumber>
1409 template <
typename somenumber>
1417 template <
typename somenumber>
1431 template <
typename somenumber>
1434 const std::vector<size_type> &permutation,
1435 const std::vector<size_type> &inverse_permutation,
1436 const number omega = 1.)
const;
1448 template <
typename somenumber>
1451 const std::vector<size_type> &permutation,
1452 const std::vector<size_type> &inverse_permutation,
1453 const number omega = 1.)
const;
1460 template <
typename somenumber>
1464 const number omega = 1.)
const;
1470 template <
typename somenumber>
1474 const number omega = 1.)
const;
1480 template <
typename somenumber>
1484 const number omega = 1.)
const;
1490 template <
typename somenumber>
1494 const number omega = 1.)
const;
1580 template <
class StreamType>
1583 const bool across =
false,
1584 const bool diagonal_first =
true)
const;
1608 const unsigned int precision = 3,
1609 const bool scientific =
true,
1610 const unsigned int width = 0,
1611 const char * zero_string =
" ",
1612 const double denominator = 1.)
const;
1632 const unsigned int precision = 9)
const;
1677 <<
"You are trying to access the matrix entry with index <"
1678 << arg1 <<
',' << arg2
1679 <<
">, but this entry does not exist in the sparsity pattern "
1682 "The most common cause for this problem is that you used "
1683 "a method to build the sparsity pattern that did not "
1684 "(completely) take into account all of the entries you "
1685 "will later try to write into. An example would be "
1686 "building a sparsity pattern that does not include "
1687 "the entries you will write into due to constraints "
1688 "on degrees of freedom such as hanging nodes or periodic "
1689 "boundary conditions. In such cases, building the "
1690 "sparsity pattern will succeed, but you will get errors "
1691 "such as the current one at one point or other when "
1692 "trying to write into the entries of the matrix.");
1697 "When copying one sparse matrix into another, "
1698 "or when adding one sparse matrix to another, "
1699 "both matrices need to refer to the same "
1700 "sparsity pattern.");
1707 <<
"The iterators denote a range of " << arg1
1708 <<
" elements, but the given number of rows was " << arg2);
1713 "You are attempting an operation on two vectors that "
1714 "are the same object, but the operation requires that the "
1715 "two objects are in fact different.");
1754 std::unique_ptr<number[]>
val;
1765 template <
typename somenumber>
1767 template <
typename somenumber>
1777 template <
typename,
bool>
1779 template <
typename,
bool>
1782#ifdef DEAL_II_WITH_MPI
1784 template <
typename Number>
1797template <
typename number>
1798template <
typename number2>
1807template <
typename number>
1816template <
typename number>
1826template <
typename number>
1841 ExcInvalidIndex(i, j));
1850template <
typename number>
1851template <
typename number2>
1855 const bool elide_zero_values)
1861 for (
size_type i = 0; i < indices.size(); ++i)
1871template <
typename number>
1872template <
typename number2>
1875 const std::vector<size_type> &col_indices,
1877 const bool elide_zero_values)
1884 for (
size_type i = 0; i < row_indices.size(); ++i)
1894template <
typename number>
1895template <
typename number2>
1898 const std::vector<size_type> &col_indices,
1899 const std::vector<number2> & values,
1900 const bool elide_zero_values)
1914template <
typename number>
1922 if (
value == number())
1932 ExcInvalidIndex(i, j));
1941template <
typename number>
1942template <
typename number2>
1946 const bool elide_zero_values)
1952 for (
size_type i = 0; i < indices.size(); ++i)
1962template <
typename number>
1963template <
typename number2>
1966 const std::vector<size_type> &col_indices,
1968 const bool elide_zero_values)
1975 for (
size_type i = 0; i < row_indices.size(); ++i)
1985template <
typename number>
1986template <
typename number2>
1989 const std::vector<size_type> &col_indices,
1990 const std::vector<number2> & values,
1991 const bool elide_zero_values)
2001 std::is_sorted(col_indices.begin(), col_indices.end()));
2006template <
typename number>
2013 number * val_ptr = val.get();
2014 const number *
const end_ptr = val.get() + cols->n_nonzero_elements();
2016 while (val_ptr != end_ptr)
2017 *val_ptr++ *= factor;
2024template <
typename number>
2032 const number factor_inv = number(1.) / factor;
2034 number * val_ptr = val.get();
2035 const number *
const end_ptr = val.get() + cols->n_nonzero_elements();
2037 while (val_ptr != end_ptr)
2038 *val_ptr++ *= factor_inv;
2045template <
typename number>
2046inline const number &
2051 ExcInvalidIndex(i, j));
2052 return val[cols->operator()(i, j)];
2057template <
typename number>
2063 ExcInvalidIndex(i, j));
2064 return val[cols->operator()(i, j)];
2069template <
typename number>
2084template <
typename number>
2094 return val[cols->rowstart[i]];
2099template <
typename number>
2109 return val[cols->rowstart[i]];
2114template <
typename number>
2115template <
typename ForwardIterator>
2118 const ForwardIterator end)
2121 ExcIteratorRange(std::distance(begin, end), m()));
2125 using inner_iterator =
2126 typename std::iterator_traits<ForwardIterator>::value_type::const_iterator;
2128 for (ForwardIterator i = begin; i !=
end; ++i, ++
row)
2130 const inner_iterator end_of_row = i->end();
2131 for (inner_iterator j = i->begin(); j != end_of_row; ++j)
2133 set(
row, j->first, j->second);
2143 template <
typename number>
2145 const std::size_t index_within_matrix)
2147 index_within_matrix)
2153 template <
typename number>
2154 inline Accessor<number, true>::Accessor(
const MatrixType *matrix)
2161 template <
typename number>
2162 inline Accessor<number, true>::Accessor(
2165 ,
matrix(&a.get_matrix())
2170 template <
typename number>
2172 Accessor<number, true>::value()
const
2175 return matrix->val[linear_index];
2180 template <
typename number>
2181 inline const typename Accessor<number, true>::MatrixType &
2182 Accessor<number, true>::get_matrix()
const
2189 template <
typename number>
2190 inline Accessor<number, false>::Reference::Reference(
const Accessor *accessor,
2192 : accessor(accessor)
2196 template <
typename number>
2197 inline Accessor<number, false>::Reference::operator number()
const
2200 accessor->matrix->n_nonzero_elements());
2201 return accessor->matrix->val[accessor->linear_index];
2206 template <
typename number>
2207 inline const typename Accessor<number, false>::Reference &
2208 Accessor<number, false>::Reference::operator=(
const number n)
const
2211 accessor->matrix->n_nonzero_elements());
2212 accessor->matrix->val[accessor->linear_index] = n;
2218 template <
typename number>
2219 inline const typename Accessor<number, false>::Reference &
2220 Accessor<number, false>::Reference::operator+=(
const number n)
const
2223 accessor->matrix->n_nonzero_elements());
2224 accessor->matrix->val[accessor->linear_index] += n;
2230 template <
typename number>
2231 inline const typename Accessor<number, false>::Reference &
2232 Accessor<number, false>::Reference::operator-=(
const number n)
const
2235 accessor->matrix->n_nonzero_elements());
2236 accessor->matrix->val[accessor->linear_index] -= n;
2242 template <
typename number>
2243 inline const typename Accessor<number, false>::Reference &
2244 Accessor<number, false>::Reference::operator*=(
const number n)
const
2247 accessor->matrix->n_nonzero_elements());
2248 accessor->matrix->val[accessor->linear_index] *= n;
2254 template <
typename number>
2255 inline const typename Accessor<number, false>::Reference &
2256 Accessor<number, false>::Reference::operator/=(
const number n)
const
2259 accessor->matrix->n_nonzero_elements());
2260 accessor->matrix->val[accessor->linear_index] /= n;
2266 template <
typename number>
2267 inline Accessor<number, false>::Accessor(MatrixType * matrix,
2268 const std::size_t index)
2275 template <
typename number>
2276 inline Accessor<number, false>::Accessor(MatrixType *matrix)
2283 template <
typename number>
2284 inline typename Accessor<number, false>::Reference
2285 Accessor<number, false>::value()
const
2287 return Reference(
this,
true);
2292 template <
typename number>
2293 inline typename Accessor<number, false>::MatrixType &
2294 Accessor<number, false>::get_matrix()
const
2301 template <
typename number,
bool Constness>
2302 inline Iterator<number, Constness>::Iterator(MatrixType * matrix,
2303 const std::size_t index)
2309 template <
typename number,
bool Constness>
2310 inline Iterator<number, Constness>::Iterator(MatrixType *matrix)
2316 template <
typename number,
bool Constness>
2317 inline Iterator<number, Constness>::Iterator(
2324 template <
typename number,
bool Constness>
2325 inline const Iterator<number, Constness> &
2326 Iterator<number, Constness>::operator=(
2335 template <
typename number,
bool Constness>
2336 inline Iterator<number, Constness> &
2337 Iterator<number, Constness>::operator++()
2344 template <
typename number,
bool Constness>
2345 inline Iterator<number, Constness>
2346 Iterator<number, Constness>::operator++(
int)
2348 const Iterator iter = *
this;
2354 template <
typename number,
bool Constness>
2355 inline const Accessor<number, Constness> &
2356 Iterator<number, Constness>::operator*()
const
2362 template <
typename number,
bool Constness>
2363 inline const Accessor<number, Constness> *
2364 Iterator<number, Constness>::operator->()
const
2370 template <
typename number,
bool Constness>
2372 Iterator<number, Constness>::operator==(
const Iterator &other)
const
2374 return (accessor == other.accessor);
2378 template <
typename number,
bool Constness>
2380 Iterator<number, Constness>::operator!=(
const Iterator &other)
const
2382 return !(*
this == other);
2386 template <
typename number,
bool Constness>
2388 Iterator<number, Constness>::operator<(
const Iterator &other)
const
2390 Assert(&accessor.get_matrix() == &other.accessor.get_matrix(),
2393 return (accessor < other.accessor);
2397 template <
typename number,
bool Constness>
2399 Iterator<number, Constness>::operator>(
const Iterator &other)
const
2401 return (other < *
this);
2405 template <
typename number,
bool Constness>
2407 Iterator<number, Constness>::operator-(
const Iterator &other)
const
2409 Assert(&accessor.get_matrix() == &other.accessor.get_matrix(),
2412 return (*this)->linear_index - other->linear_index;
2417 template <
typename number,
bool Constness>
2418 inline Iterator<number, Constness>
2419 Iterator<number, Constness>::operator+(
const size_type n)
const
2422 for (size_type i = 0; i < n; ++i)
2432template <
typename number>
2436 return const_iterator(
this, 0);
2440template <
typename number>
2444 return const_iterator(
this);
2448template <
typename number>
2452 return iterator(
this, 0);
2456template <
typename number>
2460 return iterator(
this, cols->rowstart[cols->rows]);
2464template <
typename number>
2470 return const_iterator(
this, cols->rowstart[r]);
2475template <
typename number>
2481 return const_iterator(
this, cols->rowstart[r + 1]);
2486template <
typename number>
2492 return iterator(
this, cols->rowstart[r]);
2497template <
typename number>
2503 return iterator(
this, cols->rowstart[r + 1]);
2508template <
typename number>
2509template <
class StreamType>
2513 const bool diagonal_first)
const
2518 bool hanging_diagonal =
false;
2521 for (size_type i = 0; i < cols->rows; ++i)
2523 for (size_type j = cols->rowstart[i]; j < cols->rowstart[i + 1]; ++j)
2525 if (!diagonal_first && i == cols->colnums[j])
2528 hanging_diagonal =
true;
2532 if (hanging_diagonal && cols->colnums[j] > i)
2535 out <<
' ' << i <<
',' << i <<
':' <<
diagonal;
2537 out <<
'(' << i <<
',' << i <<
") " <<
diagonal
2539 hanging_diagonal =
false;
2542 out <<
' ' << i <<
',' << cols->colnums[j] <<
':' << val[j];
2544 out <<
'(' << i <<
',' << cols->colnums[j] <<
") " << val[j]
2548 if (hanging_diagonal)
2551 out <<
' ' << i <<
',' << i <<
':' <<
diagonal;
2553 out <<
'(' << i <<
',' << i <<
") " <<
diagonal << std::endl;
2554 hanging_diagonal =
false;
2562template <
typename number>
2571template <
typename number>
const Reference & operator-=(const number n) const
Reference(const Accessor *accessor, const bool dummy)
const Reference & operator/=(const number n) const
const Accessor * accessor
const Reference & operator+=(const number n) const
const Reference & operator*=(const number n) const
const Reference & operator=(const number n) const
SparseMatrix< number > MatrixType
MatrixType & get_matrix() const
Accessor(MatrixType *matrix, const std::size_t index)
Accessor(MatrixType *matrix)
Accessor(MatrixType *matrix)
Accessor(const SparseMatrixIterators::Accessor< number, false > &a)
Accessor(MatrixType *matrix, const std::size_t index_within_matrix)
const MatrixType & get_matrix() const
const SparseMatrix< number > MatrixType
const SparseMatrix< number > & get_matrix() const
const Iterator< number, Constness > & operator=(const SparseMatrixIterators::Iterator< number, false > &i)
bool operator>(const Iterator &) const
bool operator==(const Iterator &) const
int operator-(const Iterator &p) const
bool operator<(const Iterator &) const
Iterator(MatrixType *matrix)
const Accessor< number, Constness > & value_type
Iterator operator+(const size_type n) const
const Accessor< number, Constness > & operator*() const
Accessor< number, Constness > accessor
Iterator(const SparseMatrixIterators::Iterator< number, false > &i)
Iterator(MatrixType *matrix, const std::size_t index_within_matrix)
const Accessor< number, Constness > * operator->() const
typename Accessor< number, Constness >::MatrixType MatrixType
bool operator!=(const Iterator &) const
void set(const size_type row, const std::vector< size_type > &col_indices, const std::vector< number2 > &values, const bool elide_zero_values=false)
somenumber matrix_scalar_product(const Vector< somenumber > &u, const Vector< somenumber > &v) const
void TSOR_step(Vector< somenumber > &v, const Vector< somenumber > &b, const number omega=1.) const
void precondition_Jacobi(Vector< somenumber > &dst, const Vector< somenumber > &src, const number omega=1.) const
SparseMatrix(const SparsityPattern &sparsity)
void add(const number factor, const SparseMatrix< somenumber > &matrix)
std::size_t n_nonzero_elements() const
size_type get_row_length(const size_type row) const
void SOR_step(Vector< somenumber > &v, const Vector< somenumber > &b, const number omega=1.) const
somenumber residual(Vector< somenumber > &dst, const Vector< somenumber > &x, const Vector< somenumber > &b) const
number & diag_element(const size_type i)
void Tmmult(SparseMatrix< numberC > &C, const SparseMatrix< numberB > &B, const Vector< number > &V=Vector< number >(), const bool rebuild_sparsity_pattern=true) const
void Tvmult(OutVector &dst, const InVector &src) const
const_iterator begin(const size_type r) const
SparseMatrix< number > & operator=(const SparseMatrix< number > &)
const_iterator end() const
void SSOR_step(Vector< somenumber > &v, const Vector< somenumber > &b, const number omega=1.) const
void set(const std::vector< size_type > &indices, const FullMatrix< number2 > &full_matrix, const bool elide_zero_values=false)
void print_as_numpy_arrays(std::ostream &out, const unsigned int precision=9) const
SmartPointer< const SparsityPattern, SparseMatrix< number > > cols
void mmult(SparseMatrix< numberC > &C, const SparseMatrix< numberB > &B, const Vector< number > &V=Vector< number >(), const bool rebuild_sparsity_pattern=true) const
const SparsityPattern & get_sparsity_pattern() const
void set(const size_type i, const size_type j, const number value)
const_iterator begin() const
SparseMatrix< number > & copy_from(const SparseMatrix< somenumber > &source)
void precondition_TSOR(Vector< somenumber > &dst, const Vector< somenumber > &src, const number omega=1.) const
virtual ~SparseMatrix() override
void vmult_add(OutVector &dst, const InVector &src) const
number diag_element(const size_type i) const
void add(const std::vector< size_type > &row_indices, const std::vector< size_type > &col_indices, const FullMatrix< number2 > &full_matrix, const bool elide_zero_values=true)
void SSOR(Vector< somenumber > &v, const number omega=1.) const
somenumber matrix_norm_square(const Vector< somenumber > &v) const
SparseMatrix(SparseMatrix< number > &&m) noexcept
void TSOR(Vector< somenumber > &v, const number omega=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
SparseMatrix< number > & copy_from(const TrilinosWrappers::SparseMatrix &matrix)
SparseMatrix(const SparsityPattern &sparsity, const IdentityMatrix &id)
void print_pattern(std::ostream &out, const double threshold=0.) const
void vmult(OutVector &dst, const InVector &src) const
std::size_t memory_consumption() const
void PSOR(Vector< somenumber > &v, const std::vector< size_type > &permutation, const std::vector< size_type > &inverse_permutation, const number omega=1.) const
void block_write(std::ostream &out) const
void copy_from(const ForwardIterator begin, const ForwardIterator end)
const number & operator()(const size_type i, const size_type j) const
number & operator()(const size_type i, const size_type j)
void block_read(std::istream &in)
number el(const size_type i, const size_type j) const
SparseMatrix< number > & operator=(SparseMatrix< number > &&m) noexcept
void precondition_SSOR(Vector< somenumber > &dst, const Vector< somenumber > &src, const number omega=1., const std::vector< std::size_t > &pos_right_of_diagonal=std::vector< std::size_t >()) const
void print(StreamType &out, const bool across=false, const bool diagonal_first=true) const
const_iterator end(const size_type r) const
void SOR(Vector< somenumber > &v, const number omega=1.) const
SparseMatrix & operator/=(const number factor)
std::unique_ptr< number[]> val
iterator begin(const size_type r)
void precondition_SOR(Vector< somenumber > &dst, const Vector< somenumber > &src, const number omega=1.) const
typename numbers::NumberTraits< number >::real_type real_type
void reinit(const SparseMatrix< number2 > &sparse_matrix)
void add(const size_type row, const size_type n_cols, const size_type *col_indices, const number2 *values, const bool elide_zero_values=true, const bool col_indices_are_sorted=false)
void add(const size_type i, const size_type j, const number value)
void copy_from(const FullMatrix< somenumber > &matrix)
iterator end(const size_type r)
SparseMatrix & operator*=(const number factor)
void compress(VectorOperation::values)
SparseMatrix(const SparseMatrix &)
SparseMatrix< number > & operator=(const IdentityMatrix &id)
SparseMatrix & operator=(const double d)
real_type frobenius_norm() const
void Tvmult_add(OutVector &dst, const InVector &src) const
real_type linfty_norm() const
void add(const size_type row, const std::vector< size_type > &col_indices, const std::vector< number2 > &values, const bool elide_zero_values=true)
void set(const std::vector< size_type > &row_indices, const std::vector< size_type > &col_indices, const FullMatrix< number2 > &full_matrix, const bool elide_zero_values=false)
real_type l1_norm() const
std::size_t n_actually_nonzero_elements(const double threshold=0.) const
void add(const std::vector< size_type > &indices, const FullMatrix< number2 > &full_matrix, const bool elide_zero_values=true)
void TPSOR(Vector< somenumber > &v, const std::vector< size_type > &permutation, const std::vector< size_type > &inverse_permutation, const number omega=1.) const
virtual void reinit(const SparsityPattern &sparsity)
void set(const size_type row, const size_type n_cols, const size_type *col_indices, const number2 *values, const bool elide_zero_values=false)
void Jacobi_step(Vector< somenumber > &v, const Vector< somenumber > &b, const number omega=1.) const
friend class ChunkSparsityPatternIterators::Accessor
SparsityPatternIterators::size_type size_type
static constexpr size_type invalid_entry
#define DEAL_II_NAMESPACE_OPEN
#define DEAL_II_NAMESPACE_CLOSE
__global__ void set(Number *val, const Number s, const size_type N)
static ::ExceptionBase & ExcDifferentSparsityPatterns()
static ::ExceptionBase & ExcInvalidIndex(int arg1, int arg2)
#define Assert(cond, exc)
static ::ExceptionBase & ExcSourceEqualsDestination()
#define AssertIsFinite(number)
#define DeclException2(Exception2, type1, type2, outsequence)
static ::ExceptionBase & ExcNeedsSparsityPattern()
#define AssertIndexRange(index, range)
#define DeclExceptionMsg(Exception, defaulttext)
static ::ExceptionBase & ExcInternalError()
static ::ExceptionBase & ExcDivideByZero()
static ::ExceptionBase & ExcIteratorRange(int arg1, int arg2)
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
static ::ExceptionBase & ExcNotInitialized()
static ::ExceptionBase & ExcNotQuadratic()
@ matrix
Contents is actually a matrix.
@ diagonal
Matrix is diagonal.
types::global_dof_index size_type
VectorType::value_type * end(VectorType &V)
T sum(const T &t, const MPI_Comm mpi_communicator)
unsigned int global_dof_index
static const bool zero_addition_can_be_elided
typename ::SparseMatrixIterators::Iterator< number, Constness >::difference_type difference_type
forward_iterator_tag iterator_category
typename ::SparseMatrixIterators::Iterator< number, Constness >::value_type value_type