// This file is part of Eigen, a lightweight C++ template library // for linear algebra. // // Copyright (C) 2009 Benoit Jacob // // 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 . #ifndef EIGEN_JACOBI_H #define EIGEN_JACOBI_H template inline void MatrixBase::applyJacobiOnTheLeft(int p, int q, Scalar c, Scalar s) { RowXpr x(row(p)); RowXpr y(row(q)); ei_apply_rotation_in_the_plane(x, y, c, s); } template inline void MatrixBase::applyJacobiOnTheRight(int p, int q, Scalar c, Scalar s) { ColXpr x(col(p)); ColXpr y(col(q)); ei_apply_rotation_in_the_plane(x, y, c, s); } template bool ei_makeJacobi(Scalar x, Scalar y, Scalar z, Scalar *c, Scalar *s) { if(y == 0) { *c = Scalar(1); *s = Scalar(0); return false; } else { Scalar tau = (z - x) / (2 * y); Scalar w = ei_sqrt(1 + ei_abs2(tau)); Scalar t; if(tau>0) t = Scalar(1) / (tau + w); else t = Scalar(1) / (tau - w); *c = Scalar(1) / ei_sqrt(1 + ei_abs2(t)); *s = *c * t; return true; } } template inline bool MatrixBase::makeJacobi(int p, int q, Scalar *c, Scalar *s) const { return ei_makeJacobi(coeff(p,p), coeff(p,q), coeff(q,q), c, s); } template inline bool MatrixBase::makeJacobiForAtA(int p, int q, Scalar *c, Scalar *s) const { return ei_makeJacobi(ei_abs2(coeff(p,p)) + ei_abs2(coeff(q,p)), ei_conj(coeff(p,p))*coeff(p,q) + ei_conj(coeff(q,p))*coeff(q,q), ei_abs2(coeff(p,q)) + ei_abs2(coeff(q,q)), c,s); } template inline bool MatrixBase::makeJacobiForAAt(int p, int q, Scalar *c, Scalar *s) const { return ei_makeJacobi(ei_abs2(coeff(p,p)) + ei_abs2(coeff(p,q)), ei_conj(coeff(q,p))*coeff(p,p) + ei_conj(coeff(q,q))*coeff(p,q), ei_abs2(coeff(q,p)) + ei_abs2(coeff(q,q)), c,s); } template inline void ei_normalizeJacobi(Scalar *c, Scalar *s, const Scalar& x, const Scalar& y) { Scalar a = x * *c - y * *s; Scalar b = x * *s + y * *c; if(ei_abs(b)>ei_abs(a)) { Scalar x = *c; *c = -*s; *s = x; } } #endif // EIGEN_JACOBI_H