jacobi.cpp
Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
00005 // Copyright (C) 2009 Benoit Jacob <jacob.benoit.1@gmail.com>
00006 //
00007 // Eigen is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU Lesser General Public
00009 // License as published by the Free Software Foundation; either
00010 // version 3 of the License, or (at your option) any later version.
00011 //
00012 // Alternatively, you can redistribute it and/or
00013 // modify it under the terms of the GNU General Public License as
00014 // published by the Free Software Foundation; either version 2 of
00015 // the License, or (at your option) any later version.
00016 //
00017 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00018 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00019 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00020 // GNU General Public License for more details.
00021 //
00022 // You should have received a copy of the GNU Lesser General Public
00023 // License and a copy of the GNU General Public License along with
00024 // Eigen. If not, see <http://www.gnu.org/licenses/>.
00025 
00026 #include "main.h"
00027 #include <Eigen/SVD>
00028 
00029 template<typename MatrixType, typename JacobiScalar>
00030 void jacobi(const MatrixType& m = MatrixType())
00031 {
00032   typedef typename MatrixType::Scalar Scalar;
00033   typedef typename MatrixType::Index Index;
00034   Index rows = m.rows();
00035   Index cols = m.cols();
00036 
00037   enum {
00038     RowsAtCompileTime = MatrixType::RowsAtCompileTime,
00039     ColsAtCompileTime = MatrixType::ColsAtCompileTime
00040   };
00041 
00042   typedef Matrix<JacobiScalar, 2, 1> JacobiVector;
00043 
00044   const MatrixType a(MatrixType::Random(rows, cols));
00045 
00046   JacobiVector v = JacobiVector::Random().normalized();
00047   JacobiScalar c = v.x(), s = v.y();
00048   JacobiRotation<JacobiScalar> rot(c, s);
00049 
00050   {
00051     Index p = internal::random<Index>(0, rows-1);
00052     Index q;
00053     do {
00054       q = internal::random<Index>(0, rows-1);
00055     } while (q == p);
00056 
00057     MatrixType b = a;
00058     b.applyOnTheLeft(p, q, rot);
00059     VERIFY_IS_APPROX(b.row(p), c * a.row(p) + internal::conj(s) * a.row(q));
00060     VERIFY_IS_APPROX(b.row(q), -s * a.row(p) + internal::conj(c) * a.row(q));
00061   }
00062 
00063   {
00064     Index p = internal::random<Index>(0, cols-1);
00065     Index q;
00066     do {
00067       q = internal::random<Index>(0, cols-1);
00068     } while (q == p);
00069 
00070     MatrixType b = a;
00071     b.applyOnTheRight(p, q, rot);
00072     VERIFY_IS_APPROX(b.col(p), c * a.col(p) - s * a.col(q));
00073     VERIFY_IS_APPROX(b.col(q), internal::conj(s) * a.col(p) + internal::conj(c) * a.col(q));
00074   }
00075 }
00076 
00077 void test_jacobi()
00078 {
00079   for(int i = 0; i < g_repeat; i++) {
00080     CALL_SUBTEST_1(( jacobi<Matrix3f, float>() ));
00081     CALL_SUBTEST_2(( jacobi<Matrix4d, double>() ));
00082     CALL_SUBTEST_3(( jacobi<Matrix4cf, float>() ));
00083     CALL_SUBTEST_3(( jacobi<Matrix4cf, std::complex<float> >() ));
00084 
00085     int r = internal::random<int>(2, 20),
00086         c = internal::random<int>(2, 20);
00087     CALL_SUBTEST_4(( jacobi<MatrixXf, float>(MatrixXf(r,c)) ));
00088     CALL_SUBTEST_5(( jacobi<MatrixXcd, double>(MatrixXcd(r,c)) ));
00089     CALL_SUBTEST_5(( jacobi<MatrixXcd, std::complex<double> >(MatrixXcd(r,c)) ));
00090     // complex<float> is really important to test as it is the only way to cover conjugation issues in certain unaligned paths
00091     CALL_SUBTEST_6(( jacobi<MatrixXcf, float>(MatrixXcf(r,c)) ));
00092     CALL_SUBTEST_6(( jacobi<MatrixXcf, std::complex<float> >(MatrixXcf(r,c)) ));
00093     (void) r;
00094     (void) c;
00095   }
00096 }


re_vision
Author(s): Dorian Galvez-Lopez
autogenerated on Sun Jan 5 2014 11:31:29