Commit f5bfc5d2 authored by laurence's avatar laurence
Browse files

ENH: scalarMatrices: Add determinant calculation

parent c574abb2
......@@ -116,17 +116,19 @@ int main(int argc, char *argv[])
squareMatrix[2][1] = -43;
squareMatrix[2][2] = 98;
const scalarSquareMatrix squareMatrixCopy = squareMatrix;
Info<< nl << "Square Matrix = " << squareMatrix << endl;
scalarDiagonalMatrix rhs(3, 0);
rhs[0] = 1;
rhs[1] = 2;
rhs[2] = 3;
Info<< "det = " << det(squareMatrixCopy) << endl;
LUsolve(squareMatrix, rhs);
labelList rhs(3, 0);
label sign;
LUDecompose(squareMatrix, rhs, sign);
Info<< "Decomposition = " << squareMatrix << endl;
Info<< "Solution = " << rhs << endl;
Info<< "Pivots = " << rhs << endl;
Info<< "Sign = " << sign << endl;
Info<< "det = " << detDecomposed(squareMatrix, sign) << endl;
}
Info<< "\nEnd\n" << endl;
......
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2013 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 <http://www.gnu.org/licenses/>.
\*---------------------------------------------------------------------------*/
#include "SquareMatrix.H"
#include "labelList.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
template<class Type>
Foam::scalar Foam::detDecomposed
(
const SquareMatrix<Type>& matrix,
const label sign
)
{
scalar diagProduct = 1.0;
for (label i = 0; i < matrix.n(); ++i)
{
diagProduct *= matrix[i][i];
}
return sign*diagProduct;
}
template<class Type>
Foam::scalar Foam::det(const SquareMatrix<Type>& matrix)
{
SquareMatrix<Type> matrixTmp = matrix;
labelList pivotIndices(matrix.n());
label sign;
LUDecompose(matrixTmp, pivotIndices, sign);
return detDecomposed(matrixTmp, sign);
}
template<class Type>
Foam::scalar Foam::det(SquareMatrix<Type>& matrix)
{
labelList pivotIndices(matrix.n());
label sign;
LUDecompose(matrix, pivotIndices, sign);
return detDecomposed(matrix, sign);
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
\\ / A nd | Copyright (C) 2011-2013 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
......@@ -81,6 +81,21 @@ public:
};
// Global functions
//- Return the LU decomposed SquareMatrix det
template<class Type>
scalar detDecomposed(const SquareMatrix<Type>&, const label sign);
//- Return the SquareMatrix det
template<class Type>
scalar det(const SquareMatrix<Type>&);
//- Return the SquareMatrix det and the LU decomposition in the original matrix
template<class Type>
scalar det(SquareMatrix<Type>&);
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
......@@ -91,6 +106,12 @@ public:
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#ifdef NoRepository
# include "SquareMatrix.C"
#endif
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
// ************************************************************************* //
......@@ -33,9 +33,22 @@ void Foam::LUDecompose
scalarSquareMatrix& matrix,
labelList& pivotIndices
)
{
label sign;
LUDecompose(matrix, pivotIndices, sign);
}
void Foam::LUDecompose
(
scalarSquareMatrix& matrix,
labelList& pivotIndices,
label& sign
)
{
label n = matrix.n();
scalar vv[n];
sign = 1;
for (register label i=0; i<n; i++)
{
......@@ -113,6 +126,7 @@ void Foam::LUDecompose
Swap(matrixj[k], matrixiMax[k]);
}
sign *= -1;
vv[iMax] = vv[j];
}
......
......@@ -79,6 +79,15 @@ void LUDecompose
labelList& pivotIndices
);
//- LU decompose the matrix with pivoting.
// sign is -1 for odd number of row interchanges and 1 for even number.
void LUDecompose
(
scalarSquareMatrix& matrix,
labelList& pivotIndices,
label& sign
);
//- LU decompose the matrix into a lower (L) and upper (U) part. U = L.T()
void LUDecompose(scalarSymmetricSquareMatrix& matrix);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment