libMesh
dense_matrix_base_impl.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2025 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_IMPL_H
21 #define LIBMESH_DENSE_MATRIX_BASE_IMPL_H
22 
23 // Local Includes
24 #include "libmesh/dense_matrix.h"
25 #include "libmesh/dense_vector_base.h"
26 #include "libmesh/dense_vector.h"
27 #include "libmesh/int_range.h"
28 
29 // C++ includes
30 #include <iomanip> // for std::setw()
31 
32 namespace libMesh
33 {
34 
35  template <typename T>
36  DenseVector<T>
38  {
39  DenseVector<T> ret(_m);
40  for (decltype(_m) i = 0; i < _m; ++i)
41  ret(i) = el(i, i);
42  return ret;
43  }
44 
45  template<typename T>
47  const DenseMatrixBase<T> & M2,
48  const DenseMatrixBase<T> & M3)
49  {
50  // Assertions to make sure we have been
51  // passed matrices of the correct dimension.
52  libmesh_assert_equal_to (M1.m(), M2.m());
53  libmesh_assert_equal_to (M1.n(), M3.n());
54  libmesh_assert_equal_to (M2.n(), M3.m());
55 
56  const unsigned int m_s = M2.m();
57  const unsigned int p_s = M2.n();
58  const unsigned int n_s = M1.n();
59 
60  // Do it this way because there is a
61  // decent chance (at least for constraint matrices)
62  // that M3(k,j) = 0. when right-multiplying.
63  for (unsigned int k=0; k<p_s; k++)
64  for (unsigned int j=0; j<n_s; j++)
65  if (M3.el(k,j) != 0.)
66  for (unsigned int i=0; i<m_s; i++)
67  M1.el(i,j) += M2.el(i,k) * M3.el(k,j);
68  }
69 
70 
71 
72  template<typename T>
73  void DenseMatrixBase<T>::condense(const unsigned int iv,
74  const unsigned int jv,
75  const T val,
76  DenseVectorBase<T> & rhs)
77  {
78  libmesh_assert_equal_to (this->_m, rhs.size());
79  libmesh_assert_equal_to (iv, jv);
80 
81 
82  // move the known value into the RHS
83  // and zero the column
84  for (auto i : make_range(this->m()))
85  {
86  rhs.el(i) -= this->el(i,jv)*val;
87  this->el(i,jv) = 0.;
88  }
89 
90  // zero the row
91  for (auto j : make_range(this->n()))
92  this->el(iv,j) = 0.;
93 
94  this->el(iv,jv) = 1.;
95  rhs.el(iv) = val;
96 
97  }
98 
99 
100  template<typename T>
101  void DenseMatrixBase<T>::print_scientific (std::ostream & os, unsigned precision) const
102  {
103  // save the initial format flags
104  std::ios_base::fmtflags os_flags = os.flags();
105 
106  // Print the matrix entries.
107  for (auto i : make_range(this->m()))
108  {
109  for (auto j : make_range(this->n()))
110  os << std::setw(15)
111  << std::scientific
112  << std::setprecision(precision)
113  << this->el(i,j) << " ";
114 
115  os << std::endl;
116  }
117 
118  // reset the original format flags
119  os.flags(os_flags);
120  }
121 
122 
123 
124  template<typename T>
125  void DenseMatrixBase<T>::print (std::ostream & os) const
126  {
127  for (auto i : make_range(this->m()))
128  {
129  for (auto j : make_range(this->n()))
130  os << std::setw(8)
131  << this->el(i,j) << " ";
132 
133  os << std::endl;
134  }
135 
136  return;
137  }
138 
139 
140 
141 }
142 
143 #endif // LIBMESH_DENSE_MATRIX_BASE_IMPL_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 unsigned int size() const =0
void print_scientific(std::ostream &os, unsigned precision=8) const
Prints the matrix entries with more decimal places in scientific notation.
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:140
Defines an abstract dense vector base class for use in Finite Element-type computations.
Definition: dof_map.h:73
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.
Definition: dof_map.h:74
Defines an abstract dense matrix base class for use in Finite Element-type computations.
unsigned int n() const
virtual T el(const unsigned int i) const =0
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.