MFEM v4.7.0
Finite element discretization library
Loading...
Searching...
No Matches
matrix.hpp
Go to the documentation of this file.
1// Copyright (c) 2010-2024, Lawrence Livermore National Security, LLC. Produced
2// at the Lawrence Livermore National Laboratory. All Rights reserved. See files
3// LICENSE and NOTICE for details. LLNL-CODE-806117.
4//
5// This file is part of the MFEM library. For more information and source code
6// availability visit https://mfem.org.
7//
8// MFEM is free software; you can redistribute it and/or modify it under the
9// terms of the BSD-3 license. We welcome feedback and contributions, see file
10// CONTRIBUTING.md for details.
11
12#ifndef MFEM_MATRIX
13#define MFEM_MATRIX
14
15#include "../general/array.hpp"
17#include "operator.hpp"
18
19namespace mfem
20{
21
22// Abstract data types matrix, inverse matrix
23
24class MatrixInverse;
25
26/// Abstract data type matrix
27class Matrix : public Operator
28{
29 friend class MatrixInverse;
30public:
31
32 /// Creates a square matrix of size s.
33 explicit Matrix(int s) : Operator(s) { }
34
35 /// Creates a matrix of the given height and width.
36 explicit Matrix(int h, int w) : Operator(h, w) { }
37
38 /// Returns whether the matrix is a square matrix.
39 bool IsSquare() const { return (height == width); }
40
41 /// Returns reference to a_{ij}.
42 virtual real_t &Elem(int i, int j) = 0;
43
44 /// Returns constant reference to a_{ij}.
45 virtual const real_t &Elem(int i, int j) const = 0;
46
47 /// Returns a pointer to (an approximation) of the matrix inverse.
48 virtual MatrixInverse *Inverse() const = 0;
49
50 /// Finalizes the matrix initialization.
51 virtual void Finalize(int) { }
52
53 /// Prints matrix to stream out.
54 virtual void Print (std::ostream & out = mfem::out, int width_ = 4) const;
55
56 /// Destroys matrix.
57 virtual ~Matrix() { }
58};
59
60
61/// Abstract data type for matrix inverse
62class MatrixInverse : public Solver
63{
64public:
66
67 /// Creates approximation of the inverse of square matrix
69 : Solver(mat.height, mat.width) { }
70};
71
72/// Abstract data type for sparse matrices
74{
75public:
76 /// Creates a square matrix of the given size.
77 explicit AbstractSparseMatrix(int s = 0) : Matrix(s) { }
78
79 /// Creates a matrix of the given height and width.
80 explicit AbstractSparseMatrix(int h, int w) : Matrix(h, w) { }
81
82 /// Returns the number of non-zeros in a matrix
83 virtual int NumNonZeroElems() const = 0;
84
85 /// Gets the columns indexes and values for row *row*.
86 /** Returns:
87 - 0 if @a cols and @a srow are copies of the values in the matrix.
88 - 1 if @a cols and @a srow are views of the values in the matrix. */
89 virtual int GetRow(const int row, Array<int> &cols, Vector &srow) const = 0;
90
91 /** @brief If the matrix is square, this method will place 1 on the diagonal
92 (i,i) if row i has "almost" zero l1-norm.
93
94 If entry (i,i) does not belong to the sparsity pattern of A, then an
95 error will occur. */
96 virtual void EliminateZeroRows(const real_t threshold = 1e-12) = 0;
97
98 /// Matrix-Vector Multiplication y = A*x
99 virtual void Mult(const Vector &x, Vector &y) const = 0;
100 /// Matrix-Vector Multiplication y = y + val*A*x
101 virtual void AddMult(const Vector &x, Vector &y,
102 const real_t val = 1.) const = 0;
103 /// MatrixTranspose-Vector Multiplication y = A'*x
104 virtual void MultTranspose(const Vector &x, Vector &y) const = 0;
105 /// MatrixTranspose-Vector Multiplication y = y + val*A'*x
106 virtual void AddMultTranspose(const Vector &x, Vector &y,
107 const real_t val = 1.) const = 0;
108
109 /// Destroys AbstractSparseMatrix.
111};
112
113}
114
115#endif
Abstract data type for sparse matrices.
Definition matrix.hpp:74
virtual void Mult(const Vector &x, Vector &y) const =0
Matrix-Vector Multiplication y = A*x.
virtual ~AbstractSparseMatrix()
Destroys AbstractSparseMatrix.
Definition matrix.hpp:110
AbstractSparseMatrix(int h, int w)
Creates a matrix of the given height and width.
Definition matrix.hpp:80
virtual void AddMultTranspose(const Vector &x, Vector &y, const real_t val=1.) const =0
MatrixTranspose-Vector Multiplication y = y + val*A'*x.
virtual void AddMult(const Vector &x, Vector &y, const real_t val=1.) const =0
Matrix-Vector Multiplication y = y + val*A*x.
virtual int NumNonZeroElems() const =0
Returns the number of non-zeros in a matrix.
virtual void EliminateZeroRows(const real_t threshold=1e-12)=0
If the matrix is square, this method will place 1 on the diagonal (i,i) if row i has "almost" zero l1...
virtual void MultTranspose(const Vector &x, Vector &y) const =0
MatrixTranspose-Vector Multiplication y = A'*x.
virtual int GetRow(const int row, Array< int > &cols, Vector &srow) const =0
Gets the columns indexes and values for row row.
AbstractSparseMatrix(int s=0)
Creates a square matrix of the given size.
Definition matrix.hpp:77
Abstract data type for matrix inverse.
Definition matrix.hpp:63
MatrixInverse(const Matrix &mat)
Creates approximation of the inverse of square matrix.
Definition matrix.hpp:68
Abstract data type matrix.
Definition matrix.hpp:28
virtual real_t & Elem(int i, int j)=0
Returns reference to a_{ij}.
virtual const real_t & Elem(int i, int j) const =0
Returns constant reference to a_{ij}.
virtual MatrixInverse * Inverse() const =0
Returns a pointer to (an approximation) of the matrix inverse.
Matrix(int s)
Creates a square matrix of size s.
Definition matrix.hpp:33
virtual void Finalize(int)
Finalizes the matrix initialization.
Definition matrix.hpp:51
virtual ~Matrix()
Destroys matrix.
Definition matrix.hpp:57
virtual void Print(std::ostream &out=mfem::out, int width_=4) const
Prints matrix to stream out.
Definition matrix.cpp:22
Matrix(int h, int w)
Creates a matrix of the given height and width.
Definition matrix.hpp:36
bool IsSquare() const
Returns whether the matrix is a square matrix.
Definition matrix.hpp:39
Abstract operator.
Definition operator.hpp:25
int width
Dimension of the input / number of columns in the matrix.
Definition operator.hpp:28
int height
Dimension of the output / number of rows in the matrix.
Definition operator.hpp:27
Base class for solvers.
Definition operator.hpp:683
Vector data type.
Definition vector.hpp:80
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:66
float real_t
Definition config.hpp:43
RefCoord s[3]