swap.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) 2009 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 #include "main.h"
00027 
00028 template<typename T>
00029 struct other_matrix_type
00030 {
00031   typedef int type;
00032 };
00033 
00034 template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
00035 struct other_matrix_type<Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> >
00036 {
00037   typedef Matrix<_Scalar, _Rows, _Cols, _Options^RowMajor, _MaxRows, _MaxCols> type;
00038 };
00039 
00040 template<typename MatrixType> void swap(const MatrixType& m)
00041 {
00042   typedef typename other_matrix_type<MatrixType>::type OtherMatrixType;
00043   typedef typename MatrixType::Scalar Scalar;
00044 
00045   eigen_assert((!internal::is_same<MatrixType,OtherMatrixType>::value));
00046   typename MatrixType::Index rows = m.rows();
00047   typename MatrixType::Index cols = m.cols();
00048   
00049   // construct 3 matrix guaranteed to be distinct
00050   MatrixType m1 = MatrixType::Random(rows,cols);
00051   MatrixType m2 = MatrixType::Random(rows,cols) + Scalar(100) * MatrixType::Identity(rows,cols);
00052   OtherMatrixType m3 = OtherMatrixType::Random(rows,cols) + Scalar(200) * OtherMatrixType::Identity(rows,cols);
00053   
00054   MatrixType m1_copy = m1;
00055   MatrixType m2_copy = m2;
00056   OtherMatrixType m3_copy = m3;
00057   
00058   // test swapping 2 matrices of same type
00059   m1.swap(m2);
00060   VERIFY_IS_APPROX(m1,m2_copy);
00061   VERIFY_IS_APPROX(m2,m1_copy);
00062   m1 = m1_copy;
00063   m2 = m2_copy;
00064   
00065   // test swapping 2 matrices of different types
00066   m1.swap(m3);
00067   VERIFY_IS_APPROX(m1,m3_copy);
00068   VERIFY_IS_APPROX(m3,m1_copy);
00069   m1 = m1_copy;
00070   m3 = m3_copy;
00071   
00072   // test swapping matrix with expression
00073   m1.swap(m2.block(0,0,rows,cols));
00074   VERIFY_IS_APPROX(m1,m2_copy);
00075   VERIFY_IS_APPROX(m2,m1_copy);
00076   m1 = m1_copy;
00077   m2 = m2_copy;
00078 
00079   // test swapping two expressions of different types
00080   m1.transpose().swap(m3.transpose());
00081   VERIFY_IS_APPROX(m1,m3_copy);
00082   VERIFY_IS_APPROX(m3,m1_copy);
00083   m1 = m1_copy;
00084   m3 = m3_copy;
00085   
00086   // test assertion on mismatching size -- matrix case
00087   VERIFY_RAISES_ASSERT(m1.swap(m1.row(0)));
00088   // test assertion on mismatching size -- xpr case
00089   VERIFY_RAISES_ASSERT(m1.row(0).swap(m1));
00090 }
00091 
00092 void test_swap()
00093 {
00094   CALL_SUBTEST_1( swap(Matrix3f()) ); // fixed size, no vectorization 
00095   CALL_SUBTEST_2( swap(Matrix4d()) ); // fixed size, possible vectorization 
00096   CALL_SUBTEST_3( swap(MatrixXd(3,3)) ); // dyn size, no vectorization 
00097   CALL_SUBTEST_4( swap(MatrixXf(30,30)) ); // dyn size, possible vectorization 
00098 }


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