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 
91  VERIFY_IS_EQUAL(ei1.info(), Success);
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 
99  VERIFY_IS_EQUAL(ei2.info(), Success);
100  VERIFY_IS_EQUAL(ei2.eigenvectors(), ei1.eigenvectors());
101  VERIFY_IS_EQUAL(ei2.eigenvalues(), ei1.eigenvalues());
102  if (rows > 2) {
103  ei2.setMaxIterations(1).compute(a);
104  VERIFY_IS_EQUAL(ei2.info(), NoConvergence);
105  VERIFY_IS_EQUAL(ei2.getMaxIterations(), 1);
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 {
149 
150  MatrixType a = MatrixType::Random(m.rows(),m.cols());
151  eig.compute(a, false);
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);
168  CALL_SUBTEST_2( eigensolver_verify_assert(MatrixXcd(s,s)) );
169  CALL_SUBTEST_3( eigensolver_verify_assert(Matrix<std::complex<float>, 1, 1>()) );
171 
172  // Test problem size constructors
174 
176 }
Matrix3f m
SCALAR Scalar
Definition: bench_gemm.cpp:46
#define VERIFY_RAISES_ASSERT(a)
Definition: main.h:340
bool match(const T &xpr, std::string ref, std::string str_xpr="")
#define CALL_SUBTEST_4(FUNC)
static const Eigen::internal::all_t all
#define CALL_SUBTEST_3(FUNC)
ComplexEigenSolver & compute(const EigenBase< InputType > &matrix, bool computeEigenvectors=true)
Computes eigendecomposition of given matrix.
void verify_is_approx_upto_permutation(const VectorType &vec1, const VectorType &vec2)
int n
MatrixXf MatrixType
Holds information about the various numeric (i.e. scalar) types allowed by Eigen. ...
Definition: NumTraits.h:232
EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T maxi(const T &x, const T &y)
void eigensolver(const MatrixType &m)
const EigenvalueType & eigenvalues() const
Returns the eigenvalues of given matrix.
#define VERIFY_IS_APPROX(a, b)
#define VERIFY_IS_EQUAL(a, b)
Definition: main.h:386
#define CALL_SUBTEST_1(FUNC)
ComputationInfo info() const
Reports whether previous computation was successful.
static int g_repeat
Definition: main.h:169
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74
bool find_pivot(typename MatrixType::Scalar tol, MatrixType &diffs, Index col=0)
SelfAdjointEigenSolver< PlainMatrixType > eig(mat, computeVectors?ComputeEigenvectors:EigenvaluesOnly)
RealScalar s
const EigenvectorType & eigenvectors() const
Returns the eigenvectors of given matrix.
NumTraits< Scalar >::Real RealScalar
Definition: bench_gemm.cpp:47
void eigensolver_verify_assert(const MatrixType &m)
#define VERIFY_IS_MUCH_SMALLER_THAN(a, b)
Definition: main.h:390
RowVectorXd vec1(3)
#define TEST_SET_BUT_UNUSED_VARIABLE(X)
Definition: main.h:121
#define CALL_SUBTEST_5(FUNC)
EIGEN_DECLARE_TEST(eigensolver_complex)
#define VERIFY(a)
Definition: main.h:380
#define EIGEN_TEST_MAX_SIZE
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const CwiseAbs2ReturnType cwiseAbs2() const
ComplexEigenSolver & setMaxIterations(Index maxIters)
Sets the maximum number of iterations allowed.
m col(1)
#define CALL_SUBTEST_2(FUNC)
internal::nested_eval< T, 1 >::type eval(const T &xpr)
const G double tol
Definition: Group.h:86
The matrix class, also used for vectors and row-vectors.
Performs a complex Schur decomposition of a real or complex square matrix.
Definition: ComplexSchur.h:51
Computes eigenvalues and eigenvectors of general complex matrices.


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:34:12