mirror of
https://gitlab.com/libeigen/eigen.git
synced 2025-09-12 09:23:12 +08:00
generateTestMatrix can use processTriangularMatrix
This commit is contained in:
parent
b8f0364a1c
commit
9be658f701
@ -10,27 +10,48 @@
|
|||||||
#include "main.h"
|
#include "main.h"
|
||||||
#include <unsupported/Eigen/MatrixFunctions>
|
#include <unsupported/Eigen/MatrixFunctions>
|
||||||
|
|
||||||
|
// For complex matrices, any matrix is fine.
|
||||||
|
template<typename MatrixType, int IsComplex = NumTraits<typename internal::traits<MatrixType>::Scalar>::IsComplex>
|
||||||
|
struct processTriangularMatrix
|
||||||
|
{
|
||||||
|
static void run(MatrixType&, MatrixType&, const MatrixType&)
|
||||||
|
{ }
|
||||||
|
};
|
||||||
|
|
||||||
|
// For real matrices, make sure none of the eigenvalues are negative.
|
||||||
|
template<typename MatrixType>
|
||||||
|
struct processTriangularMatrix<MatrixType,0>
|
||||||
|
{
|
||||||
|
static void run(MatrixType& m, MatrixType& T, const MatrixType& U)
|
||||||
|
{
|
||||||
|
typedef typename MatrixType::Index Index;
|
||||||
|
const Index size = m.cols();
|
||||||
|
|
||||||
|
for (Index i=0; i < size; ++i) {
|
||||||
|
if (i == size - 1 || T.coeff(i+1,i) == 0)
|
||||||
|
T.coeffRef(i,i) = std::abs(T.coeff(i,i));
|
||||||
|
else
|
||||||
|
++i;
|
||||||
|
}
|
||||||
|
m = U * T * U.transpose();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
template <typename MatrixType, int IsComplex = NumTraits<typename internal::traits<MatrixType>::Scalar>::IsComplex>
|
template <typename MatrixType, int IsComplex = NumTraits<typename internal::traits<MatrixType>::Scalar>::IsComplex>
|
||||||
struct generateTestMatrix;
|
struct generateTestMatrix;
|
||||||
|
|
||||||
// for real matrices, make sure none of the eigenvalues are negative
|
|
||||||
template <typename MatrixType>
|
template <typename MatrixType>
|
||||||
struct generateTestMatrix<MatrixType,0>
|
struct generateTestMatrix<MatrixType,0>
|
||||||
{
|
{
|
||||||
static void run(MatrixType& result, typename MatrixType::Index size)
|
static void run(MatrixType& result, typename MatrixType::Index size)
|
||||||
{
|
{
|
||||||
MatrixType mat = MatrixType::Random(size, size);
|
result = MatrixType::Random(size, size);
|
||||||
EigenSolver<MatrixType> es(mat);
|
RealSchur<MatrixType> schur(result);
|
||||||
typename EigenSolver<MatrixType>::EigenvalueType eivals = es.eigenvalues();
|
MatrixType T = schur.matrixT();
|
||||||
for (typename MatrixType::Index i = 0; i < size; ++i) {
|
processTriangularMatrix<MatrixType>::run(result, T, schur.matrixU());
|
||||||
if (eivals(i).imag() == 0 && eivals(i).real() < 0)
|
|
||||||
eivals(i) = -eivals(i);
|
|
||||||
}
|
|
||||||
result = (es.eigenvectors() * eivals.asDiagonal() * es.eigenvectors().inverse()).real();
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// for complex matrices, any matrix is fine
|
|
||||||
template <typename MatrixType>
|
template <typename MatrixType>
|
||||||
struct generateTestMatrix<MatrixType,1>
|
struct generateTestMatrix<MatrixType,1>
|
||||||
{
|
{
|
||||||
|
@ -96,33 +96,6 @@ void testGeneral(const MatrixType& m, double tol)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// For complex matrices, any matrix is fine.
|
|
||||||
template<typename MatrixType, int IsComplex = NumTraits<typename internal::traits<MatrixType>::Scalar>::IsComplex>
|
|
||||||
struct processTriangularMatrix
|
|
||||||
{
|
|
||||||
static void run(MatrixType&, MatrixType&, const MatrixType&)
|
|
||||||
{ }
|
|
||||||
};
|
|
||||||
|
|
||||||
// For real matrices, make sure none of the eigenvalues are negative.
|
|
||||||
template<typename MatrixType>
|
|
||||||
struct processTriangularMatrix<MatrixType,0>
|
|
||||||
{
|
|
||||||
static void run(MatrixType& m, MatrixType& T, const MatrixType& U)
|
|
||||||
{
|
|
||||||
typedef typename MatrixType::Index Index;
|
|
||||||
const Index size = m.cols();
|
|
||||||
|
|
||||||
for (Index i=0; i < size; ++i) {
|
|
||||||
if (i == size - 1 || T.coeff(i+1,i) == 0)
|
|
||||||
T.coeffRef(i,i) = std::abs(T.coeff(i,i));
|
|
||||||
else
|
|
||||||
++i;
|
|
||||||
}
|
|
||||||
m = U * T * U.adjoint();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename MatrixType>
|
template<typename MatrixType>
|
||||||
void testSingular(MatrixType m, double tol)
|
void testSingular(MatrixType m, double tol)
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user