zerosized.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) 2011 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 
12 
13 template<typename MatrixType> void zeroReduction(const MatrixType& m) {
14  // Reductions that must hold for zero sized objects
15  VERIFY(m.all());
16  VERIFY(!m.any());
17  VERIFY(m.prod()==1);
18  VERIFY(m.sum()==0);
19  VERIFY(m.norm()==0);
20  VERIFY(m.squaredNorm()==0);
21  VERIFY(m.count()==0);
22  VERIFY(m.allFinite());
23  VERIFY(!m.hasNaN());
24  VERIFY_RAISES_ASSERT( m.minCoeff() );
25  VERIFY_RAISES_ASSERT( m.maxCoeff() );
26  Index i,j;
27  VERIFY_RAISES_ASSERT( m.minCoeff(&i,&j) );
28  VERIFY_RAISES_ASSERT( m.maxCoeff(&i,&j) );
29  VERIFY_RAISES_ASSERT( m.reshaped().minCoeff(&i) );
30  VERIFY_RAISES_ASSERT( m.reshaped().maxCoeff(&i) );
31 }
32 
33 
34 template<typename MatrixType> void zeroSizedMatrix()
35 {
36  MatrixType t1;
37  typedef typename MatrixType::Scalar Scalar;
38 
39  if (MatrixType::SizeAtCompileTime == Dynamic || MatrixType::SizeAtCompileTime == 0)
40  {
41  zeroReduction(t1);
42  if (MatrixType::RowsAtCompileTime == Dynamic)
43  VERIFY(t1.rows() == 0);
44  if (MatrixType::ColsAtCompileTime == Dynamic)
45  VERIFY(t1.cols() == 0);
46 
47  if (MatrixType::RowsAtCompileTime == Dynamic && MatrixType::ColsAtCompileTime == Dynamic)
48  {
49 
50  MatrixType t2(0, 0), t3(t1);
51  VERIFY(t2.rows() == 0);
52  VERIFY(t2.cols() == 0);
53 
54  zeroReduction(t2);
55  VERIFY(t1==t2);
56  }
57  }
58 
59  if(MatrixType::MaxColsAtCompileTime!=0 && MatrixType::MaxRowsAtCompileTime!=0)
60  {
61  Index rows = MatrixType::RowsAtCompileTime==Dynamic ? internal::random<Index>(1,10) : Index(MatrixType::RowsAtCompileTime);
62  Index cols = MatrixType::ColsAtCompileTime==Dynamic ? internal::random<Index>(1,10) : Index(MatrixType::ColsAtCompileTime);
64  zeroReduction(m.template block<0,MatrixType::ColsAtCompileTime>(0,0,0,cols));
65  zeroReduction(m.template block<MatrixType::RowsAtCompileTime,0>(0,0,rows,0));
66  zeroReduction(m.template block<0,1>(0,0));
67  zeroReduction(m.template block<1,0>(0,0));
68  Matrix<Scalar,Dynamic,Dynamic> prod = m.template block<MatrixType::RowsAtCompileTime,0>(0,0,rows,0) * m.template block<0,MatrixType::ColsAtCompileTime>(0,0,0,cols);
69  VERIFY(prod.rows()==rows && prod.cols()==cols);
70  VERIFY(prod.isZero());
71  prod = m.template block<1,0>(0,0) * m.template block<0,1>(0,0);
72  VERIFY(prod.size()==1);
73  VERIFY(prod.isZero());
74  }
75 }
76 
77 template<typename VectorType> void zeroSizedVector()
78 {
79  VectorType t1;
80 
81  if (VectorType::SizeAtCompileTime == Dynamic || VectorType::SizeAtCompileTime==0)
82  {
83  zeroReduction(t1);
84  VERIFY(t1.size() == 0);
85  VectorType t2(DenseIndex(0)); // DenseIndex disambiguates with 0-the-null-pointer (error with gcc 4.4 and MSVC8)
86  VERIFY(t2.size() == 0);
87  zeroReduction(t2);
88 
89  VERIFY(t1==t2);
90  }
91 }
92 
94 {
95  zeroSizedMatrix<Matrix2d>();
96  zeroSizedMatrix<Matrix3i>();
97  zeroSizedMatrix<Matrix<float, 2, Dynamic> >();
98  zeroSizedMatrix<MatrixXf>();
99  zeroSizedMatrix<Matrix<float, 0, 0> >();
100  zeroSizedMatrix<Matrix<float, Dynamic, 0, 0, 0, 0> >();
101  zeroSizedMatrix<Matrix<float, 0, Dynamic, 0, 0, 0> >();
102  zeroSizedMatrix<Matrix<float, Dynamic, Dynamic, 0, 0, 0> >();
103  zeroSizedMatrix<Matrix<float, 0, 4> >();
104  zeroSizedMatrix<Matrix<float, 4, 0> >();
105 
106  zeroSizedVector<Vector2d>();
107  zeroSizedVector<Vector3i>();
108  zeroSizedVector<VectorXf>();
109  zeroSizedVector<Matrix<float, 0, 1> >();
110  zeroSizedVector<Matrix<float, 1, 0> >();
111 }
zeroSizedVector
void zeroSizedVector()
Definition: zerosized.cpp:77
MatrixType
MatrixXf MatrixType
Definition: benchmark-blocking-sizes.cpp:52
rows
int rows
Definition: Tutorial_commainit_02.cpp:1
VERIFY_RAISES_ASSERT
#define VERIFY_RAISES_ASSERT(a)
Definition: main.h:340
EIGEN_DECLARE_TEST
EIGEN_DECLARE_TEST(zerosized)
Definition: zerosized.cpp:93
j
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2
Eigen::Dynamic
const int Dynamic
Definition: Constants.h:22
m
Matrix3f m
Definition: AngleAxis_mimic_euler.cpp:1
zeroSizedMatrix
void zeroSizedMatrix()
Definition: zerosized.cpp:34
main.h
Eigen::DenseIndex
EIGEN_DEFAULT_DENSE_INDEX_TYPE DenseIndex
Definition: Meta.h:66
Eigen::Matrix< Scalar, Dynamic, Dynamic >
VectorType
Definition: FFTW.cpp:65
cols
int cols
Definition: Tutorial_commainit_02.cpp:1
prod
EIGEN_DONT_INLINE void prod(const Lhs &a, const Rhs &b, Res &c)
Definition: product_threshold.cpp:39
zeroReduction
void zeroReduction(const MatrixType &m)
Definition: zerosized.cpp:13
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
Scalar
SCALAR Scalar
Definition: bench_gemm.cpp:46
VERIFY
#define VERIFY(a)
Definition: main.h:380
Eigen::Index
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74


gtsam
Author(s):
autogenerated on Sun Dec 22 2024 04:18:27