libMesh
dense_matrix_base.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2024 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 
30 namespace libMesh
31 {
32 
33 // Forward declarations
34 template <typename T> class DenseVectorBase;
35 template <typename T> class DenseVector;
36 
45 template<typename T>
47 {
48 
49 protected:
54  DenseMatrixBase(const unsigned int new_m=0,
55  const unsigned int new_n=0) : _m(new_m), _n(new_n) {}
56 
57 public:
62  DenseMatrixBase (DenseMatrixBase &&) = default;
63  DenseMatrixBase (const DenseMatrixBase &) = default;
64  DenseMatrixBase & operator= (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 boostcopy::enable_if_c<
140  ScalarTraits<T2>::value, void >::type
141  add (const T2 factor,
142  const DenseMatrixBase<T3> & mat);
143 
147  DenseVector<T> diagonal() const;
148 
149 protected:
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 
190 template<typename T>
191 template<typename T2, typename T3>
192 inline
193 typename boostcopy::enable_if_c<
194  ScalarTraits<T2>::value, void >::type
195 DenseMatrixBase<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
DenseVector< T > diagonal() const
Return the matrix diagonal.
unsigned int m() const
The libMesh namespace provides an interface to certain functionality in the library.
void print(std::ostream &os=libMesh::out) const
Pretty-print the matrix, by default to libMesh::out.
virtual T el(const unsigned int i, const unsigned int j) const =0
virtual void left_multiply(const DenseMatrixBase< T > &M2)=0
Performs the operation: (*this) <- M2 * (*this)
DenseMatrixBase(const unsigned int new_m=0, const unsigned int new_n=0)
Constructor.
DenseMatrixBase & operator=(const DenseMatrixBase &)=default
unsigned int _n
The column dimension.
virtual void right_multiply(const DenseMatrixBase< T > &M3)=0
Performs the operation: (*this) <- (*this) * M3.
void print_scientific(std::ostream &os, unsigned precision=8) const
Prints the matrix entries with more decimal places in scientific notation.
boostcopy::enable_if_c< ScalarTraits< T2 >::value, void >::type add(const T2 factor, const DenseMatrixBase< T3 > &mat)
Adds factor to every element in the matrix.
virtual void zero()=0
Set every element in the matrix to 0.
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:134
Defines an abstract dense vector base class for use in Finite Element-type computations.
Definition: dof_map.h:63
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)...
Defines a dense vector for use in Finite Element-type computations.
Defines an abstract dense matrix base class for use in Finite Element-type computations.
unsigned int n() const
virtual ~DenseMatrixBase()=default
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.
unsigned int _m
The row dimension.