MFEM  v3.3 Finite element discretization library
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 "operator.hpp"
17 #include <iostream>
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  /// Creates a square matrix of size s.
32  explicit Matrix(int s) : Operator(s) { }
33
34  /// Creates a matrix of the given height and width.
35  explicit Matrix(int h, int w) : Operator(h, w) { }
36
37  /// Returns reference to a_{ij}.
38  virtual double &Elem(int i, int j) = 0;
39
40  /// Returns constant reference to a_{ij}.
41  virtual const double &Elem(int i, int j) const = 0;
42
43  /// Returns a pointer to (an approximation) of the matrix inverse.
44  virtual MatrixInverse *Inverse() const = 0;
45
46  /// Finalizes the matrix initialization.
47  virtual void Finalize(int) { }
48
49  /// Prints matrix to stream out.
50  virtual void Print (std::ostream & out = std::cout, int width_ = 4) const;
51
52  /// Destroys matrix.
53  virtual ~Matrix() { }
54 };
55
56
57 /// Abstract data type for matrix inverse
58 class MatrixInverse : public Solver
59 {
60 public:
62
63  /// Creates approximation of the inverse of square matrix
64  MatrixInverse(const Matrix &mat)
65  : Solver(mat.height, mat.width) { }
66 };
67
68 /// Abstract data type for sparse matrices
70 {
71 public:
72  /// Creates a square matrix of the given size.
73  explicit AbstractSparseMatrix(int s = 0) : Matrix(s) { }
74
75  /// Creates a matrix of the given height and width.
76  explicit AbstractSparseMatrix(int h, int w) : Matrix(h, w) { }
77
78  /// Returns the number of non-zeros in a matrix
79  virtual int NumNonZeroElems() const = 0;
80
81  /** Gets the columns indexes and values for row *row*.
82  Returns:
83  0 if cols and srow are copies of the values in the matrix.
84  1 if cols and srow are views of the values in the matrix. */
85  virtual int GetRow(const int row, Array<int> &cols, Vector &srow) const = 0;
86
87  /** If the matrix is square, it will place 1 on the diagonal (i,i) if row i
88  has "almost" zero l1-norm.
89  If entry (i,i) does not belong to the sparsity pattern of A, then an
90  error will occur. */
91  virtual void EliminateZeroRows() = 0;
92
93  /// Matrix-Vector Multiplication y = A*x
94  virtual void Mult(const Vector &x, Vector &y) const = 0;
95  /// Matrix-Vector Multiplication y = y + val*A*x
96  virtual void AddMult(const Vector &x, Vector &y,
97  const double val = 1.) const = 0;
98  /// MatrixTranspose-Vector Multiplication y = A'*x
99  virtual void MultTranspose(const Vector &x, Vector &y) const = 0;
100  /// MatrixTranspose-Vector Multiplication y = y + val*A'*x
101  virtual void AddMultTranspose(const Vector &x, Vector &y,
102  const double val = 1.) const = 0;
103
104  /// Destroys AbstractSparseMatrix.
105  virtual ~AbstractSparseMatrix() { }
106 };
107
108 }
109
110 #endif
virtual void Print(std::ostream &out=std::cout, int width_=4) const
Prints matrix to stream out.
Definition: matrix.cpp:22
virtual void Finalize(int)
Finalizes the matrix initialization.
Definition: matrix.hpp:47
virtual MatrixInverse * Inverse() const =0
Returns a pointer to (an approximation) of the matrix inverse.
virtual ~AbstractSparseMatrix()
Destroys AbstractSparseMatrix.
Definition: matrix.hpp:105
Abstract data type for sparse matrices.
Definition: matrix.hpp:69
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:58
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:76
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:35
virtual ~Matrix()
Destroys matrix.
Definition: matrix.hpp:53
Abstract data type matrix.
Definition: matrix.hpp:27
virtual void EliminateZeroRows()=0
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
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:64
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:32
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:36
Base class for solvers.
Definition: operator.hpp:257
Abstract operator.
Definition: operator.hpp:21
AbstractSparseMatrix(int s=0)
Creates a square matrix of the given size.
Definition: matrix.hpp:73
int width
Dimension of the input / number of columns in the matrix.
Definition: operator.hpp:25