conservative_resize.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) 2009 Hauke Heibel <hauke.heibel@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 or1 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 #include <Eigen/Core>
00028 
00029 using namespace Eigen;
00030 
00031 template <typename Scalar, int Storage>
00032 void run_matrix_tests()
00033 {
00034   typedef Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic, Storage> MatrixType;
00035   typedef typename MatrixType::Index Index;
00036 
00037   MatrixType m, n;
00038 
00039   // boundary cases ...
00040   m = n = MatrixType::Random(50,50);
00041   m.conservativeResize(1,50);
00042   VERIFY_IS_APPROX(m, n.block(0,0,1,50));
00043 
00044   m = n = MatrixType::Random(50,50);
00045   m.conservativeResize(50,1);
00046   VERIFY_IS_APPROX(m, n.block(0,0,50,1));
00047 
00048   m = n = MatrixType::Random(50,50);
00049   m.conservativeResize(50,50);
00050   VERIFY_IS_APPROX(m, n.block(0,0,50,50));
00051 
00052   // random shrinking ...
00053   for (int i=0; i<25; ++i)
00054   {
00055     const Index rows = internal::random<Index>(1,50);
00056     const Index cols = internal::random<Index>(1,50);
00057     m = n = MatrixType::Random(50,50);
00058     m.conservativeResize(rows,cols);
00059     VERIFY_IS_APPROX(m, n.block(0,0,rows,cols));
00060   }
00061 
00062   // random growing with zeroing ...
00063   for (int i=0; i<25; ++i)
00064   {
00065     const Index rows = internal::random<Index>(50,75);
00066     const Index cols = internal::random<Index>(50,75);
00067     m = n = MatrixType::Random(50,50);
00068     m.conservativeResizeLike(MatrixType::Zero(rows,cols));
00069     VERIFY_IS_APPROX(m.block(0,0,n.rows(),n.cols()), n);
00070     VERIFY( rows<=50 || m.block(50,0,rows-50,cols).sum() == Scalar(0) );
00071     VERIFY( cols<=50 || m.block(0,50,rows,cols-50).sum() == Scalar(0) );
00072   }
00073 }
00074 
00075 template <typename Scalar>
00076 void run_vector_tests()
00077 {
00078   typedef Matrix<Scalar, 1, Eigen::Dynamic> MatrixType;
00079 
00080   MatrixType m, n;
00081 
00082   // boundary cases ...
00083   m = n = MatrixType::Random(50);
00084   m.conservativeResize(1);
00085   VERIFY_IS_APPROX(m, n.segment(0,1));
00086 
00087   m = n = MatrixType::Random(50);
00088   m.conservativeResize(50);
00089   VERIFY_IS_APPROX(m, n.segment(0,50));
00090 
00091   // random shrinking ...
00092   for (int i=0; i<50; ++i)
00093   {
00094     const int size = internal::random<int>(1,50);
00095     m = n = MatrixType::Random(50);
00096     m.conservativeResize(size);
00097     VERIFY_IS_APPROX(m, n.segment(0,size));
00098   }
00099 
00100   // random growing with zeroing ...
00101   for (int i=0; i<50; ++i)
00102   {
00103     const int size = internal::random<int>(50,100);
00104     m = n = MatrixType::Random(50);
00105     m.conservativeResizeLike(MatrixType::Zero(size));
00106     VERIFY_IS_APPROX(m.segment(0,50), n);
00107     VERIFY( size<=50 || m.segment(50,size-50).sum() == Scalar(0) );
00108   }
00109 }
00110 
00111 void test_conservative_resize()
00112 {
00113   CALL_SUBTEST_1((run_matrix_tests<int, Eigen::RowMajor>()));
00114   CALL_SUBTEST_1((run_matrix_tests<int, Eigen::ColMajor>()));
00115   CALL_SUBTEST_2((run_matrix_tests<float, Eigen::RowMajor>()));
00116   CALL_SUBTEST_2((run_matrix_tests<float, Eigen::ColMajor>()));
00117   CALL_SUBTEST_3((run_matrix_tests<double, Eigen::RowMajor>()));
00118   CALL_SUBTEST_3((run_matrix_tests<double, Eigen::ColMajor>()));
00119   CALL_SUBTEST_4((run_matrix_tests<std::complex<float>, Eigen::RowMajor>()));
00120   CALL_SUBTEST_4((run_matrix_tests<std::complex<float>, Eigen::ColMajor>()));
00121   CALL_SUBTEST_5((run_matrix_tests<std::complex<double>, Eigen::RowMajor>()));
00122   CALL_SUBTEST_6((run_matrix_tests<std::complex<double>, Eigen::ColMajor>()));
00123 
00124   CALL_SUBTEST_1((run_vector_tests<int>()));
00125   CALL_SUBTEST_2((run_vector_tests<float>()));
00126   CALL_SUBTEST_3((run_vector_tests<double>()));
00127   CALL_SUBTEST_4((run_vector_tests<std::complex<float> >()));
00128   CALL_SUBTEST_5((run_vector_tests<std::complex<double> >()));
00129 }


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