From e9199c6e14ddef7b19d0d791b3986701ecba86f9 Mon Sep 17 00:00:00 2001 From: Henry Weller Date: Thu, 24 Mar 2016 14:49:25 +0000 Subject: [PATCH] QRMatrix: New class to provide QR-decomposition by Householder reflection This development is sponsored by Carnegie Wave Energy Ltd. --- applications/test/Matrix/Test-Matrix.C | 27 +++ src/OpenFOAM/matrices/QRMatrix/QRMatrix.C | 249 +++++++++++++++++++++ src/OpenFOAM/matrices/QRMatrix/QRMatrix.H | 133 +++++++++++ src/OpenFOAM/matrices/QRMatrix/QRMatrixI.H | 51 +++++ 4 files changed, 460 insertions(+) create mode 100644 src/OpenFOAM/matrices/QRMatrix/QRMatrix.C create mode 100644 src/OpenFOAM/matrices/QRMatrix/QRMatrix.H create mode 100644 src/OpenFOAM/matrices/QRMatrix/QRMatrixI.H diff --git a/applications/test/Matrix/Test-Matrix.C b/applications/test/Matrix/Test-Matrix.C index c8691e81b5..87a39386fc 100644 --- a/applications/test/Matrix/Test-Matrix.C +++ b/applications/test/Matrix/Test-Matrix.C @@ -25,6 +25,7 @@ License #include "scalarMatrices.H" #include "LLTMatrix.H" +#include "QRMatrix.H" #include "vector.H" #include "tensor.H" #include "IFstream.H" @@ -161,6 +162,32 @@ int main(int argc, char *argv[]) Info<< "LLT solve residual " << (squareMatrix*x - source) << endl; } + { + scalarSquareMatrix squareMatrix(3, Zero); + + squareMatrix(0, 0) = 4; + squareMatrix(0, 1) = 12; + squareMatrix(0, 2) = -16; + squareMatrix(1, 0) = 12; + squareMatrix(1, 1) = 37; + squareMatrix(1, 2) = -43; + squareMatrix(2, 0) = -16; + squareMatrix(2, 1) = -43; + squareMatrix(2, 2) = 98; + + scalarField source(3, 1); + + QRMatrix QR(squareMatrix); + scalarField x(QR.solve(source)); + + Info<< "QR solve residual " + << (squareMatrix*x - source) << endl; + + Info<< "QR inverse solve residual " + << (x - QR.inverse()*source) << endl; + + } + Info<< "\nEnd\n" << endl; return 0; diff --git a/src/OpenFOAM/matrices/QRMatrix/QRMatrix.C b/src/OpenFOAM/matrices/QRMatrix/QRMatrix.C new file mode 100644 index 0000000000..80e33b0fb2 --- /dev/null +++ b/src/OpenFOAM/matrices/QRMatrix/QRMatrix.C @@ -0,0 +1,249 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2016 OpenFOAM Foundation + \\/ M anipulation | +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +\*---------------------------------------------------------------------------*/ + +#include "QRMatrix.H" + +// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // + +template +Foam::QRMatrix::QRMatrix(const MatrixType& M) +{ + decompose(M); +} + + +// * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * // + +template +void Foam::QRMatrix::decompose(const MatrixType& M) +{ + const label m = M.m(); + const label n = M.n(); + + // Initialize the R-matrix to M + R_ = M; + + // Initialize the Q-matrix to I + Q_.setSize(m); + Q_ = I; + + // Pre-allocate temporary storage for the Householder steps + QMatrixType Qk(m); + QMatrixType Rk(m); + Field uk(m); + + for (label k=0; k 0) + { + alpha = -alpha; + } + + // uk = column k of Rk - alpha*ek + // rSumSqrUk = 2/sum(sqr(uk)) + uk[k] = R_(k, k) - alpha; + cmptType rSumSqrUk = sqr(uk[k]); + for (label bi=k+1; bi +void Foam::QRMatrix::solvex +( + Field& x +) const +{ + const label n = R_.n(); + + for (int i=n-1; i>=0; i--) + { + cmptType sum = x[i]; + + for (label j=i+1; j +void Foam::QRMatrix::solve +( + Field& x, + const Field& source +) const +{ + const label m = Q_.m(); + + // x = Q_.T()*source; + for (label i=0; i +Foam::tmp> +Foam::QRMatrix::solve +( + const Field& source +) const +{ + tmp> tx(new Field(Q_.m())); + Field& x = tx.ref(); + + solve(x, source); + + return tx; +} + + +template +typename Foam::QRMatrix::QMatrixType +Foam::QRMatrix::inverse() const +{ + const label m = Q_.m(); + + Field x(m); + QMatrixType inv(m); + + for (label i=0; i. + +Class + Foam::QRMatrix + +Description + Class templated on matrix type to perform the QR decomposition using + Householder reflections on a square or rectangular matrix. + +SourceFiles + QRMatrixI.H + QRMatrix.C + +\*---------------------------------------------------------------------------*/ + +#ifndef QRMatrix_H +#define QRMatrix_H + +#include "SquareMatrix.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ + +/*---------------------------------------------------------------------------*\ + Class QRMatrix Declaration +\*---------------------------------------------------------------------------*/ + +template +class QRMatrix +{ + +public: + + typedef typename MatrixType::cmptType cmptType; + typedef SquareMatrix QMatrixType; + typedef MatrixType RMatrixType; + +private: + + // Private data + + //- The Q-matrix + QMatrixType Q_; + + //- The R-matrix + RMatrixType R_; + + + // Private member functions + + //- Solve the linear system with the Field argument x initialized to + // the appropriate transformed source (e.g. Q.T()*source) + // and return the solution in x + void solvex(Field& x) const; + + +public: + + // Constructors + + //- Construct null + inline QRMatrix(); + + //- Construct decomposing given matrix + QRMatrix(const MatrixType& M); + + + // Member Functions + + //- Return Q-matrix + inline const QMatrixType& Q() const; + + //- Return R-matrix + inline const RMatrixType& R() const; + + //- Decompose given matrix + void decompose(const MatrixType& M); + + //- Solve the linear system with the given source + // and returning the solution in the Field argument x + void solve(Field& x, const Field& source) const; + + //- Solve the linear system with the given source + // returning the solution + tmp> solve(const Field& source) const; + + //- Return the inverse of a square matrix + QMatrixType inverse() const; +}; + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +} // End namespace Foam + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#include "QRMatrixI.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#ifdef NoRepository + #include "QRMatrix.C" +#endif + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* // diff --git a/src/OpenFOAM/matrices/QRMatrix/QRMatrixI.H b/src/OpenFOAM/matrices/QRMatrix/QRMatrixI.H new file mode 100644 index 0000000000..1d9ca4b620 --- /dev/null +++ b/src/OpenFOAM/matrices/QRMatrix/QRMatrixI.H @@ -0,0 +1,51 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2016 OpenFOAM Foundation + \\/ M anipulation | +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +\*---------------------------------------------------------------------------*/ + +// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // + +template +inline Foam::QRMatrix::QRMatrix() +{} + + +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // + +template +inline const typename Foam::QRMatrix::QMatrixType& +Foam::QRMatrix::Q() const +{ + return Q_; +} + + +template +inline const typename Foam::QRMatrix::RMatrixType& +Foam::QRMatrix::R() const +{ + return R_; +} + + +// ************************************************************************* //