QRMatrixI.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2016 OpenFOAM Foundation
9  Copyright (C) 2019 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
30 
31 template<class MatrixType>
33 (
34  MatrixType& A,
35  const RMatrix& reflector,
36  const label k
37 )
38 {
39  applyLeftReflector(A, reflector, k, k);
40 
41  if (outputType_ == outputTypes::FULL_QR)
42  {
43  applyRightReflector(Q_, reflector, k);
44  }
45 }
46 
47 
48 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
49 template<class MatrixType>
52 (
53  RMatrix u
54 )
55 {
56  #ifdef FULLDEBUG
57  // Check if the given RectangularMatrix is effectively a column vector
58  if (u.n() != 1)
59  {
61  << "Input matrix is not a column vector." << exit(FatalError);
62  }
63  #endif
64 
65  scalar magnitude(mag(u(0,0)));
66  if (magnitude < VSMALL)
67  {
68  magnitude = SMALL;
69  #if FULLDEBUG
71  << "Almost zero leading elem in Householder matrix."
72  << abort(FatalError);
73  #endif
74  }
75 
76  u(0,0) += u(0,0)/magnitude*u.columnNorm(0);
77 
78  scalar colNorm(u.columnNorm(0));
79  if (colNorm < VSMALL)
80  {
81  colNorm = SMALL;
82  #if FULLDEBUG
84  << "Almost zero norm in the Householder matrix."
85  << abort(FatalError);
86  #endif
87  }
88 
89  u /= cmptType(colNorm);
90 
91  return u;
92 }
93 
94 
95 template<class MatrixType>
97 (
98  MatrixType& A,
99  const RMatrix& reflector,
100  const label k,
101  const label k1
102 )
103 {
104  // const RMatrix& A0(A.subMatrix(k1, k));
105  // A0 -= (cmptType(2)*reflector)*(reflector & A0);
106 
107  for (label j = k; j < A.n(); ++j)
108  {
109  cmptType sum = Zero;
110  for (label i = 0; i < reflector.m(); ++i)
111  {
112  sum += Detail::conj(reflector(i, 0))*A(i + k1, j);
113  }
114 
115  sum *= cmptType(2);
116  for (label i = 0; i < reflector.m(); ++i)
117  {
118  A(i + k1, j) -= reflector(i, 0)*sum;
119  }
120  }
121 }
122 
123 
124 template<class MatrixType>
126 (
127  MatrixType& A,
128  const RMatrix& reflector,
129  const label k
130 )
131 {
132  // const RMatrix A0(A.subMatrix(0, k));
133  // A0 -= ((A0*reflector)^(cmptType(2)*reflector))
134 
135  for (label i = 0; i < A.m(); ++i)
136  {
137  cmptType sum = Zero;
138  for (label j = 0; j < reflector.m(); ++j)
139  {
140  sum += A(i, j + k)*reflector(j, 0);
141  }
142 
143  sum *= cmptType(2);
144  for (label j = 0; j < reflector.m(); ++j)
145  {
146  A(i, j + k) -= Detail::conj(reflector(j, 0))*sum;
147  }
148  }
149 }
150 
151 
152 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
153 
154 template<class MatrixType>
155 inline const MatrixType& Foam::QRMatrix<MatrixType>::Q() const
156 {
157  const_cast<MatrixType&>(Q_).round();
158 
159  return Q_;
160 }
161 
162 
163 template<class MatrixType>
164 inline const MatrixType& Foam::QRMatrix<MatrixType>::R() const
165 {
166  return R_;
167 }
168 
169 
170 template<class MatrixType>
172 {
173  return P_;
174 }
175 
176 
177 template<class MatrixType>
180 {
181  SquareMatrix<cmptType> permMat(P_.size(), Zero);
182 
183  forAll(P_, jcol)
184  {
185  permMat(P_[jcol], jcol) = pTraits<cmptType>::one;
186  }
187 
188  return permMat;
189 }
190 
191 
192 // ************************************************************************* //
Foam::QRMatrix::applyLeftReflector
void applyLeftReflector(MatrixType &A, const RMatrix &reflector, const label k=0, const label k1=0)
Apply (in-place) Householder reflectors from the left side: u*A.
Definition: QRMatrixI.H:97
Foam::Matrix::n
label n() const noexcept
The number of columns.
Definition: MatrixI.H:103
Foam::Zero
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
Foam::QRMatrix::householderReflector
RMatrix householderReflector(RMatrix u)
Compute Householder reflector on a given matrix column, u.
Definition: QRMatrixI.H:52
Foam::QRMatrix::cmptType
MatrixType::cmptType cmptType
Definition: QRMatrix.H:157
A
static const Foam::dimensionedScalar A("", Foam::dimPressure, 611.21)
Foam::QRMatrix::Q
const MatrixType & Q() const
Return the unitary similarity matrix.
Definition: QRMatrixI.H:155
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
Foam::QRMatrix
QRMatrix (i.e. QR decomposition, QR factorisation or orthogonal-triangular decomposition) decomposes ...
Definition: QRMatrix.H:152
Foam::QRMatrix::applyRightReflector
void applyRightReflector(MatrixType &A, const RMatrix &reflector, const label k=0)
Apply (in-place) Householder reflectors from the right side: (u*A)*u.
Definition: QRMatrixI.H:126
Foam::Matrix::columnNorm
scalar columnNorm(const label colIndex, const bool noSqrt=false) const
Return L2-Norm of chosen column.
Definition: Matrix.C:463
Foam::RectangularMatrix
A templated (M x N) rectangular matrix of objects of <Type>, containing M*N elements,...
Definition: RectangularMatrix.H:57
Foam::QRMatrix::R
const MatrixType & R() const
Return the upper triangular matrix.
Definition: QRMatrixI.H:164
Foam::FatalError
error FatalError
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:144
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::SquareMatrix
A templated (N x N) square matrix of objects of <Type>, containing N*N elements, derived from Matrix.
Definition: SquareMatrix.H:63
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Foam::Matrix::m
label m() const noexcept
The number of rows.
Definition: MatrixI.H:96
Foam::Detail::conj
std::enable_if< !std::is_same< complex, T >::value, const T & >::type conj(const T &val)
Definition: complex.H:339
Foam::List< label >
Foam::pTraits
A traits class, which is primarily used for primitives.
Definition: pTraits.H:56
Foam::mag
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
Foam::QRMatrix::orderP
const labelList & orderP() const
Return the permutation order (P) as a list.
Definition: QRMatrixI.H:171
k
label k
Boltzmann constant.
Definition: LISASMDCalcMethod2.H:41
Foam::sum
dimensioned< Type > sum(const DimensionedField< Type, GeoMesh > &df)
Definition: DimensionedFieldFunctions.C:327
Foam::QRMatrix::P
SMatrix P() const
Create and return the permutation matrix.
Definition: QRMatrixI.H:179