conservative_resize.cpp
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2009 Hauke Heibel <hauke.heibel@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 
12 #include <Eigen/Core>
13 
14 using namespace Eigen;
15 
16 template <typename Scalar, int Storage>
18 {
20 
21  MatrixType m, n;
22 
23  // boundary cases ...
24  m = n = MatrixType::Random(50,50);
25  m.conservativeResize(1,50);
26  VERIFY_IS_APPROX(m, n.block(0,0,1,50));
27 
28  m = n = MatrixType::Random(50,50);
29  m.conservativeResize(50,1);
30  VERIFY_IS_APPROX(m, n.block(0,0,50,1));
31 
32  m = n = MatrixType::Random(50,50);
33  m.conservativeResize(50,50);
34  VERIFY_IS_APPROX(m, n.block(0,0,50,50));
35 
36  // random shrinking ...
37  for (int i=0; i<25; ++i)
38  {
39  const Index rows = internal::random<Index>(1,50);
40  const Index cols = internal::random<Index>(1,50);
41  m = n = MatrixType::Random(50,50);
42  m.conservativeResize(rows,cols);
43  VERIFY_IS_APPROX(m, n.block(0,0,rows,cols));
44  }
45 
46  // random growing with zeroing ...
47  for (int i=0; i<25; ++i)
48  {
49  const Index rows = internal::random<Index>(50,75);
50  const Index cols = internal::random<Index>(50,75);
51  m = n = MatrixType::Random(50,50);
52  m.conservativeResizeLike(MatrixType::Zero(rows,cols));
53  VERIFY_IS_APPROX(m.block(0,0,n.rows(),n.cols()), n);
54  VERIFY( rows<=50 || m.block(50,0,rows-50,cols).sum() == Scalar(0) );
55  VERIFY( cols<=50 || m.block(0,50,rows,cols-50).sum() == Scalar(0) );
56  }
57 }
58 
59 template <typename Scalar>
61 {
63 
64  VectorType m, n;
65 
66  // boundary cases ...
67  m = n = VectorType::Random(50);
68  m.conservativeResize(1);
69  VERIFY_IS_APPROX(m, n.segment(0,1));
70 
71  m = n = VectorType::Random(50);
72  m.conservativeResize(50);
73  VERIFY_IS_APPROX(m, n.segment(0,50));
74 
75  m = n = VectorType::Random(50);
76  m.conservativeResize(m.rows(),1);
77  VERIFY_IS_APPROX(m, n.segment(0,1));
78 
79  m = n = VectorType::Random(50);
80  m.conservativeResize(m.rows(),50);
81  VERIFY_IS_APPROX(m, n.segment(0,50));
82 
83  // random shrinking ...
84  for (int i=0; i<50; ++i)
85  {
86  const int size = internal::random<int>(1,50);
87  m = n = VectorType::Random(50);
88  m.conservativeResize(size);
89  VERIFY_IS_APPROX(m, n.segment(0,size));
90 
91  m = n = VectorType::Random(50);
92  m.conservativeResize(m.rows(), size);
93  VERIFY_IS_APPROX(m, n.segment(0,size));
94  }
95 
96  // random growing with zeroing ...
97  for (int i=0; i<50; ++i)
98  {
99  const int size = internal::random<int>(50,100);
100  m = n = VectorType::Random(50);
101  m.conservativeResizeLike(VectorType::Zero(size));
102  VERIFY_IS_APPROX(m.segment(0,50), n);
103  VERIFY( size<=50 || m.segment(50,size-50).sum() == Scalar(0) );
104 
105  m = n = VectorType::Random(50);
106  m.conservativeResizeLike(Matrix<Scalar,Dynamic,Dynamic>::Zero(1,size));
107  VERIFY_IS_APPROX(m.segment(0,50), n);
108  VERIFY( size<=50 || m.segment(50,size-50).sum() == Scalar(0) );
109  }
110 }
111 
113 {
114  for(int i=0; i<g_repeat; ++i)
115  {
116  CALL_SUBTEST_1((run_matrix_tests<int, Eigen::RowMajor>()));
117  CALL_SUBTEST_1((run_matrix_tests<int, Eigen::ColMajor>()));
118  CALL_SUBTEST_2((run_matrix_tests<float, Eigen::RowMajor>()));
119  CALL_SUBTEST_2((run_matrix_tests<float, Eigen::ColMajor>()));
120  CALL_SUBTEST_3((run_matrix_tests<double, Eigen::RowMajor>()));
121  CALL_SUBTEST_3((run_matrix_tests<double, Eigen::ColMajor>()));
122  CALL_SUBTEST_4((run_matrix_tests<std::complex<float>, Eigen::RowMajor>()));
123  CALL_SUBTEST_4((run_matrix_tests<std::complex<float>, Eigen::ColMajor>()));
124  CALL_SUBTEST_5((run_matrix_tests<std::complex<double>, Eigen::RowMajor>()));
125  CALL_SUBTEST_6((run_matrix_tests<std::complex<double>, Eigen::ColMajor>()));
126 
127  CALL_SUBTEST_1((run_vector_tests<int>()));
128  CALL_SUBTEST_2((run_vector_tests<float>()));
129  CALL_SUBTEST_3((run_vector_tests<double>()));
130  CALL_SUBTEST_4((run_vector_tests<std::complex<float> >()));
131  CALL_SUBTEST_5((run_vector_tests<std::complex<double> >()));
132  }
133 }
Matrix3f m
SCALAR Scalar
Definition: bench_gemm.cpp:33
void run_matrix_tests()
void run_vector_tests()
int n
Namespace containing all symbols from the Eigen library.
Definition: jet.h:637
MatrixXf MatrixType
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
#define VERIFY_IS_APPROX(a, b)
static int g_repeat
Definition: main.h:144
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:33
void test_conservative_resize()
#define VERIFY(a)
Definition: main.h:325
The matrix class, also used for vectors and row-vectors.


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:41:50