source: pacpussensors/trunk/Vislab/lib3dv/eigen/test/nullary.cpp@ 139

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

Doc

File size: 4.4 KB
Line 
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2010-2011 Jitse Niesen <jitse@maths.leeds.ac.uk>
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>
13bool equalsIdentity(const MatrixType& A)
14{
15 typedef typename MatrixType::Index Index;
16 typedef typename MatrixType::Scalar Scalar;
17 Scalar zero = static_cast<Scalar>(0);
18
19 bool offDiagOK = true;
20 for (Index i = 0; i < A.rows(); ++i) {
21 for (Index j = i+1; j < A.cols(); ++j) {
22 offDiagOK = offDiagOK && (A(i,j) == zero);
23 }
24 }
25 for (Index i = 0; i < A.rows(); ++i) {
26 for (Index j = 0; j < (std::min)(i, A.cols()); ++j) {
27 offDiagOK = offDiagOK && (A(i,j) == zero);
28 }
29 }
30
31 bool diagOK = (A.diagonal().array() == 1).all();
32 return offDiagOK && diagOK;
33}
34
35template<typename VectorType>
36void testVectorType(const VectorType& base)
37{
38 typedef typename internal::traits<VectorType>::Index Index;
39 typedef typename internal::traits<VectorType>::Scalar Scalar;
40
41 const Index size = base.size();
42
43 Scalar high = internal::random<Scalar>(-500,500);
44 Scalar low = (size == 1 ? high : internal::random<Scalar>(-500,500));
45 if (low>high) std::swap(low,high);
46
47 const Scalar step = ((size == 1) ? 1 : (high-low)/(size-1));
48
49 // check whether the result yields what we expect it to do
50 VectorType m(base);
51 m.setLinSpaced(size,low,high);
52
53 VectorType n(size);
54 for (int i=0; i<size; ++i)
55 n(i) = low+i*step;
56
57 VERIFY_IS_APPROX(m,n);
58
59 // random access version
60 m = VectorType::LinSpaced(size,low,high);
61 VERIFY_IS_APPROX(m,n);
62
63 // Assignment of a RowVectorXd to a MatrixXd (regression test for bug #79).
64 VERIFY( (MatrixXd(RowVectorXd::LinSpaced(3, 0, 1)) - RowVector3d(0, 0.5, 1)).norm() < std::numeric_limits<Scalar>::epsilon() );
65
66 // These guys sometimes fail! This is not good. Any ideas how to fix them!?
67 //VERIFY( m(m.size()-1) == high );
68 //VERIFY( m(0) == low );
69
70 // sequential access version
71 m = VectorType::LinSpaced(Sequential,size,low,high);
72 VERIFY_IS_APPROX(m,n);
73
74 // These guys sometimes fail! This is not good. Any ideas how to fix them!?
75 //VERIFY( m(m.size()-1) == high );
76 //VERIFY( m(0) == low );
77
78 // check whether everything works with row and col major vectors
79 Matrix<Scalar,Dynamic,1> row_vector(size);
80 Matrix<Scalar,1,Dynamic> col_vector(size);
81 row_vector.setLinSpaced(size,low,high);
82 col_vector.setLinSpaced(size,low,high);
83 // when using the extended precision (e.g., FPU) the relative error might exceed 1 bit
84 // when computing the squared sum in isApprox, thus the 2x factor.
85 VERIFY( row_vector.isApprox(col_vector.transpose(), Scalar(2)*NumTraits<Scalar>::epsilon()));
86
87 Matrix<Scalar,Dynamic,1> size_changer(size+50);
88 size_changer.setLinSpaced(size,low,high);
89 VERIFY( size_changer.size() == size );
90
91 typedef Matrix<Scalar,1,1> ScalarMatrix;
92 ScalarMatrix scalar;
93 scalar.setLinSpaced(1,low,high);
94 VERIFY_IS_APPROX( scalar, ScalarMatrix::Constant(high) );
95 VERIFY_IS_APPROX( ScalarMatrix::LinSpaced(1,low,high), ScalarMatrix::Constant(high) );
96
97 // regression test for bug 526 (linear vectorized transversal)
98 if (size > 1) {
99 m.tail(size-1).setLinSpaced(low, high);
100 VERIFY_IS_APPROX(m(size-1), high);
101 }
102}
103
104template<typename MatrixType>
105void testMatrixType(const MatrixType& m)
106{
107 typedef typename MatrixType::Index Index;
108 const Index rows = m.rows();
109 const Index cols = m.cols();
110
111 MatrixType A;
112 A.setIdentity(rows, cols);
113 VERIFY(equalsIdentity(A));
114 VERIFY(equalsIdentity(MatrixType::Identity(rows, cols)));
115}
116
117void test_nullary()
118{
119 CALL_SUBTEST_1( testMatrixType(Matrix2d()) );
120 CALL_SUBTEST_2( testMatrixType(MatrixXcf(internal::random<int>(1,300),internal::random<int>(1,300))) );
121 CALL_SUBTEST_3( testMatrixType(MatrixXf(internal::random<int>(1,300),internal::random<int>(1,300))) );
122
123 for(int i = 0; i < g_repeat; i++) {
124 CALL_SUBTEST_4( testVectorType(VectorXd(internal::random<int>(1,300))) );
125 CALL_SUBTEST_5( testVectorType(Vector4d()) ); // regression test for bug 232
126 CALL_SUBTEST_6( testVectorType(Vector3d()) );
127 CALL_SUBTEST_7( testVectorType(VectorXf(internal::random<int>(1,300))) );
128 CALL_SUBTEST_8( testVectorType(Vector3f()) );
129 CALL_SUBTEST_8( testVectorType(Matrix<float,1,1>()) );
130 }
131}
Note: See TracBrowser for help on using the repository browser.