source: pacpussensors/trunk/Vislab/lib3dv-1.2.0/lib3dv/eigen/test/eigen2/eigen2_sum.cpp

Last change on this file was 136, checked in by ldecherf, 8 years ago

Doc

File size: 2.2 KB
Line 
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra. Eigen itself is part of the KDE project.
3//
4// Copyright (C) 2008 Benoit Jacob <jacob.benoit.1@gmail.com>
5//
6// This Source Code Form is subject to the terms of the Mozilla
7// Public License v. 2.0. If a copy of the MPL was not distributed
8// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9
10#include "main.h"
11
12template<typename MatrixType> void matrixSum(const MatrixType& m)
13{
14 typedef typename MatrixType::Scalar Scalar;
15
16 int rows = m.rows();
17 int cols = m.cols();
18
19 MatrixType m1 = MatrixType::Random(rows, cols);
20
21 VERIFY_IS_MUCH_SMALLER_THAN(MatrixType::Zero(rows, cols).sum(), Scalar(1));
22 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
23 Scalar x = Scalar(0);
24 for(int i = 0; i < rows; i++) for(int j = 0; j < cols; j++) x += m1(i,j);
25 VERIFY_IS_APPROX(m1.sum(), x);
26}
27
28template<typename VectorType> void vectorSum(const VectorType& w)
29{
30 typedef typename VectorType::Scalar Scalar;
31 int size = w.size();
32
33 VectorType v = VectorType::Random(size);
34 for(int i = 1; i < size; i++)
35 {
36 Scalar s = Scalar(0);
37 for(int j = 0; j < i; j++) s += v[j];
38 VERIFY_IS_APPROX(s, v.start(i).sum());
39 }
40
41 for(int i = 0; i < size-1; i++)
42 {
43 Scalar s = Scalar(0);
44 for(int j = i; j < size; j++) s += v[j];
45 VERIFY_IS_APPROX(s, v.end(size-i).sum());
46 }
47
48 for(int i = 0; i < size/2; i++)
49 {
50 Scalar s = Scalar(0);
51 for(int j = i; j < size-i; j++) s += v[j];
52 VERIFY_IS_APPROX(s, v.segment(i, size-2*i).sum());
53 }
54}
55
56void test_eigen2_sum()
57{
58 for(int i = 0; i < g_repeat; i++) {
59 CALL_SUBTEST_1( matrixSum(Matrix<float, 1, 1>()) );
60 CALL_SUBTEST_2( matrixSum(Matrix2f()) );
61 CALL_SUBTEST_3( matrixSum(Matrix4d()) );
62 CALL_SUBTEST_4( matrixSum(MatrixXcf(3, 3)) );
63 CALL_SUBTEST_5( matrixSum(MatrixXf(8, 12)) );
64 CALL_SUBTEST_6( matrixSum(MatrixXi(8, 12)) );
65 }
66 for(int i = 0; i < g_repeat; i++) {
67 CALL_SUBTEST_5( vectorSum(VectorXf(5)) );
68 CALL_SUBTEST_7( vectorSum(VectorXd(10)) );
69 CALL_SUBTEST_5( vectorSum(VectorXf(33)) );
70 }
71}
Note: See TracBrowser for help on using the repository browser.