Squashed 'third_party/eigen/' changes from 61d72f6..cf794d3


Change-Id: I9b814151b01f49af6337a8605d0c42a3a1ed4c72
git-subtree-dir: third_party/eigen
git-subtree-split: cf794d3b741a6278df169e58461f8529f43bce5d
diff --git a/test/eigensolver_complex.cpp b/test/eigensolver_complex.cpp
index c9d8c08..7269452 100644
--- a/test/eigensolver_complex.cpp
+++ b/test/eigensolver_complex.cpp
@@ -13,26 +13,64 @@
 #include <Eigen/Eigenvalues>
 #include <Eigen/LU>
 
-/* Check that two column vectors are approximately equal upto permutations,
-   by checking that the k-th power sums are equal for k = 1, ..., vec1.rows() */
+template<typename MatrixType> bool find_pivot(typename MatrixType::Scalar tol, MatrixType &diffs, Index col=0)
+{
+  bool match = diffs.diagonal().sum() <= tol;
+  if(match || col==diffs.cols())
+  {
+    return match;
+  }
+  else
+  {
+    Index n = diffs.cols();
+    std::vector<std::pair<Index,Index> > transpositions;
+    for(Index i=col; i<n; ++i)
+    {
+      Index best_index(0);
+      if(diffs.col(col).segment(col,n-i).minCoeff(&best_index) > tol)
+        break;
+      
+      best_index += col;
+      
+      diffs.row(col).swap(diffs.row(best_index));
+      if(find_pivot(tol,diffs,col+1)) return true;
+      diffs.row(col).swap(diffs.row(best_index));
+      
+      // move current pivot to the end
+      diffs.row(n-(i-col)-1).swap(diffs.row(best_index));
+      transpositions.push_back(std::pair<Index,Index>(n-(i-col)-1,best_index));
+    }
+    // restore
+    for(Index k=transpositions.size()-1; k>=0; --k)
+      diffs.row(transpositions[k].first).swap(diffs.row(transpositions[k].second));
+  }
+  return false;
+}
+
+/* Check that two column vectors are approximately equal upto permutations.
+ * Initially, this method checked that the k-th power sums are equal for all k = 1, ..., vec1.rows(),
+ * however this strategy is numerically inacurate because of numerical cancellation issues.
+ */
 template<typename VectorType>
 void verify_is_approx_upto_permutation(const VectorType& vec1, const VectorType& vec2)
 {
-  typedef typename NumTraits<typename VectorType::Scalar>::Real RealScalar;
+  typedef typename VectorType::Scalar Scalar;
+  typedef typename NumTraits<Scalar>::Real RealScalar;
 
   VERIFY(vec1.cols() == 1);
   VERIFY(vec2.cols() == 1);
   VERIFY(vec1.rows() == vec2.rows());
-  for (int k = 1; k <= vec1.rows(); ++k)
-  {
-    VERIFY_IS_APPROX(vec1.array().pow(RealScalar(k)).sum(), vec2.array().pow(RealScalar(k)).sum());
-  }
+  
+  Index n = vec1.rows();
+  RealScalar tol = test_precision<RealScalar>()*test_precision<RealScalar>()*numext::maxi(vec1.squaredNorm(),vec2.squaredNorm());
+  Matrix<RealScalar,Dynamic,Dynamic> diffs = (vec1.rowwise().replicate(n) - vec2.rowwise().replicate(n).transpose()).cwiseAbs2();
+  
+  VERIFY( find_pivot(tol, diffs) );
 }
 
 
 template<typename MatrixType> void eigensolver(const MatrixType& m)
 {
-  typedef typename MatrixType::Index Index;
   /* this test covers the following files:
      ComplexEigenSolver.h, and indirectly ComplexSchur.h
   */
@@ -79,13 +117,28 @@
   MatrixType id = MatrixType::Identity(rows, cols);
   VERIFY_IS_APPROX(id.operatorNorm(), RealScalar(1));
 
-  if (rows > 1)
+  if (rows > 1 && rows < 20)
   {
     // Test matrix with NaN
     a(0,0) = std::numeric_limits<typename MatrixType::RealScalar>::quiet_NaN();
     ComplexEigenSolver<MatrixType> eiNaN(a);
     VERIFY_IS_EQUAL(eiNaN.info(), NoConvergence);
   }
+
+  // regression test for bug 1098
+  {
+    ComplexEigenSolver<MatrixType> eig(a.adjoint() * a);
+    eig.compute(a.adjoint() * a);
+  }
+
+  // regression test for bug 478
+  {
+    a.setZero();
+    ComplexEigenSolver<MatrixType> ei3(a);
+    VERIFY_IS_EQUAL(ei3.info(), Success);
+    VERIFY_IS_MUCH_SMALLER_THAN(ei3.eigenvalues().norm(),RealScalar(1));
+    VERIFY((ei3.eigenvectors().transpose()*ei3.eigenvectors().transpose()).eval().isIdentity());
+  }
 }
 
 template<typename MatrixType> void eigensolver_verify_assert(const MatrixType& m)
@@ -108,6 +161,7 @@
     CALL_SUBTEST_2( eigensolver(MatrixXcd(s,s)) );
     CALL_SUBTEST_3( eigensolver(Matrix<std::complex<float>, 1, 1>()) );
     CALL_SUBTEST_4( eigensolver(Matrix3f()) );
+    TEST_SET_BUT_UNUSED_VARIABLE(s)
   }
   CALL_SUBTEST_1( eigensolver_verify_assert(Matrix4cf()) );
   s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE/4);