aboutsummaryrefslogtreecommitdiffhomepage
path: root/test
diff options
context:
space:
mode:
authorGravatar Benoit Jacob <jacob.benoit.1@gmail.com>2010-04-16 11:25:50 -0400
committerGravatar Benoit Jacob <jacob.benoit.1@gmail.com>2010-04-16 11:25:50 -0400
commit0ab431d7b860afc6766c7c20f7bb39a1d71bff62 (patch)
treef8da6ce3cc7738735f315f7954bbbabf48e0c621 /test
parentff6a46105d86e92753858c1b2aea8bcaf4575819 (diff)
parentea1a2df37092f88f5594dfea1f7e4996dd8e612d (diff)
* merge with mainline
* adapt Eigenvalues module to the new rule that the RowMajorBit must have the proper value for vectors * Fix RowMajorBit in ei_traits<ProductBase> * Fix vectorizability logic in CoeffBasedProduct
Diffstat (limited to 'test')
-rw-r--r--test/CMakeLists.txt4
-rw-r--r--test/eigensolver_complex.cpp1
-rw-r--r--test/gsl_helper.h22
-rw-r--r--test/hessenberg.cpp38
-rw-r--r--test/schur_complex.cpp67
-rw-r--r--test/schur_real.cpp85
-rw-r--r--test/sparse_vector.cpp4
7 files changed, 209 insertions, 12 deletions
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 072f63e1d..f7d5ed8e9 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -138,7 +138,9 @@ ei_add_test(qr)
ei_add_test(qr_colpivoting)
ei_add_test(qr_fullpivoting)
ei_add_test(upperbidiagonalization)
-ei_add_test(hessenberg " " "${GSL_LIBRARIES}")
+ei_add_test(hessenberg)
+ei_add_test(schur_real)
+ei_add_test(schur_complex)
ei_add_test(eigensolver_selfadjoint " " "${GSL_LIBRARIES}")
ei_add_test(eigensolver_generic " " "${GSL_LIBRARIES}")
ei_add_test(eigensolver_complex)
diff --git a/test/eigensolver_complex.cpp b/test/eigensolver_complex.cpp
index fa052a9ae..4e973c877 100644
--- a/test/eigensolver_complex.cpp
+++ b/test/eigensolver_complex.cpp
@@ -61,5 +61,6 @@ void test_eigensolver_complex()
CALL_SUBTEST_1( eigensolver(Matrix4cf()) );
CALL_SUBTEST_2( eigensolver(MatrixXcd(14,14)) );
CALL_SUBTEST_3( eigensolver(Matrix<std::complex<float>, 1, 1>()) );
+ CALL_SUBTEST_4( eigensolver(Matrix3f()) );
}
}
diff --git a/test/gsl_helper.h b/test/gsl_helper.h
index f8f7a5d79..eefffe792 100644
--- a/test/gsl_helper.h
+++ b/test/gsl_helper.h
@@ -33,6 +33,7 @@
#include <gsl/gsl_linalg.h>
#include <gsl/gsl_complex.h>
#include <gsl/gsl_complex_math.h>
+#include <gsl/gsl_poly.h>
namespace Eigen {
@@ -69,6 +70,27 @@ template<typename Scalar, bool IsComplex = NumTraits<Scalar>::IsComplex> struct
gsl_eigen_gensymmv_free(w);
free(a);
}
+
+ template<class EIGEN_VECTOR, class EIGEN_ROOTS>
+ static void eigen_poly_solve(const EIGEN_VECTOR& poly, EIGEN_ROOTS& roots )
+ {
+ const int deg = poly.size()-1;
+ double *z = new double[2*deg];
+ double *a = new double[poly.size()];
+ for( int i=0; i<poly.size(); ++i ){ a[i] = poly[i]; }
+
+ gsl_poly_complex_workspace * w = gsl_poly_complex_workspace_alloc (poly.size());
+ gsl_poly_complex_solve(a, poly.size(), w, z);
+ gsl_poly_complex_workspace_free (w);
+ for( int i=0; i<deg; ++i )
+ {
+ roots[i].real() = z[2*i];
+ roots[i].imag() = z[2*i+1];
+ }
+
+ delete[] a;
+ delete[] z;
+ }
};
template<typename Scalar> struct GslTraits<Scalar,true>
diff --git a/test/hessenberg.cpp b/test/hessenberg.cpp
index d917be357..cba9c8fda 100644
--- a/test/hessenberg.cpp
+++ b/test/hessenberg.cpp
@@ -2,6 +2,7 @@
// for linear algebra.
//
// Copyright (C) 2009 Gael Guennebaud <g.gael@free.fr>
+// Copyright (C) 2010 Jitse Niesen <jitse@maths.leeds.ac.uk>
//
// Eigen is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
@@ -28,19 +29,36 @@
template<typename Scalar,int Size> void hessenberg(int size = Size)
{
typedef Matrix<Scalar,Size,Size> MatrixType;
- MatrixType m = MatrixType::Random(size,size);
- HessenbergDecomposition<MatrixType> hess(m);
- VERIFY_IS_APPROX(m, hess.matrixQ() * hess.matrixH() * hess.matrixQ().adjoint());
+ // Test basic functionality: A = U H U* and H is Hessenberg
+ for(int counter = 0; counter < g_repeat; ++counter) {
+ MatrixType m = MatrixType::Random(size,size);
+ HessenbergDecomposition<MatrixType> hess(m);
+ VERIFY_IS_APPROX(m, hess.matrixQ() * hess.matrixH() * hess.matrixQ().adjoint());
+ MatrixType H = hess.matrixH();
+ for(int row = 2; row < size; ++row) {
+ for(int col = 0; col < row-1; ++col) {
+ VERIFY(H(row,col) == (typename MatrixType::Scalar)0);
+ }
+ }
+ }
+
+ // Test whether compute() and constructor returns same result
+ MatrixType A = MatrixType::Random(size, size);
+ HessenbergDecomposition<MatrixType> cs1;
+ cs1.compute(A);
+ HessenbergDecomposition<MatrixType> cs2(A);
+ VERIFY_IS_EQUAL(cs1.matrixQ(), cs2.matrixQ());
+ VERIFY_IS_EQUAL(cs1.matrixH(), cs2.matrixH());
+
+ // TODO: Add tests for packedMatrix() and householderCoefficients()
}
void test_hessenberg()
{
- for(int i = 0; i < g_repeat; i++) {
- CALL_SUBTEST_1(( hessenberg<std::complex<double>,1>() ));
- CALL_SUBTEST_2(( hessenberg<std::complex<double>,2>() ));
- CALL_SUBTEST_3(( hessenberg<std::complex<float>,4>() ));
- CALL_SUBTEST_4(( hessenberg<float,Dynamic>(ei_random<int>(1,320)) ));
- CALL_SUBTEST_5(( hessenberg<std::complex<double>,Dynamic>(ei_random<int>(1,320)) ));
- }
+ CALL_SUBTEST_1(( hessenberg<std::complex<double>,1>() ));
+ CALL_SUBTEST_2(( hessenberg<std::complex<double>,2>() ));
+ CALL_SUBTEST_3(( hessenberg<std::complex<float>,4>() ));
+ CALL_SUBTEST_4(( hessenberg<float,Dynamic>(ei_random<int>(1,320)) ));
+ CALL_SUBTEST_5(( hessenberg<std::complex<double>,Dynamic>(ei_random<int>(1,320)) ));
}
diff --git a/test/schur_complex.cpp b/test/schur_complex.cpp
new file mode 100644
index 000000000..3659a074c
--- /dev/null
+++ b/test/schur_complex.cpp
@@ -0,0 +1,67 @@
+// This file is part of Eigen, a lightweight C++ template library
+// for linear algebra. Eigen itself is part of the KDE project.
+//
+// Copyright (C) 2010 Jitse Niesen <jitse@maths.leeds.ac.uk>
+//
+// 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 <http://www.gnu.org/licenses/>.
+
+#include "main.h"
+#include <Eigen/Eigenvalues>
+
+template<typename MatrixType> void schur(int size = MatrixType::ColsAtCompileTime)
+{
+ typedef typename ComplexSchur<MatrixType>::ComplexScalar ComplexScalar;
+ typedef typename ComplexSchur<MatrixType>::ComplexMatrixType ComplexMatrixType;
+
+ // Test basic functionality: T is triangular and A = U T U*
+ for(int counter = 0; counter < g_repeat; ++counter) {
+ MatrixType A = MatrixType::Random(size, size);
+ ComplexSchur<MatrixType> schurOfA(A);
+ ComplexMatrixType U = schurOfA.matrixU();
+ ComplexMatrixType T = schurOfA.matrixT();
+ for(int row = 1; row < size; ++row) {
+ for(int col = 0; col < row; ++col) {
+ VERIFY(T(row,col) == (typename MatrixType::Scalar)0);
+ }
+ }
+ VERIFY_IS_APPROX(A.template cast<ComplexScalar>(), U * T * U.adjoint());
+ }
+
+ // Test asserts when not initialized
+ ComplexSchur<MatrixType> csUninitialized;
+ VERIFY_RAISES_ASSERT(csUninitialized.matrixT());
+ VERIFY_RAISES_ASSERT(csUninitialized.matrixU());
+
+ // Test whether compute() and constructor returns same result
+ MatrixType A = MatrixType::Random(size, size);
+ ComplexSchur<MatrixType> cs1;
+ cs1.compute(A);
+ ComplexSchur<MatrixType> cs2(A);
+ VERIFY_IS_EQUAL(cs1.matrixT(), cs2.matrixT());
+ VERIFY_IS_EQUAL(cs1.matrixU(), cs2.matrixU());
+}
+
+void test_schur_complex()
+{
+ CALL_SUBTEST_1(( schur<Matrix4cd>() ));
+ CALL_SUBTEST_2(( schur<MatrixXcf>(ei_random<int>(1,50)) ));
+ CALL_SUBTEST_3(( schur<Matrix<std::complex<float>, 1, 1> >() ));
+ CALL_SUBTEST_4(( schur<Matrix<float, 3, 3, Eigen::RowMajor> >() ));
+}
diff --git a/test/schur_real.cpp b/test/schur_real.cpp
new file mode 100644
index 000000000..c1747e2f5
--- /dev/null
+++ b/test/schur_real.cpp
@@ -0,0 +1,85 @@
+// This file is part of Eigen, a lightweight C++ template library
+// for linear algebra. Eigen itself is part of the KDE project.
+//
+// Copyright (C) 2010 Jitse Niesen <jitse@maths.leeds.ac.uk>
+//
+// 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 <http://www.gnu.org/licenses/>.
+
+#include "main.h"
+#include <Eigen/Eigenvalues>
+
+template<typename MatrixType> void verifyIsQuasiTriangular(const MatrixType& T)
+{
+ const int size = T.cols();
+ typedef typename MatrixType::Scalar Scalar;
+
+ // Check T is lower Hessenberg
+ for(int row = 2; row < size; ++row) {
+ for(int col = 0; col < row - 1; ++col) {
+ VERIFY(T(row,col) == Scalar(0));
+ }
+ }
+
+ // Check that any non-zero on the subdiagonal is followed by a zero and is
+ // part of a 2x2 diagonal block with imaginary eigenvalues.
+ for(int row = 1; row < size; ++row) {
+ if (T(row,row-1) != Scalar(0)) {
+ VERIFY(row == size-1 || T(row+1,row) == 0);
+ Scalar tr = T(row-1,row-1) + T(row,row);
+ Scalar det = T(row-1,row-1) * T(row,row) - T(row-1,row) * T(row,row-1);
+ VERIFY(4 * det > tr * tr);
+ }
+ }
+}
+
+template<typename MatrixType> void schur(int size = MatrixType::ColsAtCompileTime)
+{
+ // Test basic functionality: T is quasi-triangular and A = U T U*
+ for(int counter = 0; counter < g_repeat; ++counter) {
+ MatrixType A = MatrixType::Random(size, size);
+ RealSchur<MatrixType> schurOfA(A);
+ MatrixType U = schurOfA.matrixU();
+ MatrixType T = schurOfA.matrixT();
+ std::cout << "T = \n" << T << "\n\n";
+ verifyIsQuasiTriangular(T);
+ VERIFY_IS_APPROX(A, U * T * U.transpose());
+ }
+
+ // Test asserts when not initialized
+ RealSchur<MatrixType> rsUninitialized;
+ VERIFY_RAISES_ASSERT(rsUninitialized.matrixT());
+ VERIFY_RAISES_ASSERT(rsUninitialized.matrixU());
+
+ // Test whether compute() and constructor returns same result
+ MatrixType A = MatrixType::Random(size, size);
+ RealSchur<MatrixType> rs1;
+ rs1.compute(A);
+ RealSchur<MatrixType> rs2(A);
+ VERIFY_IS_EQUAL(rs1.matrixT(), rs2.matrixT());
+ VERIFY_IS_EQUAL(rs1.matrixU(), rs2.matrixU());
+}
+
+void test_schur_real()
+{
+ CALL_SUBTEST_1(( schur<Matrix4f>() ));
+ CALL_SUBTEST_2(( schur<MatrixXd>(ei_random<int>(1,50)) ));
+ CALL_SUBTEST_3(( schur<Matrix<float, 1, 1> >() ));
+ CALL_SUBTEST_4(( schur<Matrix<double, 3, 3, Eigen::RowMajor> >() ));
+}
diff --git a/test/sparse_vector.cpp b/test/sparse_vector.cpp
index 5c6dadc00..a5101398c 100644
--- a/test/sparse_vector.cpp
+++ b/test/sparse_vector.cpp
@@ -79,13 +79,15 @@ template<typename Scalar> void sparse_vector(int rows, int cols)
VERIFY_IS_APPROX(v1*=s1, refV1*=s1);
VERIFY_IS_APPROX(v1/=s1, refV1/=s1);
-
+
VERIFY_IS_APPROX(v1+=v2, refV1+=refV2);
VERIFY_IS_APPROX(v1-=v2, refV1-=refV2);
VERIFY_IS_APPROX(v1.dot(v2), refV1.dot(refV2));
VERIFY_IS_APPROX(v1.dot(refV2), refV1.dot(refV2));
+ VERIFY_IS_APPROX(v1.squaredNorm(), refV1.squaredNorm());
+
}
void test_sparse_vector()