Reference documentation for deal.II version Git 7026f387cc 2019-10-15 14:19:01 -0400
\(\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 #ifndef DOXYGEN
32 template <typename number>
33 class Vector;
34 #endif
35 
52 template <typename number>
54 {
55 public:
57 
58 
62 
69  TridiagonalMatrix(size_type n = 0, bool symmetric = false);
70 
75  void
76  reinit(size_type n, bool symmetric = false);
77 
78 
80 
82 
83 
88  size_type
89  m() const;
90 
95  size_type
96  n() const;
97 
103  bool
104  all_zero() const;
105 
107 
109 
110 
114  number
115  operator()(size_type i, size_type j) const;
116 
126  number &
128 
130 
132 
133 
143  void
144  vmult(Vector<number> & w,
145  const Vector<number> &v,
146  const bool adding = false) const;
147 
154  void
155  vmult_add(Vector<number> &w, const Vector<number> &v) const;
156 
166  void
168  const Vector<number> &v,
169  const bool adding = false) const;
170 
178  void
179  Tvmult_add(Vector<number> &w, const Vector<number> &v) const;
180 
186  number
187  matrix_scalar_product(const Vector<number> &u, const Vector<number> &v) const;
188 
198  number
199  matrix_norm_square(const Vector<number> &v) const;
200 
202 
204 
205 
211  void
216  number
217  eigenvalue(const size_type i) const;
219 
221 
222 
225  template <class OutputStream>
226  void
227  print(OutputStream & s,
228  const unsigned int width = 5,
229  const unsigned int precision = 2) const;
231 
232 private:
236  std::vector<number> diagonal;
246  std::vector<number> left;
252  std::vector<number> right;
253 
259 
267 };
268 
271 //---------------------------------------------------------------------------
272 #ifndef DOXYGEN
273 
274 template <typename number>
277 {
278  return diagonal.size();
279 }
280 
281 
282 
283 template <typename number>
286 {
287  return diagonal.size();
288 }
289 
290 
291 template <typename number>
292 inline number
294 {
295  Assert(i < n(), ExcIndexRange(i, 0, n()));
296  Assert(j < n(), ExcIndexRange(j, 0, n()));
297  Assert(i <= j + 1, ExcIndexRange(i, j - 1, j + 2));
298  Assert(j <= i + 1, ExcIndexRange(j, i - 1, i + 2));
299 
300  if (j == i)
301  return diagonal[i];
302  if (j == i - 1)
303  {
304  if (is_symmetric)
305  return right[i - 1];
306  else
307  return left[i];
308  }
309 
310  if (j == i + 1)
311  return right[i];
312 
313  Assert(false, ExcInternalError());
314  return 0;
315 }
316 
317 
318 template <typename number>
319 inline number &
321 {
322  Assert(i < n(), ExcIndexRange(i, 0, n()));
323  Assert(j < n(), ExcIndexRange(j, 0, n()));
324  Assert(i <= j + 1, ExcIndexRange(i, j - 1, j + 2));
325  Assert(j <= i + 1, ExcIndexRange(j, i - 1, i + 2));
326 
327  if (j == i)
328  return diagonal[i];
329  if (j == i - 1)
330  {
331  if (is_symmetric)
332  return right[i - 1];
333  else
334  return left[i];
335  }
336 
337  if (j == i + 1)
338  return right[i];
339 
340  Assert(false, ExcInternalError());
341  return diagonal[0];
342 }
343 
344 
345 template <typename number>
346 template <class OutputStream>
347 void
348 TridiagonalMatrix<number>::print(OutputStream & s,
349  const unsigned int width,
350  const unsigned int) const
351 {
352  for (size_type i = 0; i < n(); ++i)
353  {
354  if (i > 0)
355  s << std::setw(width) << (*this)(i, i - 1);
356  else
357  s << std::setw(width) << "";
358 
359  s << ' ' << (*this)(i, i) << ' ';
360 
361  if (i < n() - 1)
362  s << std::setw(width) << (*this)(i, i + 1);
363 
364  s << std::endl;
365  }
366 }
367 
368 
369 #endif // DOXYGEN
370 
371 DEAL_II_NAMESPACE_CLOSE
372 
373 #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
#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()