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.
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 #define EIGEN_NO_STATIC_ASSERT
00026 
00027 #include "main.h"
00028 
00029 template<typename MatrixType> void adjoint(const MatrixType& m)
00030 {
00031   /* this test covers the following files:
00032      Transpose.h Conjugate.h Dot.h
00033   */
00034   typedef typename MatrixType::Index Index;
00035   typedef typename MatrixType::Scalar Scalar;
00036   typedef typename NumTraits<Scalar>::Real RealScalar;
00037   typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
00038   typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime> SquareMatrixType;
00039   
00040   Index rows = m.rows();
00041   Index cols = m.cols();
00042 
00043   MatrixType m1 = MatrixType::Random(rows, cols),
00044              m2 = MatrixType::Random(rows, cols),
00045              m3(rows, cols),
00046              mzero = MatrixType::Zero(rows, cols),
00047              identity = SquareMatrixType::Identity(rows, rows),
00048              square = SquareMatrixType::Random(rows, rows);
00049   VectorType v1 = VectorType::Random(rows),
00050              v2 = VectorType::Random(rows),
00051              v3 = VectorType::Random(rows),
00052              vzero = VectorType::Zero(rows);
00053 
00054   Scalar s1 = internal::random<Scalar>(),
00055          s2 = internal::random<Scalar>();
00056 
00057   // check basic compatibility of adjoint, transpose, conjugate
00058   VERIFY_IS_APPROX(m1.transpose().conjugate().adjoint(),    m1);
00059   VERIFY_IS_APPROX(m1.adjoint().conjugate().transpose(),    m1);
00060 
00061   // check multiplicative behavior
00062   VERIFY_IS_APPROX((m1.adjoint() * m2).adjoint(),           m2.adjoint() * m1);
00063   VERIFY_IS_APPROX((s1 * m1).adjoint(),                     internal::conj(s1) * m1.adjoint());
00064 
00065   // check basic properties of dot, norm, norm2
00066   typedef typename NumTraits<Scalar>::Real RealScalar;
00067   
00068   RealScalar ref = NumTraits<Scalar>::IsInteger ? 0 : std::max((s1 * v1 + s2 * v2).norm(),v3.norm());
00069   VERIFY(test_isApproxWithRef((s1 * v1 + s2 * v2).dot(v3),     internal::conj(s1) * v1.dot(v3) + internal::conj(s2) * v2.dot(v3), ref));
00070   VERIFY(test_isApproxWithRef(v3.dot(s1 * v1 + s2 * v2),       s1*v3.dot(v1)+s2*v3.dot(v2), ref));
00071   VERIFY_IS_APPROX(internal::conj(v1.dot(v2)),               v2.dot(v1));
00072   VERIFY_IS_APPROX(internal::real(v1.dot(v1)),                v1.squaredNorm());
00073   if(!NumTraits<Scalar>::IsInteger)
00074     VERIFY_IS_APPROX(v1.squaredNorm(),                v1.norm() * v1.norm());
00075   VERIFY_IS_MUCH_SMALLER_THAN(internal::abs(vzero.dot(v1)),  static_cast<RealScalar>(1));
00076 
00077   // check compatibility of dot and adjoint
00078   
00079   ref = NumTraits<Scalar>::IsInteger ? 0 : std::max(std::max(v1.norm(),v2.norm()),std::max((square * v2).norm(),(square.adjoint() * v1).norm()));
00080   VERIFY(test_isApproxWithRef(v1.dot(square * v2), (square.adjoint() * v1).dot(v2), ref));
00081 
00082   // like in testBasicStuff, test operator() to check const-qualification
00083   Index r = internal::random<Index>(0, rows-1),
00084       c = internal::random<Index>(0, cols-1);
00085   VERIFY_IS_APPROX(m1.conjugate()(r,c), internal::conj(m1(r,c)));
00086   VERIFY_IS_APPROX(m1.adjoint()(c,r), internal::conj(m1(r,c)));
00087 
00088   if(!NumTraits<Scalar>::IsInteger)
00089   {
00090     // check that Random().normalized() works: tricky as the random xpr must be evaluated by
00091     // normalized() in order to produce a consistent result.
00092     VERIFY_IS_APPROX(VectorType::Random(rows).normalized().norm(), RealScalar(1));
00093   }
00094 
00095   // check inplace transpose
00096   m3 = m1;
00097   m3.transposeInPlace();
00098   VERIFY_IS_APPROX(m3,m1.transpose());
00099   m3.transposeInPlace();
00100   VERIFY_IS_APPROX(m3,m1);
00101 
00102   // check inplace adjoint
00103   m3 = m1;
00104   m3.adjointInPlace();
00105   VERIFY_IS_APPROX(m3,m1.adjoint());
00106   m3.transposeInPlace();
00107   VERIFY_IS_APPROX(m3,m1.conjugate());
00108 
00109   // check mixed dot product
00110   typedef Matrix<RealScalar, MatrixType::RowsAtCompileTime, 1> RealVectorType;
00111   RealVectorType rv1 = RealVectorType::Random(rows);
00112   VERIFY_IS_APPROX(v1.dot(rv1.template cast<Scalar>()), v1.dot(rv1));
00113   VERIFY_IS_APPROX(rv1.template cast<Scalar>().dot(v1), rv1.dot(v1));
00114 }
00115 
00116 void test_adjoint()
00117 {
00118   for(int i = 0; i < g_repeat; i++) {
00119     CALL_SUBTEST_1( adjoint(Matrix<float, 1, 1>()) );
00120     CALL_SUBTEST_2( adjoint(Matrix3d()) );
00121     CALL_SUBTEST_3( adjoint(Matrix4f()) );
00122     CALL_SUBTEST_4( adjoint(MatrixXcf(internal::random<int>(1,50), internal::random<int>(1,50))) );
00123     CALL_SUBTEST_5( adjoint(MatrixXi(internal::random<int>(1,50), internal::random<int>(1,50))) );
00124     CALL_SUBTEST_6( adjoint(MatrixXf(internal::random<int>(1,50), internal::random<int>(1,50))) );
00125   }
00126   // test a large matrix only once
00127   CALL_SUBTEST_7( adjoint(Matrix<float, 100, 100>()) );
00128 
00129 #ifdef EIGEN_TEST_PART_4
00130   {
00131     MatrixXcf a(10,10), b(10,10);
00132     VERIFY_RAISES_ASSERT(a = a.transpose());
00133     VERIFY_RAISES_ASSERT(a = a.transpose() + b);
00134     VERIFY_RAISES_ASSERT(a = b + a.transpose());
00135     VERIFY_RAISES_ASSERT(a = a.conjugate().transpose());
00136     VERIFY_RAISES_ASSERT(a = a.adjoint());
00137     VERIFY_RAISES_ASSERT(a = a.adjoint() + b);
00138     VERIFY_RAISES_ASSERT(a = b + a.adjoint());
00139 
00140     // no assertion should be triggered for these cases:
00141     a.transpose() = a.transpose();
00142     a.transpose() += a.transpose();
00143     a.transpose() += a.transpose() + b;
00144     a.transpose() = a.adjoint();
00145     a.transpose() += a.adjoint();
00146     a.transpose() += a.adjoint() + b;
00147   }
00148 #endif
00149 }
00150 


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