eigensolver_complex.cpp
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2008-2009 Gael Guennebaud <gael.guennebaud@inria.fr>
5 // Copyright (C) 2010 Jitse Niesen <jitse@maths.leeds.ac.uk>
6 //
7 // This Source Code Form is subject to the terms of the Mozilla
8 // Public License v. 2.0. If a copy of the MPL was not distributed
9 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 
11 #include "main.h"
12 #include <limits>
13 #include <Eigen/Eigenvalues>
14 #include <Eigen/LU>
15 
16 template<typename MatrixType> bool find_pivot(typename MatrixType::Scalar tol, MatrixType &diffs, Index col=0)
17 {
18  bool match = diffs.diagonal().sum() <= tol;
19  if(match || col==diffs.cols())
20  {
21  return match;
22  }
23  else
24  {
25  Index n = diffs.cols();
26  std::vector<std::pair<Index,Index> > transpositions;
27  for(Index i=col; i<n; ++i)
28  {
29  Index best_index(0);
30  if(diffs.col(col).segment(col,n-i).minCoeff(&best_index) > tol)
31  break;
32 
33  best_index += col;
34 
35  diffs.row(col).swap(diffs.row(best_index));
36  if(find_pivot(tol,diffs,col+1)) return true;
37  diffs.row(col).swap(diffs.row(best_index));
38 
39  // move current pivot to the end
40  diffs.row(n-(i-col)-1).swap(diffs.row(best_index));
41  transpositions.push_back(std::pair<Index,Index>(n-(i-col)-1,best_index));
42  }
43  // restore
44  for(Index k=transpositions.size()-1; k>=0; --k)
45  diffs.row(transpositions[k].first).swap(diffs.row(transpositions[k].second));
46  }
47  return false;
48 }
49 
50 /* Check that two column vectors are approximately equal up to permutations.
51  * Initially, this method checked that the k-th power sums are equal for all k = 1, ..., vec1.rows(),
52  * however this strategy is numerically inacurate because of numerical cancellation issues.
53  */
54 template<typename VectorType>
56 {
57  typedef typename VectorType::Scalar Scalar;
58  typedef typename NumTraits<Scalar>::Real RealScalar;
59 
60  VERIFY(vec1.cols() == 1);
61  VERIFY(vec2.cols() == 1);
62  VERIFY(vec1.rows() == vec2.rows());
63 
64  Index n = vec1.rows();
65  RealScalar tol = test_precision<RealScalar>()*test_precision<RealScalar>()*numext::maxi(vec1.squaredNorm(),vec2.squaredNorm());
66  Matrix<RealScalar,Dynamic,Dynamic> diffs = (vec1.rowwise().replicate(n) - vec2.rowwise().replicate(n).transpose()).cwiseAbs2();
67 
68  VERIFY( find_pivot(tol, diffs) );
69 }
70 
71 
72 template<typename MatrixType> void eigensolver(const MatrixType& m)
73 {
74  /* this test covers the following files:
75  ComplexEigenSolver.h, and indirectly ComplexSchur.h
76  */
77  Index rows = m.rows();
78  Index cols = m.cols();
79 
80  typedef typename MatrixType::Scalar Scalar;
81  typedef typename NumTraits<Scalar>::Real RealScalar;
82 
83  MatrixType a = MatrixType::Random(rows,cols);
84  MatrixType symmA = a.adjoint() * a;
85 
88  VERIFY_IS_APPROX(symmA * ei0.eigenvectors(), ei0.eigenvectors() * ei0.eigenvalues().asDiagonal());
89 
92  VERIFY_IS_APPROX(a * ei1.eigenvectors(), ei1.eigenvectors() * ei1.eigenvalues().asDiagonal());
93  // Note: If MatrixType is real then a.eigenvalues() uses EigenSolver and thus
94  // another algorithm so results may differ slightly
95  verify_is_approx_upto_permutation(a.eigenvalues(), ei1.eigenvalues());
96 
102  if (rows > 2) {
103  ei2.setMaxIterations(1).compute(a);
106  }
107 
108  ComplexEigenSolver<MatrixType> eiNoEivecs(a, false);
109  VERIFY_IS_EQUAL(eiNoEivecs.info(), Success);
110  VERIFY_IS_APPROX(ei1.eigenvalues(), eiNoEivecs.eigenvalues());
111 
112  // Regression test for issue #66
113  MatrixType z = MatrixType::Zero(rows,cols);
115  VERIFY((eiz.eigenvalues().cwiseEqual(0)).all());
116 
117  MatrixType id = MatrixType::Identity(rows, cols);
118  VERIFY_IS_APPROX(id.operatorNorm(), RealScalar(1));
119 
120  if (rows > 1 && rows < 20)
121  {
122  // Test matrix with NaN
123  a(0,0) = std::numeric_limits<typename MatrixType::RealScalar>::quiet_NaN();
126  }
127 
128  // regression test for bug 1098
129  {
130  ComplexEigenSolver<MatrixType> eig(a.adjoint() * a);
131  eig.compute(a.adjoint() * a);
132  }
133 
134  // regression test for bug 478
135  {
136  a.setZero();
138  VERIFY_IS_EQUAL(ei3.info(), Success);
140  VERIFY((ei3.eigenvectors().transpose()*ei3.eigenvectors().transpose()).eval().isIdentity());
141  }
142 }
143 
144 template<typename MatrixType> void eigensolver_verify_assert(const MatrixType& m)
145 {
147  VERIFY_RAISES_ASSERT(eig.eigenvectors());
148  VERIFY_RAISES_ASSERT(eig.eigenvalues());
149 
150  MatrixType a = MatrixType::Random(m.rows(),m.cols());
151  eig.compute(a, false);
152  VERIFY_RAISES_ASSERT(eig.eigenvectors());
153 }
154 
155 EIGEN_DECLARE_TEST(eigensolver_complex)
156 {
157  int s = 0;
158  for(int i = 0; i < g_repeat; i++) {
159  CALL_SUBTEST_1( eigensolver(Matrix4cf()) );
160  s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE/4);
161  CALL_SUBTEST_2( eigensolver(MatrixXcd(s,s)) );
162  CALL_SUBTEST_3( eigensolver(Matrix<std::complex<float>, 1, 1>()) );
163  CALL_SUBTEST_4( eigensolver(Matrix3f()) );
165  }
167  s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE/4);
169  CALL_SUBTEST_3( eigensolver_verify_assert(Matrix<std::complex<float>, 1, 1>()) );
171 
172  // Test problem size constructors
174 
176 }
VERIFY_IS_MUCH_SMALLER_THAN
#define VERIFY_IS_MUCH_SMALLER_THAN(a, b)
Definition: main.h:390
col
m col(1)
s
RealScalar s
Definition: level1_cplx_impl.h:126
MatrixType
MatrixXf MatrixType
Definition: benchmark-blocking-sizes.cpp:52
VERIFY_IS_EQUAL
#define VERIFY_IS_EQUAL(a, b)
Definition: main.h:386
Eigen::ComplexEigenSolver::setMaxIterations
ComplexEigenSolver & setMaxIterations(Index maxIters)
Sets the maximum number of iterations allowed.
Definition: ComplexEigenSolver.h:226
Eigen::Success
@ Success
Definition: Constants.h:442
rows
int rows
Definition: Tutorial_commainit_02.cpp:1
EIGEN_DECLARE_TEST
EIGEN_DECLARE_TEST(eigensolver_complex)
Definition: eigensolver_complex.cpp:155
VERIFY_RAISES_ASSERT
#define VERIFY_RAISES_ASSERT(a)
Definition: main.h:340
verify_is_approx_upto_permutation
void verify_is_approx_upto_permutation(const VectorType &vec1, const VectorType &vec2)
Definition: eigensolver_complex.cpp:55
CALL_SUBTEST_4
#define CALL_SUBTEST_4(FUNC)
Definition: split_test_helper.h:22
Eigen::ComplexEigenSolver::eigenvalues
const EigenvalueType & eigenvalues() const
Returns the eigenvalues of given matrix.
Definition: ComplexEigenSolver.h:182
n
int n
Definition: BiCGSTAB_simple.cpp:1
CALL_SUBTEST_3
#define CALL_SUBTEST_3(FUNC)
Definition: split_test_helper.h:16
CALL_SUBTEST_1
#define CALL_SUBTEST_1(FUNC)
Definition: split_test_helper.h:4
Eigen::NoConvergence
@ NoConvergence
Definition: Constants.h:446
match
bool match(const T &xpr, std::string ref, std::string str_xpr="")
Definition: indexed_view.cpp:54
Eigen::internal::first
EIGEN_CONSTEXPR Index first(const T &x) EIGEN_NOEXCEPT
Definition: IndexedViewHelper.h:81
CALL_SUBTEST_5
#define CALL_SUBTEST_5(FUNC)
Definition: split_test_helper.h:28
Eigen::g_repeat
static int g_repeat
Definition: main.h:169
pybind_wrapper_test_script.z
z
Definition: pybind_wrapper_test_script.py:61
m
Matrix3f m
Definition: AngleAxis_mimic_euler.cpp:1
eigensolver_verify_assert
void eigensolver_verify_assert(const MatrixType &m)
Definition: eigensolver_complex.cpp:144
eig
SelfAdjointEigenSolver< PlainMatrixType > eig(mat, computeVectors?ComputeEigenvectors:EigenvaluesOnly)
CALL_SUBTEST_2
#define CALL_SUBTEST_2(FUNC)
Definition: split_test_helper.h:10
cwiseAbs2
EIGEN_DEVICE_FUNC const EIGEN_STRONG_INLINE CwiseAbs2ReturnType cwiseAbs2() const
Definition: MatrixCwiseUnaryOps.h:46
VERIFY_IS_APPROX
#define VERIFY_IS_APPROX(a, b)
Definition: integer_types.cpp:15
RealScalar
NumTraits< Scalar >::Real RealScalar
Definition: bench_gemm.cpp:47
a
ArrayXXi a
Definition: Array_initializer_list_23_cxx11.cpp:1
Eigen::ComplexEigenSolver::eigenvectors
const EigenvectorType & eigenvectors() const
Returns the eigenvectors of given matrix.
Definition: ComplexEigenSolver.h:157
Eigen::ComplexEigenSolver::getMaxIterations
Index getMaxIterations()
Returns the maximum number of iterations.
Definition: ComplexEigenSolver.h:233
Eigen::ComplexEigenSolver::info
ComputationInfo info() const
Reports whether previous computation was successful.
Definition: ComplexEigenSolver.h:219
main.h
Eigen::ComplexEigenSolver
Computes eigenvalues and eigenvectors of general complex matrices.
Definition: ComplexEigenSolver.h:45
EIGEN_TEST_MAX_SIZE
#define EIGEN_TEST_MAX_SIZE
Definition: boostmultiprec.cpp:16
eigensolver
void eigensolver(const MatrixType &m)
Definition: eigensolver_complex.cpp:72
find_pivot
bool find_pivot(typename MatrixType::Scalar tol, MatrixType &diffs, Index col=0)
Definition: eigensolver_complex.cpp:16
Eigen::ComplexEigenSolver::compute
ComplexEigenSolver & compute(const EigenBase< InputType > &matrix, bool computeEigenvectors=true)
Computes eigendecomposition of given matrix.
gtsam::tol
const G double tol
Definition: Group.h:79
Eigen::Matrix
The matrix class, also used for vectors and row-vectors.
Definition: 3rdparty/Eigen/Eigen/src/Core/Matrix.h:178
Eigen::numext::maxi
EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T maxi(const T &x, const T &y)
Definition: Eigen/src/Core/MathFunctions.h:1093
VectorType
Definition: FFTW.cpp:65
cols
int cols
Definition: Tutorial_commainit_02.cpp:1
Eigen::ComplexSchur
Performs a complex Schur decomposition of a real or complex square matrix.
Definition: ComplexSchur.h:51
Eigen::NumTraits
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:232
vec1
RowVectorXd vec1(3)
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
TEST_SET_BUT_UNUSED_VARIABLE
#define TEST_SET_BUT_UNUSED_VARIABLE(X)
Definition: main.h:121
Scalar
SCALAR Scalar
Definition: bench_gemm.cpp:46
VERIFY
#define VERIFY(a)
Definition: main.h:380
Eigen::Index
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74


gtsam
Author(s):
autogenerated on Sun Dec 22 2024 04:11:30