libMesh
Loading...
Searching...
No Matches
dense_matrix_base.h
Go to the documentation of this file.
1// The libMesh Finite Element Library.
2// Copyright (C) 2002-2026 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3
4// This library is free software; you can redistribute it and/or
5// modify it under the terms of the GNU Lesser General Public
6// License as published by the Free Software Foundation; either
7// version 2.1 of the License, or (at your option) any later version.
8
9// This library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12// Lesser General Public License for more details.
13
14// You should have received a copy of the GNU Lesser General Public
15// License along with this library; if not, write to the Free Software
16// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
18
19
20#ifndef LIBMESH_DENSE_MATRIX_BASE_H
21#define LIBMESH_DENSE_MATRIX_BASE_H
22
23// Local Includes
24#include "libmesh/libmesh_common.h"
25#include "libmesh/compare_types.h"
26#include "libmesh/int_range.h"
27
28// C++ includes
29
30namespace libMesh
31{
32
33// Forward declarations
34template <typename T> class DenseVectorBase;
35template <typename T> class DenseVector;
36
45template<typename T>
47{
48
49protected:
54 DenseMatrixBase(const unsigned int new_m=0,
55 const unsigned int new_n=0) : _m(new_m), _n(new_n) {}
56
57public:
63 DenseMatrixBase (const DenseMatrixBase &) = default;
66 virtual ~DenseMatrixBase() = default;
67
73 virtual void zero() = 0;
74
80 virtual T el(const unsigned int i,
81 const unsigned int j) const = 0;
82
88 virtual T & el(const unsigned int i,
89 const unsigned int j) = 0;
90
94 virtual void left_multiply (const DenseMatrixBase<T> & M2) = 0;
95
99 virtual void right_multiply (const DenseMatrixBase<T> & M3) = 0;
100
104 unsigned int m() const { return _m; }
105
109 unsigned int n() const { return _n; }
110
114 void print(std::ostream & os = libMesh::out) const;
115
121 friend std::ostream & operator << (std::ostream & os, const DenseMatrixBase<T> & m)
122 {
123 m.print(os);
124 return os;
125 }
126
131 void print_scientific(std::ostream & os, unsigned precision=8) const;
132
138 template <typename T2, typename T3>
139 typename std::enable_if<
140 ScalarTraits<T2>::value, void >::type
141 add (const T2 factor,
142 const DenseMatrixBase<T3> & mat);
143
147 DenseVector<T> diagonal() const;
148
149protected:
150
158 static void multiply (DenseMatrixBase<T> & M1,
159 const DenseMatrixBase<T> & M2,
160 const DenseMatrixBase<T> & M3);
161
168 void condense(const unsigned int i,
169 const unsigned int j,
170 const T val,
171 DenseVectorBase<T> & rhs);
172
176 unsigned int _m;
177
181 unsigned int _n;
182};
183
184
185
186
187
188
189
190template<typename T>
191template<typename T2, typename T3>
192inline
193typename std::enable_if<
194 ScalarTraits<T2>::value, void >::type
195DenseMatrixBase<T>::add (const T2 factor,
196 const DenseMatrixBase<T3> & mat)
197{
198 libmesh_assert_equal_to (this->m(), mat.m());
199 libmesh_assert_equal_to (this->n(), mat.n());
200
201 for (auto j : make_range(this->n()))
202 for (auto i : make_range(this->m()))
203 this->el(i,j) += factor*mat.el(i,j);
204}
205
206
207} // namespace libMesh
208
209#endif // LIBMESH_DENSE_MATRIX_BASE_H
Defines an abstract dense matrix base class for use in Finite Element-type computations.
void condense(const unsigned int i, const unsigned int j, const T val, DenseVectorBase< T > &rhs)
Condense-out the (i,j) entry of the matrix, forcing it to take on the value val.
virtual T el(const unsigned int i, const unsigned int j) const =0
virtual void right_multiply(const DenseMatrixBase< T > &M3)=0
Performs the operation: (*this) <- (*this) * M3.
void print(std::ostream &os=libMesh::out) const
Pretty-print the matrix, by default to libMesh::out.
friend std::ostream & operator<<(std::ostream &os, const DenseMatrixBase< T > &m)
Formatted print as above but allows you to do DenseMatrix K; libMesh::out << K << std::endl;.
std::enable_if< ScalarTraits< T2 >::value, void >::type add(const T2 factor, const DenseMatrixBase< T3 > &mat)
Adds factor to every element in the matrix.
DenseMatrixBase(const DenseMatrixBase &)=default
unsigned int _n
The column dimension.
static void multiply(DenseMatrixBase< T > &M1, const DenseMatrixBase< T > &M2, const DenseMatrixBase< T > &M3)
Helper function - Performs the computation M1 = M2 * M3 where: M1 = (m x n) M2 = (m x p) M3 = (p x n)
virtual ~DenseMatrixBase()=default
virtual void zero()=0
Set every element in the matrix to 0.
unsigned int _m
The row dimension.
DenseMatrixBase(DenseMatrixBase &&)=default
The 5 special functions can be defaulted for this class, as it does not manage any memory itself.
DenseVector< T > diagonal() const
Return the matrix diagonal.
virtual T & el(const unsigned int i, const unsigned int j)=0
DenseMatrixBase(const unsigned int new_m=0, const unsigned int new_n=0)
Constructor.
void print_scientific(std::ostream &os, unsigned precision=8) const
Prints the matrix entries with more decimal places in scientific notation.
DenseMatrixBase & operator=(const DenseMatrixBase &)=default
virtual void left_multiply(const DenseMatrixBase< T > &M2)=0
Performs the operation: (*this) <- M2 * (*this)
Defines an abstract dense vector base class for use in Finite Element-type computations.
Defines a dense vector for use in Finite Element-type computations.
The libMesh namespace provides an interface to certain functionality in the library.
OStreamProxy out
IntRange< T > make_range(T beg, T end)
The 2-parameter make_range() helper function returns an IntRange<T> when both input parameters are of...
Definition int_range.h:176