Reference documentation for deal.II version 9.0.0
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 at
12 // the top level of the deal.II distribution.
13 //
14 // ---------------------------------------------------------------------
15 
16 #ifndef dealii_tridiagonal_matrix_h
17 #define dealii_tridiagonal_matrix_h
18 
19 #include <deal.II/base/config.h>
20 #include <deal.II/base/subscriptor.h>
21 #include <deal.II/lac/lapack_support.h>
22 
23 #include <vector>
24 #include <iomanip>
25 
26 DEAL_II_NAMESPACE_OPEN
27 
28 // forward declarations
29 template <typename number> class Vector;
30 
31 
48 template <typename number>
50 {
51 public:
53 
54 
58 
66  bool symmetric = false);
67 
72  void reinit(size_type n,
73  bool symmetric = false);
74 
75 
77 
79 
80 
85  size_type m () const;
86 
91  size_type n () const;
92 
98  bool all_zero () const;
99 
101 
103 
104 
108  number operator()(size_type i, size_type j) const;
109 
119  number &operator()(size_type i, size_type j);
120 
122 
124 
125 
135  void vmult (Vector<number> &w,
136  const Vector<number> &v,
137  const bool adding=false) const;
138 
145  void vmult_add (Vector<number> &w,
146  const Vector<number> &v) const;
147 
157  void Tvmult (Vector<number> &w,
158  const Vector<number> &v,
159  const bool adding=false) const;
160 
168  void Tvmult_add (Vector<number> &w,
169  const Vector<number> &v) const;
170 
176  number matrix_scalar_product (const Vector<number> &u,
177  const Vector<number> &v) const;
178 
188  number matrix_norm_square (const Vector<number> &v) const;
189 
191 
193 
194 
200  void compute_eigenvalues();
204  number eigenvalue(const size_type i) const;
206 
208 
209 
212  template <class OutputStream>
213  void print(OutputStream &s,
214  const unsigned int width=5,
215  const unsigned int precision=2) const;
217 
218 private:
222  std::vector<number> diagonal;
232  std::vector<number> left;
238  std::vector<number> right;
239 
245 
253 };
254 
257 //---------------------------------------------------------------------------
258 #ifndef DOXYGEN
259 
260 template <typename number>
263 {
264  return diagonal.size();
265 }
266 
267 
268 
269 template <typename number>
272 {
273  return diagonal.size();
274 }
275 
276 
277 template <typename number>
278 inline
279 number
280 TridiagonalMatrix<number>::operator()(size_type i, size_type j) const
281 {
282  Assert(i<n(), ExcIndexRange(i,0,n()));
283  Assert(j<n(), ExcIndexRange(j,0,n()));
284  Assert (i<=j+1, ExcIndexRange(i,j-1,j+2));
285  Assert (j<=i+1, ExcIndexRange(j,i-1,i+2));
286 
287  if (j==i)
288  return diagonal[i];
289  if (j==i-1)
290  {
291  if (is_symmetric)
292  return right[i-1];
293  else
294  return left[i];
295  }
296 
297  if (j==i+1)
298  return right[i];
299 
300  Assert (false, ExcInternalError());
301  return 0;
302 }
303 
304 
305 template <typename number>
306 inline
307 number &
308 TridiagonalMatrix<number>::operator()(size_type i, size_type j)
309 {
310  Assert(i<n(), ExcIndexRange(i,0,n()));
311  Assert(j<n(), ExcIndexRange(j,0,n()));
312  Assert (i<=j+1, ExcIndexRange(i,j-1,j+2));
313  Assert (j<=i+1, ExcIndexRange(j,i-1,i+2));
314 
315  if (j==i)
316  return diagonal[i];
317  if (j==i-1)
318  {
319  if (is_symmetric)
320  return right[i-1];
321  else
322  return left[i];
323  }
324 
325  if (j==i+1)
326  return right[i];
327 
328  Assert (false, ExcInternalError());
329  return diagonal[0];
330 }
331 
332 
333 template <typename number>
334 template <class OutputStream>
335 void
337  OutputStream &s,
338  const unsigned int width,
339  const unsigned int) const
340 {
341  for (size_type i=0; i<n(); ++i)
342  {
343  if (i>0)
344  s << std::setw(width) << (*this)(i,i-1);
345  else
346  s << std::setw(width) << "";
347 
348  s << ' ' << (*this)(i,i) << ' ';
349 
350  if (i<n()-1)
351  s << std::setw(width) << (*this)(i,i+1);
352 
353  s << std::endl;
354  }
355 }
356 
357 
358 #endif // DOXYGEN
359 
360 DEAL_II_NAMESPACE_CLOSE
361 
362 #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
Matrix is diagonal.
unsigned int global_dof_index
Definition: types.h:88
#define Assert(cond, exc)
Definition: exceptions.h:1142
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)
types::global_dof_index size_type
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
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
static ::ExceptionBase & ExcInternalError()