00001 // This file is part of Eigen, a lightweight C++ template library 00002 // for linear algebra. 00003 // 00004 // Copyright (C) 2010 Gael Guennebaud <gael.guennebaud@inria.fr> 00005 // 00006 // Eigen is free software; you can redistribute it and/or 00007 // modify it under the terms of the GNU Lesser General Public 00008 // License as published by the Free Software Foundation; either 00009 // version 3 of the License, or (at your option) any later version. 00010 // 00011 // Alternatively, you can redistribute it and/or 00012 // modify it under the terms of the GNU General Public License as 00013 // published by the Free Software Foundation; either version 2 of 00014 // the License, or (at your option) any later version. 00015 // 00016 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY 00017 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 00018 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the 00019 // GNU General Public License for more details. 00020 // 00021 // You should have received a copy of the GNU Lesser General Public 00022 // License and a copy of the GNU General Public License along with 00023 // Eigen. If not, see <http://www.gnu.org/licenses/>. 00024 00025 // The computeRoots function included in this is based on materials 00026 // covered by the following copyright and license: 00027 // 00028 // Geometric Tools, LLC 00029 // Copyright (c) 1998-2010 00030 // Distributed under the Boost Software License, Version 1.0. 00031 // 00032 // Permission is hereby granted, free of charge, to any person or organization 00033 // obtaining a copy of the software and accompanying documentation covered by 00034 // this license (the "Software") to use, reproduce, display, distribute, 00035 // execute, and transmit the Software, and to prepare derivative works of the 00036 // Software, and to permit third-parties to whom the Software is furnished to 00037 // do so, all subject to the following: 00038 // 00039 // The copyright notices in the Software and this entire statement, including 00040 // the above license grant, this restriction and the following disclaimer, 00041 // must be included in all copies of the Software, in whole or in part, and 00042 // all derivative works of the Software, unless such copies or derivative 00043 // works are solely in the form of machine-executable object code generated by 00044 // a source language processor. 00045 // 00046 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00047 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00048 // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 00049 // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 00050 // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 00051 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 00052 // DEALINGS IN THE SOFTWARE. 00053 00054 #include <iostream> 00055 #include <Eigen/Core> 00056 #include <Eigen/Eigenvalues> 00057 #include <Eigen/Geometry> 00058 #include <bench/BenchTimer.h> 00059 00060 using namespace Eigen; 00061 using namespace std; 00062 00063 template<typename Matrix, typename Roots> 00064 inline void computeRoots(const Matrix& m, Roots& roots) 00065 { 00066 typedef typename Matrix::Scalar Scalar; 00067 const Scalar s_inv3 = 1.0/3.0; 00068 const Scalar s_sqrt3 = internal::sqrt(Scalar(3.0)); 00069 00070 // The characteristic equation is x^3 - c2*x^2 + c1*x - c0 = 0. The 00071 // eigenvalues are the roots to this equation, all guaranteed to be 00072 // real-valued, because the matrix is symmetric. 00073 Scalar c0 = m(0,0)*m(1,1)*m(2,2) + Scalar(2)*m(0,1)*m(0,2)*m(1,2) - m(0,0)*m(1,2)*m(1,2) - m(1,1)*m(0,2)*m(0,2) - m(2,2)*m(0,1)*m(0,1); 00074 Scalar c1 = m(0,0)*m(1,1) - m(0,1)*m(0,1) + m(0,0)*m(2,2) - m(0,2)*m(0,2) + m(1,1)*m(2,2) - m(1,2)*m(1,2); 00075 Scalar c2 = m(0,0) + m(1,1) + m(2,2); 00076 00077 // Construct the parameters used in classifying the roots of the equation 00078 // and in solving the equation for the roots in closed form. 00079 Scalar c2_over_3 = c2*s_inv3; 00080 Scalar a_over_3 = (c1 - c2*c2_over_3)*s_inv3; 00081 if (a_over_3 > Scalar(0)) 00082 a_over_3 = Scalar(0); 00083 00084 Scalar half_b = Scalar(0.5)*(c0 + c2_over_3*(Scalar(2)*c2_over_3*c2_over_3 - c1)); 00085 00086 Scalar q = half_b*half_b + a_over_3*a_over_3*a_over_3; 00087 if (q > Scalar(0)) 00088 q = Scalar(0); 00089 00090 // Compute the eigenvalues by solving for the roots of the polynomial. 00091 Scalar rho = internal::sqrt(-a_over_3); 00092 Scalar theta = std::atan2(internal::sqrt(-q),half_b)*s_inv3; 00093 Scalar cos_theta = internal::cos(theta); 00094 Scalar sin_theta = internal::sin(theta); 00095 roots(0) = c2_over_3 + Scalar(2)*rho*cos_theta; 00096 roots(1) = c2_over_3 - rho*(cos_theta + s_sqrt3*sin_theta); 00097 roots(2) = c2_over_3 - rho*(cos_theta - s_sqrt3*sin_theta); 00098 00099 // Sort in increasing order. 00100 if (roots(0) >= roots(1)) 00101 std::swap(roots(0),roots(1)); 00102 if (roots(1) >= roots(2)) 00103 { 00104 std::swap(roots(1),roots(2)); 00105 if (roots(0) >= roots(1)) 00106 std::swap(roots(0),roots(1)); 00107 } 00108 } 00109 00110 template<typename Matrix, typename Vector> 00111 void eigen33(const Matrix& mat, Matrix& evecs, Vector& evals) 00112 { 00113 typedef typename Matrix::Scalar Scalar; 00114 // Scale the matrix so its entries are in [-1,1]. The scaling is applied 00115 // only when at least one matrix entry has magnitude larger than 1. 00116 00117 Scalar scale = mat.cwiseAbs()/*.template triangularView<Lower>()*/.maxCoeff(); 00118 scale = std::max(scale,Scalar(1)); 00119 Matrix scaledMat = mat / scale; 00120 00121 // Compute the eigenvalues 00122 // scaledMat.setZero(); 00123 computeRoots(scaledMat,evals); 00124 00125 // compute the eigen vectors 00126 // **here we assume 3 differents eigenvalues** 00127 00128 // "optimized version" which appears to be slower with gcc! 00129 // Vector base; 00130 // Scalar alpha, beta; 00131 // base << scaledMat(1,0) * scaledMat(2,1), 00132 // scaledMat(1,0) * scaledMat(2,0), 00133 // -scaledMat(1,0) * scaledMat(1,0); 00134 // for(int k=0; k<2; ++k) 00135 // { 00136 // alpha = scaledMat(0,0) - evals(k); 00137 // beta = scaledMat(1,1) - evals(k); 00138 // evecs.col(k) = (base + Vector(-beta*scaledMat(2,0), -alpha*scaledMat(2,1), alpha*beta)).normalized(); 00139 // } 00140 // evecs.col(2) = evecs.col(0).cross(evecs.col(1)).normalized(); 00141 00142 // // naive version 00143 // Matrix tmp; 00144 // tmp = scaledMat; 00145 // tmp.diagonal().array() -= evals(0); 00146 // evecs.col(0) = tmp.row(0).cross(tmp.row(1)).normalized(); 00147 // 00148 // tmp = scaledMat; 00149 // tmp.diagonal().array() -= evals(1); 00150 // evecs.col(1) = tmp.row(0).cross(tmp.row(1)).normalized(); 00151 // 00152 // tmp = scaledMat; 00153 // tmp.diagonal().array() -= evals(2); 00154 // evecs.col(2) = tmp.row(0).cross(tmp.row(1)).normalized(); 00155 00156 // a more stable version: 00157 if((evals(2)-evals(0))<=Eigen::NumTraits<Scalar>::epsilon()) 00158 { 00159 evecs.setIdentity(); 00160 } 00161 else 00162 { 00163 Matrix tmp; 00164 tmp = scaledMat; 00165 tmp.diagonal ().array () -= evals (2); 00166 evecs.col (2) = tmp.row (0).cross (tmp.row (1)).normalized (); 00167 00168 tmp = scaledMat; 00169 tmp.diagonal ().array () -= evals (1); 00170 evecs.col(1) = tmp.row (0).cross(tmp.row (1)); 00171 Scalar n1 = evecs.col(1).norm(); 00172 if(n1<=Eigen::NumTraits<Scalar>::epsilon()) 00173 evecs.col(1) = evecs.col(2).unitOrthogonal(); 00174 else 00175 evecs.col(1) /= n1; 00176 00177 // make sure that evecs[1] is orthogonal to evecs[2] 00178 evecs.col(1) = evecs.col(2).cross(evecs.col(1).cross(evecs.col(2))).normalized(); 00179 evecs.col(0) = evecs.col(2).cross(evecs.col(1)); 00180 } 00181 00182 // Rescale back to the original size. 00183 evals *= scale; 00184 } 00185 00186 int main() 00187 { 00188 BenchTimer t; 00189 int tries = 10; 00190 int rep = 400000; 00191 typedef Matrix3f Mat; 00192 typedef Vector3f Vec; 00193 Mat A = Mat::Random(3,3); 00194 A = A.adjoint() * A; 00195 00196 SelfAdjointEigenSolver<Mat> eig(A); 00197 BENCH(t, tries, rep, eig.compute(A)); 00198 std::cout << "Eigen: " << t.best() << "s\n"; 00199 00200 Mat evecs; 00201 Vec evals; 00202 BENCH(t, tries, rep, eigen33(A,evecs,evals)); 00203 std::cout << "Direct: " << t.best() << "s\n\n"; 00204 00205 std::cerr << "Eigenvalue/eigenvector diffs:\n"; 00206 std::cerr << (evals - eig.eigenvalues()).transpose() << "\n"; 00207 for(int k=0;k<3;++k) 00208 if(evecs.col(k).dot(eig.eigenvectors().col(k))<0) 00209 evecs.col(k) = -evecs.col(k); 00210 std::cerr << evecs - eig.eigenvectors() << "\n\n"; 00211 }