MFEM  v3.4
Finite element discretization library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
matrix.hpp
Go to the documentation of this file.
1 // Copyright (c) 2010, Lawrence Livermore National Security, LLC. Produced at
2 // the Lawrence Livermore National Laboratory. LLNL-CODE-443211. All Rights
3 // reserved. See file COPYRIGHT for details.
4 //
5 // This file is part of the MFEM library. For more information and source code
6 // availability see http://mfem.org.
7 //
8 // MFEM is free software; you can redistribute it and/or modify it under the
9 // terms of the GNU Lesser General Public License (as published by the Free
10 // Software Foundation) version 2.1 dated February 1999.
11 
12 #ifndef MFEM_MATRIX
13 #define MFEM_MATRIX
14 
15 #include "../general/array.hpp"
16 #include "../general/globals.hpp"
17 #include "operator.hpp"
18 
19 namespace mfem
20 {
21 
22 // Abstract data types matrix, inverse matrix
23 
24 class MatrixInverse;
25 
26 /// Abstract data type matrix
27 class Matrix : public Operator
28 {
29  friend class MatrixInverse;
30 public:
31  //// Defines matrix diagonal policy upon elimination of rows and/or columns.
33  {
34  DIAG_ZERO, ///< Set the diagonal value to zero
35  DIAG_ONE, ///< Set the diagonal value to one
36  DIAG_KEEP ///< Keep the diagonal value
37  };
38 
39  /// Creates a square matrix of size s.
40  explicit Matrix(int s) : Operator(s) { }
41 
42  /// Creates a matrix of the given height and width.
43  explicit Matrix(int h, int w) : Operator(h, w) { }
44 
45  /// Returns reference to a_{ij}.
46  virtual double &Elem(int i, int j) = 0;
47 
48  /// Returns constant reference to a_{ij}.
49  virtual const double &Elem(int i, int j) const = 0;
50 
51  /// Returns a pointer to (an approximation) of the matrix inverse.
52  virtual MatrixInverse *Inverse() const = 0;
53 
54  /// Finalizes the matrix initialization.
55  virtual void Finalize(int) { }
56 
57  /// Prints matrix to stream out.
58  virtual void Print (std::ostream & out = mfem::out, int width_ = 4) const;
59 
60  /// Destroys matrix.
61  virtual ~Matrix() { }
62 };
63 
64 
65 /// Abstract data type for matrix inverse
66 class MatrixInverse : public Solver
67 {
68 public:
70 
71  /// Creates approximation of the inverse of square matrix
72  MatrixInverse(const Matrix &mat)
73  : Solver(mat.height, mat.width) { }
74 };
75 
76 /// Abstract data type for sparse matrices
78 {
79 public:
80  /// Creates a square matrix of the given size.
81  explicit AbstractSparseMatrix(int s = 0) : Matrix(s) { }
82 
83  /// Creates a matrix of the given height and width.
84  explicit AbstractSparseMatrix(int h, int w) : Matrix(h, w) { }
85 
86  /// Returns the number of non-zeros in a matrix
87  virtual int NumNonZeroElems() const = 0;
88 
89  /// Gets the columns indexes and values for row *row*.
90  /** Returns:
91  - 0 if @a cols and @a srow are copies of the values in the matrix.
92  - 1 if @a cols and @a srow are views of the values in the matrix. */
93  virtual int GetRow(const int row, Array<int> &cols, Vector &srow) const = 0;
94 
95  /** @brief If the matrix is square, this method will place 1 on the diagonal
96  (i,i) if row i has "almost" zero l1-norm.
97 
98  If entry (i,i) does not belong to the sparsity pattern of A, then an
99  error will occur. */
100  virtual void EliminateZeroRows(const double threshold = 1e-12) = 0;
101 
102  /// Matrix-Vector Multiplication y = A*x
103  virtual void Mult(const Vector &x, Vector &y) const = 0;
104  /// Matrix-Vector Multiplication y = y + val*A*x
105  virtual void AddMult(const Vector &x, Vector &y,
106  const double val = 1.) const = 0;
107  /// MatrixTranspose-Vector Multiplication y = A'*x
108  virtual void MultTranspose(const Vector &x, Vector &y) const = 0;
109  /// MatrixTranspose-Vector Multiplication y = y + val*A'*x
110  virtual void AddMultTranspose(const Vector &x, Vector &y,
111  const double val = 1.) const = 0;
112 
113  /// Destroys AbstractSparseMatrix.
114  virtual ~AbstractSparseMatrix() { }
115 };
116 
117 }
118 
119 #endif
virtual void Finalize(int)
Finalizes the matrix initialization.
Definition: matrix.hpp:55
virtual MatrixInverse * Inverse() const =0
Returns a pointer to (an approximation) of the matrix inverse.
Set the diagonal value to zero.
Definition: matrix.hpp:34
virtual ~AbstractSparseMatrix()
Destroys AbstractSparseMatrix.
Definition: matrix.hpp:114
Abstract data type for sparse matrices.
Definition: matrix.hpp:77
virtual void AddMult(const Vector &x, Vector &y, const double val=1.) const =0
Matrix-Vector Multiplication y = y + val*A*x.
Abstract data type for matrix inverse.
Definition: matrix.hpp:66
virtual void Print(std::ostream &out=mfem::out, int width_=4) const
Prints matrix to stream out.
Definition: matrix.cpp:22
virtual int NumNonZeroElems() const =0
Returns the number of non-zeros in a matrix.
AbstractSparseMatrix(int h, int w)
Creates a matrix of the given height and width.
Definition: matrix.hpp:84
virtual void MultTranspose(const Vector &x, Vector &y) const =0
MatrixTranspose-Vector Multiplication y = A&#39;*x.
Matrix(int h, int w)
Creates a matrix of the given height and width.
Definition: matrix.hpp:43
Keep the diagonal value.
Definition: matrix.hpp:36
virtual ~Matrix()
Destroys matrix.
Definition: matrix.hpp:61
Abstract data type matrix.
Definition: matrix.hpp:27
virtual double & Elem(int i, int j)=0
Returns reference to a_{ij}.
virtual int GetRow(const int row, Array< int > &cols, Vector &srow) const =0
Gets the columns indexes and values for row row.
virtual void Mult(const Vector &x, Vector &y) const =0
Matrix-Vector Multiplication y = A*x.
MatrixInverse(const Matrix &mat)
Creates approximation of the inverse of square matrix.
Definition: matrix.hpp:72
Set the diagonal value to one.
Definition: matrix.hpp:35
virtual void EliminateZeroRows(const double threshold=1e-12)=0
If the matrix is square, this method will place 1 on the diagonal (i,i) if row i has &quot;almost&quot; zero l1...
int height
Dimension of the output / number of rows in the matrix.
Definition: operator.hpp:24
Matrix(int s)
Creates a square matrix of size s.
Definition: matrix.hpp:40
virtual void AddMultTranspose(const Vector &x, Vector &y, const double val=1.) const =0
MatrixTranspose-Vector Multiplication y = y + val*A&#39;*x.
Vector data type.
Definition: vector.hpp:48
Base class for solvers.
Definition: operator.hpp:268
OutStream out(std::cout)
Global stream used by the library for standard output. Initially it uses the same std::streambuf as s...
Definition: globals.hpp:64
Abstract operator.
Definition: operator.hpp:21
AbstractSparseMatrix(int s=0)
Creates a square matrix of the given size.
Definition: matrix.hpp:81
int width
Dimension of the input / number of columns in the matrix.
Definition: operator.hpp:25