libMesh
dense_matrix_base.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2019 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 : IntRange<unsigned int>(0, this->n()))
202  for (auto i : IntRange<unsigned int>(0, 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
libMesh::DenseMatrixBase::operator<<
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;.
Definition: dense_matrix_base.h:121
libMesh::DenseMatrixBase::diagonal
DenseVector< T > diagonal() const
Return the matrix diagonal.
Definition: dense_matrix_base_impl.h:37
libMesh::DenseMatrixBase::print_scientific
void print_scientific(std::ostream &os, unsigned precision=8) const
Prints the matrix entries with more decimal places in scientific notation.
Definition: dense_matrix_base_impl.h:101
libMesh::DenseMatrixBase::multiply
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)
Definition: dense_matrix_base_impl.h:46
libMesh
The libMesh namespace provides an interface to certain functionality in the library.
Definition: factoryfunction.C:55
libMesh::DenseMatrixBase::operator=
DenseMatrixBase & operator=(const DenseMatrixBase &)=default
libMesh::DenseMatrixBase::left_multiply
virtual void left_multiply(const DenseMatrixBase< T > &M2)=0
Performs the operation: (*this) <- M2 * (*this)
libMesh::DenseVectorBase
Defines an abstract dense vector base class for use in Finite Element-type computations.
Definition: dof_map.h:71
libMesh::DenseMatrixBase::n
unsigned int n() const
Definition: dense_matrix_base.h:109
libMesh::boostcopy::enable_if_c
Definition: compare_types.h:38
libMesh::DenseMatrixBase::condense
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.
Definition: dense_matrix_base_impl.h:73
libMesh::DenseMatrixBase::m
unsigned int m() const
Definition: dense_matrix_base.h:104
libMesh::DenseMatrixBase::add
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.
Definition: dense_matrix_base.h:195
libMesh::DenseMatrixBase::right_multiply
virtual void right_multiply(const DenseMatrixBase< T > &M3)=0
Performs the operation: (*this) <- (*this) * M3.
libMesh::IntRange
The IntRange templated class is intended to make it easy to loop over integers which are indices of a...
Definition: int_range.h:53
libMesh::DenseMatrixBase
Defines an abstract dense matrix base class for use in Finite Element-type computations.
Definition: dense_matrix_base.h:46
libMesh::DenseMatrixBase::~DenseMatrixBase
virtual ~DenseMatrixBase()=default
libMesh::ScalarTraits
Definition: compare_types.h:63
libMesh::DenseMatrixBase::_n
unsigned int _n
The column dimension.
Definition: dense_matrix_base.h:181
libMesh::DenseMatrixBase::print
void print(std::ostream &os=libMesh::out) const
Pretty-print the matrix, by default to libMesh::out.
Definition: dense_matrix_base_impl.h:125
libMesh::DenseMatrixBase::zero
virtual void zero()=0
Set every element in the matrix to 0.
libMesh::DenseMatrixBase::DenseMatrixBase
DenseMatrixBase(const unsigned int new_m=0, const unsigned int new_n=0)
Constructor.
Definition: dense_matrix_base.h:54
libMesh::DenseMatrixBase::el
virtual T el(const unsigned int i, const unsigned int j) const =0
libMesh::out
OStreamProxy out
libMesh::DenseMatrixBase::_m
unsigned int _m
The row dimension.
Definition: dense_matrix_base.h:176
libMesh::DenseVector
Defines a dense vector for use in Finite Element-type computations.
Definition: meshless_interpolation_function.h:39