eigen2_sum.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) 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 matrixSum(const MatrixType& m)
00028 {
00029   typedef typename MatrixType::Scalar Scalar;
00030 
00031   int rows = m.rows();
00032   int cols = m.cols();
00033 
00034   MatrixType m1 = MatrixType::Random(rows, cols);
00035 
00036   VERIFY_IS_MUCH_SMALLER_THAN(MatrixType::Zero(rows, cols).sum(), Scalar(1));
00037   VERIFY_IS_APPROX(MatrixType::Ones(rows, cols).sum(), Scalar(float(rows*cols))); // the float() here to shut up excessive MSVC warning about int->complex conversion being lossy
00038   Scalar x = Scalar(0);
00039   for(int i = 0; i < rows; i++) for(int j = 0; j < cols; j++) x += m1(i,j);
00040   VERIFY_IS_APPROX(m1.sum(), x);
00041 }
00042 
00043 template<typename VectorType> void vectorSum(const VectorType& w)
00044 {
00045   typedef typename VectorType::Scalar Scalar;
00046   int size = w.size();
00047 
00048   VectorType v = VectorType::Random(size);
00049   for(int i = 1; i < size; i++)
00050   {
00051     Scalar s = Scalar(0);
00052     for(int j = 0; j < i; j++) s += v[j];
00053     VERIFY_IS_APPROX(s, v.start(i).sum());
00054   }
00055 
00056   for(int i = 0; i < size-1; i++)
00057   {
00058     Scalar s = Scalar(0);
00059     for(int j = i; j < size; j++) s += v[j];
00060     VERIFY_IS_APPROX(s, v.end(size-i).sum());
00061   }
00062 
00063   for(int i = 0; i < size/2; i++)
00064   {
00065     Scalar s = Scalar(0);
00066     for(int j = i; j < size-i; j++) s += v[j];
00067     VERIFY_IS_APPROX(s, v.segment(i, size-2*i).sum());
00068   }
00069 }
00070 
00071 void test_eigen2_sum()
00072 {
00073   for(int i = 0; i < g_repeat; i++) {
00074     CALL_SUBTEST_1( matrixSum(Matrix<float, 1, 1>()) );
00075     CALL_SUBTEST_2( matrixSum(Matrix2f()) );
00076     CALL_SUBTEST_3( matrixSum(Matrix4d()) );
00077     CALL_SUBTEST_4( matrixSum(MatrixXcf(3, 3)) );
00078     CALL_SUBTEST_5( matrixSum(MatrixXf(8, 12)) );
00079     CALL_SUBTEST_6( matrixSum(MatrixXi(8, 12)) );
00080   }
00081   for(int i = 0; i < g_repeat; i++) {
00082     CALL_SUBTEST_5( vectorSum(VectorXf(5)) );
00083     CALL_SUBTEST_7( vectorSum(VectorXd(10)) );
00084     CALL_SUBTEST_5( vectorSum(VectorXf(33)) );
00085   }
00086 }


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