eigen2_adjoint.cpp
Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra. Eigen itself is part of the KDE project.
00003 //
00004 // Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
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 #include "main.h"
00026 
00027 template<typename MatrixType> void adjoint(const MatrixType& m)
00028 {
00029   /* this test covers the following files:
00030      Transpose.h Conjugate.h Dot.h
00031   */
00032 
00033   typedef typename MatrixType::Scalar Scalar;
00034   typedef typename NumTraits<Scalar>::Real RealScalar;
00035   typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
00036   typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime> SquareMatrixType;
00037   int rows = m.rows();
00038   int cols = m.cols();
00039 
00040   RealScalar largerEps = test_precision<RealScalar>();
00041   if (ei_is_same_type<RealScalar,float>::ret)
00042     largerEps = RealScalar(1e-3f);
00043 
00044   MatrixType m1 = MatrixType::Random(rows, cols),
00045              m2 = MatrixType::Random(rows, cols),
00046              m3(rows, cols),
00047              mzero = MatrixType::Zero(rows, cols),
00048              identity = SquareMatrixType::Identity(rows, rows),
00049              square = SquareMatrixType::Random(rows, rows);
00050   VectorType v1 = VectorType::Random(rows),
00051              v2 = VectorType::Random(rows),
00052              v3 = VectorType::Random(rows),
00053              vzero = VectorType::Zero(rows);
00054 
00055   Scalar s1 = ei_random<Scalar>(),
00056          s2 = ei_random<Scalar>();
00057 
00058   // check basic compatibility of adjoint, transpose, conjugate
00059   VERIFY_IS_APPROX(m1.transpose().conjugate().adjoint(),    m1);
00060   VERIFY_IS_APPROX(m1.adjoint().conjugate().transpose(),    m1);
00061 
00062   // check multiplicative behavior
00063   VERIFY_IS_APPROX((m1.adjoint() * m2).adjoint(),           m2.adjoint() * m1);
00064   VERIFY_IS_APPROX((s1 * m1).adjoint(),                     ei_conj(s1) * m1.adjoint());
00065 
00066   // check basic properties of dot, norm, norm2
00067   typedef typename NumTraits<Scalar>::Real RealScalar;
00068   VERIFY(ei_isApprox((s1 * v1 + s2 * v2).eigen2_dot(v3),   s1 * v1.eigen2_dot(v3) + s2 * v2.eigen2_dot(v3), largerEps));
00069   VERIFY(ei_isApprox(v3.eigen2_dot(s1 * v1 + s2 * v2),     ei_conj(s1)*v3.eigen2_dot(v1)+ei_conj(s2)*v3.eigen2_dot(v2), largerEps));
00070   VERIFY_IS_APPROX(ei_conj(v1.eigen2_dot(v2)),               v2.eigen2_dot(v1));
00071   VERIFY_IS_APPROX(ei_real(v1.eigen2_dot(v1)),               v1.squaredNorm());
00072   if(NumTraits<Scalar>::HasFloatingPoint)
00073     VERIFY_IS_APPROX(v1.squaredNorm(),                      v1.norm() * v1.norm());
00074   VERIFY_IS_MUCH_SMALLER_THAN(ei_abs(vzero.eigen2_dot(v1)),  static_cast<RealScalar>(1));
00075   if(NumTraits<Scalar>::HasFloatingPoint)
00076     VERIFY_IS_MUCH_SMALLER_THAN(vzero.norm(),         static_cast<RealScalar>(1));
00077 
00078   // check compatibility of dot and adjoint
00079   VERIFY(ei_isApprox(v1.eigen2_dot(square * v2), (square.adjoint() * v1).eigen2_dot(v2), largerEps));
00080 
00081   // like in testBasicStuff, test operator() to check const-qualification
00082   int r = ei_random<int>(0, rows-1),
00083       c = ei_random<int>(0, cols-1);
00084   VERIFY_IS_APPROX(m1.conjugate()(r,c), ei_conj(m1(r,c)));
00085   VERIFY_IS_APPROX(m1.adjoint()(c,r), ei_conj(m1(r,c)));
00086 
00087   if(NumTraits<Scalar>::HasFloatingPoint)
00088   {
00089     // check that Random().normalized() works: tricky as the random xpr must be evaluated by
00090     // normalized() in order to produce a consistent result.
00091     VERIFY_IS_APPROX(VectorType::Random(rows).normalized().norm(), RealScalar(1));
00092   }
00093 
00094   // check inplace transpose
00095   m3 = m1;
00096   m3.transposeInPlace();
00097   VERIFY_IS_APPROX(m3,m1.transpose());
00098   m3.transposeInPlace();
00099   VERIFY_IS_APPROX(m3,m1);
00100   
00101 }
00102 
00103 void test_eigen2_adjoint()
00104 {
00105   for(int i = 0; i < g_repeat; i++) {
00106     CALL_SUBTEST_1( adjoint(Matrix<float, 1, 1>()) );
00107     CALL_SUBTEST_2( adjoint(Matrix3d()) );
00108     CALL_SUBTEST_3( adjoint(Matrix4f()) );
00109     CALL_SUBTEST_4( adjoint(MatrixXcf(4, 4)) );
00110     CALL_SUBTEST_5( adjoint(MatrixXi(8, 12)) );
00111     CALL_SUBTEST_6( adjoint(MatrixXf(21, 21)) );
00112   }
00113   // test a large matrix only once
00114   CALL_SUBTEST_7( adjoint(Matrix<float, 100, 100>()) );
00115 }
00116 


libicr
Author(s): Robert Krug
autogenerated on Mon Jan 6 2014 11:32:37