Reference documentation for deal.II version 9.1.1
\(\newcommand{\dealcoloneq}{\mathrel{\vcenter{:}}=}\)
tridiagonal_matrix.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2005 - 2018 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.md at
12 // the top level directory of deal.II.
13 //
14 // ---------------------------------------------------------------------
15 
16 #ifndef dealii_tridiagonal_matrix_h
17 #define dealii_tridiagonal_matrix_h
18 
19 #include <deal.II/base/config.h>
20 
21 #include <deal.II/base/subscriptor.h>
22 
23 #include <deal.II/lac/lapack_support.h>
24 
25 #include <iomanip>
26 #include <vector>
27 
28 DEAL_II_NAMESPACE_OPEN
29 
30 // forward declarations
31 template <typename number>
32 class Vector;
33 
34 
51 template <typename number>
53 {
54 public:
56 
57 
61 
68  TridiagonalMatrix(size_type n = 0, bool symmetric = false);
69 
74  void
75  reinit(size_type n, bool symmetric = false);
76 
77 
79 
81 
82 
87  size_type
88  m() const;
89 
94  size_type
95  n() const;
96 
102  bool
103  all_zero() const;
104 
106 
108 
109 
113  number
114  operator()(size_type i, size_type j) const;
115 
125  number &
127 
129 
131 
132 
142  void
143  vmult(Vector<number> & w,
144  const Vector<number> &v,
145  const bool adding = false) const;
146 
153  void
154  vmult_add(Vector<number> &w, const Vector<number> &v) const;
155 
165  void
167  const Vector<number> &v,
168  const bool adding = false) const;
169 
177  void
178  Tvmult_add(Vector<number> &w, const Vector<number> &v) const;
179 
185  number
186  matrix_scalar_product(const Vector<number> &u, const Vector<number> &v) const;
187 
197  number
198  matrix_norm_square(const Vector<number> &v) const;
199 
201 
203 
204 
210  void
215  number
216  eigenvalue(const size_type i) const;
218 
220 
221 
224  template <class OutputStream>
225  void
226  print(OutputStream & s,
227  const unsigned int width = 5,
228  const unsigned int precision = 2) const;
230 
231 private:
235  std::vector<number> diagonal;
245  std::vector<number> left;
251  std::vector<number> right;
252 
258 
266 };
267 
270 //---------------------------------------------------------------------------
271 #ifndef DOXYGEN
272 
273 template <typename number>
276 {
277  return diagonal.size();
278 }
279 
280 
281 
282 template <typename number>
285 {
286  return diagonal.size();
287 }
288 
289 
290 template <typename number>
291 inline number
292 TridiagonalMatrix<number>::operator()(size_type i, size_type j) const
293 {
294  Assert(i < n(), ExcIndexRange(i, 0, n()));
295  Assert(j < n(), ExcIndexRange(j, 0, n()));
296  Assert(i <= j + 1, ExcIndexRange(i, j - 1, j + 2));
297  Assert(j <= i + 1, ExcIndexRange(j, i - 1, i + 2));
298 
299  if (j == i)
300  return diagonal[i];
301  if (j == i - 1)
302  {
303  if (is_symmetric)
304  return right[i - 1];
305  else
306  return left[i];
307  }
308 
309  if (j == i + 1)
310  return right[i];
311 
312  Assert(false, ExcInternalError());
313  return 0;
314 }
315 
316 
317 template <typename number>
318 inline number &
319 TridiagonalMatrix<number>::operator()(size_type i, size_type j)
320 {
321  Assert(i < n(), ExcIndexRange(i, 0, n()));
322  Assert(j < n(), ExcIndexRange(j, 0, n()));
323  Assert(i <= j + 1, ExcIndexRange(i, j - 1, j + 2));
324  Assert(j <= i + 1, ExcIndexRange(j, i - 1, i + 2));
325 
326  if (j == i)
327  return diagonal[i];
328  if (j == i - 1)
329  {
330  if (is_symmetric)
331  return right[i - 1];
332  else
333  return left[i];
334  }
335 
336  if (j == i + 1)
337  return right[i];
338 
339  Assert(false, ExcInternalError());
340  return diagonal[0];
341 }
342 
343 
344 template <typename number>
345 template <class OutputStream>
346 void
347 TridiagonalMatrix<number>::print(OutputStream & s,
348  const unsigned int width,
349  const unsigned int) const
350 {
351  for (size_type i = 0; i < n(); ++i)
352  {
353  if (i > 0)
354  s << std::setw(width) << (*this)(i, i - 1);
355  else
356  s << std::setw(width) << "";
357 
358  s << ' ' << (*this)(i, i) << ' ';
359 
360  if (i < n() - 1)
361  s << std::setw(width) << (*this)(i, i + 1);
362 
363  s << std::endl;
364  }
365 }
366 
367 
368 #endif // DOXYGEN
369 
370 DEAL_II_NAMESPACE_CLOSE
371 
372 #endif
std::vector< number > left
void print(OutputStream &s, const unsigned int width=5, const unsigned int precision=2) const
void vmult(Vector< number > &w, const Vector< number > &v, const bool adding=false) const
static ::ExceptionBase & ExcIndexRange(int arg1, int arg2, int arg3)
void Tvmult_add(Vector< number > &w, const Vector< number > &v) const
LinearAlgebra::distributed::Vector< Number > Vector
Matrix is diagonal.
#define Assert(cond, exc)
Definition: exceptions.h:1407
std::vector< number > right
number matrix_scalar_product(const Vector< number > &u, const Vector< number > &v) const
TridiagonalMatrix(size_type n=0, bool symmetric=false)
number matrix_norm_square(const Vector< number > &v) const
size_type n() const
number eigenvalue(const size_type i) const
void Tvmult(Vector< number > &w, const Vector< number > &v, const bool adding=false) const
unsigned int global_dof_index
Definition: types.h:89
number operator()(size_type i, size_type j) const
LAPACKSupport::State state
std::vector< number > diagonal
size_type m() const
void reinit(size_type n, bool symmetric=false)
void vmult_add(Vector< number > &w, const Vector< number > &v) const
types::global_dof_index size_type
static ::ExceptionBase & ExcInternalError()