From 75e60121f4116d8fd71916d13a5b2a39322b679c Mon Sep 17 00:00:00 2001 From: Benoit Jacob Date: Tue, 12 Oct 2010 09:12:36 -0400 Subject: add Jacobi unit test. jacobi_5 fails, exposing bug #39. --- test/jacobi.cpp | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 test/jacobi.cpp (limited to 'test/jacobi.cpp') diff --git a/test/jacobi.cpp b/test/jacobi.cpp new file mode 100644 index 000000000..eb839fa77 --- /dev/null +++ b/test/jacobi.cpp @@ -0,0 +1,93 @@ +// This file is part of Eigen, a lightweight C++ template library +// for linear algebra. +// +// Copyright (C) 2008 Gael Guennebaud +// Copyright (C) 2009 Benoit Jacob +// +// Eigen is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 3 of the License, or (at your option) any later version. +// +// Alternatively, 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 2 of +// the License, or (at your option) any later version. +// +// Eigen 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 Lesser General Public License or the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License and a copy of the GNU General Public License along with +// Eigen. If not, see . + +#include "main.h" +#include + +template +void jacobi(const MatrixType& m = MatrixType()) +{ + typedef typename MatrixType::Scalar Scalar; + typedef typename MatrixType::Index Index; + Index rows = m.rows(); + Index cols = m.cols(); + + enum { + RowsAtCompileTime = MatrixType::RowsAtCompileTime, + ColsAtCompileTime = MatrixType::ColsAtCompileTime + }; + + typedef Matrix JacobiVector; + + const MatrixType a(MatrixType::Random(rows, cols)); + + JacobiVector v = JacobiVector::Random().normalized(); + JacobiScalar c = v.x(), s = v.y(); + PlanarRotation rot(c, s); + + { + Index p = ei_random(0, rows-1); + Index q; + do { + q = ei_random(0, rows-1); + } while (q == p); + + MatrixType b = a; + b.applyOnTheLeft(p, q, rot); + VERIFY_IS_APPROX(b.row(p), c * a.row(p) + ei_conj(s) * a.row(q)); + VERIFY_IS_APPROX(b.row(q), -s * a.row(p) + ei_conj(c) * a.row(q)); + } + + { + Index p = ei_random(0, cols-1); + Index q; + do { + q = ei_random(0, cols-1); + } while (q == p); + + MatrixType b = a; + b.applyOnTheRight(p, q, rot); + VERIFY_IS_APPROX(b.col(p), c * a.col(p) - s * a.col(q)); + VERIFY_IS_APPROX(b.col(q), ei_conj(s) * a.col(p) + ei_conj(c) * a.col(q)); + } +} + +void test_jacobi() +{ + for(int i = 0; i < g_repeat; i++) { + CALL_SUBTEST_1(( jacobi() )); + CALL_SUBTEST_2(( jacobi() )); + CALL_SUBTEST_3(( jacobi() )); + CALL_SUBTEST_3(( jacobi >() )); + + int r = ei_random(2, 20), + c = ei_random(2, 20); + CALL_SUBTEST_4(( jacobi(MatrixXf(r,c)) )); + CALL_SUBTEST_5(( jacobi(MatrixXcd(r,c)) )); + CALL_SUBTEST_5(( jacobi >(MatrixXcd(r,c)) )); + (void) r; + (void) c; + } +} -- cgit v1.2.3