From e92fe88159a762710651f1a8f9a428bc572a27df Mon Sep 17 00:00:00 2001 From: Chen-Pang He Date: Sun, 30 Sep 2012 19:21:53 +0800 Subject: Add test for real MatrixPowerTriangular. --- unsupported/test/matrix_power.cpp | 56 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 53 insertions(+), 3 deletions(-) (limited to 'unsupported/test/matrix_power.cpp') diff --git a/unsupported/test/matrix_power.cpp b/unsupported/test/matrix_power.cpp index 95c63c574..b7b6423a8 100644 --- a/unsupported/test/matrix_power.cpp +++ b/unsupported/test/matrix_power.cpp @@ -9,6 +9,33 @@ #include "matrix_functions.h" +template ::IsComplex> +struct generateTriangularMatrix; + +// for real matrices, make sure none of the eigenvalues are negative +template +struct generateTriangularMatrix +{ + static void run(MatrixType& result, typename MatrixType::Index size) + { + result.resize(size, size); + result.template triangularView() = MatrixType::Random(size, size); + for (typename MatrixType::Index i = 0; i < size; ++i) + result.coeffRef(i,i) = std::abs(result.coeff(i,i)); + } +}; + +// for complex matrices, any matrix is fine +template +struct generateTriangularMatrix +{ + static void run(MatrixType& result, typename MatrixType::Index size) + { + result.resize(size, size); + result.template triangularView() = MatrixType::Random(size, size); + } +}; + template void test2dRotation(double tol) { @@ -59,7 +86,7 @@ void testExponentLaws(const MatrixType& m, double tol) MatrixType m1, m2, m3, m4, m5; RealScalar x, y; - for (int i=0; i::run(m1, m.rows()); MatrixPower mpow(m1); @@ -90,7 +117,7 @@ void testProduct(const MatrixType& m, const VectorType& v, double tol) VectorType v1, v2, v3; RealScalar p; - for (int i=0; i::run(m1, m.rows()); MatrixPower mpow(m1); @@ -99,7 +126,29 @@ void testProduct(const MatrixType& m, const VectorType& v, double tol) v2.noalias() = mpow(p) * v1; v3.noalias() = mpow(p).eval() * v1; - std::cout << "testMatrixVectorProduct: error powerm = " << relerr(v2, v3) << '\n'; + std::cout << "testProduct: error powerm = " << relerr(v2, v3) << '\n'; + VERIFY(v2.isApprox(v3, static_cast(tol))); + } +} + +template +void testTriangularProduct(const MatrixType& m, const VectorType& v, double tol) +{ + typedef typename MatrixType::RealScalar RealScalar; + MatrixType m1; + VectorType v1, v2, v3; + RealScalar p; + + for (int i=0; i < g_repeat; ++i) { + generateTriangularMatrix::run(m1, m.rows()); + MatrixPowerTriangular mpow(m1); + + v1 = VectorType::Random(v.rows(), v.cols()); + p = internal::random(); + + v2.noalias() = mpow(p) * v1; + v3.noalias() = mpow(p).eval() * v1; + std::cout << "testTriangularProduct: error powerm = " << relerr(v2, v3) << '\n'; VERIFY(v2.isApprox(v3, static_cast(tol))); } } @@ -109,6 +158,7 @@ void testMatrixVector(const MatrixType& m, const VectorType& v, double tol) { testExponentLaws(m,tol); testProduct(m,v,tol); + testTriangularProduct(m,v,tol); } void test_matrix_power() -- cgit v1.2.3