adfa5456 commited on
Commit
0162054
·
verified ·
1 Parent(s): 56ba8bc

Add files using upload-large-folder tool

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. include/eigen/test/AnnoyingScalar.h +165 -0
  2. include/eigen/test/SafeScalar.h +30 -0
  3. include/eigen/test/adjoint.cpp +219 -0
  4. include/eigen/test/array_for_matrix.cpp +341 -0
  5. include/eigen/test/array_of_string.cpp +32 -0
  6. include/eigen/test/array_replicate.cpp +81 -0
  7. include/eigen/test/bandmatrix.cpp +71 -0
  8. include/eigen/test/basicstuff.cpp +356 -0
  9. include/eigen/test/bicgstab.cpp +34 -0
  10. include/eigen/test/blasutil.cpp +210 -0
  11. include/eigen/test/block.cpp +317 -0
  12. include/eigen/test/bug1213.cpp +13 -0
  13. include/eigen/test/bug1213.h +8 -0
  14. include/eigen/test/bug1213_main.cpp +18 -0
  15. include/eigen/test/cholmod_support.cpp +69 -0
  16. include/eigen/test/conjugate_gradient.cpp +34 -0
  17. include/eigen/test/conservative_resize.cpp +167 -0
  18. include/eigen/test/constructor.cpp +98 -0
  19. include/eigen/test/corners.cpp +117 -0
  20. include/eigen/test/ctorleak.cpp +81 -0
  21. include/eigen/test/dense_storage.cpp +190 -0
  22. include/eigen/test/determinant.cpp +66 -0
  23. include/eigen/test/dontalign.cpp +62 -0
  24. include/eigen/test/dynalloc.cpp +177 -0
  25. include/eigen/test/eigensolver_complex.cpp +176 -0
  26. include/eigen/test/eigensolver_generic.cpp +247 -0
  27. include/eigen/test/evaluator_common.h +0 -0
  28. include/eigen/test/evaluators.cpp +525 -0
  29. include/eigen/test/exceptions.cpp +49 -0
  30. include/eigen/test/fastmath.cpp +99 -0
  31. include/eigen/test/first_aligned.cpp +51 -0
  32. include/eigen/test/geo_alignedbox.cpp +531 -0
  33. include/eigen/test/geo_eulerangles.cpp +112 -0
  34. include/eigen/test/geo_homogeneous.cpp +125 -0
  35. include/eigen/test/geo_orthomethods.cpp +134 -0
  36. include/eigen/test/geo_parametrizedline.cpp +125 -0
  37. include/eigen/test/geo_quaternion.cpp +332 -0
  38. include/eigen/test/geo_transformations.cpp +737 -0
  39. include/eigen/test/half_float.cpp +352 -0
  40. include/eigen/test/householder.cpp +148 -0
  41. include/eigen/test/incomplete_cholesky.cpp +69 -0
  42. include/eigen/test/indexed_view.cpp +473 -0
  43. include/eigen/test/initializer_list_construction.cpp +385 -0
  44. include/eigen/test/inplace_decomposition.cpp +110 -0
  45. include/eigen/test/integer_types.cpp +173 -0
  46. include/eigen/test/io.cpp +71 -0
  47. include/eigen/test/is_same_dense.cpp +41 -0
  48. include/eigen/test/jacobi.cpp +80 -0
  49. include/eigen/test/lscg.cpp +37 -0
  50. include/eigen/test/main.h +884 -0
include/eigen/test/AnnoyingScalar.h ADDED
@@ -0,0 +1,165 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // This file is part of Eigen, a lightweight C++ template library
2
+ // for linear algebra.
3
+ //
4
+ // Copyright (C) 2011-2018 Gael Guennebaud <gael.guennebaud@inria.fr>
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
+ #ifndef EIGEN_TEST_ANNOYING_SCALAR_H
11
+ #define EIGEN_TEST_ANNOYING_SCALAR_H
12
+
13
+ #include <ostream>
14
+
15
+ #if EIGEN_COMP_GNUC
16
+ #pragma GCC diagnostic ignored "-Wshadow"
17
+ #endif
18
+
19
+ #ifndef EIGEN_TEST_ANNOYING_SCALAR_DONT_THROW
20
+ struct my_exception
21
+ {
22
+ my_exception() {}
23
+ ~my_exception() {}
24
+ };
25
+ #endif
26
+
27
+ // An AnnoyingScalar is a pseudo scalar type that:
28
+ // - can randomly through an exception in operator +
29
+ // - randomly allocate on the heap or initialize a reference to itself making it non trivially copyable, nor movable, nor relocatable.
30
+
31
+ class AnnoyingScalar
32
+ {
33
+ public:
34
+ AnnoyingScalar() { init(); *v = 0; }
35
+ AnnoyingScalar(long double _v) { init(); *v = _v; }
36
+ AnnoyingScalar(double _v) { init(); *v = _v; }
37
+ AnnoyingScalar(float _v) { init(); *v = _v; }
38
+ AnnoyingScalar(int _v) { init(); *v = _v; }
39
+ AnnoyingScalar(long _v) { init(); *v = _v; }
40
+ #if EIGEN_HAS_CXX11
41
+ AnnoyingScalar(long long _v) { init(); *v = _v; }
42
+ #endif
43
+ AnnoyingScalar(const AnnoyingScalar& other) { init(); *v = *(other.v); }
44
+ ~AnnoyingScalar() {
45
+ if(v!=&data)
46
+ delete v;
47
+ instances--;
48
+ }
49
+
50
+ void init() {
51
+ if(internal::random<bool>())
52
+ v = new float;
53
+ else
54
+ v = &data;
55
+ instances++;
56
+ }
57
+
58
+ AnnoyingScalar operator+(const AnnoyingScalar& other) const
59
+ {
60
+ #ifndef EIGEN_TEST_ANNOYING_SCALAR_DONT_THROW
61
+ countdown--;
62
+ if(countdown<=0 && !dont_throw)
63
+ throw my_exception();
64
+ #endif
65
+ return AnnoyingScalar(*v+*other.v);
66
+ }
67
+
68
+ AnnoyingScalar operator-() const
69
+ { return AnnoyingScalar(-*v); }
70
+
71
+ AnnoyingScalar operator-(const AnnoyingScalar& other) const
72
+ { return AnnoyingScalar(*v-*other.v); }
73
+
74
+ AnnoyingScalar operator*(const AnnoyingScalar& other) const
75
+ { return AnnoyingScalar((*v)*(*other.v)); }
76
+
77
+ AnnoyingScalar operator/(const AnnoyingScalar& other) const
78
+ { return AnnoyingScalar((*v)/(*other.v)); }
79
+
80
+ AnnoyingScalar& operator+=(const AnnoyingScalar& other) { *v += *other.v; return *this; }
81
+ AnnoyingScalar& operator-=(const AnnoyingScalar& other) { *v -= *other.v; return *this; }
82
+ AnnoyingScalar& operator*=(const AnnoyingScalar& other) { *v *= *other.v; return *this; }
83
+ AnnoyingScalar& operator/=(const AnnoyingScalar& other) { *v /= *other.v; return *this; }
84
+ AnnoyingScalar& operator= (const AnnoyingScalar& other) { *v = *other.v; return *this; }
85
+
86
+ bool operator==(const AnnoyingScalar& other) const { return *v == *other.v; }
87
+ bool operator!=(const AnnoyingScalar& other) const { return *v != *other.v; }
88
+ bool operator<=(const AnnoyingScalar& other) const { return *v <= *other.v; }
89
+ bool operator< (const AnnoyingScalar& other) const { return *v < *other.v; }
90
+ bool operator>=(const AnnoyingScalar& other) const { return *v >= *other.v; }
91
+ bool operator> (const AnnoyingScalar& other) const { return *v > *other.v; }
92
+
93
+ float* v;
94
+ float data;
95
+ static int instances;
96
+ #ifndef EIGEN_TEST_ANNOYING_SCALAR_DONT_THROW
97
+ static int countdown;
98
+ static bool dont_throw;
99
+ #endif
100
+ };
101
+
102
+ AnnoyingScalar real(const AnnoyingScalar &x) { return x; }
103
+ AnnoyingScalar imag(const AnnoyingScalar & ) { return 0; }
104
+ AnnoyingScalar conj(const AnnoyingScalar &x) { return x; }
105
+ AnnoyingScalar sqrt(const AnnoyingScalar &x) { return std::sqrt(*x.v); }
106
+ AnnoyingScalar abs (const AnnoyingScalar &x) { return std::abs(*x.v); }
107
+ AnnoyingScalar cos (const AnnoyingScalar &x) { return std::cos(*x.v); }
108
+ AnnoyingScalar sin (const AnnoyingScalar &x) { return std::sin(*x.v); }
109
+ AnnoyingScalar acos(const AnnoyingScalar &x) { return std::acos(*x.v); }
110
+ AnnoyingScalar atan2(const AnnoyingScalar &y,const AnnoyingScalar &x) { return std::atan2(*y.v,*x.v); }
111
+
112
+ std::ostream& operator<<(std::ostream& stream,const AnnoyingScalar& x) {
113
+ stream << (*(x.v));
114
+ return stream;
115
+ }
116
+
117
+ int AnnoyingScalar::instances = 0;
118
+
119
+ #ifndef EIGEN_TEST_ANNOYING_SCALAR_DONT_THROW
120
+ int AnnoyingScalar::countdown = 0;
121
+ bool AnnoyingScalar::dont_throw = false;
122
+ #endif
123
+
124
+ namespace Eigen {
125
+ template<>
126
+ struct NumTraits<AnnoyingScalar> : NumTraits<float>
127
+ {
128
+ enum {
129
+ RequireInitialization = 1
130
+ };
131
+ typedef AnnoyingScalar Real;
132
+ typedef AnnoyingScalar Nested;
133
+ typedef AnnoyingScalar Literal;
134
+ typedef AnnoyingScalar NonInteger;
135
+ };
136
+
137
+ template<> inline AnnoyingScalar test_precision<AnnoyingScalar>() { return test_precision<float>(); }
138
+
139
+ namespace numext {
140
+ template<>
141
+ EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
142
+ bool (isfinite)(const AnnoyingScalar& x) {
143
+ return (numext::isfinite)(*x.v);
144
+ }
145
+ }
146
+
147
+ namespace internal {
148
+ template<> EIGEN_STRONG_INLINE double cast(const AnnoyingScalar& x) { return double(*x.v); }
149
+ template<> EIGEN_STRONG_INLINE float cast(const AnnoyingScalar& x) { return *x.v; }
150
+ }
151
+ } // namespace Eigen
152
+
153
+ AnnoyingScalar get_test_precision(const AnnoyingScalar&)
154
+ { return Eigen::test_precision<AnnoyingScalar>(); }
155
+
156
+ AnnoyingScalar test_relative_error(const AnnoyingScalar &a, const AnnoyingScalar &b)
157
+ { return test_relative_error(*a.v, *b.v); }
158
+
159
+ inline bool test_isApprox(const AnnoyingScalar &a, const AnnoyingScalar &b)
160
+ { return internal::isApprox(*a.v, *b.v, test_precision<float>()); }
161
+
162
+ inline bool test_isMuchSmallerThan(const AnnoyingScalar &a, const AnnoyingScalar &b)
163
+ { return test_isMuchSmallerThan(*a.v, *b.v); }
164
+
165
+ #endif // EIGEN_TEST_ANNOYING_SCALAR_H
include/eigen/test/SafeScalar.h ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ // A Scalar that asserts for uninitialized access.
3
+ template<typename T>
4
+ class SafeScalar {
5
+ public:
6
+ SafeScalar() : initialized_(false) {}
7
+ SafeScalar(const SafeScalar& other) {
8
+ *this = other;
9
+ }
10
+ SafeScalar& operator=(const SafeScalar& other) {
11
+ val_ = T(other);
12
+ initialized_ = true;
13
+ return *this;
14
+ }
15
+
16
+ SafeScalar(T val) : val_(val), initialized_(true) {}
17
+ SafeScalar& operator=(T val) {
18
+ val_ = val;
19
+ initialized_ = true;
20
+ }
21
+
22
+ operator T() const {
23
+ VERIFY(initialized_ && "Uninitialized access.");
24
+ return val_;
25
+ }
26
+
27
+ private:
28
+ T val_;
29
+ bool initialized_;
30
+ };
include/eigen/test/adjoint.cpp ADDED
@@ -0,0 +1,219 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // This file is part of Eigen, a lightweight C++ template library
2
+ // for linear algebra.
3
+ //
4
+ // Copyright (C) 2006-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
+ #define EIGEN_NO_STATIC_ASSERT
11
+
12
+ #include "main.h"
13
+
14
+ template<bool IsInteger> struct adjoint_specific;
15
+
16
+ template<> struct adjoint_specific<true> {
17
+ template<typename Vec, typename Mat, typename Scalar>
18
+ static void run(const Vec& v1, const Vec& v2, Vec& v3, const Mat& square, Scalar s1, Scalar s2) {
19
+ VERIFY(test_isApproxWithRef((s1 * v1 + s2 * v2).dot(v3), numext::conj(s1) * v1.dot(v3) + numext::conj(s2) * v2.dot(v3), 0));
20
+ VERIFY(test_isApproxWithRef(v3.dot(s1 * v1 + s2 * v2), s1*v3.dot(v1)+s2*v3.dot(v2), 0));
21
+
22
+ // check compatibility of dot and adjoint
23
+ VERIFY(test_isApproxWithRef(v1.dot(square * v2), (square.adjoint() * v1).dot(v2), 0));
24
+ }
25
+ };
26
+
27
+ template<> struct adjoint_specific<false> {
28
+ template<typename Vec, typename Mat, typename Scalar>
29
+ static void run(const Vec& v1, const Vec& v2, Vec& v3, const Mat& square, Scalar s1, Scalar s2) {
30
+ typedef typename NumTraits<Scalar>::Real RealScalar;
31
+ using std::abs;
32
+
33
+ RealScalar ref = NumTraits<Scalar>::IsInteger ? RealScalar(0) : (std::max)((s1 * v1 + s2 * v2).norm(),v3.norm());
34
+ VERIFY(test_isApproxWithRef((s1 * v1 + s2 * v2).dot(v3), numext::conj(s1) * v1.dot(v3) + numext::conj(s2) * v2.dot(v3), ref));
35
+ VERIFY(test_isApproxWithRef(v3.dot(s1 * v1 + s2 * v2), s1*v3.dot(v1)+s2*v3.dot(v2), ref));
36
+
37
+ VERIFY_IS_APPROX(v1.squaredNorm(), v1.norm() * v1.norm());
38
+ // check normalized() and normalize()
39
+ VERIFY_IS_APPROX(v1, v1.norm() * v1.normalized());
40
+ v3 = v1;
41
+ v3.normalize();
42
+ VERIFY_IS_APPROX(v1, v1.norm() * v3);
43
+ VERIFY_IS_APPROX(v3, v1.normalized());
44
+ VERIFY_IS_APPROX(v3.norm(), RealScalar(1));
45
+
46
+ // check null inputs
47
+ VERIFY_IS_APPROX((v1*0).normalized(), (v1*0));
48
+ #if (!EIGEN_ARCH_i386) || defined(EIGEN_VECTORIZE)
49
+ RealScalar very_small = (std::numeric_limits<RealScalar>::min)();
50
+ VERIFY( (v1*very_small).norm() == 0 );
51
+ VERIFY_IS_APPROX((v1*very_small).normalized(), (v1*very_small));
52
+ v3 = v1*very_small;
53
+ v3.normalize();
54
+ VERIFY_IS_APPROX(v3, (v1*very_small));
55
+ #endif
56
+
57
+ // check compatibility of dot and adjoint
58
+ ref = NumTraits<Scalar>::IsInteger ? 0 : (std::max)((std::max)(v1.norm(),v2.norm()),(std::max)((square * v2).norm(),(square.adjoint() * v1).norm()));
59
+ VERIFY(internal::isMuchSmallerThan(abs(v1.dot(square * v2) - (square.adjoint() * v1).dot(v2)), ref, test_precision<Scalar>()));
60
+
61
+ // check that Random().normalized() works: tricky as the random xpr must be evaluated by
62
+ // normalized() in order to produce a consistent result.
63
+ VERIFY_IS_APPROX(Vec::Random(v1.size()).normalized().norm(), RealScalar(1));
64
+ }
65
+ };
66
+
67
+ template<typename MatrixType> void adjoint(const MatrixType& m)
68
+ {
69
+ /* this test covers the following files:
70
+ Transpose.h Conjugate.h Dot.h
71
+ */
72
+ using std::abs;
73
+ typedef typename MatrixType::Scalar Scalar;
74
+ typedef typename NumTraits<Scalar>::Real RealScalar;
75
+ typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
76
+ typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime> SquareMatrixType;
77
+ const Index PacketSize = internal::packet_traits<Scalar>::size;
78
+
79
+ Index rows = m.rows();
80
+ Index cols = m.cols();
81
+
82
+ MatrixType m1 = MatrixType::Random(rows, cols),
83
+ m2 = MatrixType::Random(rows, cols),
84
+ m3(rows, cols),
85
+ square = SquareMatrixType::Random(rows, rows);
86
+ VectorType v1 = VectorType::Random(rows),
87
+ v2 = VectorType::Random(rows),
88
+ v3 = VectorType::Random(rows),
89
+ vzero = VectorType::Zero(rows);
90
+
91
+ Scalar s1 = internal::random<Scalar>(),
92
+ s2 = internal::random<Scalar>();
93
+
94
+ // check basic compatibility of adjoint, transpose, conjugate
95
+ VERIFY_IS_APPROX(m1.transpose().conjugate().adjoint(), m1);
96
+ VERIFY_IS_APPROX(m1.adjoint().conjugate().transpose(), m1);
97
+
98
+ // check multiplicative behavior
99
+ VERIFY_IS_APPROX((m1.adjoint() * m2).adjoint(), m2.adjoint() * m1);
100
+ VERIFY_IS_APPROX((s1 * m1).adjoint(), numext::conj(s1) * m1.adjoint());
101
+
102
+ // check basic properties of dot, squaredNorm
103
+ VERIFY_IS_APPROX(numext::conj(v1.dot(v2)), v2.dot(v1));
104
+ VERIFY_IS_APPROX(numext::real(v1.dot(v1)), v1.squaredNorm());
105
+
106
+ adjoint_specific<NumTraits<Scalar>::IsInteger>::run(v1, v2, v3, square, s1, s2);
107
+
108
+ VERIFY_IS_MUCH_SMALLER_THAN(abs(vzero.dot(v1)), static_cast<RealScalar>(1));
109
+
110
+ // like in testBasicStuff, test operator() to check const-qualification
111
+ Index r = internal::random<Index>(0, rows-1),
112
+ c = internal::random<Index>(0, cols-1);
113
+ VERIFY_IS_APPROX(m1.conjugate()(r,c), numext::conj(m1(r,c)));
114
+ VERIFY_IS_APPROX(m1.adjoint()(c,r), numext::conj(m1(r,c)));
115
+
116
+ // check inplace transpose
117
+ m3 = m1;
118
+ m3.transposeInPlace();
119
+ VERIFY_IS_APPROX(m3,m1.transpose());
120
+ m3.transposeInPlace();
121
+ VERIFY_IS_APPROX(m3,m1);
122
+
123
+ if(PacketSize<m3.rows() && PacketSize<m3.cols())
124
+ {
125
+ m3 = m1;
126
+ Index i = internal::random<Index>(0,m3.rows()-PacketSize);
127
+ Index j = internal::random<Index>(0,m3.cols()-PacketSize);
128
+ m3.template block<PacketSize,PacketSize>(i,j).transposeInPlace();
129
+ VERIFY_IS_APPROX( (m3.template block<PacketSize,PacketSize>(i,j)), (m1.template block<PacketSize,PacketSize>(i,j).transpose()) );
130
+ m3.template block<PacketSize,PacketSize>(i,j).transposeInPlace();
131
+ VERIFY_IS_APPROX(m3,m1);
132
+ }
133
+
134
+ // check inplace adjoint
135
+ m3 = m1;
136
+ m3.adjointInPlace();
137
+ VERIFY_IS_APPROX(m3,m1.adjoint());
138
+ m3.transposeInPlace();
139
+ VERIFY_IS_APPROX(m3,m1.conjugate());
140
+
141
+ // check mixed dot product
142
+ typedef Matrix<RealScalar, MatrixType::RowsAtCompileTime, 1> RealVectorType;
143
+ RealVectorType rv1 = RealVectorType::Random(rows);
144
+ VERIFY_IS_APPROX(v1.dot(rv1.template cast<Scalar>()), v1.dot(rv1));
145
+ VERIFY_IS_APPROX(rv1.template cast<Scalar>().dot(v1), rv1.dot(v1));
146
+
147
+ VERIFY( is_same_type(m1,m1.template conjugateIf<false>()) );
148
+ VERIFY( is_same_type(m1.conjugate(),m1.template conjugateIf<true>()) );
149
+ }
150
+
151
+ template<int>
152
+ void adjoint_extra()
153
+ {
154
+ MatrixXcf a(10,10), b(10,10);
155
+ VERIFY_RAISES_ASSERT(a = a.transpose());
156
+ VERIFY_RAISES_ASSERT(a = a.transpose() + b);
157
+ VERIFY_RAISES_ASSERT(a = b + a.transpose());
158
+ VERIFY_RAISES_ASSERT(a = a.conjugate().transpose());
159
+ VERIFY_RAISES_ASSERT(a = a.adjoint());
160
+ VERIFY_RAISES_ASSERT(a = a.adjoint() + b);
161
+ VERIFY_RAISES_ASSERT(a = b + a.adjoint());
162
+
163
+ // no assertion should be triggered for these cases:
164
+ a.transpose() = a.transpose();
165
+ a.transpose() += a.transpose();
166
+ a.transpose() += a.transpose() + b;
167
+ a.transpose() = a.adjoint();
168
+ a.transpose() += a.adjoint();
169
+ a.transpose() += a.adjoint() + b;
170
+
171
+ // regression tests for check_for_aliasing
172
+ MatrixXd c(10,10);
173
+ c = 1.0 * MatrixXd::Ones(10,10) + c;
174
+ c = MatrixXd::Ones(10,10) * 1.0 + c;
175
+ c = c + MatrixXd::Ones(10,10) .cwiseProduct( MatrixXd::Zero(10,10) );
176
+ c = MatrixXd::Ones(10,10) * MatrixXd::Zero(10,10);
177
+
178
+ // regression for bug 1646
179
+ for (int j = 0; j < 10; ++j) {
180
+ c.col(j).head(j) = c.row(j).head(j);
181
+ }
182
+
183
+ for (int j = 0; j < 10; ++j) {
184
+ c.col(j) = c.row(j);
185
+ }
186
+
187
+ a.conservativeResize(1,1);
188
+ a = a.transpose();
189
+
190
+ a.conservativeResize(0,0);
191
+ a = a.transpose();
192
+ }
193
+
194
+ EIGEN_DECLARE_TEST(adjoint)
195
+ {
196
+ for(int i = 0; i < g_repeat; i++) {
197
+ CALL_SUBTEST_1( adjoint(Matrix<float, 1, 1>()) );
198
+ CALL_SUBTEST_2( adjoint(Matrix3d()) );
199
+ CALL_SUBTEST_3( adjoint(Matrix4f()) );
200
+
201
+ CALL_SUBTEST_4( adjoint(MatrixXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2), internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2))) );
202
+ CALL_SUBTEST_5( adjoint(MatrixXi(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
203
+ CALL_SUBTEST_6( adjoint(MatrixXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
204
+
205
+ // Complement for 128 bits vectorization:
206
+ CALL_SUBTEST_8( adjoint(Matrix2d()) );
207
+ CALL_SUBTEST_9( adjoint(Matrix<int,4,4>()) );
208
+
209
+ // 256 bits vectorization:
210
+ CALL_SUBTEST_10( adjoint(Matrix<float,8,8>()) );
211
+ CALL_SUBTEST_11( adjoint(Matrix<double,4,4>()) );
212
+ CALL_SUBTEST_12( adjoint(Matrix<int,8,8>()) );
213
+ }
214
+ // test a large static matrix only once
215
+ CALL_SUBTEST_7( adjoint(Matrix<float, 100, 100>()) );
216
+
217
+ CALL_SUBTEST_13( adjoint_extra<0>() );
218
+ }
219
+
include/eigen/test/array_for_matrix.cpp ADDED
@@ -0,0 +1,341 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // This file is part of Eigen, a lightweight C++ template library
2
+ // for linear algebra.
3
+ //
4
+ // Copyright (C) 2008-2009 Gael Guennebaud <gael.guennebaud@inria.fr>
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
+ template<typename MatrixType> void array_for_matrix(const MatrixType& m)
13
+ {
14
+ typedef typename MatrixType::Scalar Scalar;
15
+ typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> ColVectorType;
16
+ typedef Matrix<Scalar, 1, MatrixType::ColsAtCompileTime> RowVectorType;
17
+
18
+ Index rows = m.rows();
19
+ Index cols = m.cols();
20
+
21
+ MatrixType m1 = MatrixType::Random(rows, cols),
22
+ m2 = MatrixType::Random(rows, cols),
23
+ m3(rows, cols);
24
+
25
+ ColVectorType cv1 = ColVectorType::Random(rows);
26
+ RowVectorType rv1 = RowVectorType::Random(cols);
27
+
28
+ Scalar s1 = internal::random<Scalar>(),
29
+ s2 = internal::random<Scalar>();
30
+
31
+ // scalar addition
32
+ VERIFY_IS_APPROX(m1.array() + s1, s1 + m1.array());
33
+ VERIFY_IS_APPROX((m1.array() + s1).matrix(), MatrixType::Constant(rows,cols,s1) + m1);
34
+ VERIFY_IS_APPROX(((m1*Scalar(2)).array() - s2).matrix(), (m1+m1) - MatrixType::Constant(rows,cols,s2) );
35
+ m3 = m1;
36
+ m3.array() += s2;
37
+ VERIFY_IS_APPROX(m3, (m1.array() + s2).matrix());
38
+ m3 = m1;
39
+ m3.array() -= s1;
40
+ VERIFY_IS_APPROX(m3, (m1.array() - s1).matrix());
41
+
42
+ // reductions
43
+ VERIFY_IS_MUCH_SMALLER_THAN(m1.colwise().sum().sum() - m1.sum(), m1.squaredNorm());
44
+ VERIFY_IS_MUCH_SMALLER_THAN(m1.rowwise().sum().sum() - m1.sum(), m1.squaredNorm());
45
+ VERIFY_IS_MUCH_SMALLER_THAN(m1.colwise().sum() + m2.colwise().sum() - (m1+m2).colwise().sum(), (m1+m2).squaredNorm());
46
+ VERIFY_IS_MUCH_SMALLER_THAN(m1.rowwise().sum() - m2.rowwise().sum() - (m1-m2).rowwise().sum(), (m1-m2).squaredNorm());
47
+ VERIFY_IS_APPROX(m1.colwise().sum(), m1.colwise().redux(internal::scalar_sum_op<Scalar,Scalar>()));
48
+
49
+ // vector-wise ops
50
+ m3 = m1;
51
+ VERIFY_IS_APPROX(m3.colwise() += cv1, m1.colwise() + cv1);
52
+ m3 = m1;
53
+ VERIFY_IS_APPROX(m3.colwise() -= cv1, m1.colwise() - cv1);
54
+ m3 = m1;
55
+ VERIFY_IS_APPROX(m3.rowwise() += rv1, m1.rowwise() + rv1);
56
+ m3 = m1;
57
+ VERIFY_IS_APPROX(m3.rowwise() -= rv1, m1.rowwise() - rv1);
58
+
59
+ // empty objects
60
+ VERIFY_IS_APPROX((m1.template block<0,Dynamic>(0,0,0,cols).colwise().sum()), RowVectorType::Zero(cols));
61
+ VERIFY_IS_APPROX((m1.template block<Dynamic,0>(0,0,rows,0).rowwise().sum()), ColVectorType::Zero(rows));
62
+ VERIFY_IS_APPROX((m1.template block<0,Dynamic>(0,0,0,cols).colwise().prod()), RowVectorType::Ones(cols));
63
+ VERIFY_IS_APPROX((m1.template block<Dynamic,0>(0,0,rows,0).rowwise().prod()), ColVectorType::Ones(rows));
64
+
65
+ VERIFY_IS_APPROX(m1.block(0,0,0,cols).colwise().sum(), RowVectorType::Zero(cols));
66
+ VERIFY_IS_APPROX(m1.block(0,0,rows,0).rowwise().sum(), ColVectorType::Zero(rows));
67
+ VERIFY_IS_APPROX(m1.block(0,0,0,cols).colwise().prod(), RowVectorType::Ones(cols));
68
+ VERIFY_IS_APPROX(m1.block(0,0,rows,0).rowwise().prod(), ColVectorType::Ones(rows));
69
+
70
+ // verify the const accessors exist
71
+ const Scalar& ref_m1 = m.matrix().array().coeffRef(0);
72
+ const Scalar& ref_m2 = m.matrix().array().coeffRef(0,0);
73
+ const Scalar& ref_a1 = m.array().matrix().coeffRef(0);
74
+ const Scalar& ref_a2 = m.array().matrix().coeffRef(0,0);
75
+ VERIFY(&ref_a1 == &ref_m1);
76
+ VERIFY(&ref_a2 == &ref_m2);
77
+
78
+ // Check write accessors:
79
+ m1.array().coeffRef(0,0) = 1;
80
+ VERIFY_IS_APPROX(m1(0,0),Scalar(1));
81
+ m1.array()(0,0) = 2;
82
+ VERIFY_IS_APPROX(m1(0,0),Scalar(2));
83
+ m1.array().matrix().coeffRef(0,0) = 3;
84
+ VERIFY_IS_APPROX(m1(0,0),Scalar(3));
85
+ m1.array().matrix()(0,0) = 4;
86
+ VERIFY_IS_APPROX(m1(0,0),Scalar(4));
87
+ }
88
+
89
+ template<typename MatrixType> void comparisons(const MatrixType& m)
90
+ {
91
+ using std::abs;
92
+ typedef typename MatrixType::Scalar Scalar;
93
+ typedef typename NumTraits<Scalar>::Real RealScalar;
94
+
95
+ Index rows = m.rows();
96
+ Index cols = m.cols();
97
+
98
+ Index r = internal::random<Index>(0, rows-1),
99
+ c = internal::random<Index>(0, cols-1);
100
+
101
+ MatrixType m1 = MatrixType::Random(rows, cols),
102
+ m2 = MatrixType::Random(rows, cols),
103
+ m3(rows, cols);
104
+
105
+ VERIFY(((m1.array() + Scalar(1)) > m1.array()).all());
106
+ VERIFY(((m1.array() - Scalar(1)) < m1.array()).all());
107
+ if (rows*cols>1)
108
+ {
109
+ m3 = m1;
110
+ m3(r,c) += 1;
111
+ VERIFY(! (m1.array() < m3.array()).all() );
112
+ VERIFY(! (m1.array() > m3.array()).all() );
113
+ }
114
+
115
+ // comparisons to scalar
116
+ VERIFY( (m1.array() != (m1(r,c)+1) ).any() );
117
+ VERIFY( (m1.array() > (m1(r,c)-1) ).any() );
118
+ VERIFY( (m1.array() < (m1(r,c)+1) ).any() );
119
+ VERIFY( (m1.array() == m1(r,c) ).any() );
120
+ VERIFY( m1.cwiseEqual(m1(r,c)).any() );
121
+
122
+ // test Select
123
+ VERIFY_IS_APPROX( (m1.array()<m2.array()).select(m1,m2), m1.cwiseMin(m2) );
124
+ VERIFY_IS_APPROX( (m1.array()>m2.array()).select(m1,m2), m1.cwiseMax(m2) );
125
+ Scalar mid = (m1.cwiseAbs().minCoeff() + m1.cwiseAbs().maxCoeff())/Scalar(2);
126
+ for (int j=0; j<cols; ++j)
127
+ for (int i=0; i<rows; ++i)
128
+ m3(i,j) = abs(m1(i,j))<mid ? 0 : m1(i,j);
129
+ VERIFY_IS_APPROX( (m1.array().abs()<MatrixType::Constant(rows,cols,mid).array())
130
+ .select(MatrixType::Zero(rows,cols),m1), m3);
131
+ // shorter versions:
132
+ VERIFY_IS_APPROX( (m1.array().abs()<MatrixType::Constant(rows,cols,mid).array())
133
+ .select(0,m1), m3);
134
+ VERIFY_IS_APPROX( (m1.array().abs()>=MatrixType::Constant(rows,cols,mid).array())
135
+ .select(m1,0), m3);
136
+ // even shorter version:
137
+ VERIFY_IS_APPROX( (m1.array().abs()<mid).select(0,m1), m3);
138
+
139
+ // count
140
+ VERIFY(((m1.array().abs()+1)>RealScalar(0.1)).count() == rows*cols);
141
+
142
+ // and/or
143
+ VERIFY( ((m1.array()<RealScalar(0)).matrix() && (m1.array()>RealScalar(0)).matrix()).count() == 0);
144
+ VERIFY( ((m1.array()<RealScalar(0)).matrix() || (m1.array()>=RealScalar(0)).matrix()).count() == rows*cols);
145
+ RealScalar a = m1.cwiseAbs().mean();
146
+ VERIFY( ((m1.array()<-a).matrix() || (m1.array()>a).matrix()).count() == (m1.cwiseAbs().array()>a).count());
147
+
148
+ typedef Matrix<Index, Dynamic, 1> VectorOfIndices;
149
+
150
+ // TODO allows colwise/rowwise for array
151
+ VERIFY_IS_APPROX(((m1.array().abs()+1)>RealScalar(0.1)).matrix().colwise().count(), VectorOfIndices::Constant(cols,rows).transpose());
152
+ VERIFY_IS_APPROX(((m1.array().abs()+1)>RealScalar(0.1)).matrix().rowwise().count(), VectorOfIndices::Constant(rows, cols));
153
+ }
154
+
155
+ template<typename VectorType> void lpNorm(const VectorType& v)
156
+ {
157
+ using std::sqrt;
158
+ typedef typename VectorType::RealScalar RealScalar;
159
+ VectorType u = VectorType::Random(v.size());
160
+
161
+ if(v.size()==0)
162
+ {
163
+ VERIFY_IS_APPROX(u.template lpNorm<Infinity>(), RealScalar(0));
164
+ VERIFY_IS_APPROX(u.template lpNorm<1>(), RealScalar(0));
165
+ VERIFY_IS_APPROX(u.template lpNorm<2>(), RealScalar(0));
166
+ VERIFY_IS_APPROX(u.template lpNorm<5>(), RealScalar(0));
167
+ }
168
+ else
169
+ {
170
+ VERIFY_IS_APPROX(u.template lpNorm<Infinity>(), u.cwiseAbs().maxCoeff());
171
+ }
172
+
173
+ VERIFY_IS_APPROX(u.template lpNorm<1>(), u.cwiseAbs().sum());
174
+ VERIFY_IS_APPROX(u.template lpNorm<2>(), sqrt(u.array().abs().square().sum()));
175
+ VERIFY_IS_APPROX(numext::pow(u.template lpNorm<5>(), typename VectorType::RealScalar(5)), u.array().abs().pow(5).sum());
176
+ }
177
+
178
+ template<typename MatrixType> void cwise_min_max(const MatrixType& m)
179
+ {
180
+ typedef typename MatrixType::Scalar Scalar;
181
+
182
+ Index rows = m.rows();
183
+ Index cols = m.cols();
184
+
185
+ MatrixType m1 = MatrixType::Random(rows, cols);
186
+
187
+ // min/max with array
188
+ Scalar maxM1 = m1.maxCoeff();
189
+ Scalar minM1 = m1.minCoeff();
190
+
191
+ VERIFY_IS_APPROX(MatrixType::Constant(rows,cols, minM1), m1.cwiseMin(MatrixType::Constant(rows,cols, minM1)));
192
+ VERIFY_IS_APPROX(m1, m1.cwiseMin(MatrixType::Constant(rows,cols, maxM1)));
193
+
194
+ VERIFY_IS_APPROX(MatrixType::Constant(rows,cols, maxM1), m1.cwiseMax(MatrixType::Constant(rows,cols, maxM1)));
195
+ VERIFY_IS_APPROX(m1, m1.cwiseMax(MatrixType::Constant(rows,cols, minM1)));
196
+
197
+ // min/max with scalar input
198
+ VERIFY_IS_APPROX(MatrixType::Constant(rows,cols, minM1), m1.cwiseMin( minM1));
199
+ VERIFY_IS_APPROX(m1, m1.cwiseMin(maxM1));
200
+ VERIFY_IS_APPROX(-m1, (-m1).cwiseMin(-minM1));
201
+ VERIFY_IS_APPROX(-m1.array(), ((-m1).array().min)( -minM1));
202
+
203
+ VERIFY_IS_APPROX(MatrixType::Constant(rows,cols, maxM1), m1.cwiseMax( maxM1));
204
+ VERIFY_IS_APPROX(m1, m1.cwiseMax(minM1));
205
+ VERIFY_IS_APPROX(-m1, (-m1).cwiseMax(-maxM1));
206
+ VERIFY_IS_APPROX(-m1.array(), ((-m1).array().max)(-maxM1));
207
+
208
+ VERIFY_IS_APPROX(MatrixType::Constant(rows,cols, minM1).array(), (m1.array().min)( minM1));
209
+ VERIFY_IS_APPROX(m1.array(), (m1.array().min)( maxM1));
210
+
211
+ VERIFY_IS_APPROX(MatrixType::Constant(rows,cols, maxM1).array(), (m1.array().max)( maxM1));
212
+ VERIFY_IS_APPROX(m1.array(), (m1.array().max)( minM1));
213
+
214
+ // Test NaN propagation for min/max.
215
+ if (!NumTraits<Scalar>::IsInteger) {
216
+ m1(0,0) = NumTraits<Scalar>::quiet_NaN();
217
+ // Elementwise.
218
+ VERIFY((numext::isnan)(m1.template cwiseMax<PropagateNaN>(MatrixType::Constant(rows,cols, Scalar(1)))(0,0)));
219
+ VERIFY((numext::isnan)(m1.template cwiseMin<PropagateNaN>(MatrixType::Constant(rows,cols, Scalar(1)))(0,0)));
220
+ VERIFY(!(numext::isnan)(m1.template cwiseMax<PropagateNumbers>(MatrixType::Constant(rows,cols, Scalar(1)))(0,0)));
221
+ VERIFY(!(numext::isnan)(m1.template cwiseMin<PropagateNumbers>(MatrixType::Constant(rows,cols, Scalar(1)))(0,0)));
222
+ VERIFY((numext::isnan)(m1.template cwiseMax<PropagateNaN>(Scalar(1))(0,0)));
223
+ VERIFY((numext::isnan)(m1.template cwiseMin<PropagateNaN>(Scalar(1))(0,0)));
224
+ VERIFY(!(numext::isnan)(m1.template cwiseMax<PropagateNumbers>(Scalar(1))(0,0)));
225
+ VERIFY(!(numext::isnan)(m1.template cwiseMin<PropagateNumbers>(Scalar(1))(0,0)));
226
+
227
+
228
+ VERIFY((numext::isnan)(m1.array().template max<PropagateNaN>(MatrixType::Constant(rows,cols, Scalar(1)).array())(0,0)));
229
+ VERIFY((numext::isnan)(m1.array().template min<PropagateNaN>(MatrixType::Constant(rows,cols, Scalar(1)).array())(0,0)));
230
+ VERIFY(!(numext::isnan)(m1.array().template max<PropagateNumbers>(MatrixType::Constant(rows,cols, Scalar(1)).array())(0,0)));
231
+ VERIFY(!(numext::isnan)(m1.array().template min<PropagateNumbers>(MatrixType::Constant(rows,cols, Scalar(1)).array())(0,0)));
232
+ VERIFY((numext::isnan)(m1.array().template max<PropagateNaN>(Scalar(1))(0,0)));
233
+ VERIFY((numext::isnan)(m1.array().template min<PropagateNaN>(Scalar(1))(0,0)));
234
+ VERIFY(!(numext::isnan)(m1.array().template max<PropagateNumbers>(Scalar(1))(0,0)));
235
+ VERIFY(!(numext::isnan)(m1.array().template min<PropagateNumbers>(Scalar(1))(0,0)));
236
+
237
+ // Reductions.
238
+ VERIFY((numext::isnan)(m1.template maxCoeff<PropagateNaN>()));
239
+ VERIFY((numext::isnan)(m1.template minCoeff<PropagateNaN>()));
240
+ if (m1.size() > 1) {
241
+ VERIFY(!(numext::isnan)(m1.template maxCoeff<PropagateNumbers>()));
242
+ VERIFY(!(numext::isnan)(m1.template minCoeff<PropagateNumbers>()));
243
+ } else {
244
+ VERIFY((numext::isnan)(m1.template maxCoeff<PropagateNumbers>()));
245
+ VERIFY((numext::isnan)(m1.template minCoeff<PropagateNumbers>()));
246
+ }
247
+ }
248
+ }
249
+
250
+ template<typename MatrixTraits> void resize(const MatrixTraits& t)
251
+ {
252
+ typedef typename MatrixTraits::Scalar Scalar;
253
+ typedef Matrix<Scalar,Dynamic,Dynamic> MatrixType;
254
+ typedef Array<Scalar,Dynamic,Dynamic> Array2DType;
255
+ typedef Matrix<Scalar,Dynamic,1> VectorType;
256
+ typedef Array<Scalar,Dynamic,1> Array1DType;
257
+
258
+ Index rows = t.rows(), cols = t.cols();
259
+
260
+ MatrixType m(rows,cols);
261
+ VectorType v(rows);
262
+ Array2DType a2(rows,cols);
263
+ Array1DType a1(rows);
264
+
265
+ m.array().resize(rows+1,cols+1);
266
+ VERIFY(m.rows()==rows+1 && m.cols()==cols+1);
267
+ a2.matrix().resize(rows+1,cols+1);
268
+ VERIFY(a2.rows()==rows+1 && a2.cols()==cols+1);
269
+ v.array().resize(cols);
270
+ VERIFY(v.size()==cols);
271
+ a1.matrix().resize(cols);
272
+ VERIFY(a1.size()==cols);
273
+ }
274
+
275
+ template<int>
276
+ void regression_bug_654()
277
+ {
278
+ ArrayXf a = RowVectorXf(3);
279
+ VectorXf v = Array<float,1,Dynamic>(3);
280
+ }
281
+
282
+ // Check propagation of LvalueBit through Array/Matrix-Wrapper
283
+ template<int>
284
+ void regrrssion_bug_1410()
285
+ {
286
+ const Matrix4i M;
287
+ const Array4i A;
288
+ ArrayWrapper<const Matrix4i> MA = M.array();
289
+ MA.row(0);
290
+ MatrixWrapper<const Array4i> AM = A.matrix();
291
+ AM.row(0);
292
+
293
+ VERIFY((internal::traits<ArrayWrapper<const Matrix4i> >::Flags&LvalueBit)==0);
294
+ VERIFY((internal::traits<MatrixWrapper<const Array4i> >::Flags&LvalueBit)==0);
295
+
296
+ VERIFY((internal::traits<ArrayWrapper<Matrix4i> >::Flags&LvalueBit)==LvalueBit);
297
+ VERIFY((internal::traits<MatrixWrapper<Array4i> >::Flags&LvalueBit)==LvalueBit);
298
+ }
299
+
300
+ EIGEN_DECLARE_TEST(array_for_matrix)
301
+ {
302
+ for(int i = 0; i < g_repeat; i++) {
303
+ CALL_SUBTEST_1( array_for_matrix(Matrix<float, 1, 1>()) );
304
+ CALL_SUBTEST_2( array_for_matrix(Matrix2f()) );
305
+ CALL_SUBTEST_3( array_for_matrix(Matrix4d()) );
306
+ CALL_SUBTEST_4( array_for_matrix(MatrixXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
307
+ CALL_SUBTEST_5( array_for_matrix(MatrixXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
308
+ CALL_SUBTEST_6( array_for_matrix(MatrixXi(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
309
+ }
310
+ for(int i = 0; i < g_repeat; i++) {
311
+ CALL_SUBTEST_1( comparisons(Matrix<float, 1, 1>()) );
312
+ CALL_SUBTEST_2( comparisons(Matrix2f()) );
313
+ CALL_SUBTEST_3( comparisons(Matrix4d()) );
314
+ CALL_SUBTEST_5( comparisons(MatrixXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
315
+ CALL_SUBTEST_6( comparisons(MatrixXi(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
316
+ }
317
+ for(int i = 0; i < g_repeat; i++) {
318
+ CALL_SUBTEST_1( cwise_min_max(Matrix<float, 1, 1>()) );
319
+ CALL_SUBTEST_2( cwise_min_max(Matrix2f()) );
320
+ CALL_SUBTEST_3( cwise_min_max(Matrix4d()) );
321
+ CALL_SUBTEST_5( cwise_min_max(MatrixXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
322
+ CALL_SUBTEST_6( cwise_min_max(MatrixXi(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
323
+ }
324
+ for(int i = 0; i < g_repeat; i++) {
325
+ CALL_SUBTEST_1( lpNorm(Matrix<float, 1, 1>()) );
326
+ CALL_SUBTEST_2( lpNorm(Vector2f()) );
327
+ CALL_SUBTEST_7( lpNorm(Vector3d()) );
328
+ CALL_SUBTEST_8( lpNorm(Vector4f()) );
329
+ CALL_SUBTEST_5( lpNorm(VectorXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
330
+ CALL_SUBTEST_4( lpNorm(VectorXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
331
+ }
332
+ CALL_SUBTEST_5( lpNorm(VectorXf(0)) );
333
+ CALL_SUBTEST_4( lpNorm(VectorXcf(0)) );
334
+ for(int i = 0; i < g_repeat; i++) {
335
+ CALL_SUBTEST_4( resize(MatrixXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
336
+ CALL_SUBTEST_5( resize(MatrixXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
337
+ CALL_SUBTEST_6( resize(MatrixXi(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
338
+ }
339
+ CALL_SUBTEST_6( regression_bug_654<0>() );
340
+ CALL_SUBTEST_6( regrrssion_bug_1410<0>() );
341
+ }
include/eigen/test/array_of_string.cpp ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // This file is part of Eigen, a lightweight C++ template library
2
+ // for linear algebra.
3
+ //
4
+ // Copyright (C) 2016 Gael Guennebaud <gael.guennebaud@inria.fr>
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
+ EIGEN_DECLARE_TEST(array_of_string)
13
+ {
14
+ typedef Array<std::string,1,Dynamic> ArrayXs;
15
+ ArrayXs a1(3), a2(3), a3(3), a3ref(3);
16
+ a1 << "one", "two", "three";
17
+ a2 << "1", "2", "3";
18
+ a3ref << "one (1)", "two (2)", "three (3)";
19
+ std::stringstream s1;
20
+ s1 << a1;
21
+ VERIFY_IS_EQUAL(s1.str(), std::string(" one two three"));
22
+ a3 = a1 + std::string(" (") + a2 + std::string(")");
23
+ VERIFY((a3==a3ref).all());
24
+
25
+ a3 = a1;
26
+ a3 += std::string(" (") + a2 + std::string(")");
27
+ VERIFY((a3==a3ref).all());
28
+
29
+ a1.swap(a3);
30
+ VERIFY((a1==a3ref).all());
31
+ VERIFY((a3!=a3ref).all());
32
+ }
include/eigen/test/array_replicate.cpp ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // This file is part of Eigen, a lightweight C++ template library
2
+ // for linear algebra.
3
+ //
4
+ // Copyright (C) 2009 Gael Guennebaud <gael.guennebaud@inria.fr>
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
+ template<typename MatrixType> void replicate(const MatrixType& m)
13
+ {
14
+ /* this test covers the following files:
15
+ Replicate.cpp
16
+ */
17
+ typedef typename MatrixType::Scalar Scalar;
18
+ typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
19
+ typedef Matrix<Scalar, Dynamic, Dynamic> MatrixX;
20
+ typedef Matrix<Scalar, Dynamic, 1> VectorX;
21
+
22
+ Index rows = m.rows();
23
+ Index cols = m.cols();
24
+
25
+ MatrixType m1 = MatrixType::Random(rows, cols),
26
+ m2 = MatrixType::Random(rows, cols);
27
+
28
+ VectorType v1 = VectorType::Random(rows);
29
+
30
+ MatrixX x1, x2;
31
+ VectorX vx1;
32
+
33
+ int f1 = internal::random<int>(1,10),
34
+ f2 = internal::random<int>(1,10);
35
+
36
+ x1.resize(rows*f1,cols*f2);
37
+ for(int j=0; j<f2; j++)
38
+ for(int i=0; i<f1; i++)
39
+ x1.block(i*rows,j*cols,rows,cols) = m1;
40
+ VERIFY_IS_APPROX(x1, m1.replicate(f1,f2));
41
+
42
+ x2.resize(2*rows,3*cols);
43
+ x2 << m2, m2, m2,
44
+ m2, m2, m2;
45
+ VERIFY_IS_APPROX(x2, (m2.template replicate<2,3>()));
46
+
47
+ x2.resize(rows,3*cols);
48
+ x2 << m2, m2, m2;
49
+ VERIFY_IS_APPROX(x2, (m2.template replicate<1,3>()));
50
+
51
+ vx1.resize(3*rows,cols);
52
+ vx1 << m2, m2, m2;
53
+ VERIFY_IS_APPROX(vx1+vx1, vx1+(m2.template replicate<3,1>()));
54
+
55
+ vx1=m2+(m2.colwise().replicate(1));
56
+
57
+ if(m2.cols()==1)
58
+ VERIFY_IS_APPROX(m2.coeff(0), (m2.template replicate<3,1>().coeff(m2.rows())));
59
+
60
+ x2.resize(rows,f1);
61
+ for (int j=0; j<f1; ++j)
62
+ x2.col(j) = v1;
63
+ VERIFY_IS_APPROX(x2, v1.rowwise().replicate(f1));
64
+
65
+ vx1.resize(rows*f2);
66
+ for (int j=0; j<f2; ++j)
67
+ vx1.segment(j*rows,rows) = v1;
68
+ VERIFY_IS_APPROX(vx1, v1.colwise().replicate(f2));
69
+ }
70
+
71
+ EIGEN_DECLARE_TEST(array_replicate)
72
+ {
73
+ for(int i = 0; i < g_repeat; i++) {
74
+ CALL_SUBTEST_1( replicate(Matrix<float, 1, 1>()) );
75
+ CALL_SUBTEST_2( replicate(Vector2f()) );
76
+ CALL_SUBTEST_3( replicate(Vector3d()) );
77
+ CALL_SUBTEST_4( replicate(Vector4f()) );
78
+ CALL_SUBTEST_5( replicate(VectorXf(16)) );
79
+ CALL_SUBTEST_6( replicate(VectorXcd(10)) );
80
+ }
81
+ }
include/eigen/test/bandmatrix.cpp ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // This file is triangularView of Eigen, a lightweight C++ template library
2
+ // for linear algebra.
3
+ //
4
+ // Copyright (C) 2008-2009 Gael Guennebaud <gael.guennebaud@inria.fr>
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
+ template<typename MatrixType> void bandmatrix(const MatrixType& _m)
13
+ {
14
+ typedef typename MatrixType::Scalar Scalar;
15
+ typedef typename NumTraits<Scalar>::Real RealScalar;
16
+ typedef Matrix<Scalar,Dynamic,Dynamic> DenseMatrixType;
17
+
18
+ Index rows = _m.rows();
19
+ Index cols = _m.cols();
20
+ Index supers = _m.supers();
21
+ Index subs = _m.subs();
22
+
23
+ MatrixType m(rows,cols,supers,subs);
24
+
25
+ DenseMatrixType dm1(rows,cols);
26
+ dm1.setZero();
27
+
28
+ m.diagonal().setConstant(123);
29
+ dm1.diagonal().setConstant(123);
30
+ for (int i=1; i<=m.supers();++i)
31
+ {
32
+ m.diagonal(i).setConstant(static_cast<RealScalar>(i));
33
+ dm1.diagonal(i).setConstant(static_cast<RealScalar>(i));
34
+ }
35
+ for (int i=1; i<=m.subs();++i)
36
+ {
37
+ m.diagonal(-i).setConstant(-static_cast<RealScalar>(i));
38
+ dm1.diagonal(-i).setConstant(-static_cast<RealScalar>(i));
39
+ }
40
+ //std::cerr << m.m_data << "\n\n" << m.toDense() << "\n\n" << dm1 << "\n\n\n\n";
41
+ VERIFY_IS_APPROX(dm1,m.toDenseMatrix());
42
+
43
+ for (int i=0; i<cols; ++i)
44
+ {
45
+ m.col(i).setConstant(static_cast<RealScalar>(i+1));
46
+ dm1.col(i).setConstant(static_cast<RealScalar>(i+1));
47
+ }
48
+ Index d = (std::min)(rows,cols);
49
+ Index a = std::max<Index>(0,cols-d-supers);
50
+ Index b = std::max<Index>(0,rows-d-subs);
51
+ if(a>0) dm1.block(0,d+supers,rows,a).setZero();
52
+ dm1.block(0,supers+1,cols-supers-1-a,cols-supers-1-a).template triangularView<Upper>().setZero();
53
+ dm1.block(subs+1,0,rows-subs-1-b,rows-subs-1-b).template triangularView<Lower>().setZero();
54
+ if(b>0) dm1.block(d+subs,0,b,cols).setZero();
55
+ //std::cerr << m.m_data << "\n\n" << m.toDense() << "\n\n" << dm1 << "\n\n";
56
+ VERIFY_IS_APPROX(dm1,m.toDenseMatrix());
57
+
58
+ }
59
+
60
+ using Eigen::internal::BandMatrix;
61
+
62
+ EIGEN_DECLARE_TEST(bandmatrix)
63
+ {
64
+ for(int i = 0; i < 10*g_repeat ; i++) {
65
+ Index rows = internal::random<Index>(1,10);
66
+ Index cols = internal::random<Index>(1,10);
67
+ Index sups = internal::random<Index>(0,cols-1);
68
+ Index subs = internal::random<Index>(0,rows-1);
69
+ CALL_SUBTEST(bandmatrix(BandMatrix<float>(rows,cols,sups,subs)) );
70
+ }
71
+ }
include/eigen/test/basicstuff.cpp ADDED
@@ -0,0 +1,356 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // This file is part of Eigen, a lightweight C++ template library
2
+ // for linear algebra.
3
+ //
4
+ // Copyright (C) 2006-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
+ #define EIGEN_NO_STATIC_ASSERT
11
+
12
+ #include "main.h"
13
+ #include "random_without_cast_overflow.h"
14
+
15
+ template<typename MatrixType> void basicStuff(const MatrixType& m)
16
+ {
17
+ typedef typename MatrixType::Scalar Scalar;
18
+ typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
19
+ typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime> SquareMatrixType;
20
+
21
+ Index rows = m.rows();
22
+ Index cols = m.cols();
23
+
24
+ // this test relies a lot on Random.h, and there's not much more that we can do
25
+ // to test it, hence I consider that we will have tested Random.h
26
+ MatrixType m1 = MatrixType::Random(rows, cols),
27
+ m2 = MatrixType::Random(rows, cols),
28
+ m3(rows, cols),
29
+ mzero = MatrixType::Zero(rows, cols),
30
+ square = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime>::Random(rows, rows);
31
+ VectorType v1 = VectorType::Random(rows),
32
+ vzero = VectorType::Zero(rows);
33
+ SquareMatrixType sm1 = SquareMatrixType::Random(rows,rows), sm2(rows,rows);
34
+
35
+ Scalar x = 0;
36
+ while(x == Scalar(0)) x = internal::random<Scalar>();
37
+
38
+ Index r = internal::random<Index>(0, rows-1),
39
+ c = internal::random<Index>(0, cols-1);
40
+
41
+ m1.coeffRef(r,c) = x;
42
+ VERIFY_IS_APPROX(x, m1.coeff(r,c));
43
+ m1(r,c) = x;
44
+ VERIFY_IS_APPROX(x, m1(r,c));
45
+ v1.coeffRef(r) = x;
46
+ VERIFY_IS_APPROX(x, v1.coeff(r));
47
+ v1(r) = x;
48
+ VERIFY_IS_APPROX(x, v1(r));
49
+ v1[r] = x;
50
+ VERIFY_IS_APPROX(x, v1[r]);
51
+
52
+ // test fetching with various index types.
53
+ Index r1 = internal::random<Index>(0, numext::mini(Index(127),rows-1));
54
+ x = v1(static_cast<char>(r1));
55
+ x = v1(static_cast<signed char>(r1));
56
+ x = v1(static_cast<unsigned char>(r1));
57
+ x = v1(static_cast<signed short>(r1));
58
+ x = v1(static_cast<unsigned short>(r1));
59
+ x = v1(static_cast<signed int>(r1));
60
+ x = v1(static_cast<unsigned int>(r1));
61
+ x = v1(static_cast<signed long>(r1));
62
+ x = v1(static_cast<unsigned long>(r1));
63
+ #if EIGEN_HAS_CXX11
64
+ x = v1(static_cast<long long int>(r1));
65
+ x = v1(static_cast<unsigned long long int>(r1));
66
+ #endif
67
+
68
+ VERIFY_IS_APPROX( v1, v1);
69
+ VERIFY_IS_NOT_APPROX( v1, 2*v1);
70
+ VERIFY_IS_MUCH_SMALLER_THAN( vzero, v1);
71
+ VERIFY_IS_MUCH_SMALLER_THAN( vzero, v1.squaredNorm());
72
+ VERIFY_IS_NOT_MUCH_SMALLER_THAN(v1, v1);
73
+ VERIFY_IS_APPROX( vzero, v1-v1);
74
+ VERIFY_IS_APPROX( m1, m1);
75
+ VERIFY_IS_NOT_APPROX( m1, 2*m1);
76
+ VERIFY_IS_MUCH_SMALLER_THAN( mzero, m1);
77
+ VERIFY_IS_NOT_MUCH_SMALLER_THAN(m1, m1);
78
+ VERIFY_IS_APPROX( mzero, m1-m1);
79
+
80
+ // always test operator() on each read-only expression class,
81
+ // in order to check const-qualifiers.
82
+ // indeed, if an expression class (here Zero) is meant to be read-only,
83
+ // hence has no _write() method, the corresponding MatrixBase method (here zero())
84
+ // should return a const-qualified object so that it is the const-qualified
85
+ // operator() that gets called, which in turn calls _read().
86
+ VERIFY_IS_MUCH_SMALLER_THAN(MatrixType::Zero(rows,cols)(r,c), static_cast<Scalar>(1));
87
+
88
+ // now test copying a row-vector into a (column-)vector and conversely.
89
+ square.col(r) = square.row(r).eval();
90
+ Matrix<Scalar, 1, MatrixType::RowsAtCompileTime> rv(rows);
91
+ Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> cv(rows);
92
+ rv = square.row(r);
93
+ cv = square.col(r);
94
+
95
+ VERIFY_IS_APPROX(rv, cv.transpose());
96
+
97
+ if(cols!=1 && rows!=1 && MatrixType::SizeAtCompileTime!=Dynamic)
98
+ {
99
+ VERIFY_RAISES_ASSERT(m1 = (m2.block(0,0, rows-1, cols-1)));
100
+ }
101
+
102
+ if(cols!=1 && rows!=1)
103
+ {
104
+ VERIFY_RAISES_ASSERT(m1[0]);
105
+ VERIFY_RAISES_ASSERT((m1+m1)[0]);
106
+ }
107
+
108
+ VERIFY_IS_APPROX(m3 = m1,m1);
109
+ MatrixType m4;
110
+ VERIFY_IS_APPROX(m4 = m1,m1);
111
+
112
+ m3.real() = m1.real();
113
+ VERIFY_IS_APPROX(static_cast<const MatrixType&>(m3).real(), static_cast<const MatrixType&>(m1).real());
114
+ VERIFY_IS_APPROX(static_cast<const MatrixType&>(m3).real(), m1.real());
115
+
116
+ // check == / != operators
117
+ VERIFY(m1==m1);
118
+ VERIFY(m1!=m2);
119
+ VERIFY(!(m1==m2));
120
+ VERIFY(!(m1!=m1));
121
+ m1 = m2;
122
+ VERIFY(m1==m2);
123
+ VERIFY(!(m1!=m2));
124
+
125
+ // check automatic transposition
126
+ sm2.setZero();
127
+ for(Index i=0;i<rows;++i)
128
+ sm2.col(i) = sm1.row(i);
129
+ VERIFY_IS_APPROX(sm2,sm1.transpose());
130
+
131
+ sm2.setZero();
132
+ for(Index i=0;i<rows;++i)
133
+ sm2.col(i).noalias() = sm1.row(i);
134
+ VERIFY_IS_APPROX(sm2,sm1.transpose());
135
+
136
+ sm2.setZero();
137
+ for(Index i=0;i<rows;++i)
138
+ sm2.col(i).noalias() += sm1.row(i);
139
+ VERIFY_IS_APPROX(sm2,sm1.transpose());
140
+
141
+ sm2.setZero();
142
+ for(Index i=0;i<rows;++i)
143
+ sm2.col(i).noalias() -= sm1.row(i);
144
+ VERIFY_IS_APPROX(sm2,-sm1.transpose());
145
+
146
+ // check ternary usage
147
+ {
148
+ bool b = internal::random<int>(0,10)>5;
149
+ m3 = b ? m1 : m2;
150
+ if(b) VERIFY_IS_APPROX(m3,m1);
151
+ else VERIFY_IS_APPROX(m3,m2);
152
+ m3 = b ? -m1 : m2;
153
+ if(b) VERIFY_IS_APPROX(m3,-m1);
154
+ else VERIFY_IS_APPROX(m3,m2);
155
+ m3 = b ? m1 : -m2;
156
+ if(b) VERIFY_IS_APPROX(m3,m1);
157
+ else VERIFY_IS_APPROX(m3,-m2);
158
+ }
159
+ }
160
+
161
+ template<typename MatrixType> void basicStuffComplex(const MatrixType& m)
162
+ {
163
+ typedef typename MatrixType::Scalar Scalar;
164
+ typedef typename NumTraits<Scalar>::Real RealScalar;
165
+ typedef Matrix<RealScalar, MatrixType::RowsAtCompileTime, MatrixType::ColsAtCompileTime> RealMatrixType;
166
+
167
+ Index rows = m.rows();
168
+ Index cols = m.cols();
169
+
170
+ Scalar s1 = internal::random<Scalar>(),
171
+ s2 = internal::random<Scalar>();
172
+
173
+ VERIFY(numext::real(s1)==numext::real_ref(s1));
174
+ VERIFY(numext::imag(s1)==numext::imag_ref(s1));
175
+ numext::real_ref(s1) = numext::real(s2);
176
+ numext::imag_ref(s1) = numext::imag(s2);
177
+ VERIFY(internal::isApprox(s1, s2, NumTraits<RealScalar>::epsilon()));
178
+ // extended precision in Intel FPUs means that s1 == s2 in the line above is not guaranteed.
179
+
180
+ RealMatrixType rm1 = RealMatrixType::Random(rows,cols),
181
+ rm2 = RealMatrixType::Random(rows,cols);
182
+ MatrixType cm(rows,cols);
183
+ cm.real() = rm1;
184
+ cm.imag() = rm2;
185
+ VERIFY_IS_APPROX(static_cast<const MatrixType&>(cm).real(), rm1);
186
+ VERIFY_IS_APPROX(static_cast<const MatrixType&>(cm).imag(), rm2);
187
+ rm1.setZero();
188
+ rm2.setZero();
189
+ rm1 = cm.real();
190
+ rm2 = cm.imag();
191
+ VERIFY_IS_APPROX(static_cast<const MatrixType&>(cm).real(), rm1);
192
+ VERIFY_IS_APPROX(static_cast<const MatrixType&>(cm).imag(), rm2);
193
+ cm.real().setZero();
194
+ VERIFY(static_cast<const MatrixType&>(cm).real().isZero());
195
+ VERIFY(!static_cast<const MatrixType&>(cm).imag().isZero());
196
+ }
197
+
198
+ template<typename SrcScalar, typename TgtScalar>
199
+ struct casting_test {
200
+ static void run() {
201
+ Matrix<SrcScalar,4,4> m;
202
+ for (int i=0; i<m.rows(); ++i) {
203
+ for (int j=0; j<m.cols(); ++j) {
204
+ m(i, j) = internal::random_without_cast_overflow<SrcScalar,TgtScalar>::value();
205
+ }
206
+ }
207
+ Matrix<TgtScalar,4,4> n = m.template cast<TgtScalar>();
208
+ for (int i=0; i<m.rows(); ++i) {
209
+ for (int j=0; j<m.cols(); ++j) {
210
+ VERIFY_IS_APPROX(n(i, j), (internal::cast<SrcScalar,TgtScalar>(m(i, j))));
211
+ }
212
+ }
213
+ }
214
+ };
215
+
216
+ template<typename SrcScalar, typename EnableIf = void>
217
+ struct casting_test_runner {
218
+ static void run() {
219
+ casting_test<SrcScalar, bool>::run();
220
+ casting_test<SrcScalar, int8_t>::run();
221
+ casting_test<SrcScalar, uint8_t>::run();
222
+ casting_test<SrcScalar, int16_t>::run();
223
+ casting_test<SrcScalar, uint16_t>::run();
224
+ casting_test<SrcScalar, int32_t>::run();
225
+ casting_test<SrcScalar, uint32_t>::run();
226
+ #if EIGEN_HAS_CXX11
227
+ casting_test<SrcScalar, int64_t>::run();
228
+ casting_test<SrcScalar, uint64_t>::run();
229
+ #endif
230
+ casting_test<SrcScalar, half>::run();
231
+ casting_test<SrcScalar, bfloat16>::run();
232
+ casting_test<SrcScalar, float>::run();
233
+ casting_test<SrcScalar, double>::run();
234
+ casting_test<SrcScalar, std::complex<float> >::run();
235
+ casting_test<SrcScalar, std::complex<double> >::run();
236
+ }
237
+ };
238
+
239
+ template<typename SrcScalar>
240
+ struct casting_test_runner<SrcScalar, typename internal::enable_if<(NumTraits<SrcScalar>::IsComplex)>::type>
241
+ {
242
+ static void run() {
243
+ // Only a few casts from std::complex<T> are defined.
244
+ casting_test<SrcScalar, half>::run();
245
+ casting_test<SrcScalar, bfloat16>::run();
246
+ casting_test<SrcScalar, std::complex<float> >::run();
247
+ casting_test<SrcScalar, std::complex<double> >::run();
248
+ }
249
+ };
250
+
251
+ void casting_all() {
252
+ casting_test_runner<bool>::run();
253
+ casting_test_runner<int8_t>::run();
254
+ casting_test_runner<uint8_t>::run();
255
+ casting_test_runner<int16_t>::run();
256
+ casting_test_runner<uint16_t>::run();
257
+ casting_test_runner<int32_t>::run();
258
+ casting_test_runner<uint32_t>::run();
259
+ #if EIGEN_HAS_CXX11
260
+ casting_test_runner<int64_t>::run();
261
+ casting_test_runner<uint64_t>::run();
262
+ #endif
263
+ casting_test_runner<half>::run();
264
+ casting_test_runner<bfloat16>::run();
265
+ casting_test_runner<float>::run();
266
+ casting_test_runner<double>::run();
267
+ casting_test_runner<std::complex<float> >::run();
268
+ casting_test_runner<std::complex<double> >::run();
269
+ }
270
+
271
+ template <typename Scalar>
272
+ void fixedSizeMatrixConstruction()
273
+ {
274
+ Scalar raw[4];
275
+ for(int k=0; k<4; ++k)
276
+ raw[k] = internal::random<Scalar>();
277
+
278
+ {
279
+ Matrix<Scalar,4,1> m(raw);
280
+ Array<Scalar,4,1> a(raw);
281
+ for(int k=0; k<4; ++k) VERIFY(m(k) == raw[k]);
282
+ for(int k=0; k<4; ++k) VERIFY(a(k) == raw[k]);
283
+ VERIFY_IS_EQUAL(m,(Matrix<Scalar,4,1>(raw[0],raw[1],raw[2],raw[3])));
284
+ VERIFY((a==(Array<Scalar,4,1>(raw[0],raw[1],raw[2],raw[3]))).all());
285
+ }
286
+ {
287
+ Matrix<Scalar,3,1> m(raw);
288
+ Array<Scalar,3,1> a(raw);
289
+ for(int k=0; k<3; ++k) VERIFY(m(k) == raw[k]);
290
+ for(int k=0; k<3; ++k) VERIFY(a(k) == raw[k]);
291
+ VERIFY_IS_EQUAL(m,(Matrix<Scalar,3,1>(raw[0],raw[1],raw[2])));
292
+ VERIFY((a==Array<Scalar,3,1>(raw[0],raw[1],raw[2])).all());
293
+ }
294
+ {
295
+ Matrix<Scalar,2,1> m(raw), m2( (DenseIndex(raw[0])), (DenseIndex(raw[1])) );
296
+ Array<Scalar,2,1> a(raw), a2( (DenseIndex(raw[0])), (DenseIndex(raw[1])) );
297
+ for(int k=0; k<2; ++k) VERIFY(m(k) == raw[k]);
298
+ for(int k=0; k<2; ++k) VERIFY(a(k) == raw[k]);
299
+ VERIFY_IS_EQUAL(m,(Matrix<Scalar,2,1>(raw[0],raw[1])));
300
+ VERIFY((a==Array<Scalar,2,1>(raw[0],raw[1])).all());
301
+ for(int k=0; k<2; ++k) VERIFY(m2(k) == DenseIndex(raw[k]));
302
+ for(int k=0; k<2; ++k) VERIFY(a2(k) == DenseIndex(raw[k]));
303
+ }
304
+ {
305
+ Matrix<Scalar,1,2> m(raw),
306
+ m2( (DenseIndex(raw[0])), (DenseIndex(raw[1])) ),
307
+ m3( (int(raw[0])), (int(raw[1])) ),
308
+ m4( (float(raw[0])), (float(raw[1])) );
309
+ Array<Scalar,1,2> a(raw), a2( (DenseIndex(raw[0])), (DenseIndex(raw[1])) );
310
+ for(int k=0; k<2; ++k) VERIFY(m(k) == raw[k]);
311
+ for(int k=0; k<2; ++k) VERIFY(a(k) == raw[k]);
312
+ VERIFY_IS_EQUAL(m,(Matrix<Scalar,1,2>(raw[0],raw[1])));
313
+ VERIFY((a==Array<Scalar,1,2>(raw[0],raw[1])).all());
314
+ for(int k=0; k<2; ++k) VERIFY(m2(k) == DenseIndex(raw[k]));
315
+ for(int k=0; k<2; ++k) VERIFY(a2(k) == DenseIndex(raw[k]));
316
+ for(int k=0; k<2; ++k) VERIFY(m3(k) == int(raw[k]));
317
+ for(int k=0; k<2; ++k) VERIFY((m4(k)) == Scalar(float(raw[k])));
318
+ }
319
+ {
320
+ Matrix<Scalar,1,1> m(raw), m1(raw[0]), m2( (DenseIndex(raw[0])) ), m3( (int(raw[0])) );
321
+ Array<Scalar,1,1> a(raw), a1(raw[0]), a2( (DenseIndex(raw[0])) );
322
+ VERIFY(m(0) == raw[0]);
323
+ VERIFY(a(0) == raw[0]);
324
+ VERIFY(m1(0) == raw[0]);
325
+ VERIFY(a1(0) == raw[0]);
326
+ VERIFY(m2(0) == DenseIndex(raw[0]));
327
+ VERIFY(a2(0) == DenseIndex(raw[0]));
328
+ VERIFY(m3(0) == int(raw[0]));
329
+ VERIFY_IS_EQUAL(m,(Matrix<Scalar,1,1>(raw[0])));
330
+ VERIFY((a==Array<Scalar,1,1>(raw[0])).all());
331
+ }
332
+ }
333
+
334
+ EIGEN_DECLARE_TEST(basicstuff)
335
+ {
336
+ for(int i = 0; i < g_repeat; i++) {
337
+ CALL_SUBTEST_1( basicStuff(Matrix<float, 1, 1>()) );
338
+ CALL_SUBTEST_2( basicStuff(Matrix4d()) );
339
+ CALL_SUBTEST_3( basicStuff(MatrixXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
340
+ CALL_SUBTEST_4( basicStuff(MatrixXi(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
341
+ CALL_SUBTEST_5( basicStuff(MatrixXcd(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
342
+ CALL_SUBTEST_6( basicStuff(Matrix<float, 100, 100>()) );
343
+ CALL_SUBTEST_7( basicStuff(Matrix<long double,Dynamic,Dynamic>(internal::random<int>(1,EIGEN_TEST_MAX_SIZE),internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
344
+ CALL_SUBTEST_8( casting_all() );
345
+
346
+ CALL_SUBTEST_3( basicStuffComplex(MatrixXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
347
+ CALL_SUBTEST_5( basicStuffComplex(MatrixXcd(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
348
+ }
349
+
350
+ CALL_SUBTEST_1(fixedSizeMatrixConstruction<unsigned char>());
351
+ CALL_SUBTEST_1(fixedSizeMatrixConstruction<float>());
352
+ CALL_SUBTEST_1(fixedSizeMatrixConstruction<double>());
353
+ CALL_SUBTEST_1(fixedSizeMatrixConstruction<int>());
354
+ CALL_SUBTEST_1(fixedSizeMatrixConstruction<long int>());
355
+ CALL_SUBTEST_1(fixedSizeMatrixConstruction<std::ptrdiff_t>());
356
+ }
include/eigen/test/bicgstab.cpp ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // This file is part of Eigen, a lightweight C++ template library
2
+ // for linear algebra.
3
+ //
4
+ // Copyright (C) 2011 Gael Guennebaud <g.gael@free.fr>
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 "sparse_solver.h"
11
+ #include <Eigen/IterativeLinearSolvers>
12
+
13
+ template<typename T, typename I_> void test_bicgstab_T()
14
+ {
15
+ BiCGSTAB<SparseMatrix<T,0,I_>, DiagonalPreconditioner<T> > bicgstab_colmajor_diag;
16
+ BiCGSTAB<SparseMatrix<T,0,I_>, IdentityPreconditioner > bicgstab_colmajor_I;
17
+ BiCGSTAB<SparseMatrix<T,0,I_>, IncompleteLUT<T,I_> > bicgstab_colmajor_ilut;
18
+ //BiCGSTAB<SparseMatrix<T>, SSORPreconditioner<T> > bicgstab_colmajor_ssor;
19
+
20
+ bicgstab_colmajor_diag.setTolerance(NumTraits<T>::epsilon()*4);
21
+ bicgstab_colmajor_ilut.setTolerance(NumTraits<T>::epsilon()*4);
22
+
23
+ CALL_SUBTEST( check_sparse_square_solving(bicgstab_colmajor_diag) );
24
+ // CALL_SUBTEST( check_sparse_square_solving(bicgstab_colmajor_I) );
25
+ CALL_SUBTEST( check_sparse_square_solving(bicgstab_colmajor_ilut) );
26
+ //CALL_SUBTEST( check_sparse_square_solving(bicgstab_colmajor_ssor) );
27
+ }
28
+
29
+ EIGEN_DECLARE_TEST(bicgstab)
30
+ {
31
+ CALL_SUBTEST_1((test_bicgstab_T<double,int>()) );
32
+ CALL_SUBTEST_2((test_bicgstab_T<std::complex<double>, int>()));
33
+ CALL_SUBTEST_3((test_bicgstab_T<double,long int>()));
34
+ }
include/eigen/test/blasutil.cpp ADDED
@@ -0,0 +1,210 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // This file is part of Eigen, a lightweight C++ template library
2
+ // for linear algebra.
3
+ //
4
+ // Copyright (C) 2020 Everton Constantino <everton.constantino@ibm.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
+ // Disable "ignoring attributes on template argument"
13
+ // for packet_traits<Packet*>
14
+ // => The only workaround would be to wrap _m128 and the likes
15
+ // within wrappers.
16
+ #if EIGEN_GNUC_AT_LEAST(6,0)
17
+ #pragma GCC diagnostic ignored "-Wignored-attributes"
18
+ #endif
19
+
20
+ #define GET(i,j) (StorageOrder == RowMajor ? (i)*stride + (j) : (i) + (j)*stride)
21
+ #define SCATTER(i,j,k) (StorageOrder == RowMajor ? ((i)+(k))*stride + (j) : (i) + ((j)+(k))*stride)
22
+
23
+ template<typename Scalar, typename Packet>
24
+ void compare(const Packet& a, const Packet& b)
25
+ {
26
+ int pktsz = internal::packet_traits<Scalar>::size;
27
+ Scalar *buffA = new Scalar[pktsz];
28
+ Scalar *buffB = new Scalar[pktsz];
29
+
30
+ internal::pstoreu<Scalar, Packet>(buffA, a);
31
+ internal::pstoreu<Scalar, Packet>(buffB, b);
32
+
33
+ for(int i = 0; i < pktsz; i++)
34
+ {
35
+ VERIFY_IS_EQUAL(buffA[i], buffB[i]);
36
+ }
37
+
38
+ delete[] buffA;
39
+ delete[] buffB;
40
+ }
41
+
42
+ template<typename Scalar, int StorageOrder, int n>
43
+ struct PacketBlockSet
44
+ {
45
+ typedef typename internal::packet_traits<Scalar>::type Packet;
46
+
47
+ void setPacketBlock(internal::PacketBlock<Packet,n>& block, Scalar value)
48
+ {
49
+ for(int idx = 0; idx < n; idx++)
50
+ {
51
+ block.packet[idx] = internal::pset1<Packet>(value);
52
+ }
53
+ }
54
+
55
+ void comparePacketBlock(Scalar *data, int i, int j, int stride, internal::PacketBlock<Packet, n>& block)
56
+ {
57
+ for(int idx = 0; idx < n; idx++)
58
+ {
59
+ Packet line = internal::ploadu<Packet>(data + SCATTER(i,j,idx));
60
+ compare<Scalar, Packet>(block.packet[idx], line);
61
+ }
62
+ }
63
+ };
64
+
65
+ template<typename Scalar, int StorageOrder, int BlockSize>
66
+ void run_bdmp_spec_1()
67
+ {
68
+ typedef internal::blas_data_mapper<Scalar, int, StorageOrder> BlasDataMapper;
69
+ int packetSize = internal::packet_traits<Scalar>::size;
70
+ int minSize = std::max<int>(packetSize, BlockSize);
71
+ typedef typename internal::packet_traits<Scalar>::type Packet;
72
+
73
+ int szm = internal::random<int>(minSize,500), szn = internal::random<int>(minSize,500);
74
+ int stride = StorageOrder == RowMajor ? szn : szm;
75
+ Scalar *d = new Scalar[szn*szm];
76
+
77
+ // Initializing with random entries
78
+ for(int i = 0; i < szm*szn; i++)
79
+ {
80
+ d[i] = internal::random<Scalar>(static_cast<Scalar>(3), static_cast<Scalar>(10));
81
+ }
82
+
83
+ BlasDataMapper bdm(d, stride);
84
+
85
+ // Testing operator()
86
+ for(int i = 0; i < szm; i++)
87
+ {
88
+ for(int j = 0; j < szn; j++)
89
+ {
90
+ VERIFY_IS_EQUAL(d[GET(i,j)], bdm(i,j));
91
+ }
92
+ }
93
+
94
+ // Testing getSubMapper and getLinearMapper
95
+ int i0 = internal::random<int>(0,szm-2);
96
+ int j0 = internal::random<int>(0,szn-2);
97
+ for(int i = i0; i < szm; i++)
98
+ {
99
+ for(int j = j0; j < szn; j++)
100
+ {
101
+ const BlasDataMapper& bdmSM = bdm.getSubMapper(i0,j0);
102
+ const internal::BlasLinearMapper<Scalar, int, 0>& bdmLM = bdm.getLinearMapper(i0,j0);
103
+
104
+ Scalar v = bdmSM(i - i0, j - j0);
105
+ Scalar vd = d[GET(i,j)];
106
+ VERIFY_IS_EQUAL(vd, v);
107
+ VERIFY_IS_EQUAL(vd, bdmLM(GET(i-i0, j-j0)));
108
+ }
109
+ }
110
+
111
+ // Testing loadPacket
112
+ for(int i = 0; i < szm - minSize; i++)
113
+ {
114
+ for(int j = 0; j < szn - minSize; j++)
115
+ {
116
+ Packet pktBDM = bdm.template loadPacket<Packet>(i,j);
117
+ Packet pktD = internal::ploadu<Packet>(d + GET(i,j));
118
+
119
+ compare<Scalar, Packet>(pktBDM, pktD);
120
+ }
121
+ }
122
+
123
+ // Testing gatherPacket
124
+ Scalar *buff = new Scalar[packetSize];
125
+ for(int i = 0; i < szm - minSize; i++)
126
+ {
127
+ for(int j = 0; j < szn - minSize; j++)
128
+ {
129
+ Packet p = bdm.template gatherPacket<Packet>(i,j);
130
+ internal::pstoreu<Scalar, Packet>(buff, p);
131
+
132
+ for(int k = 0; k < packetSize; k++)
133
+ {
134
+ VERIFY_IS_EQUAL(d[SCATTER(i,j,k)], buff[k]);
135
+ }
136
+
137
+ }
138
+ }
139
+ delete[] buff;
140
+
141
+ // Testing scatterPacket
142
+ for(int i = 0; i < szm - minSize; i++)
143
+ {
144
+ for(int j = 0; j < szn - minSize; j++)
145
+ {
146
+ Packet p = internal::pset1<Packet>(static_cast<Scalar>(1));
147
+ bdm.template scatterPacket<Packet>(i,j,p);
148
+ for(int k = 0; k < packetSize; k++)
149
+ {
150
+ VERIFY_IS_EQUAL(d[SCATTER(i,j,k)], static_cast<Scalar>(1));
151
+ }
152
+ }
153
+ }
154
+
155
+ //Testing storePacketBlock
156
+ internal::PacketBlock<Packet, BlockSize> block;
157
+
158
+ PacketBlockSet<Scalar, StorageOrder, BlockSize> pbs;
159
+ pbs.setPacketBlock(block, static_cast<Scalar>(2));
160
+
161
+ for(int i = 0; i < szm - minSize; i++)
162
+ {
163
+ for(int j = 0; j < szn - minSize; j++)
164
+ {
165
+ bdm.template storePacketBlock<Packet, BlockSize>(i, j, block);
166
+
167
+ pbs.comparePacketBlock(d, i, j, stride, block);
168
+ }
169
+ }
170
+
171
+ delete[] d;
172
+ }
173
+
174
+ template<typename Scalar>
175
+ void run_test()
176
+ {
177
+ run_bdmp_spec_1<Scalar, RowMajor, 1>();
178
+ run_bdmp_spec_1<Scalar, ColMajor, 1>();
179
+ run_bdmp_spec_1<Scalar, RowMajor, 2>();
180
+ run_bdmp_spec_1<Scalar, ColMajor, 2>();
181
+ run_bdmp_spec_1<Scalar, RowMajor, 4>();
182
+ run_bdmp_spec_1<Scalar, ColMajor, 4>();
183
+ run_bdmp_spec_1<Scalar, RowMajor, 8>();
184
+ run_bdmp_spec_1<Scalar, ColMajor, 8>();
185
+ run_bdmp_spec_1<Scalar, RowMajor, 16>();
186
+ run_bdmp_spec_1<Scalar, ColMajor, 16>();
187
+ }
188
+
189
+ EIGEN_DECLARE_TEST(blasutil)
190
+ {
191
+ for(int i = 0; i < g_repeat; i++)
192
+ {
193
+ CALL_SUBTEST_1(run_test<numext::int8_t>());
194
+ CALL_SUBTEST_2(run_test<numext::int16_t>());
195
+ CALL_SUBTEST_3(run_test<numext::int32_t>());
196
+
197
+ // TODO: Replace this by a call to numext::int64_t as soon as we have a way to
198
+ // detect the typedef for int64_t on all platforms
199
+ #if EIGEN_HAS_CXX11
200
+ CALL_SUBTEST_4(run_test<signed long long>());
201
+ #else
202
+ CALL_SUBTEST_4(run_test<signed long>());
203
+ #endif
204
+
205
+ CALL_SUBTEST_5(run_test<float_t>());
206
+ CALL_SUBTEST_6(run_test<double_t>());
207
+ CALL_SUBTEST_7(run_test<std::complex<float> >());
208
+ CALL_SUBTEST_8(run_test<std::complex<double> >());
209
+ }
210
+ }
include/eigen/test/block.cpp ADDED
@@ -0,0 +1,317 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // This file is part of Eigen, a lightweight C++ template library
2
+ // for linear algebra.
3
+ //
4
+ // Copyright (C) 2006-2010 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
+ #define EIGEN_NO_STATIC_ASSERT // otherwise we fail at compile time on unused paths
11
+ #include "main.h"
12
+
13
+ template<typename MatrixType, typename Index, typename Scalar>
14
+ typename Eigen::internal::enable_if<!NumTraits<typename MatrixType::Scalar>::IsComplex,typename MatrixType::Scalar>::type
15
+ block_real_only(const MatrixType &m1, Index r1, Index r2, Index c1, Index c2, const Scalar& s1) {
16
+ // check cwise-Functions:
17
+ VERIFY_IS_APPROX(m1.row(r1).cwiseMax(s1), m1.cwiseMax(s1).row(r1));
18
+ VERIFY_IS_APPROX(m1.col(c1).cwiseMin(s1), m1.cwiseMin(s1).col(c1));
19
+
20
+ VERIFY_IS_APPROX(m1.block(r1,c1,r2-r1+1,c2-c1+1).cwiseMin(s1), m1.cwiseMin(s1).block(r1,c1,r2-r1+1,c2-c1+1));
21
+ VERIFY_IS_APPROX(m1.block(r1,c1,r2-r1+1,c2-c1+1).cwiseMax(s1), m1.cwiseMax(s1).block(r1,c1,r2-r1+1,c2-c1+1));
22
+
23
+ return Scalar(0);
24
+ }
25
+
26
+ template<typename MatrixType, typename Index, typename Scalar>
27
+ typename Eigen::internal::enable_if<NumTraits<typename MatrixType::Scalar>::IsComplex,typename MatrixType::Scalar>::type
28
+ block_real_only(const MatrixType &, Index, Index, Index, Index, const Scalar&) {
29
+ return Scalar(0);
30
+ }
31
+
32
+ // Check at compile-time that T1==T2, and at runtime-time that a==b
33
+ template<typename T1,typename T2>
34
+ typename internal::enable_if<internal::is_same<T1,T2>::value,bool>::type
35
+ is_same_block(const T1& a, const T2& b)
36
+ {
37
+ return a.isApprox(b);
38
+ }
39
+
40
+ template<typename MatrixType> void block(const MatrixType& m)
41
+ {
42
+ typedef typename MatrixType::Scalar Scalar;
43
+ typedef typename MatrixType::RealScalar RealScalar;
44
+ typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
45
+ typedef Matrix<Scalar, 1, MatrixType::ColsAtCompileTime> RowVectorType;
46
+ typedef Matrix<Scalar, Dynamic, Dynamic, MatrixType::IsRowMajor?RowMajor:ColMajor> DynamicMatrixType;
47
+ typedef Matrix<Scalar, Dynamic, 1> DynamicVectorType;
48
+
49
+ Index rows = m.rows();
50
+ Index cols = m.cols();
51
+
52
+ MatrixType m1 = MatrixType::Random(rows, cols),
53
+ m1_copy = m1,
54
+ m2 = MatrixType::Random(rows, cols),
55
+ m3(rows, cols),
56
+ ones = MatrixType::Ones(rows, cols);
57
+ VectorType v1 = VectorType::Random(rows);
58
+
59
+ Scalar s1 = internal::random<Scalar>();
60
+
61
+ Index r1 = internal::random<Index>(0,rows-1);
62
+ Index r2 = internal::random<Index>(r1,rows-1);
63
+ Index c1 = internal::random<Index>(0,cols-1);
64
+ Index c2 = internal::random<Index>(c1,cols-1);
65
+
66
+ block_real_only(m1, r1, r2, c1, c1, s1);
67
+
68
+ //check row() and col()
69
+ VERIFY_IS_EQUAL(m1.col(c1).transpose(), m1.transpose().row(c1));
70
+ //check operator(), both constant and non-constant, on row() and col()
71
+ m1 = m1_copy;
72
+ m1.row(r1) += s1 * m1_copy.row(r2);
73
+ VERIFY_IS_APPROX(m1.row(r1), m1_copy.row(r1) + s1 * m1_copy.row(r2));
74
+ // check nested block xpr on lhs
75
+ m1.row(r1).row(0) += s1 * m1_copy.row(r2);
76
+ VERIFY_IS_APPROX(m1.row(r1), m1_copy.row(r1) + Scalar(2) * s1 * m1_copy.row(r2));
77
+ m1 = m1_copy;
78
+ m1.col(c1) += s1 * m1_copy.col(c2);
79
+ VERIFY_IS_APPROX(m1.col(c1), m1_copy.col(c1) + s1 * m1_copy.col(c2));
80
+ m1.col(c1).col(0) += s1 * m1_copy.col(c2);
81
+ VERIFY_IS_APPROX(m1.col(c1), m1_copy.col(c1) + Scalar(2) * s1 * m1_copy.col(c2));
82
+
83
+
84
+ //check block()
85
+ Matrix<Scalar,Dynamic,Dynamic> b1(1,1); b1(0,0) = m1(r1,c1);
86
+
87
+ RowVectorType br1(m1.block(r1,0,1,cols));
88
+ VectorType bc1(m1.block(0,c1,rows,1));
89
+ VERIFY_IS_EQUAL(b1, m1.block(r1,c1,1,1));
90
+ VERIFY_IS_EQUAL(m1.row(r1), br1);
91
+ VERIFY_IS_EQUAL(m1.col(c1), bc1);
92
+ //check operator(), both constant and non-constant, on block()
93
+ m1.block(r1,c1,r2-r1+1,c2-c1+1) = s1 * m2.block(0, 0, r2-r1+1,c2-c1+1);
94
+ m1.block(r1,c1,r2-r1+1,c2-c1+1)(r2-r1,c2-c1) = m2.block(0, 0, r2-r1+1,c2-c1+1)(0,0);
95
+
96
+ const Index BlockRows = 2;
97
+ const Index BlockCols = 5;
98
+
99
+ if (rows>=5 && cols>=8)
100
+ {
101
+ // test fixed block() as lvalue
102
+ m1.template block<BlockRows,BlockCols>(1,1) *= s1;
103
+ // test operator() on fixed block() both as constant and non-constant
104
+ m1.template block<BlockRows,BlockCols>(1,1)(0, 3) = m1.template block<2,5>(1,1)(1,2);
105
+ // check that fixed block() and block() agree
106
+ Matrix<Scalar,Dynamic,Dynamic> b = m1.template block<BlockRows,BlockCols>(3,3);
107
+ VERIFY_IS_EQUAL(b, m1.block(3,3,BlockRows,BlockCols));
108
+
109
+ // same tests with mixed fixed/dynamic size
110
+ m1.template block<BlockRows,Dynamic>(1,1,BlockRows,BlockCols) *= s1;
111
+ m1.template block<BlockRows,Dynamic>(1,1,BlockRows,BlockCols)(0,3) = m1.template block<2,5>(1,1)(1,2);
112
+ Matrix<Scalar,Dynamic,Dynamic> b2 = m1.template block<Dynamic,BlockCols>(3,3,2,5);
113
+ VERIFY_IS_EQUAL(b2, m1.block(3,3,BlockRows,BlockCols));
114
+
115
+ VERIFY(is_same_block(m1.block(3,3,BlockRows,BlockCols), m1.block(3,3,fix<Dynamic>(BlockRows),fix<Dynamic>(BlockCols))));
116
+ VERIFY(is_same_block(m1.template block<BlockRows,Dynamic>(1,1,BlockRows,BlockCols), m1.block(1,1,fix<BlockRows>,BlockCols)));
117
+ VERIFY(is_same_block(m1.template block<BlockRows,BlockCols>(1,1,BlockRows,BlockCols), m1.block(1,1,fix<BlockRows>(),fix<BlockCols>)));
118
+ VERIFY(is_same_block(m1.template block<BlockRows,BlockCols>(1,1,BlockRows,BlockCols), m1.block(1,1,fix<BlockRows>,fix<BlockCols>(BlockCols))));
119
+ }
120
+
121
+ if (rows>2)
122
+ {
123
+ // test sub vectors
124
+ VERIFY_IS_EQUAL(v1.template head<2>(), v1.block(0,0,2,1));
125
+ VERIFY_IS_EQUAL(v1.template head<2>(), v1.head(2));
126
+ VERIFY_IS_EQUAL(v1.template head<2>(), v1.segment(0,2));
127
+ VERIFY_IS_EQUAL(v1.template head<2>(), v1.template segment<2>(0));
128
+ Index i = rows-2;
129
+ VERIFY_IS_EQUAL(v1.template tail<2>(), v1.block(i,0,2,1));
130
+ VERIFY_IS_EQUAL(v1.template tail<2>(), v1.tail(2));
131
+ VERIFY_IS_EQUAL(v1.template tail<2>(), v1.segment(i,2));
132
+ VERIFY_IS_EQUAL(v1.template tail<2>(), v1.template segment<2>(i));
133
+ i = internal::random<Index>(0,rows-2);
134
+ VERIFY_IS_EQUAL(v1.segment(i,2), v1.template segment<2>(i));
135
+ }
136
+
137
+ // stress some basic stuffs with block matrices
138
+ VERIFY(numext::real(ones.col(c1).sum()) == RealScalar(rows));
139
+ VERIFY(numext::real(ones.row(r1).sum()) == RealScalar(cols));
140
+
141
+ VERIFY(numext::real(ones.col(c1).dot(ones.col(c2))) == RealScalar(rows));
142
+ VERIFY(numext::real(ones.row(r1).dot(ones.row(r2))) == RealScalar(cols));
143
+
144
+ // check that linear acccessors works on blocks
145
+ m1 = m1_copy;
146
+ if (c1 > 0 && r1 > 0) {
147
+ if ((MatrixType::Flags & RowMajorBit) == 0)
148
+ VERIFY_IS_EQUAL(m1.leftCols(c1).coeff(r1 + c1 * rows), m1(r1, c1));
149
+ else
150
+ VERIFY_IS_EQUAL(m1.topRows(r1).coeff(c1 + r1 * cols), m1(r1, c1));
151
+ }
152
+
153
+ // now test some block-inside-of-block.
154
+
155
+ // expressions with direct access
156
+ VERIFY_IS_EQUAL( (m1.block(r1,c1,rows-r1,cols-c1).block(r2-r1,c2-c1,rows-r2,cols-c2)) , (m1.block(r2,c2,rows-r2,cols-c2)) );
157
+ VERIFY_IS_EQUAL( (m1.block(r1,c1,r2-r1+1,c2-c1+1).row(0)) , (m1.row(r1).segment(c1,c2-c1+1)) );
158
+ VERIFY_IS_EQUAL( (m1.block(r1,c1,r2-r1+1,c2-c1+1).col(0)) , (m1.col(c1).segment(r1,r2-r1+1)) );
159
+ VERIFY_IS_EQUAL( (m1.block(r1,c1,r2-r1+1,c2-c1+1).transpose().col(0)) , (m1.row(r1).segment(c1,c2-c1+1)).transpose() );
160
+ VERIFY_IS_EQUAL( (m1.transpose().block(c1,r1,c2-c1+1,r2-r1+1).col(0)) , (m1.row(r1).segment(c1,c2-c1+1)).transpose() );
161
+
162
+ // expressions without direct access
163
+ VERIFY_IS_APPROX( ((m1+m2).block(r1,c1,rows-r1,cols-c1).block(r2-r1,c2-c1,rows-r2,cols-c2)) , ((m1+m2).block(r2,c2,rows-r2,cols-c2)) );
164
+ VERIFY_IS_APPROX( ((m1+m2).block(r1,c1,r2-r1+1,c2-c1+1).row(0)) , ((m1+m2).row(r1).segment(c1,c2-c1+1)) );
165
+ VERIFY_IS_APPROX( ((m1+m2).block(r1,c1,r2-r1+1,c2-c1+1).row(0)) , ((m1+m2).eval().row(r1).segment(c1,c2-c1+1)) );
166
+ VERIFY_IS_APPROX( ((m1+m2).block(r1,c1,r2-r1+1,c2-c1+1).col(0)) , ((m1+m2).col(c1).segment(r1,r2-r1+1)) );
167
+ VERIFY_IS_APPROX( ((m1+m2).block(r1,c1,r2-r1+1,c2-c1+1).transpose().col(0)) , ((m1+m2).row(r1).segment(c1,c2-c1+1)).transpose() );
168
+ VERIFY_IS_APPROX( ((m1+m2).transpose().block(c1,r1,c2-c1+1,r2-r1+1).col(0)) , ((m1+m2).row(r1).segment(c1,c2-c1+1)).transpose() );
169
+ VERIFY_IS_APPROX( ((m1+m2).template block<Dynamic,1>(r1,c1,r2-r1+1,1)) , ((m1+m2).eval().col(c1).eval().segment(r1,r2-r1+1)) );
170
+ VERIFY_IS_APPROX( ((m1+m2).template block<1,Dynamic>(r1,c1,1,c2-c1+1)) , ((m1+m2).eval().row(r1).eval().segment(c1,c2-c1+1)) );
171
+ VERIFY_IS_APPROX( ((m1+m2).transpose().template block<1,Dynamic>(c1,r1,1,r2-r1+1)) , ((m1+m2).eval().col(c1).eval().segment(r1,r2-r1+1)).transpose() );
172
+ VERIFY_IS_APPROX( (m1+m2).row(r1).eval(), (m1+m2).eval().row(r1) );
173
+ VERIFY_IS_APPROX( (m1+m2).adjoint().col(r1).eval(), (m1+m2).adjoint().eval().col(r1) );
174
+ VERIFY_IS_APPROX( (m1+m2).adjoint().row(c1).eval(), (m1+m2).adjoint().eval().row(c1) );
175
+ VERIFY_IS_APPROX( (m1*1).row(r1).segment(c1,c2-c1+1).eval(), m1.row(r1).eval().segment(c1,c2-c1+1).eval() );
176
+ VERIFY_IS_APPROX( m1.col(c1).reverse().segment(r1,r2-r1+1).eval(),m1.col(c1).reverse().eval().segment(r1,r2-r1+1).eval() );
177
+
178
+ VERIFY_IS_APPROX( (m1*1).topRows(r1), m1.topRows(r1) );
179
+ VERIFY_IS_APPROX( (m1*1).leftCols(c1), m1.leftCols(c1) );
180
+ VERIFY_IS_APPROX( (m1*1).transpose().topRows(c1), m1.transpose().topRows(c1) );
181
+ VERIFY_IS_APPROX( (m1*1).transpose().leftCols(r1), m1.transpose().leftCols(r1) );
182
+ VERIFY_IS_APPROX( (m1*1).transpose().middleRows(c1,c2-c1+1), m1.transpose().middleRows(c1,c2-c1+1) );
183
+ VERIFY_IS_APPROX( (m1*1).transpose().middleCols(r1,r2-r1+1), m1.transpose().middleCols(r1,r2-r1+1) );
184
+
185
+ // evaluation into plain matrices from expressions with direct access (stress MapBase)
186
+ DynamicMatrixType dm;
187
+ DynamicVectorType dv;
188
+ dm.setZero();
189
+ dm = m1.block(r1,c1,rows-r1,cols-c1).block(r2-r1,c2-c1,rows-r2,cols-c2);
190
+ VERIFY_IS_EQUAL(dm, (m1.block(r2,c2,rows-r2,cols-c2)));
191
+ dm.setZero();
192
+ dv.setZero();
193
+ dm = m1.block(r1,c1,r2-r1+1,c2-c1+1).row(0).transpose();
194
+ dv = m1.row(r1).segment(c1,c2-c1+1);
195
+ VERIFY_IS_EQUAL(dv, dm);
196
+ dm.setZero();
197
+ dv.setZero();
198
+ dm = m1.col(c1).segment(r1,r2-r1+1);
199
+ dv = m1.block(r1,c1,r2-r1+1,c2-c1+1).col(0);
200
+ VERIFY_IS_EQUAL(dv, dm);
201
+ dm.setZero();
202
+ dv.setZero();
203
+ dm = m1.block(r1,c1,r2-r1+1,c2-c1+1).transpose().col(0);
204
+ dv = m1.row(r1).segment(c1,c2-c1+1);
205
+ VERIFY_IS_EQUAL(dv, dm);
206
+ dm.setZero();
207
+ dv.setZero();
208
+ dm = m1.row(r1).segment(c1,c2-c1+1).transpose();
209
+ dv = m1.transpose().block(c1,r1,c2-c1+1,r2-r1+1).col(0);
210
+ VERIFY_IS_EQUAL(dv, dm);
211
+
212
+ VERIFY_IS_EQUAL( (m1.template block<Dynamic,1>(1,0,0,1)), m1.block(1,0,0,1));
213
+ VERIFY_IS_EQUAL( (m1.template block<1,Dynamic>(0,1,1,0)), m1.block(0,1,1,0));
214
+ VERIFY_IS_EQUAL( ((m1*1).template block<Dynamic,1>(1,0,0,1)), m1.block(1,0,0,1));
215
+ VERIFY_IS_EQUAL( ((m1*1).template block<1,Dynamic>(0,1,1,0)), m1.block(0,1,1,0));
216
+
217
+ if (rows>=2 && cols>=2)
218
+ {
219
+ VERIFY_RAISES_ASSERT( m1 += m1.col(0) );
220
+ VERIFY_RAISES_ASSERT( m1 -= m1.col(0) );
221
+ VERIFY_RAISES_ASSERT( m1.array() *= m1.col(0).array() );
222
+ VERIFY_RAISES_ASSERT( m1.array() /= m1.col(0).array() );
223
+ }
224
+
225
+ VERIFY_IS_EQUAL( m1.template subVector<Horizontal>(r1), m1.row(r1) );
226
+ VERIFY_IS_APPROX( (m1+m1).template subVector<Horizontal>(r1), (m1+m1).row(r1) );
227
+ VERIFY_IS_EQUAL( m1.template subVector<Vertical>(c1), m1.col(c1) );
228
+ VERIFY_IS_APPROX( (m1+m1).template subVector<Vertical>(c1), (m1+m1).col(c1) );
229
+ VERIFY_IS_EQUAL( m1.template subVectors<Horizontal>(), m1.rows() );
230
+ VERIFY_IS_EQUAL( m1.template subVectors<Vertical>(), m1.cols() );
231
+
232
+ if (rows>=2 || cols>=2) {
233
+ VERIFY_IS_EQUAL( int(m1.middleCols(0,0).IsRowMajor), int(m1.IsRowMajor) );
234
+ VERIFY_IS_EQUAL( m1.middleCols(0,0).outerSize(), m1.IsRowMajor ? rows : 0);
235
+ VERIFY_IS_EQUAL( m1.middleCols(0,0).innerSize(), m1.IsRowMajor ? 0 : rows);
236
+
237
+ VERIFY_IS_EQUAL( int(m1.middleRows(0,0).IsRowMajor), int(m1.IsRowMajor) );
238
+ VERIFY_IS_EQUAL( m1.middleRows(0,0).outerSize(), m1.IsRowMajor ? 0 : cols);
239
+ VERIFY_IS_EQUAL( m1.middleRows(0,0).innerSize(), m1.IsRowMajor ? cols : 0);
240
+ }
241
+ }
242
+
243
+
244
+ template<typename MatrixType>
245
+ void compare_using_data_and_stride(const MatrixType& m)
246
+ {
247
+ Index rows = m.rows();
248
+ Index cols = m.cols();
249
+ Index size = m.size();
250
+ Index innerStride = m.innerStride();
251
+ Index outerStride = m.outerStride();
252
+ Index rowStride = m.rowStride();
253
+ Index colStride = m.colStride();
254
+ const typename MatrixType::Scalar* data = m.data();
255
+
256
+ for(int j=0;j<cols;++j)
257
+ for(int i=0;i<rows;++i)
258
+ VERIFY(m.coeff(i,j) == data[i*rowStride + j*colStride]);
259
+
260
+ if(!MatrixType::IsVectorAtCompileTime)
261
+ {
262
+ for(int j=0;j<cols;++j)
263
+ for(int i=0;i<rows;++i)
264
+ VERIFY(m.coeff(i,j) == data[(MatrixType::Flags&RowMajorBit)
265
+ ? i*outerStride + j*innerStride
266
+ : j*outerStride + i*innerStride]);
267
+ }
268
+
269
+ if(MatrixType::IsVectorAtCompileTime)
270
+ {
271
+ VERIFY(innerStride == int((&m.coeff(1))-(&m.coeff(0))));
272
+ for (int i=0;i<size;++i)
273
+ VERIFY(m.coeff(i) == data[i*innerStride]);
274
+ }
275
+ }
276
+
277
+ template<typename MatrixType>
278
+ void data_and_stride(const MatrixType& m)
279
+ {
280
+ Index rows = m.rows();
281
+ Index cols = m.cols();
282
+
283
+ Index r1 = internal::random<Index>(0,rows-1);
284
+ Index r2 = internal::random<Index>(r1,rows-1);
285
+ Index c1 = internal::random<Index>(0,cols-1);
286
+ Index c2 = internal::random<Index>(c1,cols-1);
287
+
288
+ MatrixType m1 = MatrixType::Random(rows, cols);
289
+ compare_using_data_and_stride(m1.block(r1, c1, r2-r1+1, c2-c1+1));
290
+ compare_using_data_and_stride(m1.transpose().block(c1, r1, c2-c1+1, r2-r1+1));
291
+ compare_using_data_and_stride(m1.row(r1));
292
+ compare_using_data_and_stride(m1.col(c1));
293
+ compare_using_data_and_stride(m1.row(r1).transpose());
294
+ compare_using_data_and_stride(m1.col(c1).transpose());
295
+ }
296
+
297
+ EIGEN_DECLARE_TEST(block)
298
+ {
299
+ for(int i = 0; i < g_repeat; i++) {
300
+ CALL_SUBTEST_1( block(Matrix<float, 1, 1>()) );
301
+ CALL_SUBTEST_1( block(Matrix<float, 1, Dynamic>(internal::random(2,50))) );
302
+ CALL_SUBTEST_1( block(Matrix<float, Dynamic, 1>(internal::random(2,50))) );
303
+ CALL_SUBTEST_2( block(Matrix4d()) );
304
+ CALL_SUBTEST_3( block(MatrixXcf(internal::random(2,50), internal::random(2,50))) );
305
+ CALL_SUBTEST_4( block(MatrixXi(internal::random(2,50), internal::random(2,50))) );
306
+ CALL_SUBTEST_5( block(MatrixXcd(internal::random(2,50), internal::random(2,50))) );
307
+ CALL_SUBTEST_6( block(MatrixXf(internal::random(2,50), internal::random(2,50))) );
308
+ CALL_SUBTEST_7( block(Matrix<int,Dynamic,Dynamic,RowMajor>(internal::random(2,50), internal::random(2,50))) );
309
+
310
+ CALL_SUBTEST_8( block(Matrix<float,Dynamic,4>(3, 4)) );
311
+
312
+ #ifndef EIGEN_DEFAULT_TO_ROW_MAJOR
313
+ CALL_SUBTEST_6( data_and_stride(MatrixXf(internal::random(5,50), internal::random(5,50))) );
314
+ CALL_SUBTEST_7( data_and_stride(Matrix<int,Dynamic,Dynamic,RowMajor>(internal::random(5,50), internal::random(5,50))) );
315
+ #endif
316
+ }
317
+ }
include/eigen/test/bug1213.cpp ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ // This anonymous enum is essential to trigger the linking issue
3
+ enum {
4
+ Foo
5
+ };
6
+
7
+ #include "bug1213.h"
8
+
9
+ bool bug1213_1(const Eigen::Vector3f& x)
10
+ {
11
+ return bug1213_2(x);
12
+ }
13
+
include/eigen/test/bug1213.h ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+
2
+ #include <Eigen/Core>
3
+
4
+ template<typename T, int dim>
5
+ bool bug1213_2(const Eigen::Matrix<T,dim,1>& x);
6
+
7
+ bool bug1213_1(const Eigen::Vector3f& x);
8
+
include/eigen/test/bug1213_main.cpp ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ // This is a regression unit regarding a weird linking issue with gcc.
3
+
4
+ #include "bug1213.h"
5
+
6
+ int main()
7
+ {
8
+ return 0;
9
+ }
10
+
11
+
12
+ template<typename T, int dim>
13
+ bool bug1213_2(const Eigen::Matrix<T,dim,1>& )
14
+ {
15
+ return true;
16
+ }
17
+
18
+ template bool bug1213_2<float,3>(const Eigen::Vector3f&);
include/eigen/test/cholmod_support.cpp ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // This file is part of Eigen, a lightweight C++ template library
2
+ // for linear algebra.
3
+ //
4
+ // Copyright (C) 2011 Gael Guennebaud <g.gael@free.fr>
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
+ #define EIGEN_NO_DEBUG_SMALL_PRODUCT_BLOCKS
11
+ #include "sparse_solver.h"
12
+
13
+ #include <Eigen/CholmodSupport>
14
+
15
+ template<typename SparseType> void test_cholmod_ST()
16
+ {
17
+ CholmodDecomposition<SparseType, Lower> g_chol_colmajor_lower; g_chol_colmajor_lower.setMode(CholmodSupernodalLLt);
18
+ CholmodDecomposition<SparseType, Upper> g_chol_colmajor_upper; g_chol_colmajor_upper.setMode(CholmodSupernodalLLt);
19
+ CholmodDecomposition<SparseType, Lower> g_llt_colmajor_lower; g_llt_colmajor_lower.setMode(CholmodSimplicialLLt);
20
+ CholmodDecomposition<SparseType, Upper> g_llt_colmajor_upper; g_llt_colmajor_upper.setMode(CholmodSimplicialLLt);
21
+ CholmodDecomposition<SparseType, Lower> g_ldlt_colmajor_lower; g_ldlt_colmajor_lower.setMode(CholmodLDLt);
22
+ CholmodDecomposition<SparseType, Upper> g_ldlt_colmajor_upper; g_ldlt_colmajor_upper.setMode(CholmodLDLt);
23
+
24
+ CholmodSupernodalLLT<SparseType, Lower> chol_colmajor_lower;
25
+ CholmodSupernodalLLT<SparseType, Upper> chol_colmajor_upper;
26
+ CholmodSimplicialLLT<SparseType, Lower> llt_colmajor_lower;
27
+ CholmodSimplicialLLT<SparseType, Upper> llt_colmajor_upper;
28
+ CholmodSimplicialLDLT<SparseType, Lower> ldlt_colmajor_lower;
29
+ CholmodSimplicialLDLT<SparseType, Upper> ldlt_colmajor_upper;
30
+
31
+ check_sparse_spd_solving(g_chol_colmajor_lower);
32
+ check_sparse_spd_solving(g_chol_colmajor_upper);
33
+ check_sparse_spd_solving(g_llt_colmajor_lower);
34
+ check_sparse_spd_solving(g_llt_colmajor_upper);
35
+ check_sparse_spd_solving(g_ldlt_colmajor_lower);
36
+ check_sparse_spd_solving(g_ldlt_colmajor_upper);
37
+
38
+ check_sparse_spd_solving(chol_colmajor_lower);
39
+ check_sparse_spd_solving(chol_colmajor_upper);
40
+ check_sparse_spd_solving(llt_colmajor_lower);
41
+ check_sparse_spd_solving(llt_colmajor_upper);
42
+ check_sparse_spd_solving(ldlt_colmajor_lower);
43
+ check_sparse_spd_solving(ldlt_colmajor_upper);
44
+
45
+ check_sparse_spd_determinant(chol_colmajor_lower);
46
+ check_sparse_spd_determinant(chol_colmajor_upper);
47
+ check_sparse_spd_determinant(llt_colmajor_lower);
48
+ check_sparse_spd_determinant(llt_colmajor_upper);
49
+ check_sparse_spd_determinant(ldlt_colmajor_lower);
50
+ check_sparse_spd_determinant(ldlt_colmajor_upper);
51
+ }
52
+
53
+ template<typename T, int flags, typename IdxType> void test_cholmod_T()
54
+ {
55
+ test_cholmod_ST<SparseMatrix<T, flags, IdxType> >();
56
+ }
57
+
58
+ EIGEN_DECLARE_TEST(cholmod_support)
59
+ {
60
+ CALL_SUBTEST_11( (test_cholmod_T<double , ColMajor, int >()) );
61
+ CALL_SUBTEST_12( (test_cholmod_T<double , ColMajor, long>()) );
62
+ CALL_SUBTEST_13( (test_cholmod_T<double , RowMajor, int >()) );
63
+ CALL_SUBTEST_14( (test_cholmod_T<double , RowMajor, long>()) );
64
+ CALL_SUBTEST_21( (test_cholmod_T<std::complex<double>, ColMajor, int >()) );
65
+ CALL_SUBTEST_22( (test_cholmod_T<std::complex<double>, ColMajor, long>()) );
66
+ // TODO complex row-major matrices do not work at the moment:
67
+ // CALL_SUBTEST_23( (test_cholmod_T<std::complex<double>, RowMajor, int >()) );
68
+ // CALL_SUBTEST_24( (test_cholmod_T<std::complex<double>, RowMajor, long>()) );
69
+ }
include/eigen/test/conjugate_gradient.cpp ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // This file is part of Eigen, a lightweight C++ template library
2
+ // for linear algebra.
3
+ //
4
+ // Copyright (C) 2011 Gael Guennebaud <g.gael@free.fr>
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 "sparse_solver.h"
11
+ #include <Eigen/IterativeLinearSolvers>
12
+
13
+ template<typename T, typename I_> void test_conjugate_gradient_T()
14
+ {
15
+ typedef SparseMatrix<T,0,I_> SparseMatrixType;
16
+ ConjugateGradient<SparseMatrixType, Lower > cg_colmajor_lower_diag;
17
+ ConjugateGradient<SparseMatrixType, Upper > cg_colmajor_upper_diag;
18
+ ConjugateGradient<SparseMatrixType, Lower|Upper> cg_colmajor_loup_diag;
19
+ ConjugateGradient<SparseMatrixType, Lower, IdentityPreconditioner> cg_colmajor_lower_I;
20
+ ConjugateGradient<SparseMatrixType, Upper, IdentityPreconditioner> cg_colmajor_upper_I;
21
+
22
+ CALL_SUBTEST( check_sparse_spd_solving(cg_colmajor_lower_diag) );
23
+ CALL_SUBTEST( check_sparse_spd_solving(cg_colmajor_upper_diag) );
24
+ CALL_SUBTEST( check_sparse_spd_solving(cg_colmajor_loup_diag) );
25
+ CALL_SUBTEST( check_sparse_spd_solving(cg_colmajor_lower_I) );
26
+ CALL_SUBTEST( check_sparse_spd_solving(cg_colmajor_upper_I) );
27
+ }
28
+
29
+ EIGEN_DECLARE_TEST(conjugate_gradient)
30
+ {
31
+ CALL_SUBTEST_1(( test_conjugate_gradient_T<double,int>() ));
32
+ CALL_SUBTEST_2(( test_conjugate_gradient_T<std::complex<double>, int>() ));
33
+ CALL_SUBTEST_3(( test_conjugate_gradient_T<double,long int>() ));
34
+ }
include/eigen/test/conservative_resize.cpp ADDED
@@ -0,0 +1,167 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ #include "AnnoyingScalar.h"
14
+
15
+ using namespace Eigen;
16
+
17
+ template <typename Scalar, int Storage>
18
+ void run_matrix_tests()
19
+ {
20
+ typedef Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic, Storage> MatrixType;
21
+
22
+ MatrixType m, n;
23
+
24
+ // boundary cases ...
25
+ m = n = MatrixType::Random(50,50);
26
+ m.conservativeResize(1,50);
27
+ VERIFY_IS_APPROX(m, n.block(0,0,1,50));
28
+
29
+ m = n = MatrixType::Random(50,50);
30
+ m.conservativeResize(50,1);
31
+ VERIFY_IS_APPROX(m, n.block(0,0,50,1));
32
+
33
+ m = n = MatrixType::Random(50,50);
34
+ m.conservativeResize(50,50);
35
+ VERIFY_IS_APPROX(m, n.block(0,0,50,50));
36
+
37
+ // random shrinking ...
38
+ for (int i=0; i<25; ++i)
39
+ {
40
+ const Index rows = internal::random<Index>(1,50);
41
+ const Index cols = internal::random<Index>(1,50);
42
+ m = n = MatrixType::Random(50,50);
43
+ m.conservativeResize(rows,cols);
44
+ VERIFY_IS_APPROX(m, n.block(0,0,rows,cols));
45
+ }
46
+
47
+ // random growing with zeroing ...
48
+ for (int i=0; i<25; ++i)
49
+ {
50
+ const Index rows = internal::random<Index>(50,75);
51
+ const Index cols = internal::random<Index>(50,75);
52
+ m = n = MatrixType::Random(50,50);
53
+ m.conservativeResizeLike(MatrixType::Zero(rows,cols));
54
+ VERIFY_IS_APPROX(m.block(0,0,n.rows(),n.cols()), n);
55
+ VERIFY( rows<=50 || m.block(50,0,rows-50,cols).sum() == Scalar(0) );
56
+ VERIFY( cols<=50 || m.block(0,50,rows,cols-50).sum() == Scalar(0) );
57
+ }
58
+ }
59
+
60
+ template <typename Scalar>
61
+ void run_vector_tests()
62
+ {
63
+ typedef Matrix<Scalar, 1, Eigen::Dynamic> VectorType;
64
+
65
+ VectorType m, n;
66
+
67
+ // boundary cases ...
68
+ m = n = VectorType::Random(50);
69
+ m.conservativeResize(1);
70
+ VERIFY_IS_APPROX(m, n.segment(0,1));
71
+
72
+ m = n = VectorType::Random(50);
73
+ m.conservativeResize(50);
74
+ VERIFY_IS_APPROX(m, n.segment(0,50));
75
+
76
+ m = n = VectorType::Random(50);
77
+ m.conservativeResize(m.rows(),1);
78
+ VERIFY_IS_APPROX(m, n.segment(0,1));
79
+
80
+ m = n = VectorType::Random(50);
81
+ m.conservativeResize(m.rows(),50);
82
+ VERIFY_IS_APPROX(m, n.segment(0,50));
83
+
84
+ // random shrinking ...
85
+ for (int i=0; i<50; ++i)
86
+ {
87
+ const int size = internal::random<int>(1,50);
88
+ m = n = VectorType::Random(50);
89
+ m.conservativeResize(size);
90
+ VERIFY_IS_APPROX(m, n.segment(0,size));
91
+
92
+ m = n = VectorType::Random(50);
93
+ m.conservativeResize(m.rows(), size);
94
+ VERIFY_IS_APPROX(m, n.segment(0,size));
95
+ }
96
+
97
+ // random growing with zeroing ...
98
+ for (int i=0; i<50; ++i)
99
+ {
100
+ const int size = internal::random<int>(50,100);
101
+ m = n = VectorType::Random(50);
102
+ m.conservativeResizeLike(VectorType::Zero(size));
103
+ VERIFY_IS_APPROX(m.segment(0,50), n);
104
+ VERIFY( size<=50 || m.segment(50,size-50).sum() == Scalar(0) );
105
+
106
+ m = n = VectorType::Random(50);
107
+ m.conservativeResizeLike(Matrix<Scalar,Dynamic,Dynamic>::Zero(1,size));
108
+ VERIFY_IS_APPROX(m.segment(0,50), n);
109
+ VERIFY( size<=50 || m.segment(50,size-50).sum() == Scalar(0) );
110
+ }
111
+ }
112
+
113
+ // Basic memory leak check with a non-copyable scalar type
114
+ template<int> void noncopyable()
115
+ {
116
+ typedef Eigen::Matrix<AnnoyingScalar,Dynamic,1> VectorType;
117
+ typedef Eigen::Matrix<AnnoyingScalar,Dynamic,Dynamic> MatrixType;
118
+
119
+ {
120
+ #ifndef EIGEN_TEST_ANNOYING_SCALAR_DONT_THROW
121
+ AnnoyingScalar::dont_throw = true;
122
+ #endif
123
+ int n = 50;
124
+ VectorType v0(n), v1(n);
125
+ MatrixType m0(n,n), m1(n,n), m2(n,n);
126
+ v0.setOnes(); v1.setOnes();
127
+ m0.setOnes(); m1.setOnes(); m2.setOnes();
128
+ VERIFY(m0==m1);
129
+ m0.conservativeResize(2*n,2*n);
130
+ VERIFY(m0.topLeftCorner(n,n) == m1);
131
+
132
+ VERIFY(v0.head(n) == v1);
133
+ v0.conservativeResize(2*n);
134
+ VERIFY(v0.head(n) == v1);
135
+ }
136
+ VERIFY(AnnoyingScalar::instances==0 && "global memory leak detected in noncopyable");
137
+ }
138
+
139
+ EIGEN_DECLARE_TEST(conservative_resize)
140
+ {
141
+ for(int i=0; i<g_repeat; ++i)
142
+ {
143
+ CALL_SUBTEST_1((run_matrix_tests<int, Eigen::RowMajor>()));
144
+ CALL_SUBTEST_1((run_matrix_tests<int, Eigen::ColMajor>()));
145
+ CALL_SUBTEST_2((run_matrix_tests<float, Eigen::RowMajor>()));
146
+ CALL_SUBTEST_2((run_matrix_tests<float, Eigen::ColMajor>()));
147
+ CALL_SUBTEST_3((run_matrix_tests<double, Eigen::RowMajor>()));
148
+ CALL_SUBTEST_3((run_matrix_tests<double, Eigen::ColMajor>()));
149
+ CALL_SUBTEST_4((run_matrix_tests<std::complex<float>, Eigen::RowMajor>()));
150
+ CALL_SUBTEST_4((run_matrix_tests<std::complex<float>, Eigen::ColMajor>()));
151
+ CALL_SUBTEST_5((run_matrix_tests<std::complex<double>, Eigen::RowMajor>()));
152
+ CALL_SUBTEST_5((run_matrix_tests<std::complex<double>, Eigen::ColMajor>()));
153
+ CALL_SUBTEST_1((run_matrix_tests<int, Eigen::RowMajor | Eigen::DontAlign>()));
154
+
155
+ CALL_SUBTEST_1((run_vector_tests<int>()));
156
+ CALL_SUBTEST_2((run_vector_tests<float>()));
157
+ CALL_SUBTEST_3((run_vector_tests<double>()));
158
+ CALL_SUBTEST_4((run_vector_tests<std::complex<float> >()));
159
+ CALL_SUBTEST_5((run_vector_tests<std::complex<double> >()));
160
+
161
+ #ifndef EIGEN_TEST_ANNOYING_SCALAR_DONT_THROW
162
+ AnnoyingScalar::dont_throw = true;
163
+ #endif
164
+ CALL_SUBTEST_6(( run_vector_tests<AnnoyingScalar>() ));
165
+ CALL_SUBTEST_6(( noncopyable<0>() ));
166
+ }
167
+ }
include/eigen/test/constructor.cpp ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // This file is part of Eigen, a lightweight C++ template library
2
+ // for linear algebra.
3
+ //
4
+ // Copyright (C) 2017 Gael Guennebaud <gael.guennebaud@inria.fr>
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
+
11
+ #define TEST_ENABLE_TEMPORARY_TRACKING
12
+
13
+ #include "main.h"
14
+
15
+ template<typename MatrixType> struct Wrapper
16
+ {
17
+ MatrixType m_mat;
18
+ inline Wrapper(const MatrixType &x) : m_mat(x) {}
19
+ inline operator const MatrixType& () const { return m_mat; }
20
+ inline operator MatrixType& () { return m_mat; }
21
+ };
22
+
23
+ enum my_sizes { M = 12, N = 7};
24
+
25
+ template<typename MatrixType> void ctor_init1(const MatrixType& m)
26
+ {
27
+ // Check logic in PlainObjectBase::_init1
28
+ Index rows = m.rows();
29
+ Index cols = m.cols();
30
+
31
+ MatrixType m0 = MatrixType::Random(rows,cols);
32
+
33
+ VERIFY_EVALUATION_COUNT( MatrixType m1(m0), 1);
34
+ VERIFY_EVALUATION_COUNT( MatrixType m2(m0+m0), 1);
35
+ VERIFY_EVALUATION_COUNT( MatrixType m2(m0.block(0,0,rows,cols)) , 1);
36
+
37
+ Wrapper<MatrixType> wrapper(m0);
38
+ VERIFY_EVALUATION_COUNT( MatrixType m3(wrapper) , 1);
39
+ }
40
+
41
+
42
+ EIGEN_DECLARE_TEST(constructor)
43
+ {
44
+ for(int i = 0; i < g_repeat; i++) {
45
+ CALL_SUBTEST_1( ctor_init1(Matrix<float, 1, 1>()) );
46
+ CALL_SUBTEST_1( ctor_init1(Matrix4d()) );
47
+ CALL_SUBTEST_1( ctor_init1(MatrixXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
48
+ CALL_SUBTEST_1( ctor_init1(MatrixXi(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
49
+ }
50
+ {
51
+ Matrix<Index,1,1> a(123);
52
+ VERIFY_IS_EQUAL(a[0], 123);
53
+ }
54
+ {
55
+ Matrix<Index,1,1> a(123.0);
56
+ VERIFY_IS_EQUAL(a[0], 123);
57
+ }
58
+ {
59
+ Matrix<float,1,1> a(123);
60
+ VERIFY_IS_EQUAL(a[0], 123.f);
61
+ }
62
+ {
63
+ Array<Index,1,1> a(123);
64
+ VERIFY_IS_EQUAL(a[0], 123);
65
+ }
66
+ {
67
+ Array<Index,1,1> a(123.0);
68
+ VERIFY_IS_EQUAL(a[0], 123);
69
+ }
70
+ {
71
+ Array<float,1,1> a(123);
72
+ VERIFY_IS_EQUAL(a[0], 123.f);
73
+ }
74
+ {
75
+ Array<Index,3,3> a(123);
76
+ VERIFY_IS_EQUAL(a(4), 123);
77
+ }
78
+ {
79
+ Array<Index,3,3> a(123.0);
80
+ VERIFY_IS_EQUAL(a(4), 123);
81
+ }
82
+ {
83
+ Array<float,3,3> a(123);
84
+ VERIFY_IS_EQUAL(a(4), 123.f);
85
+ }
86
+ {
87
+ MatrixXi m1(M,N);
88
+ VERIFY_IS_EQUAL(m1.rows(),M);
89
+ VERIFY_IS_EQUAL(m1.cols(),N);
90
+ ArrayXXi a1(M,N);
91
+ VERIFY_IS_EQUAL(a1.rows(),M);
92
+ VERIFY_IS_EQUAL(a1.cols(),N);
93
+ VectorXi v1(M);
94
+ VERIFY_IS_EQUAL(v1.size(),M);
95
+ ArrayXi a2(M);
96
+ VERIFY_IS_EQUAL(a2.size(),M);
97
+ }
98
+ }
include/eigen/test/corners.cpp ADDED
@@ -0,0 +1,117 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // This file is part of Eigen, a lightweight C++ template library
2
+ // for linear algebra.
3
+ //
4
+ // Copyright (C) 2006-2010 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
+ #define COMPARE_CORNER(A,B) \
13
+ VERIFY_IS_EQUAL(matrix.A, matrix.B); \
14
+ VERIFY_IS_EQUAL(const_matrix.A, const_matrix.B);
15
+
16
+ template<typename MatrixType> void corners(const MatrixType& m)
17
+ {
18
+ Index rows = m.rows();
19
+ Index cols = m.cols();
20
+
21
+ Index r = internal::random<Index>(1,rows);
22
+ Index c = internal::random<Index>(1,cols);
23
+
24
+ MatrixType matrix = MatrixType::Random(rows,cols);
25
+ const MatrixType const_matrix = MatrixType::Random(rows,cols);
26
+
27
+ COMPARE_CORNER(topLeftCorner(r,c), block(0,0,r,c));
28
+ COMPARE_CORNER(topRightCorner(r,c), block(0,cols-c,r,c));
29
+ COMPARE_CORNER(bottomLeftCorner(r,c), block(rows-r,0,r,c));
30
+ COMPARE_CORNER(bottomRightCorner(r,c), block(rows-r,cols-c,r,c));
31
+
32
+ Index sr = internal::random<Index>(1,rows) - 1;
33
+ Index nr = internal::random<Index>(1,rows-sr);
34
+ Index sc = internal::random<Index>(1,cols) - 1;
35
+ Index nc = internal::random<Index>(1,cols-sc);
36
+
37
+ COMPARE_CORNER(topRows(r), block(0,0,r,cols));
38
+ COMPARE_CORNER(middleRows(sr,nr), block(sr,0,nr,cols));
39
+ COMPARE_CORNER(bottomRows(r), block(rows-r,0,r,cols));
40
+ COMPARE_CORNER(leftCols(c), block(0,0,rows,c));
41
+ COMPARE_CORNER(middleCols(sc,nc), block(0,sc,rows,nc));
42
+ COMPARE_CORNER(rightCols(c), block(0,cols-c,rows,c));
43
+ }
44
+
45
+ template<typename MatrixType, int CRows, int CCols, int SRows, int SCols> void corners_fixedsize()
46
+ {
47
+ MatrixType matrix = MatrixType::Random();
48
+ const MatrixType const_matrix = MatrixType::Random();
49
+
50
+ enum {
51
+ rows = MatrixType::RowsAtCompileTime,
52
+ cols = MatrixType::ColsAtCompileTime,
53
+ r = CRows,
54
+ c = CCols,
55
+ sr = SRows,
56
+ sc = SCols
57
+ };
58
+
59
+ VERIFY_IS_EQUAL((matrix.template topLeftCorner<r,c>()), (matrix.template block<r,c>(0,0)));
60
+ VERIFY_IS_EQUAL((matrix.template topRightCorner<r,c>()), (matrix.template block<r,c>(0,cols-c)));
61
+ VERIFY_IS_EQUAL((matrix.template bottomLeftCorner<r,c>()), (matrix.template block<r,c>(rows-r,0)));
62
+ VERIFY_IS_EQUAL((matrix.template bottomRightCorner<r,c>()), (matrix.template block<r,c>(rows-r,cols-c)));
63
+
64
+ VERIFY_IS_EQUAL((matrix.template topLeftCorner<r,c>()), (matrix.template topLeftCorner<r,Dynamic>(r,c)));
65
+ VERIFY_IS_EQUAL((matrix.template topRightCorner<r,c>()), (matrix.template topRightCorner<r,Dynamic>(r,c)));
66
+ VERIFY_IS_EQUAL((matrix.template bottomLeftCorner<r,c>()), (matrix.template bottomLeftCorner<r,Dynamic>(r,c)));
67
+ VERIFY_IS_EQUAL((matrix.template bottomRightCorner<r,c>()), (matrix.template bottomRightCorner<r,Dynamic>(r,c)));
68
+
69
+ VERIFY_IS_EQUAL((matrix.template topLeftCorner<r,c>()), (matrix.template topLeftCorner<Dynamic,c>(r,c)));
70
+ VERIFY_IS_EQUAL((matrix.template topRightCorner<r,c>()), (matrix.template topRightCorner<Dynamic,c>(r,c)));
71
+ VERIFY_IS_EQUAL((matrix.template bottomLeftCorner<r,c>()), (matrix.template bottomLeftCorner<Dynamic,c>(r,c)));
72
+ VERIFY_IS_EQUAL((matrix.template bottomRightCorner<r,c>()), (matrix.template bottomRightCorner<Dynamic,c>(r,c)));
73
+
74
+ VERIFY_IS_EQUAL((matrix.template topRows<r>()), (matrix.template block<r,cols>(0,0)));
75
+ VERIFY_IS_EQUAL((matrix.template middleRows<r>(sr)), (matrix.template block<r,cols>(sr,0)));
76
+ VERIFY_IS_EQUAL((matrix.template bottomRows<r>()), (matrix.template block<r,cols>(rows-r,0)));
77
+ VERIFY_IS_EQUAL((matrix.template leftCols<c>()), (matrix.template block<rows,c>(0,0)));
78
+ VERIFY_IS_EQUAL((matrix.template middleCols<c>(sc)), (matrix.template block<rows,c>(0,sc)));
79
+ VERIFY_IS_EQUAL((matrix.template rightCols<c>()), (matrix.template block<rows,c>(0,cols-c)));
80
+
81
+ VERIFY_IS_EQUAL((const_matrix.template topLeftCorner<r,c>()), (const_matrix.template block<r,c>(0,0)));
82
+ VERIFY_IS_EQUAL((const_matrix.template topRightCorner<r,c>()), (const_matrix.template block<r,c>(0,cols-c)));
83
+ VERIFY_IS_EQUAL((const_matrix.template bottomLeftCorner<r,c>()), (const_matrix.template block<r,c>(rows-r,0)));
84
+ VERIFY_IS_EQUAL((const_matrix.template bottomRightCorner<r,c>()), (const_matrix.template block<r,c>(rows-r,cols-c)));
85
+
86
+ VERIFY_IS_EQUAL((const_matrix.template topLeftCorner<r,c>()), (const_matrix.template topLeftCorner<r,Dynamic>(r,c)));
87
+ VERIFY_IS_EQUAL((const_matrix.template topRightCorner<r,c>()), (const_matrix.template topRightCorner<r,Dynamic>(r,c)));
88
+ VERIFY_IS_EQUAL((const_matrix.template bottomLeftCorner<r,c>()), (const_matrix.template bottomLeftCorner<r,Dynamic>(r,c)));
89
+ VERIFY_IS_EQUAL((const_matrix.template bottomRightCorner<r,c>()), (const_matrix.template bottomRightCorner<r,Dynamic>(r,c)));
90
+
91
+ VERIFY_IS_EQUAL((const_matrix.template topLeftCorner<r,c>()), (const_matrix.template topLeftCorner<Dynamic,c>(r,c)));
92
+ VERIFY_IS_EQUAL((const_matrix.template topRightCorner<r,c>()), (const_matrix.template topRightCorner<Dynamic,c>(r,c)));
93
+ VERIFY_IS_EQUAL((const_matrix.template bottomLeftCorner<r,c>()), (const_matrix.template bottomLeftCorner<Dynamic,c>(r,c)));
94
+ VERIFY_IS_EQUAL((const_matrix.template bottomRightCorner<r,c>()), (const_matrix.template bottomRightCorner<Dynamic,c>(r,c)));
95
+
96
+ VERIFY_IS_EQUAL((const_matrix.template topRows<r>()), (const_matrix.template block<r,cols>(0,0)));
97
+ VERIFY_IS_EQUAL((const_matrix.template middleRows<r>(sr)), (const_matrix.template block<r,cols>(sr,0)));
98
+ VERIFY_IS_EQUAL((const_matrix.template bottomRows<r>()), (const_matrix.template block<r,cols>(rows-r,0)));
99
+ VERIFY_IS_EQUAL((const_matrix.template leftCols<c>()), (const_matrix.template block<rows,c>(0,0)));
100
+ VERIFY_IS_EQUAL((const_matrix.template middleCols<c>(sc)), (const_matrix.template block<rows,c>(0,sc)));
101
+ VERIFY_IS_EQUAL((const_matrix.template rightCols<c>()), (const_matrix.template block<rows,c>(0,cols-c)));
102
+ }
103
+
104
+ EIGEN_DECLARE_TEST(corners)
105
+ {
106
+ for(int i = 0; i < g_repeat; i++) {
107
+ CALL_SUBTEST_1( corners(Matrix<float, 1, 1>()) );
108
+ CALL_SUBTEST_2( corners(Matrix4d()) );
109
+ CALL_SUBTEST_3( corners(Matrix<int,10,12>()) );
110
+ CALL_SUBTEST_4( corners(MatrixXcf(5, 7)) );
111
+ CALL_SUBTEST_5( corners(MatrixXf(21, 20)) );
112
+
113
+ CALL_SUBTEST_1(( corners_fixedsize<Matrix<float, 1, 1>, 1, 1, 0, 0>() ));
114
+ CALL_SUBTEST_2(( corners_fixedsize<Matrix4d,2,2,1,1>() ));
115
+ CALL_SUBTEST_3(( corners_fixedsize<Matrix<int,10,12>,4,7,5,2>() ));
116
+ }
117
+ }
include/eigen/test/ctorleak.cpp ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include "main.h"
2
+
3
+ #include <exception> // std::exception
4
+
5
+ struct Foo
6
+ {
7
+ static Index object_count;
8
+ static Index object_limit;
9
+ int dummy;
10
+
11
+ Foo() : dummy(0)
12
+ {
13
+ #ifdef EIGEN_EXCEPTIONS
14
+ // TODO: Is this the correct way to handle this?
15
+ if (Foo::object_count > Foo::object_limit) { std::cout << "\nThrow!\n"; throw Foo::Fail(); }
16
+ #endif
17
+ std::cout << '+';
18
+ ++Foo::object_count;
19
+ }
20
+
21
+ ~Foo()
22
+ {
23
+ std::cout << '-';
24
+ --Foo::object_count;
25
+ }
26
+
27
+ class Fail : public std::exception {};
28
+ };
29
+
30
+ Index Foo::object_count = 0;
31
+ Index Foo::object_limit = 0;
32
+
33
+ #undef EIGEN_TEST_MAX_SIZE
34
+ #define EIGEN_TEST_MAX_SIZE 3
35
+
36
+ EIGEN_DECLARE_TEST(ctorleak)
37
+ {
38
+ typedef Matrix<Foo, Dynamic, Dynamic> MatrixX;
39
+ typedef Matrix<Foo, Dynamic, 1> VectorX;
40
+
41
+ Foo::object_count = 0;
42
+ for(int i = 0; i < g_repeat; i++) {
43
+ Index rows = internal::random<Index>(2,EIGEN_TEST_MAX_SIZE), cols = internal::random<Index>(2,EIGEN_TEST_MAX_SIZE);
44
+ Foo::object_limit = rows*cols;
45
+ {
46
+ MatrixX r(rows, cols);
47
+ Foo::object_limit = r.size()+internal::random<Index>(0, rows*cols - 2);
48
+ std::cout << "object_limit =" << Foo::object_limit << std::endl;
49
+ #ifdef EIGEN_EXCEPTIONS
50
+ try
51
+ {
52
+ #endif
53
+ if(internal::random<bool>()) {
54
+ std::cout << "\nMatrixX m(" << rows << ", " << cols << ");\n";
55
+ MatrixX m(rows, cols);
56
+ }
57
+ else {
58
+ std::cout << "\nMatrixX m(r);\n";
59
+ MatrixX m(r);
60
+ }
61
+ #ifdef EIGEN_EXCEPTIONS
62
+ VERIFY(false); // not reached if exceptions are enabled
63
+ }
64
+ catch (const Foo::Fail&) { /* ignore */ }
65
+ #endif
66
+ }
67
+ VERIFY_IS_EQUAL(Index(0), Foo::object_count);
68
+
69
+ {
70
+ Foo::object_limit = (rows+1)*(cols+1);
71
+ MatrixX A(rows, cols);
72
+ VERIFY_IS_EQUAL(Foo::object_count, rows*cols);
73
+ VectorX v=A.row(0);
74
+ VERIFY_IS_EQUAL(Foo::object_count, (rows+1)*cols);
75
+ v = A.col(0);
76
+ VERIFY_IS_EQUAL(Foo::object_count, rows*(cols+1));
77
+ }
78
+ VERIFY_IS_EQUAL(Index(0), Foo::object_count);
79
+ }
80
+ std::cout << "\n";
81
+ }
include/eigen/test/dense_storage.cpp ADDED
@@ -0,0 +1,190 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // This file is part of Eigen, a lightweight C++ template library
2
+ // for linear algebra.
3
+ //
4
+ // Copyright (C) 2013 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
+ #include "AnnoyingScalar.h"
12
+ #include "SafeScalar.h"
13
+
14
+ #include <Eigen/Core>
15
+
16
+ #if EIGEN_HAS_TYPE_TRAITS && EIGEN_HAS_CXX11
17
+ using DenseStorageD3x3 = Eigen::DenseStorage<double, 3, 3, 3, 3>;
18
+ static_assert(std::is_trivially_move_constructible<DenseStorageD3x3>::value, "DenseStorage not trivially_move_constructible");
19
+ static_assert(std::is_trivially_move_assignable<DenseStorageD3x3>::value, "DenseStorage not trivially_move_assignable");
20
+ #if !defined(EIGEN_DENSE_STORAGE_CTOR_PLUGIN)
21
+ static_assert(std::is_trivially_copy_constructible<DenseStorageD3x3>::value, "DenseStorage not trivially_copy_constructible");
22
+ static_assert(std::is_trivially_copy_assignable<DenseStorageD3x3>::value, "DenseStorage not trivially_copy_assignable");
23
+ static_assert(std::is_trivially_copyable<DenseStorageD3x3>::value, "DenseStorage not trivially_copyable");
24
+ #endif
25
+ #endif
26
+
27
+ template <typename T, int Size, int Rows, int Cols>
28
+ void dense_storage_copy(int rows, int cols)
29
+ {
30
+ typedef DenseStorage<T, Size, Rows, Cols, 0> DenseStorageType;
31
+
32
+ const int size = rows*cols;
33
+ DenseStorageType reference(size, rows, cols);
34
+ T* raw_reference = reference.data();
35
+ for (int i=0; i<size; ++i)
36
+ raw_reference[i] = static_cast<T>(i);
37
+
38
+ DenseStorageType copied_reference(reference);
39
+ const T* raw_copied_reference = copied_reference.data();
40
+ for (int i=0; i<size; ++i)
41
+ VERIFY_IS_EQUAL(raw_reference[i], raw_copied_reference[i]);
42
+ }
43
+
44
+ template <typename T, int Size, int Rows, int Cols>
45
+ void dense_storage_assignment(int rows, int cols)
46
+ {
47
+ typedef DenseStorage<T, Size, Rows, Cols, 0> DenseStorageType;
48
+
49
+ const int size = rows*cols;
50
+ DenseStorageType reference(size, rows, cols);
51
+ T* raw_reference = reference.data();
52
+ for (int i=0; i<size; ++i)
53
+ raw_reference[i] = static_cast<T>(i);
54
+
55
+ DenseStorageType copied_reference;
56
+ copied_reference = reference;
57
+ const T* raw_copied_reference = copied_reference.data();
58
+ for (int i=0; i<size; ++i)
59
+ VERIFY_IS_EQUAL(raw_reference[i], raw_copied_reference[i]);
60
+ }
61
+
62
+ template <typename T, int Size, int Rows, int Cols>
63
+ void dense_storage_swap(int rows0, int cols0, int rows1, int cols1)
64
+ {
65
+ typedef DenseStorage<T, Size, Rows, Cols, 0> DenseStorageType;
66
+
67
+ const int size0 = rows0*cols0;
68
+ DenseStorageType a(size0, rows0, cols0);
69
+ for (int i=0; i<size0; ++i) {
70
+ a.data()[i] = static_cast<T>(i);
71
+ }
72
+
73
+ const int size1 = rows1*cols1;
74
+ DenseStorageType b(size1, rows1, cols1);
75
+ for (int i=0; i<size1; ++i) {
76
+ b.data()[i] = static_cast<T>(-i);
77
+ }
78
+
79
+ a.swap(b);
80
+
81
+ for (int i=0; i<size0; ++i) {
82
+ VERIFY_IS_EQUAL(b.data()[i], static_cast<T>(i));
83
+ }
84
+
85
+ for (int i=0; i<size1; ++i) {
86
+ VERIFY_IS_EQUAL(a.data()[i], static_cast<T>(-i));
87
+ }
88
+ }
89
+
90
+ template<typename T, int Size, std::size_t Alignment>
91
+ void dense_storage_alignment()
92
+ {
93
+ #if EIGEN_HAS_ALIGNAS
94
+
95
+ struct alignas(Alignment) Empty1 {};
96
+ VERIFY_IS_EQUAL(std::alignment_of<Empty1>::value, Alignment);
97
+
98
+ struct EIGEN_ALIGN_TO_BOUNDARY(Alignment) Empty2 {};
99
+ VERIFY_IS_EQUAL(std::alignment_of<Empty2>::value, Alignment);
100
+
101
+ struct Nested1 { EIGEN_ALIGN_TO_BOUNDARY(Alignment) T data[Size]; };
102
+ VERIFY_IS_EQUAL(std::alignment_of<Nested1>::value, Alignment);
103
+
104
+ VERIFY_IS_EQUAL( (std::alignment_of<internal::plain_array<T,Size,AutoAlign,Alignment> >::value), Alignment);
105
+
106
+ const std::size_t default_alignment = internal::compute_default_alignment<T,Size>::value;
107
+
108
+ VERIFY_IS_EQUAL( (std::alignment_of<DenseStorage<T,Size,1,1,AutoAlign> >::value), default_alignment);
109
+ VERIFY_IS_EQUAL( (std::alignment_of<Matrix<T,Size,1,AutoAlign> >::value), default_alignment);
110
+ struct Nested2 { Matrix<T,Size,1,AutoAlign> mat; };
111
+ VERIFY_IS_EQUAL(std::alignment_of<Nested2>::value, default_alignment);
112
+
113
+ #endif
114
+ }
115
+
116
+ template<typename T>
117
+ void dense_storage_tests() {
118
+ // Dynamic Storage.
119
+ dense_storage_copy<T,Dynamic,Dynamic,Dynamic>(4, 3);
120
+ dense_storage_copy<T,Dynamic,Dynamic,3>(4, 3);
121
+ dense_storage_copy<T,Dynamic,4,Dynamic>(4, 3);
122
+ // Fixed Storage.
123
+ dense_storage_copy<T,12,4,3>(4, 3);
124
+ dense_storage_copy<T,12,Dynamic,Dynamic>(4, 3);
125
+ dense_storage_copy<T,12,4,Dynamic>(4, 3);
126
+ dense_storage_copy<T,12,Dynamic,3>(4, 3);
127
+ // Fixed Storage with Uninitialized Elements.
128
+ dense_storage_copy<T,18,Dynamic,Dynamic>(4, 3);
129
+ dense_storage_copy<T,18,4,Dynamic>(4, 3);
130
+ dense_storage_copy<T,18,Dynamic,3>(4, 3);
131
+
132
+ // Dynamic Storage.
133
+ dense_storage_assignment<T,Dynamic,Dynamic,Dynamic>(4, 3);
134
+ dense_storage_assignment<T,Dynamic,Dynamic,3>(4, 3);
135
+ dense_storage_assignment<T,Dynamic,4,Dynamic>(4, 3);
136
+ // Fixed Storage.
137
+ dense_storage_assignment<T,12,4,3>(4, 3);
138
+ dense_storage_assignment<T,12,Dynamic,Dynamic>(4, 3);
139
+ dense_storage_assignment<T,12,4,Dynamic>(4, 3);
140
+ dense_storage_assignment<T,12,Dynamic,3>(4, 3);
141
+ // Fixed Storage with Uninitialized Elements.
142
+ dense_storage_assignment<T,18,Dynamic,Dynamic>(4, 3);
143
+ dense_storage_assignment<T,18,4,Dynamic>(4, 3);
144
+ dense_storage_assignment<T,18,Dynamic,3>(4, 3);
145
+
146
+ // Dynamic Storage.
147
+ dense_storage_swap<T,Dynamic,Dynamic,Dynamic>(4, 3, 4, 3);
148
+ dense_storage_swap<T,Dynamic,Dynamic,Dynamic>(4, 3, 2, 1);
149
+ dense_storage_swap<T,Dynamic,Dynamic,Dynamic>(2, 1, 4, 3);
150
+ dense_storage_swap<T,Dynamic,Dynamic,3>(4, 3, 4, 3);
151
+ dense_storage_swap<T,Dynamic,Dynamic,3>(4, 3, 2, 3);
152
+ dense_storage_swap<T,Dynamic,Dynamic,3>(2, 3, 4, 3);
153
+ dense_storage_swap<T,Dynamic,4,Dynamic>(4, 3, 4, 3);
154
+ dense_storage_swap<T,Dynamic,4,Dynamic>(4, 3, 4, 1);
155
+ dense_storage_swap<T,Dynamic,4,Dynamic>(4, 1, 4, 3);
156
+ // Fixed Storage.
157
+ dense_storage_swap<T,12,4,3>(4, 3, 4, 3);
158
+ dense_storage_swap<T,12,Dynamic,Dynamic>(4, 3, 4, 3);
159
+ dense_storage_swap<T,12,Dynamic,Dynamic>(4, 3, 2, 1);
160
+ dense_storage_swap<T,12,Dynamic,Dynamic>(2, 1, 4, 3);
161
+ dense_storage_swap<T,12,4,Dynamic>(4, 3, 4, 3);
162
+ dense_storage_swap<T,12,4,Dynamic>(4, 3, 4, 1);
163
+ dense_storage_swap<T,12,4,Dynamic>(4, 1, 4, 3);
164
+ dense_storage_swap<T,12,Dynamic,3>(4, 3, 4, 3);
165
+ dense_storage_swap<T,12,Dynamic,3>(4, 3, 2, 3);
166
+ dense_storage_swap<T,12,Dynamic,3>(2, 3, 4, 3);
167
+ // Fixed Storage with Uninitialized Elements.
168
+ dense_storage_swap<T,18,Dynamic,Dynamic>(4, 3, 4, 3);
169
+ dense_storage_swap<T,18,Dynamic,Dynamic>(4, 3, 2, 1);
170
+ dense_storage_swap<T,18,Dynamic,Dynamic>(2, 1, 4, 3);
171
+ dense_storage_swap<T,18,4,Dynamic>(4, 3, 4, 3);
172
+ dense_storage_swap<T,18,4,Dynamic>(4, 3, 4, 1);
173
+ dense_storage_swap<T,18,4,Dynamic>(4, 1, 4, 3);
174
+ dense_storage_swap<T,18,Dynamic,3>(4, 3, 4, 3);
175
+ dense_storage_swap<T,18,Dynamic,3>(4, 3, 2, 3);
176
+ dense_storage_swap<T,18,Dynamic,3>(2, 3, 4, 3);
177
+
178
+ dense_storage_alignment<T,16,8>();
179
+ dense_storage_alignment<T,16,16>();
180
+ dense_storage_alignment<T,16,32>();
181
+ dense_storage_alignment<T,16,64>();
182
+ }
183
+
184
+ EIGEN_DECLARE_TEST(dense_storage)
185
+ {
186
+ dense_storage_tests<int>();
187
+ dense_storage_tests<float>();
188
+ dense_storage_tests<SafeScalar<float> >();
189
+ dense_storage_tests<AnnoyingScalar>();
190
+ }
include/eigen/test/determinant.cpp ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // This file is part of Eigen, a lightweight C++ template library
2
+ // for linear algebra.
3
+ //
4
+ // Copyright (C) 2008 Benoit Jacob <jacob.benoit.1@gmail.com>
5
+ // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
6
+ //
7
+ // This Source Code Form is subject to the terms of the Mozilla
8
+ // Public License v. 2.0. If a copy of the MPL was not distributed
9
+ // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10
+
11
+ #include "main.h"
12
+ #include <Eigen/LU>
13
+
14
+ template<typename MatrixType> void determinant(const MatrixType& m)
15
+ {
16
+ /* this test covers the following files:
17
+ Determinant.h
18
+ */
19
+ Index size = m.rows();
20
+
21
+ MatrixType m1(size, size), m2(size, size);
22
+ m1.setRandom();
23
+ m2.setRandom();
24
+ typedef typename MatrixType::Scalar Scalar;
25
+ Scalar x = internal::random<Scalar>();
26
+ VERIFY_IS_APPROX(MatrixType::Identity(size, size).determinant(), Scalar(1));
27
+ VERIFY_IS_APPROX((m1*m2).eval().determinant(), m1.determinant() * m2.determinant());
28
+ if(size==1) return;
29
+ Index i = internal::random<Index>(0, size-1);
30
+ Index j;
31
+ do {
32
+ j = internal::random<Index>(0, size-1);
33
+ } while(j==i);
34
+ m2 = m1;
35
+ m2.row(i).swap(m2.row(j));
36
+ VERIFY_IS_APPROX(m2.determinant(), -m1.determinant());
37
+ m2 = m1;
38
+ m2.col(i).swap(m2.col(j));
39
+ VERIFY_IS_APPROX(m2.determinant(), -m1.determinant());
40
+ VERIFY_IS_APPROX(m2.determinant(), m2.transpose().determinant());
41
+ VERIFY_IS_APPROX(numext::conj(m2.determinant()), m2.adjoint().determinant());
42
+ m2 = m1;
43
+ m2.row(i) += x*m2.row(j);
44
+ VERIFY_IS_APPROX(m2.determinant(), m1.determinant());
45
+ m2 = m1;
46
+ m2.row(i) *= x;
47
+ VERIFY_IS_APPROX(m2.determinant(), m1.determinant() * x);
48
+
49
+ // check empty matrix
50
+ VERIFY_IS_APPROX(m2.block(0,0,0,0).determinant(), Scalar(1));
51
+ }
52
+
53
+ EIGEN_DECLARE_TEST(determinant)
54
+ {
55
+ for(int i = 0; i < g_repeat; i++) {
56
+ int s = 0;
57
+ CALL_SUBTEST_1( determinant(Matrix<float, 1, 1>()) );
58
+ CALL_SUBTEST_2( determinant(Matrix<double, 2, 2>()) );
59
+ CALL_SUBTEST_3( determinant(Matrix<double, 3, 3>()) );
60
+ CALL_SUBTEST_4( determinant(Matrix<double, 4, 4>()) );
61
+ CALL_SUBTEST_5( determinant(Matrix<std::complex<double>, 10, 10>()) );
62
+ s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE/4);
63
+ CALL_SUBTEST_6( determinant(MatrixXd(s, s)) );
64
+ TEST_SET_BUT_UNUSED_VARIABLE(s)
65
+ }
66
+ }
include/eigen/test/dontalign.cpp ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ #if defined EIGEN_TEST_PART_1 || defined EIGEN_TEST_PART_2 || defined EIGEN_TEST_PART_3 || defined EIGEN_TEST_PART_4
11
+ #define EIGEN_DONT_ALIGN
12
+ #elif defined EIGEN_TEST_PART_5 || defined EIGEN_TEST_PART_6 || defined EIGEN_TEST_PART_7 || defined EIGEN_TEST_PART_8
13
+ #define EIGEN_DONT_ALIGN_STATICALLY
14
+ #endif
15
+
16
+ #include "main.h"
17
+ #include <Eigen/Dense>
18
+
19
+ template<typename MatrixType>
20
+ void dontalign(const MatrixType& m)
21
+ {
22
+ typedef typename MatrixType::Scalar Scalar;
23
+ typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
24
+ typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime> SquareMatrixType;
25
+
26
+ Index rows = m.rows();
27
+ Index cols = m.cols();
28
+
29
+ MatrixType a = MatrixType::Random(rows,cols);
30
+ SquareMatrixType square = SquareMatrixType::Random(rows,rows);
31
+ VectorType v = VectorType::Random(rows);
32
+
33
+ VERIFY_IS_APPROX(v, square * square.colPivHouseholderQr().solve(v));
34
+ square = square.inverse().eval();
35
+ a = square * a;
36
+ square = square*square;
37
+ v = square * v;
38
+ v = a.adjoint() * v;
39
+ VERIFY(square.determinant() != Scalar(0));
40
+
41
+ // bug 219: MapAligned() was giving an assert with EIGEN_DONT_ALIGN, because Map Flags were miscomputed
42
+ Scalar* array = internal::aligned_new<Scalar>(rows);
43
+ v = VectorType::MapAligned(array, rows);
44
+ internal::aligned_delete(array, rows);
45
+ }
46
+
47
+ EIGEN_DECLARE_TEST(dontalign)
48
+ {
49
+ #if defined EIGEN_TEST_PART_1 || defined EIGEN_TEST_PART_5
50
+ dontalign(Matrix3d());
51
+ dontalign(Matrix4f());
52
+ #elif defined EIGEN_TEST_PART_2 || defined EIGEN_TEST_PART_6
53
+ dontalign(Matrix3cd());
54
+ dontalign(Matrix4cf());
55
+ #elif defined EIGEN_TEST_PART_3 || defined EIGEN_TEST_PART_7
56
+ dontalign(Matrix<float, 32, 32>());
57
+ dontalign(Matrix<std::complex<float>, 32, 32>());
58
+ #elif defined EIGEN_TEST_PART_4 || defined EIGEN_TEST_PART_8
59
+ dontalign(MatrixXd(32, 32));
60
+ dontalign(MatrixXcf(32, 32));
61
+ #endif
62
+ }
include/eigen/test/dynalloc.cpp ADDED
@@ -0,0 +1,177 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // This file is part of Eigen, a lightweight C++ template library
2
+ // for linear algebra.
3
+ //
4
+ // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
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
+ #if EIGEN_MAX_ALIGN_BYTES>0
13
+ #define ALIGNMENT EIGEN_MAX_ALIGN_BYTES
14
+ #else
15
+ #define ALIGNMENT 1
16
+ #endif
17
+
18
+ typedef Matrix<float,16,1> Vector16f;
19
+ typedef Matrix<float,8,1> Vector8f;
20
+
21
+ void check_handmade_aligned_malloc()
22
+ {
23
+ for(int i = 1; i < 1000; i++)
24
+ {
25
+ char *p = (char*)internal::handmade_aligned_malloc(i);
26
+ VERIFY(internal::UIntPtr(p)%ALIGNMENT==0);
27
+ // if the buffer is wrongly allocated this will give a bad write --> check with valgrind
28
+ for(int j = 0; j < i; j++) p[j]=0;
29
+ internal::handmade_aligned_free(p);
30
+ }
31
+ }
32
+
33
+ void check_aligned_malloc()
34
+ {
35
+ for(int i = ALIGNMENT; i < 1000; i++)
36
+ {
37
+ char *p = (char*)internal::aligned_malloc(i);
38
+ VERIFY(internal::UIntPtr(p)%ALIGNMENT==0);
39
+ // if the buffer is wrongly allocated this will give a bad write --> check with valgrind
40
+ for(int j = 0; j < i; j++) p[j]=0;
41
+ internal::aligned_free(p);
42
+ }
43
+ }
44
+
45
+ void check_aligned_new()
46
+ {
47
+ for(int i = ALIGNMENT; i < 1000; i++)
48
+ {
49
+ float *p = internal::aligned_new<float>(i);
50
+ VERIFY(internal::UIntPtr(p)%ALIGNMENT==0);
51
+ // if the buffer is wrongly allocated this will give a bad write --> check with valgrind
52
+ for(int j = 0; j < i; j++) p[j]=0;
53
+ internal::aligned_delete(p,i);
54
+ }
55
+ }
56
+
57
+ void check_aligned_stack_alloc()
58
+ {
59
+ for(int i = ALIGNMENT; i < 400; i++)
60
+ {
61
+ ei_declare_aligned_stack_constructed_variable(float,p,i,0);
62
+ VERIFY(internal::UIntPtr(p)%ALIGNMENT==0);
63
+ // if the buffer is wrongly allocated this will give a bad write --> check with valgrind
64
+ for(int j = 0; j < i; j++) p[j]=0;
65
+ }
66
+ }
67
+
68
+
69
+ // test compilation with both a struct and a class...
70
+ struct MyStruct
71
+ {
72
+ EIGEN_MAKE_ALIGNED_OPERATOR_NEW
73
+ char dummychar;
74
+ Vector16f avec;
75
+ };
76
+
77
+ class MyClassA
78
+ {
79
+ public:
80
+ EIGEN_MAKE_ALIGNED_OPERATOR_NEW
81
+ char dummychar;
82
+ Vector16f avec;
83
+ };
84
+
85
+ template<typename T> void check_dynaligned()
86
+ {
87
+ // TODO have to be updated once we support multiple alignment values
88
+ if(T::SizeAtCompileTime % ALIGNMENT == 0)
89
+ {
90
+ T* obj = new T;
91
+ VERIFY(T::NeedsToAlign==1);
92
+ VERIFY(internal::UIntPtr(obj)%ALIGNMENT==0);
93
+ delete obj;
94
+ }
95
+ }
96
+
97
+ template<typename T> void check_custom_new_delete()
98
+ {
99
+ {
100
+ T* t = new T;
101
+ delete t;
102
+ }
103
+
104
+ {
105
+ std::size_t N = internal::random<std::size_t>(1,10);
106
+ T* t = new T[N];
107
+ delete[] t;
108
+ }
109
+
110
+ #if EIGEN_MAX_ALIGN_BYTES>0 && (!EIGEN_HAS_CXX17_OVERALIGN)
111
+ {
112
+ T* t = static_cast<T *>((T::operator new)(sizeof(T)));
113
+ (T::operator delete)(t, sizeof(T));
114
+ }
115
+
116
+ {
117
+ T* t = static_cast<T *>((T::operator new)(sizeof(T)));
118
+ (T::operator delete)(t);
119
+ }
120
+ #endif
121
+ }
122
+
123
+ EIGEN_DECLARE_TEST(dynalloc)
124
+ {
125
+ // low level dynamic memory allocation
126
+ CALL_SUBTEST(check_handmade_aligned_malloc());
127
+ CALL_SUBTEST(check_aligned_malloc());
128
+ CALL_SUBTEST(check_aligned_new());
129
+ CALL_SUBTEST(check_aligned_stack_alloc());
130
+
131
+ for (int i=0; i<g_repeat*100; ++i)
132
+ {
133
+ CALL_SUBTEST( check_custom_new_delete<Vector4f>() );
134
+ CALL_SUBTEST( check_custom_new_delete<Vector2f>() );
135
+ CALL_SUBTEST( check_custom_new_delete<Matrix4f>() );
136
+ CALL_SUBTEST( check_custom_new_delete<MatrixXi>() );
137
+ }
138
+
139
+ // check static allocation, who knows ?
140
+ #if EIGEN_MAX_STATIC_ALIGN_BYTES
141
+ for (int i=0; i<g_repeat*100; ++i)
142
+ {
143
+ CALL_SUBTEST(check_dynaligned<Vector4f>() );
144
+ CALL_SUBTEST(check_dynaligned<Vector2d>() );
145
+ CALL_SUBTEST(check_dynaligned<Matrix4f>() );
146
+ CALL_SUBTEST(check_dynaligned<Vector4d>() );
147
+ CALL_SUBTEST(check_dynaligned<Vector4i>() );
148
+ CALL_SUBTEST(check_dynaligned<Vector8f>() );
149
+ CALL_SUBTEST(check_dynaligned<Vector16f>() );
150
+ }
151
+
152
+ {
153
+ MyStruct foo0; VERIFY(internal::UIntPtr(foo0.avec.data())%ALIGNMENT==0);
154
+ MyClassA fooA; VERIFY(internal::UIntPtr(fooA.avec.data())%ALIGNMENT==0);
155
+ }
156
+
157
+ // dynamic allocation, single object
158
+ for (int i=0; i<g_repeat*100; ++i)
159
+ {
160
+ MyStruct *foo0 = new MyStruct(); VERIFY(internal::UIntPtr(foo0->avec.data())%ALIGNMENT==0);
161
+ MyClassA *fooA = new MyClassA(); VERIFY(internal::UIntPtr(fooA->avec.data())%ALIGNMENT==0);
162
+ delete foo0;
163
+ delete fooA;
164
+ }
165
+
166
+ // dynamic allocation, array
167
+ const int N = 10;
168
+ for (int i=0; i<g_repeat*100; ++i)
169
+ {
170
+ MyStruct *foo0 = new MyStruct[N]; VERIFY(internal::UIntPtr(foo0->avec.data())%ALIGNMENT==0);
171
+ MyClassA *fooA = new MyClassA[N]; VERIFY(internal::UIntPtr(fooA->avec.data())%ALIGNMENT==0);
172
+ delete[] foo0;
173
+ delete[] fooA;
174
+ }
175
+ #endif
176
+
177
+ }
include/eigen/test/eigensolver_complex.cpp ADDED
@@ -0,0 +1,176 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // This file is part of Eigen, a lightweight C++ template library
2
+ // for linear algebra.
3
+ //
4
+ // Copyright (C) 2008-2009 Gael Guennebaud <gael.guennebaud@inria.fr>
5
+ // Copyright (C) 2010 Jitse Niesen <jitse@maths.leeds.ac.uk>
6
+ //
7
+ // This Source Code Form is subject to the terms of the Mozilla
8
+ // Public License v. 2.0. If a copy of the MPL was not distributed
9
+ // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10
+
11
+ #include "main.h"
12
+ #include <limits>
13
+ #include <Eigen/Eigenvalues>
14
+ #include <Eigen/LU>
15
+
16
+ template<typename MatrixType> bool find_pivot(typename MatrixType::Scalar tol, MatrixType &diffs, Index col=0)
17
+ {
18
+ bool match = diffs.diagonal().sum() <= tol;
19
+ if(match || col==diffs.cols())
20
+ {
21
+ return match;
22
+ }
23
+ else
24
+ {
25
+ Index n = diffs.cols();
26
+ std::vector<std::pair<Index,Index> > transpositions;
27
+ for(Index i=col; i<n; ++i)
28
+ {
29
+ Index best_index(0);
30
+ if(diffs.col(col).segment(col,n-i).minCoeff(&best_index) > tol)
31
+ break;
32
+
33
+ best_index += col;
34
+
35
+ diffs.row(col).swap(diffs.row(best_index));
36
+ if(find_pivot(tol,diffs,col+1)) return true;
37
+ diffs.row(col).swap(diffs.row(best_index));
38
+
39
+ // move current pivot to the end
40
+ diffs.row(n-(i-col)-1).swap(diffs.row(best_index));
41
+ transpositions.push_back(std::pair<Index,Index>(n-(i-col)-1,best_index));
42
+ }
43
+ // restore
44
+ for(Index k=transpositions.size()-1; k>=0; --k)
45
+ diffs.row(transpositions[k].first).swap(diffs.row(transpositions[k].second));
46
+ }
47
+ return false;
48
+ }
49
+
50
+ /* Check that two column vectors are approximately equal up to permutations.
51
+ * Initially, this method checked that the k-th power sums are equal for all k = 1, ..., vec1.rows(),
52
+ * however this strategy is numerically inacurate because of numerical cancellation issues.
53
+ */
54
+ template<typename VectorType>
55
+ void verify_is_approx_upto_permutation(const VectorType& vec1, const VectorType& vec2)
56
+ {
57
+ typedef typename VectorType::Scalar Scalar;
58
+ typedef typename NumTraits<Scalar>::Real RealScalar;
59
+
60
+ VERIFY(vec1.cols() == 1);
61
+ VERIFY(vec2.cols() == 1);
62
+ VERIFY(vec1.rows() == vec2.rows());
63
+
64
+ Index n = vec1.rows();
65
+ RealScalar tol = test_precision<RealScalar>()*test_precision<RealScalar>()*numext::maxi(vec1.squaredNorm(),vec2.squaredNorm());
66
+ Matrix<RealScalar,Dynamic,Dynamic> diffs = (vec1.rowwise().replicate(n) - vec2.rowwise().replicate(n).transpose()).cwiseAbs2();
67
+
68
+ VERIFY( find_pivot(tol, diffs) );
69
+ }
70
+
71
+
72
+ template<typename MatrixType> void eigensolver(const MatrixType& m)
73
+ {
74
+ /* this test covers the following files:
75
+ ComplexEigenSolver.h, and indirectly ComplexSchur.h
76
+ */
77
+ Index rows = m.rows();
78
+ Index cols = m.cols();
79
+
80
+ typedef typename MatrixType::Scalar Scalar;
81
+ typedef typename NumTraits<Scalar>::Real RealScalar;
82
+
83
+ MatrixType a = MatrixType::Random(rows,cols);
84
+ MatrixType symmA = a.adjoint() * a;
85
+
86
+ ComplexEigenSolver<MatrixType> ei0(symmA);
87
+ VERIFY_IS_EQUAL(ei0.info(), Success);
88
+ VERIFY_IS_APPROX(symmA * ei0.eigenvectors(), ei0.eigenvectors() * ei0.eigenvalues().asDiagonal());
89
+
90
+ ComplexEigenSolver<MatrixType> ei1(a);
91
+ VERIFY_IS_EQUAL(ei1.info(), Success);
92
+ VERIFY_IS_APPROX(a * ei1.eigenvectors(), ei1.eigenvectors() * ei1.eigenvalues().asDiagonal());
93
+ // Note: If MatrixType is real then a.eigenvalues() uses EigenSolver and thus
94
+ // another algorithm so results may differ slightly
95
+ verify_is_approx_upto_permutation(a.eigenvalues(), ei1.eigenvalues());
96
+
97
+ ComplexEigenSolver<MatrixType> ei2;
98
+ ei2.setMaxIterations(ComplexSchur<MatrixType>::m_maxIterationsPerRow * rows).compute(a);
99
+ VERIFY_IS_EQUAL(ei2.info(), Success);
100
+ VERIFY_IS_EQUAL(ei2.eigenvectors(), ei1.eigenvectors());
101
+ VERIFY_IS_EQUAL(ei2.eigenvalues(), ei1.eigenvalues());
102
+ if (rows > 2) {
103
+ ei2.setMaxIterations(1).compute(a);
104
+ VERIFY_IS_EQUAL(ei2.info(), NoConvergence);
105
+ VERIFY_IS_EQUAL(ei2.getMaxIterations(), 1);
106
+ }
107
+
108
+ ComplexEigenSolver<MatrixType> eiNoEivecs(a, false);
109
+ VERIFY_IS_EQUAL(eiNoEivecs.info(), Success);
110
+ VERIFY_IS_APPROX(ei1.eigenvalues(), eiNoEivecs.eigenvalues());
111
+
112
+ // Regression test for issue #66
113
+ MatrixType z = MatrixType::Zero(rows,cols);
114
+ ComplexEigenSolver<MatrixType> eiz(z);
115
+ VERIFY((eiz.eigenvalues().cwiseEqual(0)).all());
116
+
117
+ MatrixType id = MatrixType::Identity(rows, cols);
118
+ VERIFY_IS_APPROX(id.operatorNorm(), RealScalar(1));
119
+
120
+ if (rows > 1 && rows < 20)
121
+ {
122
+ // Test matrix with NaN
123
+ a(0,0) = std::numeric_limits<typename MatrixType::RealScalar>::quiet_NaN();
124
+ ComplexEigenSolver<MatrixType> eiNaN(a);
125
+ VERIFY_IS_EQUAL(eiNaN.info(), NoConvergence);
126
+ }
127
+
128
+ // regression test for bug 1098
129
+ {
130
+ ComplexEigenSolver<MatrixType> eig(a.adjoint() * a);
131
+ eig.compute(a.adjoint() * a);
132
+ }
133
+
134
+ // regression test for bug 478
135
+ {
136
+ a.setZero();
137
+ ComplexEigenSolver<MatrixType> ei3(a);
138
+ VERIFY_IS_EQUAL(ei3.info(), Success);
139
+ VERIFY_IS_MUCH_SMALLER_THAN(ei3.eigenvalues().norm(),RealScalar(1));
140
+ VERIFY((ei3.eigenvectors().transpose()*ei3.eigenvectors().transpose()).eval().isIdentity());
141
+ }
142
+ }
143
+
144
+ template<typename MatrixType> void eigensolver_verify_assert(const MatrixType& m)
145
+ {
146
+ ComplexEigenSolver<MatrixType> eig;
147
+ VERIFY_RAISES_ASSERT(eig.eigenvectors());
148
+ VERIFY_RAISES_ASSERT(eig.eigenvalues());
149
+
150
+ MatrixType a = MatrixType::Random(m.rows(),m.cols());
151
+ eig.compute(a, false);
152
+ VERIFY_RAISES_ASSERT(eig.eigenvectors());
153
+ }
154
+
155
+ EIGEN_DECLARE_TEST(eigensolver_complex)
156
+ {
157
+ int s = 0;
158
+ for(int i = 0; i < g_repeat; i++) {
159
+ CALL_SUBTEST_1( eigensolver(Matrix4cf()) );
160
+ s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE/4);
161
+ CALL_SUBTEST_2( eigensolver(MatrixXcd(s,s)) );
162
+ CALL_SUBTEST_3( eigensolver(Matrix<std::complex<float>, 1, 1>()) );
163
+ CALL_SUBTEST_4( eigensolver(Matrix3f()) );
164
+ TEST_SET_BUT_UNUSED_VARIABLE(s)
165
+ }
166
+ CALL_SUBTEST_1( eigensolver_verify_assert(Matrix4cf()) );
167
+ s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE/4);
168
+ CALL_SUBTEST_2( eigensolver_verify_assert(MatrixXcd(s,s)) );
169
+ CALL_SUBTEST_3( eigensolver_verify_assert(Matrix<std::complex<float>, 1, 1>()) );
170
+ CALL_SUBTEST_4( eigensolver_verify_assert(Matrix3f()) );
171
+
172
+ // Test problem size constructors
173
+ CALL_SUBTEST_5(ComplexEigenSolver<MatrixXf> tmp(s));
174
+
175
+ TEST_SET_BUT_UNUSED_VARIABLE(s)
176
+ }
include/eigen/test/eigensolver_generic.cpp ADDED
@@ -0,0 +1,247 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // This file is part of Eigen, a lightweight C++ template library
2
+ // for linear algebra.
3
+ //
4
+ // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
5
+ // Copyright (C) 2010,2012 Jitse Niesen <jitse@maths.leeds.ac.uk>
6
+ //
7
+ // This Source Code Form is subject to the terms of the Mozilla
8
+ // Public License v. 2.0. If a copy of the MPL was not distributed
9
+ // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10
+
11
+ #include "main.h"
12
+ #include <limits>
13
+ #include <Eigen/Eigenvalues>
14
+
15
+ template<typename EigType,typename MatType>
16
+ void check_eigensolver_for_given_mat(const EigType &eig, const MatType& a)
17
+ {
18
+ typedef typename NumTraits<typename MatType::Scalar>::Real RealScalar;
19
+ typedef Matrix<RealScalar, MatType::RowsAtCompileTime, 1> RealVectorType;
20
+ typedef typename std::complex<RealScalar> Complex;
21
+ Index n = a.rows();
22
+ VERIFY_IS_EQUAL(eig.info(), Success);
23
+ VERIFY_IS_APPROX(a * eig.pseudoEigenvectors(), eig.pseudoEigenvectors() * eig.pseudoEigenvalueMatrix());
24
+ VERIFY_IS_APPROX(a.template cast<Complex>() * eig.eigenvectors(),
25
+ eig.eigenvectors() * eig.eigenvalues().asDiagonal());
26
+ VERIFY_IS_APPROX(eig.eigenvectors().colwise().norm(), RealVectorType::Ones(n).transpose());
27
+ VERIFY_IS_APPROX(a.eigenvalues(), eig.eigenvalues());
28
+ }
29
+
30
+ template<typename MatrixType> void eigensolver(const MatrixType& m)
31
+ {
32
+ /* this test covers the following files:
33
+ EigenSolver.h
34
+ */
35
+ Index rows = m.rows();
36
+ Index cols = m.cols();
37
+
38
+ typedef typename MatrixType::Scalar Scalar;
39
+ typedef typename NumTraits<Scalar>::Real RealScalar;
40
+ typedef typename std::complex<RealScalar> Complex;
41
+
42
+ MatrixType a = MatrixType::Random(rows,cols);
43
+ MatrixType a1 = MatrixType::Random(rows,cols);
44
+ MatrixType symmA = a.adjoint() * a + a1.adjoint() * a1;
45
+
46
+ EigenSolver<MatrixType> ei0(symmA);
47
+ VERIFY_IS_EQUAL(ei0.info(), Success);
48
+ VERIFY_IS_APPROX(symmA * ei0.pseudoEigenvectors(), ei0.pseudoEigenvectors() * ei0.pseudoEigenvalueMatrix());
49
+ VERIFY_IS_APPROX((symmA.template cast<Complex>()) * (ei0.pseudoEigenvectors().template cast<Complex>()),
50
+ (ei0.pseudoEigenvectors().template cast<Complex>()) * (ei0.eigenvalues().asDiagonal()));
51
+
52
+ EigenSolver<MatrixType> ei1(a);
53
+ CALL_SUBTEST( check_eigensolver_for_given_mat(ei1,a) );
54
+
55
+ EigenSolver<MatrixType> ei2;
56
+ ei2.setMaxIterations(RealSchur<MatrixType>::m_maxIterationsPerRow * rows).compute(a);
57
+ VERIFY_IS_EQUAL(ei2.info(), Success);
58
+ VERIFY_IS_EQUAL(ei2.eigenvectors(), ei1.eigenvectors());
59
+ VERIFY_IS_EQUAL(ei2.eigenvalues(), ei1.eigenvalues());
60
+ if (rows > 2) {
61
+ ei2.setMaxIterations(1).compute(a);
62
+ VERIFY_IS_EQUAL(ei2.info(), NoConvergence);
63
+ VERIFY_IS_EQUAL(ei2.getMaxIterations(), 1);
64
+ }
65
+
66
+ EigenSolver<MatrixType> eiNoEivecs(a, false);
67
+ VERIFY_IS_EQUAL(eiNoEivecs.info(), Success);
68
+ VERIFY_IS_APPROX(ei1.eigenvalues(), eiNoEivecs.eigenvalues());
69
+ VERIFY_IS_APPROX(ei1.pseudoEigenvalueMatrix(), eiNoEivecs.pseudoEigenvalueMatrix());
70
+
71
+ MatrixType id = MatrixType::Identity(rows, cols);
72
+ VERIFY_IS_APPROX(id.operatorNorm(), RealScalar(1));
73
+
74
+ if (rows > 2 && rows < 20)
75
+ {
76
+ // Test matrix with NaN
77
+ a(0,0) = std::numeric_limits<typename MatrixType::RealScalar>::quiet_NaN();
78
+ EigenSolver<MatrixType> eiNaN(a);
79
+ VERIFY_IS_NOT_EQUAL(eiNaN.info(), Success);
80
+ }
81
+
82
+ // regression test for bug 1098
83
+ {
84
+ EigenSolver<MatrixType> eig(a.adjoint() * a);
85
+ eig.compute(a.adjoint() * a);
86
+ }
87
+
88
+ // regression test for bug 478
89
+ {
90
+ a.setZero();
91
+ EigenSolver<MatrixType> ei3(a);
92
+ VERIFY_IS_EQUAL(ei3.info(), Success);
93
+ VERIFY_IS_MUCH_SMALLER_THAN(ei3.eigenvalues().norm(),RealScalar(1));
94
+ VERIFY((ei3.eigenvectors().transpose()*ei3.eigenvectors().transpose()).eval().isIdentity());
95
+ }
96
+ }
97
+
98
+ template<typename MatrixType> void eigensolver_verify_assert(const MatrixType& m)
99
+ {
100
+ EigenSolver<MatrixType> eig;
101
+ VERIFY_RAISES_ASSERT(eig.eigenvectors());
102
+ VERIFY_RAISES_ASSERT(eig.pseudoEigenvectors());
103
+ VERIFY_RAISES_ASSERT(eig.pseudoEigenvalueMatrix());
104
+ VERIFY_RAISES_ASSERT(eig.eigenvalues());
105
+
106
+ MatrixType a = MatrixType::Random(m.rows(),m.cols());
107
+ eig.compute(a, false);
108
+ VERIFY_RAISES_ASSERT(eig.eigenvectors());
109
+ VERIFY_RAISES_ASSERT(eig.pseudoEigenvectors());
110
+ }
111
+
112
+
113
+ template<typename CoeffType>
114
+ Matrix<typename CoeffType::Scalar,Dynamic,Dynamic>
115
+ make_companion(const CoeffType& coeffs)
116
+ {
117
+ Index n = coeffs.size()-1;
118
+ Matrix<typename CoeffType::Scalar,Dynamic,Dynamic> res(n,n);
119
+ res.setZero();
120
+ res.row(0) = -coeffs.tail(n) / coeffs(0);
121
+ res.diagonal(-1).setOnes();
122
+ return res;
123
+ }
124
+
125
+ template<int>
126
+ void eigensolver_generic_extra()
127
+ {
128
+ {
129
+ // regression test for bug 793
130
+ MatrixXd a(3,3);
131
+ a << 0, 0, 1,
132
+ 1, 1, 1,
133
+ 1, 1e+200, 1;
134
+ Eigen::EigenSolver<MatrixXd> eig(a);
135
+ double scale = 1e-200; // scale to avoid overflow during the comparisons
136
+ VERIFY_IS_APPROX(a * eig.pseudoEigenvectors()*scale, eig.pseudoEigenvectors() * eig.pseudoEigenvalueMatrix()*scale);
137
+ VERIFY_IS_APPROX(a * eig.eigenvectors()*scale, eig.eigenvectors() * eig.eigenvalues().asDiagonal()*scale);
138
+ }
139
+ {
140
+ // check a case where all eigenvalues are null.
141
+ MatrixXd a(2,2);
142
+ a << 1, 1,
143
+ -1, -1;
144
+ Eigen::EigenSolver<MatrixXd> eig(a);
145
+ VERIFY_IS_APPROX(eig.pseudoEigenvectors().squaredNorm(), 2.);
146
+ VERIFY_IS_APPROX((a * eig.pseudoEigenvectors()).norm()+1., 1.);
147
+ VERIFY_IS_APPROX((eig.pseudoEigenvectors() * eig.pseudoEigenvalueMatrix()).norm()+1., 1.);
148
+ VERIFY_IS_APPROX((a * eig.eigenvectors()).norm()+1., 1.);
149
+ VERIFY_IS_APPROX((eig.eigenvectors() * eig.eigenvalues().asDiagonal()).norm()+1., 1.);
150
+ }
151
+
152
+ // regression test for bug 933
153
+ {
154
+ {
155
+ VectorXd coeffs(5); coeffs << 1, -3, -175, -225, 2250;
156
+ MatrixXd C = make_companion(coeffs);
157
+ EigenSolver<MatrixXd> eig(C);
158
+ CALL_SUBTEST( check_eigensolver_for_given_mat(eig,C) );
159
+ }
160
+ {
161
+ // this test is tricky because it requires high accuracy in smallest eigenvalues
162
+ VectorXd coeffs(5); coeffs << 6.154671e-15, -1.003870e-10, -9.819570e-01, 3.995715e+03, 2.211511e+08;
163
+ MatrixXd C = make_companion(coeffs);
164
+ EigenSolver<MatrixXd> eig(C);
165
+ CALL_SUBTEST( check_eigensolver_for_given_mat(eig,C) );
166
+ Index n = C.rows();
167
+ for(Index i=0;i<n;++i)
168
+ {
169
+ typedef std::complex<double> Complex;
170
+ MatrixXcd ac = C.cast<Complex>();
171
+ ac.diagonal().array() -= eig.eigenvalues()(i);
172
+ VectorXd sv = ac.jacobiSvd().singularValues();
173
+ // comparing to sv(0) is not enough here to catch the "bug",
174
+ // the hard-coded 1.0 is important!
175
+ VERIFY_IS_MUCH_SMALLER_THAN(sv(n-1), 1.0);
176
+ }
177
+ }
178
+ }
179
+ // regression test for bug 1557
180
+ {
181
+ // this test is interesting because it contains zeros on the diagonal.
182
+ MatrixXd A_bug1557(3,3);
183
+ A_bug1557 << 0, 0, 0, 1, 0, 0.5887907064808635127, 0, 1, 0;
184
+ EigenSolver<MatrixXd> eig(A_bug1557);
185
+ CALL_SUBTEST( check_eigensolver_for_given_mat(eig,A_bug1557) );
186
+ }
187
+
188
+ // regression test for bug 1174
189
+ {
190
+ Index n = 12;
191
+ MatrixXf A_bug1174(n,n);
192
+ A_bug1174 << 262144, 0, 0, 262144, 786432, 0, 0, 0, 0, 0, 0, 786432,
193
+ 262144, 0, 0, 262144, 786432, 0, 0, 0, 0, 0, 0, 786432,
194
+ 262144, 0, 0, 262144, 786432, 0, 0, 0, 0, 0, 0, 786432,
195
+ 262144, 0, 0, 262144, 786432, 0, 0, 0, 0, 0, 0, 786432,
196
+ 0, 262144, 262144, 0, 0, 262144, 262144, 262144, 262144, 262144, 262144, 0,
197
+ 0, 262144, 262144, 0, 0, 262144, 262144, 262144, 262144, 262144, 262144, 0,
198
+ 0, 262144, 262144, 0, 0, 262144, 262144, 262144, 262144, 262144, 262144, 0,
199
+ 0, 262144, 262144, 0, 0, 262144, 262144, 262144, 262144, 262144, 262144, 0,
200
+ 0, 262144, 262144, 0, 0, 262144, 262144, 262144, 262144, 262144, 262144, 0,
201
+ 0, 262144, 262144, 0, 0, 262144, 262144, 262144, 262144, 262144, 262144, 0,
202
+ 0, 262144, 262144, 0, 0, 262144, 262144, 262144, 262144, 262144, 262144, 0,
203
+ 0, 262144, 262144, 0, 0, 262144, 262144, 262144, 262144, 262144, 262144, 0;
204
+ EigenSolver<MatrixXf> eig(A_bug1174);
205
+ CALL_SUBTEST( check_eigensolver_for_given_mat(eig,A_bug1174) );
206
+ }
207
+ }
208
+
209
+ EIGEN_DECLARE_TEST(eigensolver_generic)
210
+ {
211
+ int s = 0;
212
+ for(int i = 0; i < g_repeat; i++) {
213
+ CALL_SUBTEST_1( eigensolver(Matrix4f()) );
214
+ s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE/4);
215
+ CALL_SUBTEST_2( eigensolver(MatrixXd(s,s)) );
216
+ TEST_SET_BUT_UNUSED_VARIABLE(s)
217
+
218
+ // some trivial but implementation-wise tricky cases
219
+ CALL_SUBTEST_2( eigensolver(MatrixXd(1,1)) );
220
+ CALL_SUBTEST_2( eigensolver(MatrixXd(2,2)) );
221
+ CALL_SUBTEST_3( eigensolver(Matrix<double,1,1>()) );
222
+ CALL_SUBTEST_4( eigensolver(Matrix2d()) );
223
+ }
224
+
225
+ CALL_SUBTEST_1( eigensolver_verify_assert(Matrix4f()) );
226
+ s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE/4);
227
+ CALL_SUBTEST_2( eigensolver_verify_assert(MatrixXd(s,s)) );
228
+ CALL_SUBTEST_3( eigensolver_verify_assert(Matrix<double,1,1>()) );
229
+ CALL_SUBTEST_4( eigensolver_verify_assert(Matrix2d()) );
230
+
231
+ // Test problem size constructors
232
+ CALL_SUBTEST_5(EigenSolver<MatrixXf> tmp(s));
233
+
234
+ // regression test for bug 410
235
+ CALL_SUBTEST_2(
236
+ {
237
+ MatrixXd A(1,1);
238
+ A(0,0) = std::sqrt(-1.); // is Not-a-Number
239
+ Eigen::EigenSolver<MatrixXd> solver(A);
240
+ VERIFY_IS_EQUAL(solver.info(), NumericalIssue);
241
+ }
242
+ );
243
+
244
+ CALL_SUBTEST_2( eigensolver_generic_extra<0>() );
245
+
246
+ TEST_SET_BUT_UNUSED_VARIABLE(s)
247
+ }
include/eigen/test/evaluator_common.h ADDED
File without changes
include/eigen/test/evaluators.cpp ADDED
@@ -0,0 +1,525 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ #include "main.h"
3
+
4
+ namespace Eigen {
5
+
6
+ template<typename Lhs,typename Rhs>
7
+ const Product<Lhs,Rhs>
8
+ prod(const Lhs& lhs, const Rhs& rhs)
9
+ {
10
+ return Product<Lhs,Rhs>(lhs,rhs);
11
+ }
12
+
13
+ template<typename Lhs,typename Rhs>
14
+ const Product<Lhs,Rhs,LazyProduct>
15
+ lazyprod(const Lhs& lhs, const Rhs& rhs)
16
+ {
17
+ return Product<Lhs,Rhs,LazyProduct>(lhs,rhs);
18
+ }
19
+
20
+ template<typename DstXprType, typename SrcXprType>
21
+ EIGEN_STRONG_INLINE
22
+ DstXprType& copy_using_evaluator(const EigenBase<DstXprType> &dst, const SrcXprType &src)
23
+ {
24
+ call_assignment(dst.const_cast_derived(), src.derived(), internal::assign_op<typename DstXprType::Scalar,typename SrcXprType::Scalar>());
25
+ return dst.const_cast_derived();
26
+ }
27
+
28
+ template<typename DstXprType, template <typename> class StorageBase, typename SrcXprType>
29
+ EIGEN_STRONG_INLINE
30
+ const DstXprType& copy_using_evaluator(const NoAlias<DstXprType, StorageBase>& dst, const SrcXprType &src)
31
+ {
32
+ call_assignment(dst, src.derived(), internal::assign_op<typename DstXprType::Scalar,typename SrcXprType::Scalar>());
33
+ return dst.expression();
34
+ }
35
+
36
+ template<typename DstXprType, typename SrcXprType>
37
+ EIGEN_STRONG_INLINE
38
+ DstXprType& copy_using_evaluator(const PlainObjectBase<DstXprType> &dst, const SrcXprType &src)
39
+ {
40
+ #ifdef EIGEN_NO_AUTOMATIC_RESIZING
41
+ eigen_assert((dst.size()==0 || (IsVectorAtCompileTime ? (dst.size() == src.size())
42
+ : (dst.rows() == src.rows() && dst.cols() == src.cols())))
43
+ && "Size mismatch. Automatic resizing is disabled because EIGEN_NO_AUTOMATIC_RESIZING is defined");
44
+ #else
45
+ dst.const_cast_derived().resizeLike(src.derived());
46
+ #endif
47
+
48
+ call_assignment(dst.const_cast_derived(), src.derived(), internal::assign_op<typename DstXprType::Scalar,typename SrcXprType::Scalar>());
49
+ return dst.const_cast_derived();
50
+ }
51
+
52
+ template<typename DstXprType, typename SrcXprType>
53
+ void add_assign_using_evaluator(const DstXprType& dst, const SrcXprType& src)
54
+ {
55
+ typedef typename DstXprType::Scalar Scalar;
56
+ call_assignment(const_cast<DstXprType&>(dst), src.derived(), internal::add_assign_op<Scalar,typename SrcXprType::Scalar>());
57
+ }
58
+
59
+ template<typename DstXprType, typename SrcXprType>
60
+ void subtract_assign_using_evaluator(const DstXprType& dst, const SrcXprType& src)
61
+ {
62
+ typedef typename DstXprType::Scalar Scalar;
63
+ call_assignment(const_cast<DstXprType&>(dst), src.derived(), internal::sub_assign_op<Scalar,typename SrcXprType::Scalar>());
64
+ }
65
+
66
+ template<typename DstXprType, typename SrcXprType>
67
+ void multiply_assign_using_evaluator(const DstXprType& dst, const SrcXprType& src)
68
+ {
69
+ typedef typename DstXprType::Scalar Scalar;
70
+ call_assignment(dst.const_cast_derived(), src.derived(), internal::mul_assign_op<Scalar,typename SrcXprType::Scalar>());
71
+ }
72
+
73
+ template<typename DstXprType, typename SrcXprType>
74
+ void divide_assign_using_evaluator(const DstXprType& dst, const SrcXprType& src)
75
+ {
76
+ typedef typename DstXprType::Scalar Scalar;
77
+ call_assignment(dst.const_cast_derived(), src.derived(), internal::div_assign_op<Scalar,typename SrcXprType::Scalar>());
78
+ }
79
+
80
+ template<typename DstXprType, typename SrcXprType>
81
+ void swap_using_evaluator(const DstXprType& dst, const SrcXprType& src)
82
+ {
83
+ typedef typename DstXprType::Scalar Scalar;
84
+ call_assignment(dst.const_cast_derived(), src.const_cast_derived(), internal::swap_assign_op<Scalar>());
85
+ }
86
+
87
+ namespace internal {
88
+ template<typename Dst, template <typename> class StorageBase, typename Src, typename Func>
89
+ EIGEN_DEVICE_FUNC void call_assignment(const NoAlias<Dst,StorageBase>& dst, const Src& src, const Func& func)
90
+ {
91
+ call_assignment_no_alias(dst.expression(), src, func);
92
+ }
93
+
94
+ template<typename Dst, template <typename> class StorageBase, typename Src, typename Func>
95
+ EIGEN_DEVICE_FUNC void call_restricted_packet_assignment(const NoAlias<Dst,StorageBase>& dst, const Src& src, const Func& func)
96
+ {
97
+ call_restricted_packet_assignment_no_alias(dst.expression(), src, func);
98
+ }
99
+ }
100
+
101
+ }
102
+
103
+ template<typename XprType> long get_cost(const XprType& ) { return Eigen::internal::evaluator<XprType>::CoeffReadCost; }
104
+
105
+ using namespace std;
106
+
107
+ #define VERIFY_IS_APPROX_EVALUATOR(DEST,EXPR) VERIFY_IS_APPROX(copy_using_evaluator(DEST,(EXPR)), (EXPR).eval());
108
+ #define VERIFY_IS_APPROX_EVALUATOR2(DEST,EXPR,REF) VERIFY_IS_APPROX(copy_using_evaluator(DEST,(EXPR)), (REF).eval());
109
+
110
+ EIGEN_DECLARE_TEST(evaluators)
111
+ {
112
+ // Testing Matrix evaluator and Transpose
113
+ Vector2d v = Vector2d::Random();
114
+ const Vector2d v_const(v);
115
+ Vector2d v2;
116
+ RowVector2d w;
117
+
118
+ VERIFY_IS_APPROX_EVALUATOR(v2, v);
119
+ VERIFY_IS_APPROX_EVALUATOR(v2, v_const);
120
+
121
+ // Testing Transpose
122
+ VERIFY_IS_APPROX_EVALUATOR(w, v.transpose()); // Transpose as rvalue
123
+ VERIFY_IS_APPROX_EVALUATOR(w, v_const.transpose());
124
+
125
+ copy_using_evaluator(w.transpose(), v); // Transpose as lvalue
126
+ VERIFY_IS_APPROX(w,v.transpose().eval());
127
+
128
+ copy_using_evaluator(w.transpose(), v_const);
129
+ VERIFY_IS_APPROX(w,v_const.transpose().eval());
130
+
131
+ // Testing Array evaluator
132
+ {
133
+ ArrayXXf a(2,3);
134
+ ArrayXXf b(3,2);
135
+ a << 1,2,3, 4,5,6;
136
+ const ArrayXXf a_const(a);
137
+
138
+ VERIFY_IS_APPROX_EVALUATOR(b, a.transpose());
139
+
140
+ VERIFY_IS_APPROX_EVALUATOR(b, a_const.transpose());
141
+
142
+ // Testing CwiseNullaryOp evaluator
143
+ copy_using_evaluator(w, RowVector2d::Random());
144
+ VERIFY((w.array() >= -1).all() && (w.array() <= 1).all()); // not easy to test ...
145
+
146
+ VERIFY_IS_APPROX_EVALUATOR(w, RowVector2d::Zero());
147
+
148
+ VERIFY_IS_APPROX_EVALUATOR(w, RowVector2d::Constant(3));
149
+
150
+ // mix CwiseNullaryOp and transpose
151
+ VERIFY_IS_APPROX_EVALUATOR(w, Vector2d::Zero().transpose());
152
+ }
153
+
154
+ {
155
+ // test product expressions
156
+ int s = internal::random<int>(1,100);
157
+ MatrixXf a(s,s), b(s,s), c(s,s), d(s,s);
158
+ a.setRandom();
159
+ b.setRandom();
160
+ c.setRandom();
161
+ d.setRandom();
162
+ VERIFY_IS_APPROX_EVALUATOR(d, (a + b));
163
+ VERIFY_IS_APPROX_EVALUATOR(d, (a + b).transpose());
164
+ VERIFY_IS_APPROX_EVALUATOR2(d, prod(a,b), a*b);
165
+ VERIFY_IS_APPROX_EVALUATOR2(d.noalias(), prod(a,b), a*b);
166
+ VERIFY_IS_APPROX_EVALUATOR2(d, prod(a,b) + c, a*b + c);
167
+ VERIFY_IS_APPROX_EVALUATOR2(d, s * prod(a,b), s * a*b);
168
+ VERIFY_IS_APPROX_EVALUATOR2(d, prod(a,b).transpose(), (a*b).transpose());
169
+ VERIFY_IS_APPROX_EVALUATOR2(d, prod(a,b) + prod(b,c), a*b + b*c);
170
+
171
+ // check that prod works even with aliasing present
172
+ c = a*a;
173
+ copy_using_evaluator(a, prod(a,a));
174
+ VERIFY_IS_APPROX(a,c);
175
+
176
+ // check compound assignment of products
177
+ d = c;
178
+ add_assign_using_evaluator(c.noalias(), prod(a,b));
179
+ d.noalias() += a*b;
180
+ VERIFY_IS_APPROX(c, d);
181
+
182
+ d = c;
183
+ subtract_assign_using_evaluator(c.noalias(), prod(a,b));
184
+ d.noalias() -= a*b;
185
+ VERIFY_IS_APPROX(c, d);
186
+ }
187
+
188
+ {
189
+ // test product with all possible sizes
190
+ int s = internal::random<int>(1,100);
191
+ Matrix<float, 1, 1> m11, res11; m11.setRandom(1,1);
192
+ Matrix<float, 1, 4> m14, res14; m14.setRandom(1,4);
193
+ Matrix<float, 1,Dynamic> m1X, res1X; m1X.setRandom(1,s);
194
+ Matrix<float, 4, 1> m41, res41; m41.setRandom(4,1);
195
+ Matrix<float, 4, 4> m44, res44; m44.setRandom(4,4);
196
+ Matrix<float, 4,Dynamic> m4X, res4X; m4X.setRandom(4,s);
197
+ Matrix<float,Dynamic, 1> mX1, resX1; mX1.setRandom(s,1);
198
+ Matrix<float,Dynamic, 4> mX4, resX4; mX4.setRandom(s,4);
199
+ Matrix<float,Dynamic,Dynamic> mXX, resXX; mXX.setRandom(s,s);
200
+
201
+ VERIFY_IS_APPROX_EVALUATOR2(res11, prod(m11,m11), m11*m11);
202
+ VERIFY_IS_APPROX_EVALUATOR2(res11, prod(m14,m41), m14*m41);
203
+ VERIFY_IS_APPROX_EVALUATOR2(res11, prod(m1X,mX1), m1X*mX1);
204
+ VERIFY_IS_APPROX_EVALUATOR2(res14, prod(m11,m14), m11*m14);
205
+ VERIFY_IS_APPROX_EVALUATOR2(res14, prod(m14,m44), m14*m44);
206
+ VERIFY_IS_APPROX_EVALUATOR2(res14, prod(m1X,mX4), m1X*mX4);
207
+ VERIFY_IS_APPROX_EVALUATOR2(res1X, prod(m11,m1X), m11*m1X);
208
+ VERIFY_IS_APPROX_EVALUATOR2(res1X, prod(m14,m4X), m14*m4X);
209
+ VERIFY_IS_APPROX_EVALUATOR2(res1X, prod(m1X,mXX), m1X*mXX);
210
+ VERIFY_IS_APPROX_EVALUATOR2(res41, prod(m41,m11), m41*m11);
211
+ VERIFY_IS_APPROX_EVALUATOR2(res41, prod(m44,m41), m44*m41);
212
+ VERIFY_IS_APPROX_EVALUATOR2(res41, prod(m4X,mX1), m4X*mX1);
213
+ VERIFY_IS_APPROX_EVALUATOR2(res44, prod(m41,m14), m41*m14);
214
+ VERIFY_IS_APPROX_EVALUATOR2(res44, prod(m44,m44), m44*m44);
215
+ VERIFY_IS_APPROX_EVALUATOR2(res44, prod(m4X,mX4), m4X*mX4);
216
+ VERIFY_IS_APPROX_EVALUATOR2(res4X, prod(m41,m1X), m41*m1X);
217
+ VERIFY_IS_APPROX_EVALUATOR2(res4X, prod(m44,m4X), m44*m4X);
218
+ VERIFY_IS_APPROX_EVALUATOR2(res4X, prod(m4X,mXX), m4X*mXX);
219
+ VERIFY_IS_APPROX_EVALUATOR2(resX1, prod(mX1,m11), mX1*m11);
220
+ VERIFY_IS_APPROX_EVALUATOR2(resX1, prod(mX4,m41), mX4*m41);
221
+ VERIFY_IS_APPROX_EVALUATOR2(resX1, prod(mXX,mX1), mXX*mX1);
222
+ VERIFY_IS_APPROX_EVALUATOR2(resX4, prod(mX1,m14), mX1*m14);
223
+ VERIFY_IS_APPROX_EVALUATOR2(resX4, prod(mX4,m44), mX4*m44);
224
+ VERIFY_IS_APPROX_EVALUATOR2(resX4, prod(mXX,mX4), mXX*mX4);
225
+ VERIFY_IS_APPROX_EVALUATOR2(resXX, prod(mX1,m1X), mX1*m1X);
226
+ VERIFY_IS_APPROX_EVALUATOR2(resXX, prod(mX4,m4X), mX4*m4X);
227
+ VERIFY_IS_APPROX_EVALUATOR2(resXX, prod(mXX,mXX), mXX*mXX);
228
+ }
229
+
230
+ {
231
+ ArrayXXf a(2,3);
232
+ ArrayXXf b(3,2);
233
+ a << 1,2,3, 4,5,6;
234
+ const ArrayXXf a_const(a);
235
+
236
+ // this does not work because Random is eval-before-nested:
237
+ // copy_using_evaluator(w, Vector2d::Random().transpose());
238
+
239
+ // test CwiseUnaryOp
240
+ VERIFY_IS_APPROX_EVALUATOR(v2, 3 * v);
241
+ VERIFY_IS_APPROX_EVALUATOR(w, (3 * v).transpose());
242
+ VERIFY_IS_APPROX_EVALUATOR(b, (a + 3).transpose());
243
+ VERIFY_IS_APPROX_EVALUATOR(b, (2 * a_const + 3).transpose());
244
+
245
+ // test CwiseBinaryOp
246
+ VERIFY_IS_APPROX_EVALUATOR(v2, v + Vector2d::Ones());
247
+ VERIFY_IS_APPROX_EVALUATOR(w, (v + Vector2d::Ones()).transpose().cwiseProduct(RowVector2d::Constant(3)));
248
+
249
+ // dynamic matrices and arrays
250
+ MatrixXd mat1(6,6), mat2(6,6);
251
+ VERIFY_IS_APPROX_EVALUATOR(mat1, MatrixXd::Identity(6,6));
252
+ VERIFY_IS_APPROX_EVALUATOR(mat2, mat1);
253
+ copy_using_evaluator(mat2.transpose(), mat1);
254
+ VERIFY_IS_APPROX(mat2.transpose(), mat1);
255
+
256
+ ArrayXXd arr1(6,6), arr2(6,6);
257
+ VERIFY_IS_APPROX_EVALUATOR(arr1, ArrayXXd::Constant(6,6, 3.0));
258
+ VERIFY_IS_APPROX_EVALUATOR(arr2, arr1);
259
+
260
+ // test automatic resizing
261
+ mat2.resize(3,3);
262
+ VERIFY_IS_APPROX_EVALUATOR(mat2, mat1);
263
+ arr2.resize(9,9);
264
+ VERIFY_IS_APPROX_EVALUATOR(arr2, arr1);
265
+
266
+ // test direct traversal
267
+ Matrix3f m3;
268
+ Array33f a3;
269
+ VERIFY_IS_APPROX_EVALUATOR(m3, Matrix3f::Identity()); // matrix, nullary
270
+ // TODO: find a way to test direct traversal with array
271
+ VERIFY_IS_APPROX_EVALUATOR(m3.transpose(), Matrix3f::Identity().transpose()); // transpose
272
+ VERIFY_IS_APPROX_EVALUATOR(m3, 2 * Matrix3f::Identity()); // unary
273
+ VERIFY_IS_APPROX_EVALUATOR(m3, Matrix3f::Identity() + Matrix3f::Zero()); // binary
274
+ VERIFY_IS_APPROX_EVALUATOR(m3.block(0,0,2,2), Matrix3f::Identity().block(1,1,2,2)); // block
275
+
276
+ // test linear traversal
277
+ VERIFY_IS_APPROX_EVALUATOR(m3, Matrix3f::Zero()); // matrix, nullary
278
+ VERIFY_IS_APPROX_EVALUATOR(a3, Array33f::Zero()); // array
279
+ VERIFY_IS_APPROX_EVALUATOR(m3.transpose(), Matrix3f::Zero().transpose()); // transpose
280
+ VERIFY_IS_APPROX_EVALUATOR(m3, 2 * Matrix3f::Zero()); // unary
281
+ VERIFY_IS_APPROX_EVALUATOR(m3, Matrix3f::Zero() + m3); // binary
282
+
283
+ // test inner vectorization
284
+ Matrix4f m4, m4src = Matrix4f::Random();
285
+ Array44f a4, a4src = Matrix4f::Random();
286
+ VERIFY_IS_APPROX_EVALUATOR(m4, m4src); // matrix
287
+ VERIFY_IS_APPROX_EVALUATOR(a4, a4src); // array
288
+ VERIFY_IS_APPROX_EVALUATOR(m4.transpose(), m4src.transpose()); // transpose
289
+ // TODO: find out why Matrix4f::Zero() does not allow inner vectorization
290
+ VERIFY_IS_APPROX_EVALUATOR(m4, 2 * m4src); // unary
291
+ VERIFY_IS_APPROX_EVALUATOR(m4, m4src + m4src); // binary
292
+
293
+ // test linear vectorization
294
+ MatrixXf mX(6,6), mXsrc = MatrixXf::Random(6,6);
295
+ ArrayXXf aX(6,6), aXsrc = ArrayXXf::Random(6,6);
296
+ VERIFY_IS_APPROX_EVALUATOR(mX, mXsrc); // matrix
297
+ VERIFY_IS_APPROX_EVALUATOR(aX, aXsrc); // array
298
+ VERIFY_IS_APPROX_EVALUATOR(mX.transpose(), mXsrc.transpose()); // transpose
299
+ VERIFY_IS_APPROX_EVALUATOR(mX, MatrixXf::Zero(6,6)); // nullary
300
+ VERIFY_IS_APPROX_EVALUATOR(mX, 2 * mXsrc); // unary
301
+ VERIFY_IS_APPROX_EVALUATOR(mX, mXsrc + mXsrc); // binary
302
+
303
+ // test blocks and slice vectorization
304
+ VERIFY_IS_APPROX_EVALUATOR(m4, (mXsrc.block<4,4>(1,0)));
305
+ VERIFY_IS_APPROX_EVALUATOR(aX, ArrayXXf::Constant(10, 10, 3.0).block(2, 3, 6, 6));
306
+
307
+ Matrix4f m4ref = m4;
308
+ copy_using_evaluator(m4.block(1, 1, 2, 3), m3.bottomRows(2));
309
+ m4ref.block(1, 1, 2, 3) = m3.bottomRows(2);
310
+ VERIFY_IS_APPROX(m4, m4ref);
311
+
312
+ mX.setIdentity(20,20);
313
+ MatrixXf mXref = MatrixXf::Identity(20,20);
314
+ mXsrc = MatrixXf::Random(9,12);
315
+ copy_using_evaluator(mX.block(4, 4, 9, 12), mXsrc);
316
+ mXref.block(4, 4, 9, 12) = mXsrc;
317
+ VERIFY_IS_APPROX(mX, mXref);
318
+
319
+ // test Map
320
+ const float raw[3] = {1,2,3};
321
+ float buffer[3] = {0,0,0};
322
+ Vector3f v3;
323
+ Array3f a3f;
324
+ VERIFY_IS_APPROX_EVALUATOR(v3, Map<const Vector3f>(raw));
325
+ VERIFY_IS_APPROX_EVALUATOR(a3f, Map<const Array3f>(raw));
326
+ Vector3f::Map(buffer) = 2*v3;
327
+ VERIFY(buffer[0] == 2);
328
+ VERIFY(buffer[1] == 4);
329
+ VERIFY(buffer[2] == 6);
330
+
331
+ // test CwiseUnaryView
332
+ mat1.setRandom();
333
+ mat2.setIdentity();
334
+ MatrixXcd matXcd(6,6), matXcd_ref(6,6);
335
+ copy_using_evaluator(matXcd.real(), mat1);
336
+ copy_using_evaluator(matXcd.imag(), mat2);
337
+ matXcd_ref.real() = mat1;
338
+ matXcd_ref.imag() = mat2;
339
+ VERIFY_IS_APPROX(matXcd, matXcd_ref);
340
+
341
+ // test Select
342
+ VERIFY_IS_APPROX_EVALUATOR(aX, (aXsrc > 0).select(aXsrc, -aXsrc));
343
+
344
+ // test Replicate
345
+ mXsrc = MatrixXf::Random(6, 6);
346
+ VectorXf vX = VectorXf::Random(6);
347
+ mX.resize(6, 6);
348
+ VERIFY_IS_APPROX_EVALUATOR(mX, mXsrc.colwise() + vX);
349
+ matXcd.resize(12, 12);
350
+ VERIFY_IS_APPROX_EVALUATOR(matXcd, matXcd_ref.replicate(2,2));
351
+ VERIFY_IS_APPROX_EVALUATOR(matXcd, (matXcd_ref.replicate<2,2>()));
352
+
353
+ // test partial reductions
354
+ VectorXd vec1(6);
355
+ VERIFY_IS_APPROX_EVALUATOR(vec1, mat1.rowwise().sum());
356
+ VERIFY_IS_APPROX_EVALUATOR(vec1, mat1.colwise().sum().transpose());
357
+
358
+ // test MatrixWrapper and ArrayWrapper
359
+ mat1.setRandom(6,6);
360
+ arr1.setRandom(6,6);
361
+ VERIFY_IS_APPROX_EVALUATOR(mat2, arr1.matrix());
362
+ VERIFY_IS_APPROX_EVALUATOR(arr2, mat1.array());
363
+ VERIFY_IS_APPROX_EVALUATOR(mat2, (arr1 + 2).matrix());
364
+ VERIFY_IS_APPROX_EVALUATOR(arr2, mat1.array() + 2);
365
+ mat2.array() = arr1 * arr1;
366
+ VERIFY_IS_APPROX(mat2, (arr1 * arr1).matrix());
367
+ arr2.matrix() = MatrixXd::Identity(6,6);
368
+ VERIFY_IS_APPROX(arr2, MatrixXd::Identity(6,6).array());
369
+
370
+ // test Reverse
371
+ VERIFY_IS_APPROX_EVALUATOR(arr2, arr1.reverse());
372
+ VERIFY_IS_APPROX_EVALUATOR(arr2, arr1.colwise().reverse());
373
+ VERIFY_IS_APPROX_EVALUATOR(arr2, arr1.rowwise().reverse());
374
+ arr2.reverse() = arr1;
375
+ VERIFY_IS_APPROX(arr2, arr1.reverse());
376
+ mat2.array() = mat1.array().reverse();
377
+ VERIFY_IS_APPROX(mat2.array(), mat1.array().reverse());
378
+
379
+ // test Diagonal
380
+ VERIFY_IS_APPROX_EVALUATOR(vec1, mat1.diagonal());
381
+ vec1.resize(5);
382
+ VERIFY_IS_APPROX_EVALUATOR(vec1, mat1.diagonal(1));
383
+ VERIFY_IS_APPROX_EVALUATOR(vec1, mat1.diagonal<-1>());
384
+ vec1.setRandom();
385
+
386
+ mat2 = mat1;
387
+ copy_using_evaluator(mat1.diagonal(1), vec1);
388
+ mat2.diagonal(1) = vec1;
389
+ VERIFY_IS_APPROX(mat1, mat2);
390
+
391
+ copy_using_evaluator(mat1.diagonal<-1>(), mat1.diagonal(1));
392
+ mat2.diagonal<-1>() = mat2.diagonal(1);
393
+ VERIFY_IS_APPROX(mat1, mat2);
394
+ }
395
+
396
+ {
397
+ // test swapping
398
+ MatrixXd mat1, mat2, mat1ref, mat2ref;
399
+ mat1ref = mat1 = MatrixXd::Random(6, 6);
400
+ mat2ref = mat2 = 2 * mat1 + MatrixXd::Identity(6, 6);
401
+ swap_using_evaluator(mat1, mat2);
402
+ mat1ref.swap(mat2ref);
403
+ VERIFY_IS_APPROX(mat1, mat1ref);
404
+ VERIFY_IS_APPROX(mat2, mat2ref);
405
+
406
+ swap_using_evaluator(mat1.block(0, 0, 3, 3), mat2.block(3, 3, 3, 3));
407
+ mat1ref.block(0, 0, 3, 3).swap(mat2ref.block(3, 3, 3, 3));
408
+ VERIFY_IS_APPROX(mat1, mat1ref);
409
+ VERIFY_IS_APPROX(mat2, mat2ref);
410
+
411
+ swap_using_evaluator(mat1.row(2), mat2.col(3).transpose());
412
+ mat1.row(2).swap(mat2.col(3).transpose());
413
+ VERIFY_IS_APPROX(mat1, mat1ref);
414
+ VERIFY_IS_APPROX(mat2, mat2ref);
415
+ }
416
+
417
+ {
418
+ // test compound assignment
419
+ const Matrix4d mat_const = Matrix4d::Random();
420
+ Matrix4d mat, mat_ref;
421
+ mat = mat_ref = Matrix4d::Identity();
422
+ add_assign_using_evaluator(mat, mat_const);
423
+ mat_ref += mat_const;
424
+ VERIFY_IS_APPROX(mat, mat_ref);
425
+
426
+ subtract_assign_using_evaluator(mat.row(1), 2*mat.row(2));
427
+ mat_ref.row(1) -= 2*mat_ref.row(2);
428
+ VERIFY_IS_APPROX(mat, mat_ref);
429
+
430
+ const ArrayXXf arr_const = ArrayXXf::Random(5,3);
431
+ ArrayXXf arr, arr_ref;
432
+ arr = arr_ref = ArrayXXf::Constant(5, 3, 0.5);
433
+ multiply_assign_using_evaluator(arr, arr_const);
434
+ arr_ref *= arr_const;
435
+ VERIFY_IS_APPROX(arr, arr_ref);
436
+
437
+ divide_assign_using_evaluator(arr.row(1), arr.row(2) + 1);
438
+ arr_ref.row(1) /= (arr_ref.row(2) + 1);
439
+ VERIFY_IS_APPROX(arr, arr_ref);
440
+ }
441
+
442
+ {
443
+ // test triangular shapes
444
+ MatrixXd A = MatrixXd::Random(6,6), B(6,6), C(6,6), D(6,6);
445
+ A.setRandom();B.setRandom();
446
+ VERIFY_IS_APPROX_EVALUATOR2(B, A.triangularView<Upper>(), MatrixXd(A.triangularView<Upper>()));
447
+
448
+ A.setRandom();B.setRandom();
449
+ VERIFY_IS_APPROX_EVALUATOR2(B, A.triangularView<UnitLower>(), MatrixXd(A.triangularView<UnitLower>()));
450
+
451
+ A.setRandom();B.setRandom();
452
+ VERIFY_IS_APPROX_EVALUATOR2(B, A.triangularView<UnitUpper>(), MatrixXd(A.triangularView<UnitUpper>()));
453
+
454
+ A.setRandom();B.setRandom();
455
+ C = B; C.triangularView<Upper>() = A;
456
+ copy_using_evaluator(B.triangularView<Upper>(), A);
457
+ VERIFY(B.isApprox(C) && "copy_using_evaluator(B.triangularView<Upper>(), A)");
458
+
459
+ A.setRandom();B.setRandom();
460
+ C = B; C.triangularView<Lower>() = A.triangularView<Lower>();
461
+ copy_using_evaluator(B.triangularView<Lower>(), A.triangularView<Lower>());
462
+ VERIFY(B.isApprox(C) && "copy_using_evaluator(B.triangularView<Lower>(), A.triangularView<Lower>())");
463
+
464
+
465
+ A.setRandom();B.setRandom();
466
+ C = B; C.triangularView<Lower>() = A.triangularView<Upper>().transpose();
467
+ copy_using_evaluator(B.triangularView<Lower>(), A.triangularView<Upper>().transpose());
468
+ VERIFY(B.isApprox(C) && "copy_using_evaluator(B.triangularView<Lower>(), A.triangularView<Lower>().transpose())");
469
+
470
+
471
+ A.setRandom();B.setRandom(); C = B; D = A;
472
+ C.triangularView<Upper>().swap(D.triangularView<Upper>());
473
+ swap_using_evaluator(B.triangularView<Upper>(), A.triangularView<Upper>());
474
+ VERIFY(B.isApprox(C) && "swap_using_evaluator(B.triangularView<Upper>(), A.triangularView<Upper>())");
475
+
476
+
477
+ VERIFY_IS_APPROX_EVALUATOR2(B, prod(A.triangularView<Upper>(),A), MatrixXd(A.triangularView<Upper>()*A));
478
+
479
+ VERIFY_IS_APPROX_EVALUATOR2(B, prod(A.selfadjointView<Upper>(),A), MatrixXd(A.selfadjointView<Upper>()*A));
480
+ }
481
+
482
+ {
483
+ // test diagonal shapes
484
+ VectorXd d = VectorXd::Random(6);
485
+ MatrixXd A = MatrixXd::Random(6,6), B(6,6);
486
+ A.setRandom();B.setRandom();
487
+
488
+ VERIFY_IS_APPROX_EVALUATOR2(B, lazyprod(d.asDiagonal(),A), MatrixXd(d.asDiagonal()*A));
489
+ VERIFY_IS_APPROX_EVALUATOR2(B, lazyprod(A,d.asDiagonal()), MatrixXd(A*d.asDiagonal()));
490
+ }
491
+
492
+ {
493
+ // test CoeffReadCost
494
+ Matrix4d a, b;
495
+ VERIFY_IS_EQUAL( get_cost(a), 1 );
496
+ VERIFY_IS_EQUAL( get_cost(a+b), 3);
497
+ VERIFY_IS_EQUAL( get_cost(2*a+b), 4);
498
+ VERIFY_IS_EQUAL( get_cost(a*b), 1);
499
+ VERIFY_IS_EQUAL( get_cost(a.lazyProduct(b)), 15);
500
+ VERIFY_IS_EQUAL( get_cost(a*(a*b)), 1);
501
+ VERIFY_IS_EQUAL( get_cost(a.lazyProduct(a*b)), 15);
502
+ VERIFY_IS_EQUAL( get_cost(a*(a+b)), 1);
503
+ VERIFY_IS_EQUAL( get_cost(a.lazyProduct(a+b)), 15);
504
+ }
505
+
506
+ // regression test for PR 544 and bug 1622 (introduced in #71609c4)
507
+ {
508
+ // test restricted_packet_assignment with an unaligned destination
509
+ const size_t M = 2;
510
+ const size_t K = 2;
511
+ const size_t N = 5;
512
+ float *destMem = new float[(M*N) + 1];
513
+ float *dest = (internal::UIntPtr(destMem)%EIGEN_MAX_ALIGN_BYTES) == 0 ? destMem+1 : destMem;
514
+
515
+ const Matrix<float, Dynamic, Dynamic, RowMajor> a = Matrix<float, Dynamic, Dynamic, RowMajor>::Random(M, K);
516
+ const Matrix<float, Dynamic, Dynamic, RowMajor> b = Matrix<float, Dynamic, Dynamic, RowMajor>::Random(K, N);
517
+
518
+ Map<Matrix<float, Dynamic, Dynamic, RowMajor> > z(dest, M, N);;
519
+ Product<Matrix<float, Dynamic, Dynamic, RowMajor>, Matrix<float, Dynamic, Dynamic, RowMajor>, LazyProduct> tmp(a,b);
520
+ internal::call_restricted_packet_assignment(z.noalias(), tmp.derived(), internal::assign_op<float, float>());
521
+
522
+ VERIFY_IS_APPROX(z, a*b);
523
+ delete[] destMem;
524
+ }
525
+ }
include/eigen/test/exceptions.cpp ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // This file is part of Eigen, a lightweight C++ template library
2
+ // for linear algebra.
3
+ //
4
+ // Copyright (C) 2011 Gael Guennebaud <gael.guennebaud@inria.fr>
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
+
11
+ // Various sanity tests with exceptions and non trivially copyable scalar type.
12
+ // - no memory leak when a custom scalar type trow an exceptions
13
+ // - todo: complete the list of tests!
14
+
15
+ #define EIGEN_STACK_ALLOCATION_LIMIT 100000000
16
+
17
+ #include "main.h"
18
+ #include "AnnoyingScalar.h"
19
+
20
+ #define CHECK_MEMLEAK(OP) { \
21
+ AnnoyingScalar::countdown = 100; \
22
+ int before = AnnoyingScalar::instances; \
23
+ bool exception_thrown = false; \
24
+ try { OP; } \
25
+ catch (my_exception) { \
26
+ exception_thrown = true; \
27
+ VERIFY(AnnoyingScalar::instances==before && "memory leak detected in " && EIGEN_MAKESTRING(OP)); \
28
+ } \
29
+ VERIFY( (AnnoyingScalar::dont_throw) || (exception_thrown && " no exception thrown in " && EIGEN_MAKESTRING(OP)) ); \
30
+ }
31
+
32
+ EIGEN_DECLARE_TEST(exceptions)
33
+ {
34
+ typedef Eigen::Matrix<AnnoyingScalar,Dynamic,1> VectorType;
35
+ typedef Eigen::Matrix<AnnoyingScalar,Dynamic,Dynamic> MatrixType;
36
+
37
+ {
38
+ AnnoyingScalar::dont_throw = false;
39
+ int n = 50;
40
+ VectorType v0(n), v1(n);
41
+ MatrixType m0(n,n), m1(n,n), m2(n,n);
42
+ v0.setOnes(); v1.setOnes();
43
+ m0.setOnes(); m1.setOnes(); m2.setOnes();
44
+ CHECK_MEMLEAK(v0 = m0 * m1 * v1);
45
+ CHECK_MEMLEAK(m2 = m0 * m1 * m2);
46
+ CHECK_MEMLEAK((v0+v1).dot(v0+v1));
47
+ }
48
+ VERIFY(AnnoyingScalar::instances==0 && "global memory leak detected in " && EIGEN_MAKESTRING(OP));
49
+ }
include/eigen/test/fastmath.cpp ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // This file is part of Eigen, a lightweight C++ template library
2
+ // for linear algebra.
3
+ //
4
+ // Copyright (C) 2015 Gael Guennebaud <gael.guennebaud@inria.fr>
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
+ void check(bool b, bool ref)
13
+ {
14
+ std::cout << b;
15
+ if(b==ref)
16
+ std::cout << " OK ";
17
+ else
18
+ std::cout << " BAD ";
19
+ }
20
+
21
+ #if EIGEN_COMP_MSVC && EIGEN_COMP_MSVC < 1800
22
+ namespace std {
23
+ template<typename T> bool (isfinite)(T x) { return _finite(x); }
24
+ template<typename T> bool (isnan)(T x) { return _isnan(x); }
25
+ template<typename T> bool (isinf)(T x) { return _fpclass(x)==_FPCLASS_NINF || _fpclass(x)==_FPCLASS_PINF; }
26
+ }
27
+ #endif
28
+
29
+ template<typename T>
30
+ void check_inf_nan(bool dryrun) {
31
+ Matrix<T,Dynamic,1> m(10);
32
+ m.setRandom();
33
+ m(3) = std::numeric_limits<T>::quiet_NaN();
34
+
35
+ if(dryrun)
36
+ {
37
+ std::cout << "std::isfinite(" << m(3) << ") = "; check((std::isfinite)(m(3)),false); std::cout << " ; numext::isfinite = "; check((numext::isfinite)(m(3)), false); std::cout << "\n";
38
+ std::cout << "std::isinf(" << m(3) << ") = "; check((std::isinf)(m(3)),false); std::cout << " ; numext::isinf = "; check((numext::isinf)(m(3)), false); std::cout << "\n";
39
+ std::cout << "std::isnan(" << m(3) << ") = "; check((std::isnan)(m(3)),true); std::cout << " ; numext::isnan = "; check((numext::isnan)(m(3)), true); std::cout << "\n";
40
+ std::cout << "allFinite: "; check(m.allFinite(), 0); std::cout << "\n";
41
+ std::cout << "hasNaN: "; check(m.hasNaN(), 1); std::cout << "\n";
42
+ std::cout << "\n";
43
+ }
44
+ else
45
+ {
46
+ if( (std::isfinite)(m(3))) g_test_level=1; VERIFY( !(numext::isfinite)(m(3)) ); g_test_level=0;
47
+ if( (std::isinf) (m(3))) g_test_level=1; VERIFY( !(numext::isinf)(m(3)) ); g_test_level=0;
48
+ if(!(std::isnan) (m(3))) g_test_level=1; VERIFY( (numext::isnan)(m(3)) ); g_test_level=0;
49
+ if( (std::isfinite)(m(3))) g_test_level=1; VERIFY( !m.allFinite() ); g_test_level=0;
50
+ if(!(std::isnan) (m(3))) g_test_level=1; VERIFY( m.hasNaN() ); g_test_level=0;
51
+ }
52
+ T hidden_zero = (std::numeric_limits<T>::min)()*(std::numeric_limits<T>::min)();
53
+ m(4) /= hidden_zero;
54
+ if(dryrun)
55
+ {
56
+ std::cout << "std::isfinite(" << m(4) << ") = "; check((std::isfinite)(m(4)),false); std::cout << " ; numext::isfinite = "; check((numext::isfinite)(m(4)), false); std::cout << "\n";
57
+ std::cout << "std::isinf(" << m(4) << ") = "; check((std::isinf)(m(4)),true); std::cout << " ; numext::isinf = "; check((numext::isinf)(m(4)), true); std::cout << "\n";
58
+ std::cout << "std::isnan(" << m(4) << ") = "; check((std::isnan)(m(4)),false); std::cout << " ; numext::isnan = "; check((numext::isnan)(m(4)), false); std::cout << "\n";
59
+ std::cout << "allFinite: "; check(m.allFinite(), 0); std::cout << "\n";
60
+ std::cout << "hasNaN: "; check(m.hasNaN(), 1); std::cout << "\n";
61
+ std::cout << "\n";
62
+ }
63
+ else
64
+ {
65
+ if( (std::isfinite)(m(3))) g_test_level=1; VERIFY( !(numext::isfinite)(m(4)) ); g_test_level=0;
66
+ if(!(std::isinf) (m(3))) g_test_level=1; VERIFY( (numext::isinf)(m(4)) ); g_test_level=0;
67
+ if( (std::isnan) (m(3))) g_test_level=1; VERIFY( !(numext::isnan)(m(4)) ); g_test_level=0;
68
+ if( (std::isfinite)(m(3))) g_test_level=1; VERIFY( !m.allFinite() ); g_test_level=0;
69
+ if(!(std::isnan) (m(3))) g_test_level=1; VERIFY( m.hasNaN() ); g_test_level=0;
70
+ }
71
+ m(3) = 0;
72
+ if(dryrun)
73
+ {
74
+ std::cout << "std::isfinite(" << m(3) << ") = "; check((std::isfinite)(m(3)),true); std::cout << " ; numext::isfinite = "; check((numext::isfinite)(m(3)), true); std::cout << "\n";
75
+ std::cout << "std::isinf(" << m(3) << ") = "; check((std::isinf)(m(3)),false); std::cout << " ; numext::isinf = "; check((numext::isinf)(m(3)), false); std::cout << "\n";
76
+ std::cout << "std::isnan(" << m(3) << ") = "; check((std::isnan)(m(3)),false); std::cout << " ; numext::isnan = "; check((numext::isnan)(m(3)), false); std::cout << "\n";
77
+ std::cout << "allFinite: "; check(m.allFinite(), 0); std::cout << "\n";
78
+ std::cout << "hasNaN: "; check(m.hasNaN(), 0); std::cout << "\n";
79
+ std::cout << "\n\n";
80
+ }
81
+ else
82
+ {
83
+ if(!(std::isfinite)(m(3))) g_test_level=1; VERIFY( (numext::isfinite)(m(3)) ); g_test_level=0;
84
+ if( (std::isinf) (m(3))) g_test_level=1; VERIFY( !(numext::isinf)(m(3)) ); g_test_level=0;
85
+ if( (std::isnan) (m(3))) g_test_level=1; VERIFY( !(numext::isnan)(m(3)) ); g_test_level=0;
86
+ if( (std::isfinite)(m(3))) g_test_level=1; VERIFY( !m.allFinite() ); g_test_level=0;
87
+ if( (std::isnan) (m(3))) g_test_level=1; VERIFY( !m.hasNaN() ); g_test_level=0;
88
+ }
89
+ }
90
+
91
+ EIGEN_DECLARE_TEST(fastmath) {
92
+ std::cout << "*** float *** \n\n"; check_inf_nan<float>(true);
93
+ std::cout << "*** double ***\n\n"; check_inf_nan<double>(true);
94
+ std::cout << "*** long double *** \n\n"; check_inf_nan<long double>(true);
95
+
96
+ check_inf_nan<float>(false);
97
+ check_inf_nan<double>(false);
98
+ check_inf_nan<long double>(false);
99
+ }
include/eigen/test/first_aligned.cpp ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // This file is part of Eigen, a lightweight C++ template library
2
+ // for linear algebra.
3
+ //
4
+ // Copyright (C) 2009 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
+ template<typename Scalar>
13
+ void test_first_aligned_helper(Scalar *array, int size)
14
+ {
15
+ const int packet_size = sizeof(Scalar) * internal::packet_traits<Scalar>::size;
16
+ VERIFY(((size_t(array) + sizeof(Scalar) * internal::first_default_aligned(array, size)) % packet_size) == 0);
17
+ }
18
+
19
+ template<typename Scalar>
20
+ void test_none_aligned_helper(Scalar *array, int size)
21
+ {
22
+ EIGEN_UNUSED_VARIABLE(array);
23
+ EIGEN_UNUSED_VARIABLE(size);
24
+ VERIFY(internal::packet_traits<Scalar>::size == 1 || internal::first_default_aligned(array, size) == size);
25
+ }
26
+
27
+ struct some_non_vectorizable_type { float x; };
28
+
29
+ EIGEN_DECLARE_TEST(first_aligned)
30
+ {
31
+ EIGEN_ALIGN16 float array_float[100];
32
+ test_first_aligned_helper(array_float, 50);
33
+ test_first_aligned_helper(array_float+1, 50);
34
+ test_first_aligned_helper(array_float+2, 50);
35
+ test_first_aligned_helper(array_float+3, 50);
36
+ test_first_aligned_helper(array_float+4, 50);
37
+ test_first_aligned_helper(array_float+5, 50);
38
+
39
+ EIGEN_ALIGN16 double array_double[100];
40
+ test_first_aligned_helper(array_double, 50);
41
+ test_first_aligned_helper(array_double+1, 50);
42
+ test_first_aligned_helper(array_double+2, 50);
43
+
44
+ double *array_double_plus_4_bytes = (double*)(internal::UIntPtr(array_double)+4);
45
+ test_none_aligned_helper(array_double_plus_4_bytes, 50);
46
+ test_none_aligned_helper(array_double_plus_4_bytes+1, 50);
47
+
48
+ some_non_vectorizable_type array_nonvec[100];
49
+ test_first_aligned_helper(array_nonvec, 100);
50
+ test_none_aligned_helper(array_nonvec, 100);
51
+ }
include/eigen/test/geo_alignedbox.cpp ADDED
@@ -0,0 +1,531 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // This file is part of Eigen, a lightweight C++ template library
2
+ // for linear algebra.
3
+ //
4
+ // Copyright (C) 2008-2009 Gael Guennebaud <gael.guennebaud@inria.fr>
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
+ #include <Eigen/Geometry>
12
+
13
+ using namespace std;
14
+
15
+ // NOTE the following workaround was needed on some 32 bits builds to kill extra precision of x87 registers.
16
+ // It seems that it is not needed anymore, but let's keep it here, just in case...
17
+
18
+ template<typename T> EIGEN_DONT_INLINE
19
+ void kill_extra_precision(T& /* x */) {
20
+ // This one worked but triggered a warning:
21
+ /* eigen_assert((void*)(&x) != (void*)0); */
22
+ // An alternative could be:
23
+ /* volatile T tmp = x; */
24
+ /* x = tmp; */
25
+ }
26
+
27
+
28
+ template<typename BoxType> void alignedbox(const BoxType& box)
29
+ {
30
+ /* this test covers the following files:
31
+ AlignedBox.h
32
+ */
33
+ typedef typename BoxType::Scalar Scalar;
34
+ typedef NumTraits<Scalar> ScalarTraits;
35
+ typedef typename ScalarTraits::Real RealScalar;
36
+ typedef Matrix<Scalar, BoxType::AmbientDimAtCompileTime, 1> VectorType;
37
+
38
+ const Index dim = box.dim();
39
+
40
+ VectorType p0 = VectorType::Random(dim);
41
+ VectorType p1 = VectorType::Random(dim);
42
+ while( p1 == p0 ){
43
+ p1 = VectorType::Random(dim); }
44
+ RealScalar s1 = internal::random<RealScalar>(0,1);
45
+
46
+ BoxType b0(dim);
47
+ BoxType b1(VectorType::Random(dim),VectorType::Random(dim));
48
+ BoxType b2;
49
+
50
+ kill_extra_precision(b1);
51
+ kill_extra_precision(p0);
52
+ kill_extra_precision(p1);
53
+
54
+ b0.extend(p0);
55
+ b0.extend(p1);
56
+ VERIFY(b0.contains(p0*s1+(Scalar(1)-s1)*p1));
57
+ VERIFY(b0.contains(b0.center()));
58
+ VERIFY_IS_APPROX(b0.center(),(p0+p1)/Scalar(2));
59
+
60
+ (b2 = b0).extend(b1);
61
+ VERIFY(b2.contains(b0));
62
+ VERIFY(b2.contains(b1));
63
+ VERIFY_IS_APPROX(b2.clamp(b0), b0);
64
+
65
+ // intersection
66
+ BoxType box1(VectorType::Random(dim));
67
+ box1.extend(VectorType::Random(dim));
68
+ BoxType box2(VectorType::Random(dim));
69
+ box2.extend(VectorType::Random(dim));
70
+
71
+ VERIFY(box1.intersects(box2) == !box1.intersection(box2).isEmpty());
72
+
73
+ // alignment -- make sure there is no memory alignment assertion
74
+ BoxType *bp0 = new BoxType(dim);
75
+ BoxType *bp1 = new BoxType(dim);
76
+ bp0->extend(*bp1);
77
+ delete bp0;
78
+ delete bp1;
79
+
80
+ // sampling
81
+ for( int i=0; i<10; ++i )
82
+ {
83
+ VectorType r = b0.sample();
84
+ VERIFY(b0.contains(r));
85
+ }
86
+
87
+ }
88
+
89
+ template<typename BoxType> void alignedboxTranslatable(const BoxType& box)
90
+ {
91
+ typedef typename BoxType::Scalar Scalar;
92
+ typedef Matrix<Scalar, BoxType::AmbientDimAtCompileTime, 1> VectorType;
93
+ typedef Transform<Scalar, BoxType::AmbientDimAtCompileTime, Isometry> IsometryTransform;
94
+ typedef Transform<Scalar, BoxType::AmbientDimAtCompileTime, Affine> AffineTransform;
95
+
96
+ alignedbox(box);
97
+
98
+ const VectorType Ones = VectorType::Ones();
99
+ const VectorType UnitX = VectorType::UnitX();
100
+ const Index dim = box.dim();
101
+
102
+ // box((-1, -1, -1), (1, 1, 1))
103
+ BoxType a(-Ones, Ones);
104
+
105
+ VERIFY_IS_APPROX(a.sizes(), Ones * Scalar(2));
106
+
107
+ BoxType b = a;
108
+ VectorType translate = Ones;
109
+ translate[0] = Scalar(2);
110
+ b.translate(translate);
111
+ // translate by (2, 1, 1) -> box((1, 0, 0), (3, 2, 2))
112
+
113
+ VERIFY_IS_APPROX(b.sizes(), Ones * Scalar(2));
114
+ VERIFY_IS_APPROX((b.min)(), UnitX);
115
+ VERIFY_IS_APPROX((b.max)(), Ones * Scalar(2) + UnitX);
116
+
117
+ // Test transform
118
+
119
+ IsometryTransform tf = IsometryTransform::Identity();
120
+ tf.translation() = -translate;
121
+
122
+ BoxType c = b.transformed(tf);
123
+ // translate by (-2, -1, -1) -> box((-1, -1, -1), (1, 1, 1))
124
+ VERIFY_IS_APPROX(c.sizes(), a.sizes());
125
+ VERIFY_IS_APPROX((c.min)(), (a.min)());
126
+ VERIFY_IS_APPROX((c.max)(), (a.max)());
127
+
128
+ c.transform(tf);
129
+ // translate by (-2, -1, -1) -> box((-3, -2, -2), (-1, 0, 0))
130
+ VERIFY_IS_APPROX(c.sizes(), a.sizes());
131
+ VERIFY_IS_APPROX((c.min)(), Ones * Scalar(-2) - UnitX);
132
+ VERIFY_IS_APPROX((c.max)(), -UnitX);
133
+
134
+ // Scaling
135
+
136
+ AffineTransform atf = AffineTransform::Identity();
137
+ atf.scale(Scalar(3));
138
+ c.transform(atf);
139
+ // scale by 3 -> box((-9, -6, -6), (-3, 0, 0))
140
+ VERIFY_IS_APPROX(c.sizes(), Scalar(3) * a.sizes());
141
+ VERIFY_IS_APPROX((c.min)(), Ones * Scalar(-6) - UnitX * Scalar(3));
142
+ VERIFY_IS_APPROX((c.max)(), UnitX * Scalar(-3));
143
+
144
+ atf = AffineTransform::Identity();
145
+ atf.scale(Scalar(-3));
146
+ c.transform(atf);
147
+ // scale by -3 -> box((27, 18, 18), (9, 0, 0))
148
+ VERIFY_IS_APPROX(c.sizes(), Scalar(9) * a.sizes());
149
+ VERIFY_IS_APPROX((c.min)(), UnitX * Scalar(9));
150
+ VERIFY_IS_APPROX((c.max)(), Ones * Scalar(18) + UnitX * Scalar(9));
151
+
152
+ // Check identity transform within numerical precision.
153
+ BoxType transformedC = c.transformed(IsometryTransform::Identity());
154
+ VERIFY_IS_APPROX(transformedC, c);
155
+
156
+ for (size_t i = 0; i < 10; ++i)
157
+ {
158
+ VectorType minCorner;
159
+ VectorType maxCorner;
160
+ for (Index d = 0; d < dim; ++d)
161
+ {
162
+ minCorner[d] = internal::random<Scalar>(-10,10);
163
+ maxCorner[d] = minCorner[d] + internal::random<Scalar>(0, 10);
164
+ }
165
+
166
+ c = BoxType(minCorner, maxCorner);
167
+
168
+ translate = VectorType::Random();
169
+ c.translate(translate);
170
+
171
+ VERIFY_IS_APPROX((c.min)(), minCorner + translate);
172
+ VERIFY_IS_APPROX((c.max)(), maxCorner + translate);
173
+ }
174
+ }
175
+
176
+ template<typename Scalar, typename Rotation>
177
+ Rotation rotate2D(Scalar angle) {
178
+ return Rotation2D<Scalar>(angle);
179
+ }
180
+
181
+ template<typename Scalar, typename Rotation>
182
+ Rotation rotate2DIntegral(typename NumTraits<Scalar>::NonInteger angle) {
183
+ typedef typename NumTraits<Scalar>::NonInteger NonInteger;
184
+ return Rotation2D<NonInteger>(angle).toRotationMatrix().
185
+ template cast<Scalar>();
186
+ }
187
+
188
+ template<typename Scalar, typename Rotation>
189
+ Rotation rotate3DZAxis(Scalar angle) {
190
+ return AngleAxis<Scalar>(angle, Matrix<Scalar, 3, 1>(0, 0, 1));
191
+ }
192
+
193
+ template<typename Scalar, typename Rotation>
194
+ Rotation rotate3DZAxisIntegral(typename NumTraits<Scalar>::NonInteger angle) {
195
+ typedef typename NumTraits<Scalar>::NonInteger NonInteger;
196
+ return AngleAxis<NonInteger>(angle, Matrix<NonInteger, 3, 1>(0, 0, 1)).
197
+ toRotationMatrix().template cast<Scalar>();
198
+ }
199
+
200
+ template<typename Scalar, typename Rotation>
201
+ Rotation rotate4DZWAxis(Scalar angle) {
202
+ Rotation result = Matrix<Scalar, 4, 4>::Identity();
203
+ result.block(0, 0, 3, 3) = rotate3DZAxis<Scalar, AngleAxisd>(angle).toRotationMatrix();
204
+ return result;
205
+ }
206
+
207
+ template <typename MatrixType>
208
+ MatrixType randomRotationMatrix()
209
+ {
210
+ // algorithm from
211
+ // https://www.isprs-ann-photogramm-remote-sens-spatial-inf-sci.net/III-7/103/2016/isprs-annals-III-7-103-2016.pdf
212
+ const MatrixType rand = MatrixType::Random();
213
+ const MatrixType q = rand.householderQr().householderQ();
214
+ const JacobiSVD<MatrixType> svd = q.jacobiSvd(ComputeFullU | ComputeFullV);
215
+ const typename MatrixType::Scalar det = (svd.matrixU() * svd.matrixV().transpose()).determinant();
216
+ MatrixType diag = rand.Identity();
217
+ diag(MatrixType::RowsAtCompileTime - 1, MatrixType::ColsAtCompileTime - 1) = det;
218
+ const MatrixType rotation = svd.matrixU() * diag * svd.matrixV().transpose();
219
+ return rotation;
220
+ }
221
+
222
+ template <typename Scalar, int Dim>
223
+ Matrix<Scalar, Dim, (1<<Dim)> boxGetCorners(const Matrix<Scalar, Dim, 1>& min_, const Matrix<Scalar, Dim, 1>& max_)
224
+ {
225
+ Matrix<Scalar, Dim, (1<<Dim) > result;
226
+ for(Index i=0; i<(1<<Dim); ++i)
227
+ {
228
+ for(Index j=0; j<Dim; ++j)
229
+ result(j,i) = (i & (1<<j)) ? min_(j) : max_(j);
230
+ }
231
+ return result;
232
+ }
233
+
234
+ template<typename BoxType, typename Rotation> void alignedboxRotatable(
235
+ const BoxType& box,
236
+ Rotation (*rotate)(typename NumTraits<typename BoxType::Scalar>::NonInteger /*_angle*/))
237
+ {
238
+ alignedboxTranslatable(box);
239
+
240
+ typedef typename BoxType::Scalar Scalar;
241
+ typedef typename NumTraits<Scalar>::NonInteger NonInteger;
242
+ typedef Matrix<Scalar, BoxType::AmbientDimAtCompileTime, 1> VectorType;
243
+ typedef Transform<Scalar, BoxType::AmbientDimAtCompileTime, Isometry> IsometryTransform;
244
+ typedef Transform<Scalar, BoxType::AmbientDimAtCompileTime, Affine> AffineTransform;
245
+
246
+ const VectorType Zero = VectorType::Zero();
247
+ const VectorType Ones = VectorType::Ones();
248
+ const VectorType UnitX = VectorType::UnitX();
249
+ const VectorType UnitY = VectorType::UnitY();
250
+ // this is vector (0, 0, -1, -1, -1, ...), i.e. with zeros at first and second dimensions
251
+ const VectorType UnitZ = Ones - UnitX - UnitY;
252
+
253
+ // in this kind of comments the 3D case values will be illustrated
254
+ // box((-1, -1, -1), (1, 1, 1))
255
+ BoxType a(-Ones, Ones);
256
+
257
+ // to allow templating this test for both 2D and 3D cases, we always set all
258
+ // but the first coordinate to the same value; so basically 3D case works as
259
+ // if you were looking at the scene from top
260
+
261
+ VectorType minPoint = -2 * Ones;
262
+ minPoint[0] = -3;
263
+ VectorType maxPoint = Zero;
264
+ maxPoint[0] = -1;
265
+ BoxType c(minPoint, maxPoint);
266
+ // box((-3, -2, -2), (-1, 0, 0))
267
+
268
+ IsometryTransform tf2 = IsometryTransform::Identity();
269
+ // for some weird reason the following statement has to be put separate from
270
+ // the following rotate call, otherwise precision problems arise...
271
+ Rotation rot = rotate(NonInteger(EIGEN_PI));
272
+ tf2.rotate(rot);
273
+
274
+ c.transform(tf2);
275
+ // rotate by 180 deg around origin -> box((1, 0, -2), (3, 2, 0))
276
+
277
+ VERIFY_IS_APPROX(c.sizes(), a.sizes());
278
+ VERIFY_IS_APPROX((c.min)(), UnitX - UnitZ * Scalar(2));
279
+ VERIFY_IS_APPROX((c.max)(), UnitX * Scalar(3) + UnitY * Scalar(2));
280
+
281
+ rot = rotate(NonInteger(EIGEN_PI / 2));
282
+ tf2.setIdentity();
283
+ tf2.rotate(rot);
284
+
285
+ c.transform(tf2);
286
+ // rotate by 90 deg around origin -> box((-2, 1, -2), (0, 3, 0))
287
+
288
+ VERIFY_IS_APPROX(c.sizes(), a.sizes());
289
+ VERIFY_IS_APPROX((c.min)(), Ones * Scalar(-2) + UnitY * Scalar(3));
290
+ VERIFY_IS_APPROX((c.max)(), UnitY * Scalar(3));
291
+
292
+ // box((-1, -1, -1), (1, 1, 1))
293
+ AffineTransform atf = AffineTransform::Identity();
294
+ atf.linearExt()(0, 1) = Scalar(1);
295
+ c = BoxType(-Ones, Ones);
296
+ c.transform(atf);
297
+ // 45 deg shear in x direction -> box((-2, -1, -1), (2, 1, 1))
298
+
299
+ VERIFY_IS_APPROX(c.sizes(), Ones * Scalar(2) + UnitX * Scalar(2));
300
+ VERIFY_IS_APPROX((c.min)(), -Ones - UnitX);
301
+ VERIFY_IS_APPROX((c.max)(), Ones + UnitX);
302
+ }
303
+
304
+ template<typename BoxType, typename Rotation> void alignedboxNonIntegralRotatable(
305
+ const BoxType& box,
306
+ Rotation (*rotate)(typename NumTraits<typename BoxType::Scalar>::NonInteger /*_angle*/))
307
+ {
308
+ alignedboxRotatable(box, rotate);
309
+
310
+ typedef typename BoxType::Scalar Scalar;
311
+ typedef typename NumTraits<Scalar>::NonInteger NonInteger;
312
+ enum { Dim = BoxType::AmbientDimAtCompileTime };
313
+ typedef Matrix<Scalar, Dim, 1> VectorType;
314
+ typedef Matrix<Scalar, Dim, (1 << Dim)> CornersType;
315
+ typedef Transform<Scalar, Dim, Isometry> IsometryTransform;
316
+ typedef Transform<Scalar, Dim, Affine> AffineTransform;
317
+
318
+ const Index dim = box.dim();
319
+ const VectorType Zero = VectorType::Zero();
320
+ const VectorType Ones = VectorType::Ones();
321
+
322
+ VectorType minPoint = -2 * Ones;
323
+ minPoint[1] = 1;
324
+ VectorType maxPoint = Zero;
325
+ maxPoint[1] = 3;
326
+ BoxType c(minPoint, maxPoint);
327
+ // ((-2, 1, -2), (0, 3, 0))
328
+
329
+ VectorType cornerBL = (c.min)();
330
+ VectorType cornerTR = (c.max)();
331
+ VectorType cornerBR = (c.min)(); cornerBR[0] = cornerTR[0];
332
+ VectorType cornerTL = (c.max)(); cornerTL[0] = cornerBL[0];
333
+
334
+ NonInteger angle = NonInteger(EIGEN_PI/3);
335
+ Rotation rot = rotate(angle);
336
+ IsometryTransform tf2;
337
+ tf2.setIdentity();
338
+ tf2.rotate(rot);
339
+
340
+ c.transform(tf2);
341
+ // rotate by 60 deg -> box((-3.59, -1.23, -2), (-0.86, 1.5, 0))
342
+
343
+ cornerBL = tf2 * cornerBL;
344
+ cornerBR = tf2 * cornerBR;
345
+ cornerTL = tf2 * cornerTL;
346
+ cornerTR = tf2 * cornerTR;
347
+
348
+ VectorType minCorner = Ones * Scalar(-2);
349
+ VectorType maxCorner = Zero;
350
+ minCorner[0] = (min)((min)(cornerBL[0], cornerBR[0]), (min)(cornerTL[0], cornerTR[0]));
351
+ maxCorner[0] = (max)((max)(cornerBL[0], cornerBR[0]), (max)(cornerTL[0], cornerTR[0]));
352
+ minCorner[1] = (min)((min)(cornerBL[1], cornerBR[1]), (min)(cornerTL[1], cornerTR[1]));
353
+ maxCorner[1] = (max)((max)(cornerBL[1], cornerBR[1]), (max)(cornerTL[1], cornerTR[1]));
354
+
355
+ for (Index d = 2; d < dim; ++d)
356
+ VERIFY_IS_APPROX(c.sizes()[d], Scalar(2));
357
+
358
+ VERIFY_IS_APPROX((c.min)(), minCorner);
359
+ VERIFY_IS_APPROX((c.max)(), maxCorner);
360
+
361
+ VectorType minCornerValue = Ones * Scalar(-2);
362
+ VectorType maxCornerValue = Zero;
363
+ minCornerValue[0] = Scalar(Scalar(-sqrt(2*2 + 3*3)) * Scalar(cos(Scalar(atan(2.0/3.0)) - angle/2)));
364
+ minCornerValue[1] = Scalar(Scalar(-sqrt(1*1 + 2*2)) * Scalar(sin(Scalar(atan(2.0/1.0)) - angle/2)));
365
+ maxCornerValue[0] = Scalar(-sin(angle));
366
+ maxCornerValue[1] = Scalar(3 * cos(angle));
367
+ VERIFY_IS_APPROX((c.min)(), minCornerValue);
368
+ VERIFY_IS_APPROX((c.max)(), maxCornerValue);
369
+
370
+ // randomized test - translate and rotate the box and compare to a box made of transformed vertices
371
+ for (size_t i = 0; i < 10; ++i)
372
+ {
373
+ for (Index d = 0; d < dim; ++d)
374
+ {
375
+ minCorner[d] = internal::random<Scalar>(-10,10);
376
+ maxCorner[d] = minCorner[d] + internal::random<Scalar>(0, 10);
377
+ }
378
+
379
+ c = BoxType(minCorner, maxCorner);
380
+
381
+ CornersType corners = boxGetCorners(minCorner, maxCorner);
382
+
383
+ typename AffineTransform::LinearMatrixType rotation =
384
+ randomRotationMatrix<typename AffineTransform::LinearMatrixType>();
385
+
386
+ tf2.setIdentity();
387
+ tf2.rotate(rotation);
388
+ tf2.translate(VectorType::Random());
389
+
390
+ c.transform(tf2);
391
+ corners = tf2 * corners;
392
+
393
+ minCorner = corners.rowwise().minCoeff();
394
+ maxCorner = corners.rowwise().maxCoeff();
395
+
396
+ VERIFY_IS_APPROX((c.min)(), minCorner);
397
+ VERIFY_IS_APPROX((c.max)(), maxCorner);
398
+ }
399
+
400
+ // randomized test - transform the box with a random affine matrix and compare to a box made of transformed vertices
401
+ for (size_t i = 0; i < 10; ++i)
402
+ {
403
+ for (Index d = 0; d < dim; ++d)
404
+ {
405
+ minCorner[d] = internal::random<Scalar>(-10,10);
406
+ maxCorner[d] = minCorner[d] + internal::random<Scalar>(0, 10);
407
+ }
408
+
409
+ c = BoxType(minCorner, maxCorner);
410
+
411
+ CornersType corners = boxGetCorners(minCorner, maxCorner);
412
+
413
+ AffineTransform atf = AffineTransform::Identity();
414
+ atf.linearExt() = AffineTransform::LinearPart::Random();
415
+ atf.translate(VectorType::Random());
416
+
417
+ c.transform(atf);
418
+ corners = atf * corners;
419
+
420
+ minCorner = corners.rowwise().minCoeff();
421
+ maxCorner = corners.rowwise().maxCoeff();
422
+
423
+ VERIFY_IS_APPROX((c.min)(), minCorner);
424
+ VERIFY_IS_APPROX((c.max)(), maxCorner);
425
+ }
426
+ }
427
+
428
+ template<typename BoxType>
429
+ void alignedboxCastTests(const BoxType& box)
430
+ {
431
+ // casting
432
+ typedef typename BoxType::Scalar Scalar;
433
+ typedef Matrix<Scalar, BoxType::AmbientDimAtCompileTime, 1> VectorType;
434
+
435
+ const Index dim = box.dim();
436
+
437
+ VectorType p0 = VectorType::Random(dim);
438
+ VectorType p1 = VectorType::Random(dim);
439
+
440
+ BoxType b0(dim);
441
+
442
+ b0.extend(p0);
443
+ b0.extend(p1);
444
+
445
+ const int Dim = BoxType::AmbientDimAtCompileTime;
446
+ typedef typename GetDifferentType<Scalar>::type OtherScalar;
447
+ AlignedBox<OtherScalar,Dim> hp1f = b0.template cast<OtherScalar>();
448
+ VERIFY_IS_APPROX(hp1f.template cast<Scalar>(),b0);
449
+ AlignedBox<Scalar,Dim> hp1d = b0.template cast<Scalar>();
450
+ VERIFY_IS_APPROX(hp1d.template cast<Scalar>(),b0);
451
+ }
452
+
453
+
454
+ void specificTest1()
455
+ {
456
+ Vector2f m; m << -1.0f, -2.0f;
457
+ Vector2f M; M << 1.0f, 5.0f;
458
+
459
+ typedef AlignedBox2f BoxType;
460
+ BoxType box( m, M );
461
+
462
+ Vector2f sides = M-m;
463
+ VERIFY_IS_APPROX(sides, box.sizes() );
464
+ VERIFY_IS_APPROX(sides[1], box.sizes()[1] );
465
+ VERIFY_IS_APPROX(sides[1], box.sizes().maxCoeff() );
466
+ VERIFY_IS_APPROX(sides[0], box.sizes().minCoeff() );
467
+
468
+ VERIFY_IS_APPROX( 14.0f, box.volume() );
469
+ VERIFY_IS_APPROX( 53.0f, box.diagonal().squaredNorm() );
470
+ VERIFY_IS_APPROX( std::sqrt( 53.0f ), box.diagonal().norm() );
471
+
472
+ VERIFY_IS_APPROX( m, box.corner( BoxType::BottomLeft ) );
473
+ VERIFY_IS_APPROX( M, box.corner( BoxType::TopRight ) );
474
+ Vector2f bottomRight; bottomRight << M[0], m[1];
475
+ Vector2f topLeft; topLeft << m[0], M[1];
476
+ VERIFY_IS_APPROX( bottomRight, box.corner( BoxType::BottomRight ) );
477
+ VERIFY_IS_APPROX( topLeft, box.corner( BoxType::TopLeft ) );
478
+ }
479
+
480
+
481
+ void specificTest2()
482
+ {
483
+ Vector3i m; m << -1, -2, 0;
484
+ Vector3i M; M << 1, 5, 3;
485
+
486
+ typedef AlignedBox3i BoxType;
487
+ BoxType box( m, M );
488
+
489
+ Vector3i sides = M-m;
490
+ VERIFY_IS_APPROX(sides, box.sizes() );
491
+ VERIFY_IS_APPROX(sides[1], box.sizes()[1] );
492
+ VERIFY_IS_APPROX(sides[1], box.sizes().maxCoeff() );
493
+ VERIFY_IS_APPROX(sides[0], box.sizes().minCoeff() );
494
+
495
+ VERIFY_IS_APPROX( 42, box.volume() );
496
+ VERIFY_IS_APPROX( 62, box.diagonal().squaredNorm() );
497
+
498
+ VERIFY_IS_APPROX( m, box.corner( BoxType::BottomLeftFloor ) );
499
+ VERIFY_IS_APPROX( M, box.corner( BoxType::TopRightCeil ) );
500
+ Vector3i bottomRightFloor; bottomRightFloor << M[0], m[1], m[2];
501
+ Vector3i topLeftFloor; topLeftFloor << m[0], M[1], m[2];
502
+ VERIFY_IS_APPROX( bottomRightFloor, box.corner( BoxType::BottomRightFloor ) );
503
+ VERIFY_IS_APPROX( topLeftFloor, box.corner( BoxType::TopLeftFloor ) );
504
+ }
505
+
506
+
507
+ EIGEN_DECLARE_TEST(geo_alignedbox)
508
+ {
509
+ for(int i = 0; i < g_repeat; i++)
510
+ {
511
+ CALL_SUBTEST_1( (alignedboxNonIntegralRotatable<AlignedBox2f, Rotation2Df>(AlignedBox2f(), &rotate2D)) );
512
+ CALL_SUBTEST_2( alignedboxCastTests(AlignedBox2f()) );
513
+
514
+ CALL_SUBTEST_3( (alignedboxNonIntegralRotatable<AlignedBox3f, AngleAxisf>(AlignedBox3f(), &rotate3DZAxis)) );
515
+ CALL_SUBTEST_4( alignedboxCastTests(AlignedBox3f()) );
516
+
517
+ CALL_SUBTEST_5( (alignedboxNonIntegralRotatable<AlignedBox4d, Matrix4d>(AlignedBox4d(), &rotate4DZWAxis)) );
518
+ CALL_SUBTEST_6( alignedboxCastTests(AlignedBox4d()) );
519
+
520
+ CALL_SUBTEST_7( alignedboxTranslatable(AlignedBox1d()) );
521
+ CALL_SUBTEST_8( alignedboxCastTests(AlignedBox1d()) );
522
+
523
+ CALL_SUBTEST_9( alignedboxTranslatable(AlignedBox1i()) );
524
+ CALL_SUBTEST_10( (alignedboxRotatable<AlignedBox2i, Matrix2i>(AlignedBox2i(), &rotate2DIntegral<int, Matrix2i>)) );
525
+ CALL_SUBTEST_11( (alignedboxRotatable<AlignedBox3i, Matrix3i>(AlignedBox3i(), &rotate3DZAxisIntegral<int, Matrix3i>)) );
526
+
527
+ CALL_SUBTEST_14( alignedbox(AlignedBox<double,Dynamic>(4)) );
528
+ }
529
+ CALL_SUBTEST_12( specificTest1() );
530
+ CALL_SUBTEST_13( specificTest2() );
531
+ }
include/eigen/test/geo_eulerangles.cpp ADDED
@@ -0,0 +1,112 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // This file is part of Eigen, a lightweight C++ template library
2
+ // for linear algebra.
3
+ //
4
+ // Copyright (C) 2008-2012 Gael Guennebaud <gael.guennebaud@inria.fr>
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
+ #include <Eigen/Geometry>
12
+ #include <Eigen/LU>
13
+ #include <Eigen/SVD>
14
+
15
+
16
+ template<typename Scalar>
17
+ void verify_euler(const Matrix<Scalar,3,1>& ea, int i, int j, int k)
18
+ {
19
+ typedef Matrix<Scalar,3,3> Matrix3;
20
+ typedef Matrix<Scalar,3,1> Vector3;
21
+ typedef AngleAxis<Scalar> AngleAxisx;
22
+ using std::abs;
23
+ Matrix3 m(AngleAxisx(ea[0], Vector3::Unit(i)) * AngleAxisx(ea[1], Vector3::Unit(j)) * AngleAxisx(ea[2], Vector3::Unit(k)));
24
+ Vector3 eabis = m.eulerAngles(i, j, k);
25
+ Matrix3 mbis(AngleAxisx(eabis[0], Vector3::Unit(i)) * AngleAxisx(eabis[1], Vector3::Unit(j)) * AngleAxisx(eabis[2], Vector3::Unit(k)));
26
+ VERIFY_IS_APPROX(m, mbis);
27
+ /* If I==K, and ea[1]==0, then there no unique solution. */
28
+ /* The remark apply in the case where I!=K, and |ea[1]| is close to pi/2. */
29
+ if( (i!=k || ea[1]!=0) && (i==k || !internal::isApprox(abs(ea[1]),Scalar(EIGEN_PI/2),test_precision<Scalar>())) )
30
+ VERIFY((ea-eabis).norm() <= test_precision<Scalar>());
31
+
32
+ // approx_or_less_than does not work for 0
33
+ VERIFY(0 < eabis[0] || test_isMuchSmallerThan(eabis[0], Scalar(1)));
34
+ VERIFY_IS_APPROX_OR_LESS_THAN(eabis[0], Scalar(EIGEN_PI));
35
+ VERIFY_IS_APPROX_OR_LESS_THAN(-Scalar(EIGEN_PI), eabis[1]);
36
+ VERIFY_IS_APPROX_OR_LESS_THAN(eabis[1], Scalar(EIGEN_PI));
37
+ VERIFY_IS_APPROX_OR_LESS_THAN(-Scalar(EIGEN_PI), eabis[2]);
38
+ VERIFY_IS_APPROX_OR_LESS_THAN(eabis[2], Scalar(EIGEN_PI));
39
+ }
40
+
41
+ template<typename Scalar> void check_all_var(const Matrix<Scalar,3,1>& ea)
42
+ {
43
+ verify_euler(ea, 0,1,2);
44
+ verify_euler(ea, 0,1,0);
45
+ verify_euler(ea, 0,2,1);
46
+ verify_euler(ea, 0,2,0);
47
+
48
+ verify_euler(ea, 1,2,0);
49
+ verify_euler(ea, 1,2,1);
50
+ verify_euler(ea, 1,0,2);
51
+ verify_euler(ea, 1,0,1);
52
+
53
+ verify_euler(ea, 2,0,1);
54
+ verify_euler(ea, 2,0,2);
55
+ verify_euler(ea, 2,1,0);
56
+ verify_euler(ea, 2,1,2);
57
+ }
58
+
59
+ template<typename Scalar> void eulerangles()
60
+ {
61
+ typedef Matrix<Scalar,3,3> Matrix3;
62
+ typedef Matrix<Scalar,3,1> Vector3;
63
+ typedef Array<Scalar,3,1> Array3;
64
+ typedef Quaternion<Scalar> Quaternionx;
65
+ typedef AngleAxis<Scalar> AngleAxisx;
66
+
67
+ Scalar a = internal::random<Scalar>(-Scalar(EIGEN_PI), Scalar(EIGEN_PI));
68
+ Quaternionx q1;
69
+ q1 = AngleAxisx(a, Vector3::Random().normalized());
70
+ Matrix3 m;
71
+ m = q1;
72
+
73
+ Vector3 ea = m.eulerAngles(0,1,2);
74
+ check_all_var(ea);
75
+ ea = m.eulerAngles(0,1,0);
76
+ check_all_var(ea);
77
+
78
+ // Check with purely random Quaternion:
79
+ q1.coeffs() = Quaternionx::Coefficients::Random().normalized();
80
+ m = q1;
81
+ ea = m.eulerAngles(0,1,2);
82
+ check_all_var(ea);
83
+ ea = m.eulerAngles(0,1,0);
84
+ check_all_var(ea);
85
+
86
+ // Check with random angles in range [0:pi]x[-pi:pi]x[-pi:pi].
87
+ ea = (Array3::Random() + Array3(1,0,0))*Scalar(EIGEN_PI)*Array3(0.5,1,1);
88
+ check_all_var(ea);
89
+
90
+ ea[2] = ea[0] = internal::random<Scalar>(0,Scalar(EIGEN_PI));
91
+ check_all_var(ea);
92
+
93
+ ea[0] = ea[1] = internal::random<Scalar>(0,Scalar(EIGEN_PI));
94
+ check_all_var(ea);
95
+
96
+ ea[1] = 0;
97
+ check_all_var(ea);
98
+
99
+ ea.head(2).setZero();
100
+ check_all_var(ea);
101
+
102
+ ea.setZero();
103
+ check_all_var(ea);
104
+ }
105
+
106
+ EIGEN_DECLARE_TEST(geo_eulerangles)
107
+ {
108
+ for(int i = 0; i < g_repeat; i++) {
109
+ CALL_SUBTEST_1( eulerangles<float>() );
110
+ CALL_SUBTEST_2( eulerangles<double>() );
111
+ }
112
+ }
include/eigen/test/geo_homogeneous.cpp ADDED
@@ -0,0 +1,125 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // This file is part of Eigen, a lightweight C++ template library
2
+ // for linear algebra.
3
+ //
4
+ // Copyright (C) 2009 Gael Guennebaud <gael.guennebaud@inria.fr>
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
+ #include <Eigen/Geometry>
12
+
13
+ template<typename Scalar,int Size> void homogeneous(void)
14
+ {
15
+ /* this test covers the following files:
16
+ Homogeneous.h
17
+ */
18
+
19
+ typedef Matrix<Scalar,Size,Size> MatrixType;
20
+ typedef Matrix<Scalar,Size,1, ColMajor> VectorType;
21
+
22
+ typedef Matrix<Scalar,Size+1,Size> HMatrixType;
23
+ typedef Matrix<Scalar,Size+1,1> HVectorType;
24
+
25
+ typedef Matrix<Scalar,Size,Size+1> T1MatrixType;
26
+ typedef Matrix<Scalar,Size+1,Size+1> T2MatrixType;
27
+ typedef Matrix<Scalar,Size+1,Size> T3MatrixType;
28
+
29
+ VectorType v0 = VectorType::Random(),
30
+ ones = VectorType::Ones();
31
+
32
+ HVectorType hv0 = HVectorType::Random();
33
+
34
+ MatrixType m0 = MatrixType::Random();
35
+
36
+ HMatrixType hm0 = HMatrixType::Random();
37
+
38
+ hv0 << v0, 1;
39
+ VERIFY_IS_APPROX(v0.homogeneous(), hv0);
40
+ VERIFY_IS_APPROX(v0, hv0.hnormalized());
41
+
42
+ VERIFY_IS_APPROX(v0.homogeneous().sum(), hv0.sum());
43
+ VERIFY_IS_APPROX(v0.homogeneous().minCoeff(), hv0.minCoeff());
44
+ VERIFY_IS_APPROX(v0.homogeneous().maxCoeff(), hv0.maxCoeff());
45
+
46
+ hm0 << m0, ones.transpose();
47
+ VERIFY_IS_APPROX(m0.colwise().homogeneous(), hm0);
48
+ VERIFY_IS_APPROX(m0, hm0.colwise().hnormalized());
49
+ hm0.row(Size-1).setRandom();
50
+ for(int j=0; j<Size; ++j)
51
+ m0.col(j) = hm0.col(j).head(Size) / hm0(Size,j);
52
+ VERIFY_IS_APPROX(m0, hm0.colwise().hnormalized());
53
+
54
+ T1MatrixType t1 = T1MatrixType::Random();
55
+ VERIFY_IS_APPROX(t1 * (v0.homogeneous().eval()), t1 * v0.homogeneous());
56
+ VERIFY_IS_APPROX(t1 * (m0.colwise().homogeneous().eval()), t1 * m0.colwise().homogeneous());
57
+
58
+ T2MatrixType t2 = T2MatrixType::Random();
59
+ VERIFY_IS_APPROX(t2 * (v0.homogeneous().eval()), t2 * v0.homogeneous());
60
+ VERIFY_IS_APPROX(t2 * (m0.colwise().homogeneous().eval()), t2 * m0.colwise().homogeneous());
61
+ VERIFY_IS_APPROX(t2 * (v0.homogeneous().asDiagonal()), t2 * hv0.asDiagonal());
62
+ VERIFY_IS_APPROX((v0.homogeneous().asDiagonal()) * t2, hv0.asDiagonal() * t2);
63
+
64
+ VERIFY_IS_APPROX((v0.transpose().rowwise().homogeneous().eval()) * t2,
65
+ v0.transpose().rowwise().homogeneous() * t2);
66
+ VERIFY_IS_APPROX((m0.transpose().rowwise().homogeneous().eval()) * t2,
67
+ m0.transpose().rowwise().homogeneous() * t2);
68
+
69
+ T3MatrixType t3 = T3MatrixType::Random();
70
+ VERIFY_IS_APPROX((v0.transpose().rowwise().homogeneous().eval()) * t3,
71
+ v0.transpose().rowwise().homogeneous() * t3);
72
+ VERIFY_IS_APPROX((m0.transpose().rowwise().homogeneous().eval()) * t3,
73
+ m0.transpose().rowwise().homogeneous() * t3);
74
+
75
+ // test product with a Transform object
76
+ Transform<Scalar, Size, Affine> aff;
77
+ Transform<Scalar, Size, AffineCompact> caff;
78
+ Transform<Scalar, Size, Projective> proj;
79
+ Matrix<Scalar, Size, Dynamic> pts;
80
+ Matrix<Scalar, Size+1, Dynamic> pts1, pts2;
81
+
82
+ aff.affine().setRandom();
83
+ proj = caff = aff;
84
+ pts.setRandom(Size,internal::random<int>(1,20));
85
+
86
+ pts1 = pts.colwise().homogeneous();
87
+ VERIFY_IS_APPROX(aff * pts.colwise().homogeneous(), (aff * pts1).colwise().hnormalized());
88
+ VERIFY_IS_APPROX(caff * pts.colwise().homogeneous(), (caff * pts1).colwise().hnormalized());
89
+ VERIFY_IS_APPROX(proj * pts.colwise().homogeneous(), (proj * pts1));
90
+
91
+ VERIFY_IS_APPROX((aff * pts1).colwise().hnormalized(), aff * pts);
92
+ VERIFY_IS_APPROX((caff * pts1).colwise().hnormalized(), caff * pts);
93
+
94
+ pts2 = pts1;
95
+ pts2.row(Size).setRandom();
96
+ VERIFY_IS_APPROX((aff * pts2).colwise().hnormalized(), aff * pts2.colwise().hnormalized());
97
+ VERIFY_IS_APPROX((caff * pts2).colwise().hnormalized(), caff * pts2.colwise().hnormalized());
98
+ VERIFY_IS_APPROX((proj * pts2).colwise().hnormalized(), (proj * pts2.colwise().hnormalized().colwise().homogeneous()).colwise().hnormalized());
99
+
100
+ // Test combination of homogeneous
101
+
102
+ VERIFY_IS_APPROX( (t2 * v0.homogeneous()).hnormalized(),
103
+ (t2.template topLeftCorner<Size,Size>() * v0 + t2.template topRightCorner<Size,1>())
104
+ / ((t2.template bottomLeftCorner<1,Size>()*v0).value() + t2(Size,Size)) );
105
+
106
+ VERIFY_IS_APPROX( (t2 * pts.colwise().homogeneous()).colwise().hnormalized(),
107
+ (Matrix<Scalar, Size+1, Dynamic>(t2 * pts1).colwise().hnormalized()) );
108
+
109
+ VERIFY_IS_APPROX( (t2 .lazyProduct( v0.homogeneous() )).hnormalized(), (t2 * v0.homogeneous()).hnormalized() );
110
+ VERIFY_IS_APPROX( (t2 .lazyProduct ( pts.colwise().homogeneous() )).colwise().hnormalized(), (t2 * pts1).colwise().hnormalized() );
111
+
112
+ VERIFY_IS_APPROX( (v0.transpose().homogeneous() .lazyProduct( t2 )).hnormalized(), (v0.transpose().homogeneous()*t2).hnormalized() );
113
+ VERIFY_IS_APPROX( (pts.transpose().rowwise().homogeneous() .lazyProduct( t2 )).rowwise().hnormalized(), (pts1.transpose()*t2).rowwise().hnormalized() );
114
+
115
+ VERIFY_IS_APPROX( (t2.template triangularView<Lower>() * v0.homogeneous()).eval(), (t2.template triangularView<Lower>()*hv0) );
116
+ }
117
+
118
+ EIGEN_DECLARE_TEST(geo_homogeneous)
119
+ {
120
+ for(int i = 0; i < g_repeat; i++) {
121
+ CALL_SUBTEST_1(( homogeneous<float,1>() ));
122
+ CALL_SUBTEST_2(( homogeneous<double,3>() ));
123
+ CALL_SUBTEST_3(( homogeneous<double,8>() ));
124
+ }
125
+ }
include/eigen/test/geo_orthomethods.cpp ADDED
@@ -0,0 +1,134 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // This file is part of Eigen, a lightweight C++ template library
2
+ // for linear algebra.
3
+ //
4
+ // Copyright (C) 2008-2009 Gael Guennebaud <gael.guennebaud@inria.fr>
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
+ #include <Eigen/Geometry>
12
+ #include <Eigen/LU>
13
+ #include <Eigen/SVD>
14
+
15
+ /* this test covers the following files:
16
+ Geometry/OrthoMethods.h
17
+ */
18
+
19
+ template<typename Scalar> void orthomethods_3()
20
+ {
21
+ typedef typename NumTraits<Scalar>::Real RealScalar;
22
+ typedef Matrix<Scalar,3,3> Matrix3;
23
+ typedef Matrix<Scalar,3,1> Vector3;
24
+
25
+ typedef Matrix<Scalar,4,1> Vector4;
26
+
27
+ Vector3 v0 = Vector3::Random(),
28
+ v1 = Vector3::Random(),
29
+ v2 = Vector3::Random();
30
+
31
+ // cross product
32
+ VERIFY_IS_MUCH_SMALLER_THAN(v1.cross(v2).dot(v1), Scalar(1));
33
+ VERIFY_IS_MUCH_SMALLER_THAN(v1.dot(v1.cross(v2)), Scalar(1));
34
+ VERIFY_IS_MUCH_SMALLER_THAN(v1.cross(v2).dot(v2), Scalar(1));
35
+ VERIFY_IS_MUCH_SMALLER_THAN(v2.dot(v1.cross(v2)), Scalar(1));
36
+ VERIFY_IS_MUCH_SMALLER_THAN(v1.cross(Vector3::Random()).dot(v1), Scalar(1));
37
+ Matrix3 mat3;
38
+ mat3 << v0.normalized(),
39
+ (v0.cross(v1)).normalized(),
40
+ (v0.cross(v1).cross(v0)).normalized();
41
+ VERIFY(mat3.isUnitary());
42
+
43
+ mat3.setRandom();
44
+ VERIFY_IS_APPROX(v0.cross(mat3*v1), -(mat3*v1).cross(v0));
45
+ VERIFY_IS_APPROX(v0.cross(mat3.lazyProduct(v1)), -(mat3.lazyProduct(v1)).cross(v0));
46
+
47
+ // colwise/rowwise cross product
48
+ mat3.setRandom();
49
+ Vector3 vec3 = Vector3::Random();
50
+ Matrix3 mcross;
51
+ int i = internal::random<int>(0,2);
52
+ mcross = mat3.colwise().cross(vec3);
53
+ VERIFY_IS_APPROX(mcross.col(i), mat3.col(i).cross(vec3));
54
+
55
+ VERIFY_IS_MUCH_SMALLER_THAN((mat3.adjoint() * mat3.colwise().cross(vec3)).diagonal().cwiseAbs().sum(), Scalar(1));
56
+ VERIFY_IS_MUCH_SMALLER_THAN((mat3.adjoint() * mat3.colwise().cross(Vector3::Random())).diagonal().cwiseAbs().sum(), Scalar(1));
57
+
58
+ VERIFY_IS_MUCH_SMALLER_THAN((vec3.adjoint() * mat3.colwise().cross(vec3)).cwiseAbs().sum(), Scalar(1));
59
+ VERIFY_IS_MUCH_SMALLER_THAN((vec3.adjoint() * Matrix3::Random().colwise().cross(vec3)).cwiseAbs().sum(), Scalar(1));
60
+
61
+ mcross = mat3.rowwise().cross(vec3);
62
+ VERIFY_IS_APPROX(mcross.row(i), mat3.row(i).cross(vec3));
63
+
64
+ // cross3
65
+ Vector4 v40 = Vector4::Random(),
66
+ v41 = Vector4::Random(),
67
+ v42 = Vector4::Random();
68
+ v40.w() = v41.w() = v42.w() = 0;
69
+ v42.template head<3>() = v40.template head<3>().cross(v41.template head<3>());
70
+ VERIFY_IS_APPROX(v40.cross3(v41), v42);
71
+ VERIFY_IS_MUCH_SMALLER_THAN(v40.cross3(Vector4::Random()).dot(v40), Scalar(1));
72
+
73
+ // check mixed product
74
+ typedef Matrix<RealScalar, 3, 1> RealVector3;
75
+ RealVector3 rv1 = RealVector3::Random();
76
+ v2 = rv1.template cast<Scalar>();
77
+ VERIFY_IS_APPROX(v1.cross(v2), v1.cross(rv1));
78
+ VERIFY_IS_APPROX(v2.cross(v1), rv1.cross(v1));
79
+ }
80
+
81
+ template<typename Scalar, int Size> void orthomethods(int size=Size)
82
+ {
83
+ typedef typename NumTraits<Scalar>::Real RealScalar;
84
+ typedef Matrix<Scalar,Size,1> VectorType;
85
+ typedef Matrix<Scalar,3,Size> Matrix3N;
86
+ typedef Matrix<Scalar,Size,3> MatrixN3;
87
+ typedef Matrix<Scalar,3,1> Vector3;
88
+
89
+ VectorType v0 = VectorType::Random(size);
90
+
91
+ // unitOrthogonal
92
+ VERIFY_IS_MUCH_SMALLER_THAN(v0.unitOrthogonal().dot(v0), Scalar(1));
93
+ VERIFY_IS_APPROX(v0.unitOrthogonal().norm(), RealScalar(1));
94
+
95
+ if (size>=3)
96
+ {
97
+ v0.template head<2>().setZero();
98
+ v0.tail(size-2).setRandom();
99
+
100
+ VERIFY_IS_MUCH_SMALLER_THAN(v0.unitOrthogonal().dot(v0), Scalar(1));
101
+ VERIFY_IS_APPROX(v0.unitOrthogonal().norm(), RealScalar(1));
102
+ }
103
+
104
+ // colwise/rowwise cross product
105
+ Vector3 vec3 = Vector3::Random();
106
+ int i = internal::random<int>(0,size-1);
107
+
108
+ Matrix3N mat3N(3,size), mcross3N(3,size);
109
+ mat3N.setRandom();
110
+ mcross3N = mat3N.colwise().cross(vec3);
111
+ VERIFY_IS_APPROX(mcross3N.col(i), mat3N.col(i).cross(vec3));
112
+
113
+ MatrixN3 matN3(size,3), mcrossN3(size,3);
114
+ matN3.setRandom();
115
+ mcrossN3 = matN3.rowwise().cross(vec3);
116
+ VERIFY_IS_APPROX(mcrossN3.row(i), matN3.row(i).cross(vec3));
117
+ }
118
+
119
+ EIGEN_DECLARE_TEST(geo_orthomethods)
120
+ {
121
+ for(int i = 0; i < g_repeat; i++) {
122
+ CALL_SUBTEST_1( orthomethods_3<float>() );
123
+ CALL_SUBTEST_2( orthomethods_3<double>() );
124
+ CALL_SUBTEST_4( orthomethods_3<std::complex<double> >() );
125
+ CALL_SUBTEST_1( (orthomethods<float,2>()) );
126
+ CALL_SUBTEST_2( (orthomethods<double,2>()) );
127
+ CALL_SUBTEST_1( (orthomethods<float,3>()) );
128
+ CALL_SUBTEST_2( (orthomethods<double,3>()) );
129
+ CALL_SUBTEST_3( (orthomethods<float,7>()) );
130
+ CALL_SUBTEST_4( (orthomethods<std::complex<double>,8>()) );
131
+ CALL_SUBTEST_5( (orthomethods<float,Dynamic>(36)) );
132
+ CALL_SUBTEST_6( (orthomethods<double,Dynamic>(35)) );
133
+ }
134
+ }
include/eigen/test/geo_parametrizedline.cpp ADDED
@@ -0,0 +1,125 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // This file is part of Eigen, a lightweight C++ template library
2
+ // for linear algebra.
3
+ //
4
+ // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
5
+ // Copyright (C) 2008 Benoit Jacob <jacob.benoit.1@gmail.com>
6
+ //
7
+ // This Source Code Form is subject to the terms of the Mozilla
8
+ // Public License v. 2.0. If a copy of the MPL was not distributed
9
+ // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10
+
11
+ #include "main.h"
12
+ #include <Eigen/Geometry>
13
+ #include <Eigen/LU>
14
+ #include <Eigen/QR>
15
+
16
+ template<typename LineType> void parametrizedline(const LineType& _line)
17
+ {
18
+ /* this test covers the following files:
19
+ ParametrizedLine.h
20
+ */
21
+ using std::abs;
22
+ const Index dim = _line.dim();
23
+ typedef typename LineType::Scalar Scalar;
24
+ typedef typename NumTraits<Scalar>::Real RealScalar;
25
+ typedef Matrix<Scalar, LineType::AmbientDimAtCompileTime, 1> VectorType;
26
+ typedef Hyperplane<Scalar,LineType::AmbientDimAtCompileTime> HyperplaneType;
27
+ typedef Matrix<Scalar, HyperplaneType::AmbientDimAtCompileTime,
28
+ HyperplaneType::AmbientDimAtCompileTime> MatrixType;
29
+
30
+ VectorType p0 = VectorType::Random(dim);
31
+ VectorType p1 = VectorType::Random(dim);
32
+
33
+ VectorType d0 = VectorType::Random(dim).normalized();
34
+
35
+ LineType l0(p0, d0);
36
+
37
+ Scalar s0 = internal::random<Scalar>();
38
+ Scalar s1 = abs(internal::random<Scalar>());
39
+
40
+ VERIFY_IS_MUCH_SMALLER_THAN( l0.distance(p0), RealScalar(1) );
41
+ VERIFY_IS_MUCH_SMALLER_THAN( l0.distance(p0+s0*d0), RealScalar(1) );
42
+ VERIFY_IS_APPROX( (l0.projection(p1)-p1).norm(), l0.distance(p1) );
43
+ VERIFY_IS_MUCH_SMALLER_THAN( l0.distance(l0.projection(p1)), RealScalar(1) );
44
+ VERIFY_IS_APPROX( Scalar(l0.distance((p0+s0*d0) + d0.unitOrthogonal() * s1)), s1 );
45
+
46
+ // casting
47
+ const int Dim = LineType::AmbientDimAtCompileTime;
48
+ typedef typename GetDifferentType<Scalar>::type OtherScalar;
49
+ ParametrizedLine<OtherScalar,Dim> hp1f = l0.template cast<OtherScalar>();
50
+ VERIFY_IS_APPROX(hp1f.template cast<Scalar>(),l0);
51
+ ParametrizedLine<Scalar,Dim> hp1d = l0.template cast<Scalar>();
52
+ VERIFY_IS_APPROX(hp1d.template cast<Scalar>(),l0);
53
+
54
+ // intersections
55
+ VectorType p2 = VectorType::Random(dim);
56
+ VectorType n2 = VectorType::Random(dim).normalized();
57
+ HyperplaneType hp(p2,n2);
58
+ Scalar t = l0.intersectionParameter(hp);
59
+ VectorType pi = l0.pointAt(t);
60
+ VERIFY_IS_MUCH_SMALLER_THAN(hp.signedDistance(pi), RealScalar(1));
61
+ VERIFY_IS_MUCH_SMALLER_THAN(l0.distance(pi), RealScalar(1));
62
+ VERIFY_IS_APPROX(l0.intersectionPoint(hp), pi);
63
+
64
+ // transform
65
+ if (!NumTraits<Scalar>::IsComplex)
66
+ {
67
+ MatrixType rot = MatrixType::Random(dim,dim).householderQr().householderQ();
68
+ DiagonalMatrix<Scalar,LineType::AmbientDimAtCompileTime> scaling(VectorType::Random());
69
+ Translation<Scalar,LineType::AmbientDimAtCompileTime> translation(VectorType::Random());
70
+
71
+ while(scaling.diagonal().cwiseAbs().minCoeff()<RealScalar(1e-4)) scaling.diagonal() = VectorType::Random();
72
+
73
+ LineType l1 = l0;
74
+ VectorType p3 = l0.pointAt(Scalar(1));
75
+ VERIFY_IS_MUCH_SMALLER_THAN( l1.transform(rot).distance(rot * p3), Scalar(1) );
76
+ l1 = l0;
77
+ VERIFY_IS_MUCH_SMALLER_THAN( l1.transform(rot,Isometry).distance(rot * p3), Scalar(1) );
78
+ l1 = l0;
79
+ VERIFY_IS_MUCH_SMALLER_THAN( l1.transform(rot*scaling).distance((rot*scaling) * p3), Scalar(1) );
80
+ l1 = l0;
81
+ VERIFY_IS_MUCH_SMALLER_THAN( l1.transform(rot*scaling*translation)
82
+ .distance((rot*scaling*translation) * p3), Scalar(1) );
83
+ l1 = l0;
84
+ VERIFY_IS_MUCH_SMALLER_THAN( l1.transform(rot*translation,Isometry)
85
+ .distance((rot*translation) * p3), Scalar(1) );
86
+ }
87
+
88
+ }
89
+
90
+ template<typename Scalar> void parametrizedline_alignment()
91
+ {
92
+ typedef ParametrizedLine<Scalar,4,AutoAlign> Line4a;
93
+ typedef ParametrizedLine<Scalar,4,DontAlign> Line4u;
94
+
95
+ EIGEN_ALIGN_MAX Scalar array1[16];
96
+ EIGEN_ALIGN_MAX Scalar array2[16];
97
+ EIGEN_ALIGN_MAX Scalar array3[16+1];
98
+ Scalar* array3u = array3+1;
99
+
100
+ Line4a *p1 = ::new(reinterpret_cast<void*>(array1)) Line4a;
101
+ Line4u *p2 = ::new(reinterpret_cast<void*>(array2)) Line4u;
102
+ Line4u *p3 = ::new(reinterpret_cast<void*>(array3u)) Line4u;
103
+
104
+ p1->origin().setRandom();
105
+ p1->direction().setRandom();
106
+ *p2 = *p1;
107
+ *p3 = *p1;
108
+
109
+ VERIFY_IS_APPROX(p1->origin(), p2->origin());
110
+ VERIFY_IS_APPROX(p1->origin(), p3->origin());
111
+ VERIFY_IS_APPROX(p1->direction(), p2->direction());
112
+ VERIFY_IS_APPROX(p1->direction(), p3->direction());
113
+ }
114
+
115
+ EIGEN_DECLARE_TEST(geo_parametrizedline)
116
+ {
117
+ for(int i = 0; i < g_repeat; i++) {
118
+ CALL_SUBTEST_1( parametrizedline(ParametrizedLine<float,2>()) );
119
+ CALL_SUBTEST_2( parametrizedline(ParametrizedLine<float,3>()) );
120
+ CALL_SUBTEST_2( parametrizedline_alignment<float>() );
121
+ CALL_SUBTEST_3( parametrizedline(ParametrizedLine<double,4>()) );
122
+ CALL_SUBTEST_3( parametrizedline_alignment<double>() );
123
+ CALL_SUBTEST_4( parametrizedline(ParametrizedLine<std::complex<double>,5>()) );
124
+ }
125
+ }
include/eigen/test/geo_quaternion.cpp ADDED
@@ -0,0 +1,332 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // This file is part of Eigen, a lightweight C++ template library
2
+ // for linear algebra.
3
+ //
4
+ // Copyright (C) 2008-2009 Gael Guennebaud <gael.guennebaud@inria.fr>
5
+ // Copyright (C) 2009 Mathieu Gautier <mathieu.gautier@cea.fr>
6
+ //
7
+ // This Source Code Form is subject to the terms of the Mozilla
8
+ // Public License v. 2.0. If a copy of the MPL was not distributed
9
+ // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10
+
11
+ #include "main.h"
12
+ #include <Eigen/Geometry>
13
+ #include <Eigen/LU>
14
+ #include <Eigen/SVD>
15
+ #include "AnnoyingScalar.h"
16
+
17
+ template<typename T> T bounded_acos(T v)
18
+ {
19
+ using std::acos;
20
+ using std::min;
21
+ using std::max;
22
+ return acos((max)(T(-1),(min)(v,T(1))));
23
+ }
24
+
25
+ template<typename QuatType> void check_slerp(const QuatType& q0, const QuatType& q1)
26
+ {
27
+ using std::abs;
28
+ typedef typename QuatType::Scalar Scalar;
29
+ typedef AngleAxis<Scalar> AA;
30
+
31
+ Scalar largeEps = test_precision<Scalar>();
32
+
33
+ Scalar theta_tot = AA(q1*q0.inverse()).angle();
34
+ if(theta_tot>Scalar(EIGEN_PI))
35
+ theta_tot = Scalar(2.)*Scalar(EIGEN_PI)-theta_tot;
36
+ for(Scalar t=0; t<=Scalar(1.001); t+=Scalar(0.1))
37
+ {
38
+ QuatType q = q0.slerp(t,q1);
39
+ Scalar theta = AA(q*q0.inverse()).angle();
40
+ VERIFY(abs(q.norm() - 1) < largeEps);
41
+ if(theta_tot==0) VERIFY(theta_tot==0);
42
+ else VERIFY(abs(theta - t * theta_tot) < largeEps);
43
+ }
44
+ }
45
+
46
+ template<typename Scalar, int Options> void quaternion(void)
47
+ {
48
+ /* this test covers the following files:
49
+ Quaternion.h
50
+ */
51
+ using std::abs;
52
+ typedef Matrix<Scalar,3,1> Vector3;
53
+ typedef Matrix<Scalar,3,3> Matrix3;
54
+ typedef Quaternion<Scalar,Options> Quaternionx;
55
+ typedef AngleAxis<Scalar> AngleAxisx;
56
+
57
+ Scalar largeEps = test_precision<Scalar>();
58
+ if (internal::is_same<Scalar,float>::value)
59
+ largeEps = Scalar(1e-3);
60
+
61
+ Scalar eps = internal::random<Scalar>() * Scalar(1e-2);
62
+
63
+ Vector3 v0 = Vector3::Random(),
64
+ v1 = Vector3::Random(),
65
+ v2 = Vector3::Random(),
66
+ v3 = Vector3::Random();
67
+
68
+ Scalar a = internal::random<Scalar>(-Scalar(EIGEN_PI), Scalar(EIGEN_PI)),
69
+ b = internal::random<Scalar>(-Scalar(EIGEN_PI), Scalar(EIGEN_PI));
70
+
71
+ // Quaternion: Identity(), setIdentity();
72
+ Quaternionx q1, q2;
73
+ q2.setIdentity();
74
+ VERIFY_IS_APPROX(Quaternionx(Quaternionx::Identity()).coeffs(), q2.coeffs());
75
+ q1.coeffs().setRandom();
76
+ VERIFY_IS_APPROX(q1.coeffs(), (q1*q2).coeffs());
77
+
78
+ #ifndef EIGEN_NO_IO
79
+ // Printing
80
+ std::ostringstream ss;
81
+ ss << q2;
82
+ VERIFY(ss.str() == "0i + 0j + 0k + 1");
83
+ #endif
84
+
85
+ // concatenation
86
+ q1 *= q2;
87
+
88
+ q1 = AngleAxisx(a, v0.normalized());
89
+ q2 = AngleAxisx(a, v1.normalized());
90
+
91
+ // angular distance
92
+ Scalar refangle = abs(AngleAxisx(q1.inverse()*q2).angle());
93
+ if (refangle>Scalar(EIGEN_PI))
94
+ refangle = Scalar(2)*Scalar(EIGEN_PI) - refangle;
95
+
96
+ if((q1.coeffs()-q2.coeffs()).norm() > Scalar(10)*largeEps)
97
+ {
98
+ VERIFY_IS_MUCH_SMALLER_THAN(abs(q1.angularDistance(q2) - refangle), Scalar(1));
99
+ }
100
+
101
+ // rotation matrix conversion
102
+ VERIFY_IS_APPROX(q1 * v2, q1.toRotationMatrix() * v2);
103
+ VERIFY_IS_APPROX(q1 * q2 * v2,
104
+ q1.toRotationMatrix() * q2.toRotationMatrix() * v2);
105
+
106
+ VERIFY( (q2*q1).isApprox(q1*q2, largeEps)
107
+ || !(q2 * q1 * v2).isApprox(q1.toRotationMatrix() * q2.toRotationMatrix() * v2));
108
+
109
+ q2 = q1.toRotationMatrix();
110
+ VERIFY_IS_APPROX(q1*v1,q2*v1);
111
+
112
+ Matrix3 rot1(q1);
113
+ VERIFY_IS_APPROX(q1*v1,rot1*v1);
114
+ Quaternionx q3(rot1.transpose()*rot1);
115
+ VERIFY_IS_APPROX(q3*v1,v1);
116
+
117
+
118
+ // angle-axis conversion
119
+ AngleAxisx aa = AngleAxisx(q1);
120
+ VERIFY_IS_APPROX(q1 * v1, Quaternionx(aa) * v1);
121
+
122
+ // Do not execute the test if the rotation angle is almost zero, or
123
+ // the rotation axis and v1 are almost parallel.
124
+ if (abs(aa.angle()) > Scalar(5)*test_precision<Scalar>()
125
+ && (aa.axis() - v1.normalized()).norm() < Scalar(1.99)
126
+ && (aa.axis() + v1.normalized()).norm() < Scalar(1.99))
127
+ {
128
+ VERIFY_IS_NOT_APPROX(q1 * v1, Quaternionx(AngleAxisx(aa.angle()*2,aa.axis())) * v1);
129
+ }
130
+
131
+ // from two vector creation
132
+ VERIFY_IS_APPROX( v2.normalized(),(q2.setFromTwoVectors(v1, v2)*v1).normalized());
133
+ VERIFY_IS_APPROX( v1.normalized(),(q2.setFromTwoVectors(v1, v1)*v1).normalized());
134
+ VERIFY_IS_APPROX(-v1.normalized(),(q2.setFromTwoVectors(v1,-v1)*v1).normalized());
135
+ if (internal::is_same<Scalar,double>::value)
136
+ {
137
+ v3 = (v1.array()+eps).matrix();
138
+ VERIFY_IS_APPROX( v3.normalized(),(q2.setFromTwoVectors(v1, v3)*v1).normalized());
139
+ VERIFY_IS_APPROX(-v3.normalized(),(q2.setFromTwoVectors(v1,-v3)*v1).normalized());
140
+ }
141
+
142
+ // from two vector creation static function
143
+ VERIFY_IS_APPROX( v2.normalized(),(Quaternionx::FromTwoVectors(v1, v2)*v1).normalized());
144
+ VERIFY_IS_APPROX( v1.normalized(),(Quaternionx::FromTwoVectors(v1, v1)*v1).normalized());
145
+ VERIFY_IS_APPROX(-v1.normalized(),(Quaternionx::FromTwoVectors(v1,-v1)*v1).normalized());
146
+ if (internal::is_same<Scalar,double>::value)
147
+ {
148
+ v3 = (v1.array()+eps).matrix();
149
+ VERIFY_IS_APPROX( v3.normalized(),(Quaternionx::FromTwoVectors(v1, v3)*v1).normalized());
150
+ VERIFY_IS_APPROX(-v3.normalized(),(Quaternionx::FromTwoVectors(v1,-v3)*v1).normalized());
151
+ }
152
+
153
+ // inverse and conjugate
154
+ VERIFY_IS_APPROX(q1 * (q1.inverse() * v1), v1);
155
+ VERIFY_IS_APPROX(q1 * (q1.conjugate() * v1), v1);
156
+
157
+ // test casting
158
+ Quaternion<float> q1f = q1.template cast<float>();
159
+ VERIFY_IS_APPROX(q1f.template cast<Scalar>(),q1);
160
+ Quaternion<double> q1d = q1.template cast<double>();
161
+ VERIFY_IS_APPROX(q1d.template cast<Scalar>(),q1);
162
+
163
+ // test bug 369 - improper alignment.
164
+ Quaternionx *q = new Quaternionx;
165
+ delete q;
166
+
167
+ q1 = Quaternionx::UnitRandom();
168
+ q2 = Quaternionx::UnitRandom();
169
+ check_slerp(q1,q2);
170
+
171
+ q1 = AngleAxisx(b, v1.normalized());
172
+ q2 = AngleAxisx(b+Scalar(EIGEN_PI), v1.normalized());
173
+ check_slerp(q1,q2);
174
+
175
+ q1 = AngleAxisx(b, v1.normalized());
176
+ q2 = AngleAxisx(-b, -v1.normalized());
177
+ check_slerp(q1,q2);
178
+
179
+ q1 = Quaternionx::UnitRandom();
180
+ q2.coeffs() = -q1.coeffs();
181
+ check_slerp(q1,q2);
182
+ }
183
+
184
+ template<typename Scalar> void mapQuaternion(void){
185
+ typedef Map<Quaternion<Scalar>, Aligned> MQuaternionA;
186
+ typedef Map<const Quaternion<Scalar>, Aligned> MCQuaternionA;
187
+ typedef Map<Quaternion<Scalar> > MQuaternionUA;
188
+ typedef Map<const Quaternion<Scalar> > MCQuaternionUA;
189
+ typedef Quaternion<Scalar> Quaternionx;
190
+ typedef Matrix<Scalar,3,1> Vector3;
191
+ typedef AngleAxis<Scalar> AngleAxisx;
192
+
193
+ Vector3 v0 = Vector3::Random(),
194
+ v1 = Vector3::Random();
195
+ Scalar a = internal::random<Scalar>(-Scalar(EIGEN_PI), Scalar(EIGEN_PI));
196
+
197
+ EIGEN_ALIGN_MAX Scalar array1[4];
198
+ EIGEN_ALIGN_MAX Scalar array2[4];
199
+ EIGEN_ALIGN_MAX Scalar array3[4+1];
200
+ Scalar* array3unaligned = array3+1;
201
+
202
+ MQuaternionA mq1(array1);
203
+ MCQuaternionA mcq1(array1);
204
+ MQuaternionA mq2(array2);
205
+ MQuaternionUA mq3(array3unaligned);
206
+ MCQuaternionUA mcq3(array3unaligned);
207
+
208
+ // std::cerr << array1 << " " << array2 << " " << array3 << "\n";
209
+ mq1 = AngleAxisx(a, v0.normalized());
210
+ mq2 = mq1;
211
+ mq3 = mq1;
212
+
213
+ Quaternionx q1 = mq1;
214
+ Quaternionx q2 = mq2;
215
+ Quaternionx q3 = mq3;
216
+ Quaternionx q4 = MCQuaternionUA(array3unaligned);
217
+
218
+ VERIFY_IS_APPROX(q1.coeffs(), q2.coeffs());
219
+ VERIFY_IS_APPROX(q1.coeffs(), q3.coeffs());
220
+ VERIFY_IS_APPROX(q4.coeffs(), q3.coeffs());
221
+
222
+ VERIFY_IS_APPROX(mq1 * (mq1.inverse() * v1), v1);
223
+ VERIFY_IS_APPROX(mq1 * (mq1.conjugate() * v1), v1);
224
+
225
+ VERIFY_IS_APPROX(mcq1 * (mcq1.inverse() * v1), v1);
226
+ VERIFY_IS_APPROX(mcq1 * (mcq1.conjugate() * v1), v1);
227
+
228
+ VERIFY_IS_APPROX(mq3 * (mq3.inverse() * v1), v1);
229
+ VERIFY_IS_APPROX(mq3 * (mq3.conjugate() * v1), v1);
230
+
231
+ VERIFY_IS_APPROX(mcq3 * (mcq3.inverse() * v1), v1);
232
+ VERIFY_IS_APPROX(mcq3 * (mcq3.conjugate() * v1), v1);
233
+
234
+ VERIFY_IS_APPROX(mq1*mq2, q1*q2);
235
+ VERIFY_IS_APPROX(mq3*mq2, q3*q2);
236
+ VERIFY_IS_APPROX(mcq1*mq2, q1*q2);
237
+ VERIFY_IS_APPROX(mcq3*mq2, q3*q2);
238
+
239
+ // Bug 1461, compilation issue with Map<const Quat>::w(), and other reference/constness checks:
240
+ VERIFY_IS_APPROX(mcq3.coeffs().x() + mcq3.coeffs().y() + mcq3.coeffs().z() + mcq3.coeffs().w(), mcq3.coeffs().sum());
241
+ VERIFY_IS_APPROX(mcq3.x() + mcq3.y() + mcq3.z() + mcq3.w(), mcq3.coeffs().sum());
242
+ mq3.w() = 1;
243
+ const Quaternionx& cq3(q3);
244
+ VERIFY( &cq3.x() == &q3.x() );
245
+ const MQuaternionUA& cmq3(mq3);
246
+ VERIFY( &cmq3.x() == &mq3.x() );
247
+ // FIXME the following should be ok. The problem is that currently the LValueBit flag
248
+ // is used to determine whether we can return a coeff by reference or not, which is not enough for Map<const ...>.
249
+ //const MCQuaternionUA& cmcq3(mcq3);
250
+ //VERIFY( &cmcq3.x() == &mcq3.x() );
251
+
252
+ // test cast
253
+ {
254
+ Quaternion<float> q1f = mq1.template cast<float>();
255
+ VERIFY_IS_APPROX(q1f.template cast<Scalar>(),mq1);
256
+ Quaternion<double> q1d = mq1.template cast<double>();
257
+ VERIFY_IS_APPROX(q1d.template cast<Scalar>(),mq1);
258
+ }
259
+ }
260
+
261
+ template<typename Scalar> void quaternionAlignment(void){
262
+ typedef Quaternion<Scalar,AutoAlign> QuaternionA;
263
+ typedef Quaternion<Scalar,DontAlign> QuaternionUA;
264
+
265
+ EIGEN_ALIGN_MAX Scalar array1[4];
266
+ EIGEN_ALIGN_MAX Scalar array2[4];
267
+ EIGEN_ALIGN_MAX Scalar array3[4+1];
268
+ Scalar* arrayunaligned = array3+1;
269
+
270
+ QuaternionA *q1 = ::new(reinterpret_cast<void*>(array1)) QuaternionA;
271
+ QuaternionUA *q2 = ::new(reinterpret_cast<void*>(array2)) QuaternionUA;
272
+ QuaternionUA *q3 = ::new(reinterpret_cast<void*>(arrayunaligned)) QuaternionUA;
273
+
274
+ q1->coeffs().setRandom();
275
+ *q2 = *q1;
276
+ *q3 = *q1;
277
+
278
+ VERIFY_IS_APPROX(q1->coeffs(), q2->coeffs());
279
+ VERIFY_IS_APPROX(q1->coeffs(), q3->coeffs());
280
+ }
281
+
282
+ template<typename PlainObjectType> void check_const_correctness(const PlainObjectType&)
283
+ {
284
+ // there's a lot that we can't test here while still having this test compile!
285
+ // the only possible approach would be to run a script trying to compile stuff and checking that it fails.
286
+ // CMake can help with that.
287
+
288
+ // verify that map-to-const don't have LvalueBit
289
+ typedef typename internal::add_const<PlainObjectType>::type ConstPlainObjectType;
290
+ VERIFY( !(internal::traits<Map<ConstPlainObjectType> >::Flags & LvalueBit) );
291
+ VERIFY( !(internal::traits<Map<ConstPlainObjectType, Aligned> >::Flags & LvalueBit) );
292
+ VERIFY( !(Map<ConstPlainObjectType>::Flags & LvalueBit) );
293
+ VERIFY( !(Map<ConstPlainObjectType, Aligned>::Flags & LvalueBit) );
294
+ }
295
+
296
+ #if EIGEN_HAS_RVALUE_REFERENCES
297
+
298
+ // Regression for bug 1573
299
+ struct MovableClass {
300
+ // The following line is a workaround for gcc 4.7 and 4.8 (see bug 1573 comments).
301
+ static_assert(std::is_nothrow_move_constructible<Quaternionf>::value,"");
302
+ MovableClass() = default;
303
+ MovableClass(const MovableClass&) = default;
304
+ MovableClass(MovableClass&&) noexcept = default;
305
+ MovableClass& operator=(const MovableClass&) = default;
306
+ MovableClass& operator=(MovableClass&&) = default;
307
+ Quaternionf m_quat;
308
+ };
309
+
310
+ #endif
311
+
312
+ EIGEN_DECLARE_TEST(geo_quaternion)
313
+ {
314
+ for(int i = 0; i < g_repeat; i++) {
315
+ CALL_SUBTEST_1(( quaternion<float,AutoAlign>() ));
316
+ CALL_SUBTEST_1( check_const_correctness(Quaternionf()) );
317
+ CALL_SUBTEST_1(( quaternion<float,DontAlign>() ));
318
+ CALL_SUBTEST_1(( quaternionAlignment<float>() ));
319
+ CALL_SUBTEST_1( mapQuaternion<float>() );
320
+
321
+ CALL_SUBTEST_2(( quaternion<double,AutoAlign>() ));
322
+ CALL_SUBTEST_2( check_const_correctness(Quaterniond()) );
323
+ CALL_SUBTEST_2(( quaternion<double,DontAlign>() ));
324
+ CALL_SUBTEST_2(( quaternionAlignment<double>() ));
325
+ CALL_SUBTEST_2( mapQuaternion<double>() );
326
+
327
+ #ifndef EIGEN_TEST_ANNOYING_SCALAR_DONT_THROW
328
+ AnnoyingScalar::dont_throw = true;
329
+ #endif
330
+ CALL_SUBTEST_3(( quaternion<AnnoyingScalar,AutoAlign>() ));
331
+ }
332
+ }
include/eigen/test/geo_transformations.cpp ADDED
@@ -0,0 +1,737 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // This file is part of Eigen, a lightweight C++ template library
2
+ // for linear algebra.
3
+ //
4
+ // Copyright (C) 2008-2009 Gael Guennebaud <gael.guennebaud@inria.fr>
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
+ #include <Eigen/Geometry>
12
+ #include <Eigen/LU>
13
+ #include <Eigen/SVD>
14
+
15
+ template<typename T>
16
+ Matrix<T,2,1> angleToVec(T a)
17
+ {
18
+ return Matrix<T,2,1>(std::cos(a), std::sin(a));
19
+ }
20
+
21
+ // This permits to workaround a bug in clang/llvm code generation.
22
+ template<typename T>
23
+ EIGEN_DONT_INLINE
24
+ void dont_over_optimize(T& x) { volatile typename T::Scalar tmp = x(0); x(0) = tmp; }
25
+
26
+ template<typename Scalar, int Mode, int Options> void non_projective_only()
27
+ {
28
+ /* this test covers the following files:
29
+ Cross.h Quaternion.h, Transform.cpp
30
+ */
31
+ typedef Matrix<Scalar,3,1> Vector3;
32
+ typedef Quaternion<Scalar> Quaternionx;
33
+ typedef AngleAxis<Scalar> AngleAxisx;
34
+ typedef Transform<Scalar,3,Mode,Options> Transform3;
35
+ typedef DiagonalMatrix<Scalar,3> AlignedScaling3;
36
+ typedef Translation<Scalar,3> Translation3;
37
+
38
+ Vector3 v0 = Vector3::Random(),
39
+ v1 = Vector3::Random();
40
+
41
+ Transform3 t0, t1, t2;
42
+
43
+ Scalar a = internal::random<Scalar>(-Scalar(EIGEN_PI), Scalar(EIGEN_PI));
44
+
45
+ Quaternionx q1, q2;
46
+
47
+ q1 = AngleAxisx(a, v0.normalized());
48
+
49
+ t0 = Transform3::Identity();
50
+ VERIFY_IS_APPROX(t0.matrix(), Transform3::MatrixType::Identity());
51
+
52
+ t0.linear() = q1.toRotationMatrix();
53
+
54
+ v0 << 50, 2, 1;
55
+ t0.scale(v0);
56
+
57
+ VERIFY_IS_APPROX( (t0 * Vector3(1,0,0)).template head<3>().norm(), v0.x());
58
+
59
+ t0.setIdentity();
60
+ t1.setIdentity();
61
+ v1 << 1, 2, 3;
62
+ t0.linear() = q1.toRotationMatrix();
63
+ t0.pretranslate(v0);
64
+ t0.scale(v1);
65
+ t1.linear() = q1.conjugate().toRotationMatrix();
66
+ t1.prescale(v1.cwiseInverse());
67
+ t1.translate(-v0);
68
+
69
+ VERIFY((t0 * t1).matrix().isIdentity(test_precision<Scalar>()));
70
+
71
+ t1.fromPositionOrientationScale(v0, q1, v1);
72
+ VERIFY_IS_APPROX(t1.matrix(), t0.matrix());
73
+ VERIFY_IS_APPROX(t1*v1, t0*v1);
74
+
75
+ // translation * vector
76
+ t0.setIdentity();
77
+ t0.translate(v0);
78
+ VERIFY_IS_APPROX((t0 * v1).template head<3>(), Translation3(v0) * v1);
79
+
80
+ // AlignedScaling * vector
81
+ t0.setIdentity();
82
+ t0.scale(v0);
83
+ VERIFY_IS_APPROX((t0 * v1).template head<3>(), AlignedScaling3(v0) * v1);
84
+ }
85
+
86
+ template<typename Scalar, int Mode, int Options> void transformations()
87
+ {
88
+ /* this test covers the following files:
89
+ Cross.h Quaternion.h, Transform.cpp
90
+ */
91
+ using std::cos;
92
+ using std::abs;
93
+ typedef Matrix<Scalar,3,3> Matrix3;
94
+ typedef Matrix<Scalar,4,4> Matrix4;
95
+ typedef Matrix<Scalar,2,1> Vector2;
96
+ typedef Matrix<Scalar,3,1> Vector3;
97
+ typedef Matrix<Scalar,4,1> Vector4;
98
+ typedef Quaternion<Scalar> Quaternionx;
99
+ typedef AngleAxis<Scalar> AngleAxisx;
100
+ typedef Transform<Scalar,2,Mode,Options> Transform2;
101
+ typedef Transform<Scalar,3,Mode,Options> Transform3;
102
+ typedef typename Transform3::MatrixType MatrixType;
103
+ typedef DiagonalMatrix<Scalar,3> AlignedScaling3;
104
+ typedef Translation<Scalar,2> Translation2;
105
+ typedef Translation<Scalar,3> Translation3;
106
+
107
+ Vector3 v0 = Vector3::Random(),
108
+ v1 = Vector3::Random();
109
+ Matrix3 matrot1, m;
110
+
111
+ Scalar a = internal::random<Scalar>(-Scalar(EIGEN_PI), Scalar(EIGEN_PI));
112
+ Scalar s0 = internal::random<Scalar>(), s1 = internal::random<Scalar>();
113
+
114
+ while(v0.norm() < test_precision<Scalar>()) v0 = Vector3::Random();
115
+ while(v1.norm() < test_precision<Scalar>()) v1 = Vector3::Random();
116
+
117
+ VERIFY_IS_APPROX(v0, AngleAxisx(a, v0.normalized()) * v0);
118
+ VERIFY_IS_APPROX(-v0, AngleAxisx(Scalar(EIGEN_PI), v0.unitOrthogonal()) * v0);
119
+ if(abs(cos(a)) > test_precision<Scalar>())
120
+ {
121
+ VERIFY_IS_APPROX(cos(a)*v0.squaredNorm(), v0.dot(AngleAxisx(a, v0.unitOrthogonal()) * v0));
122
+ }
123
+ m = AngleAxisx(a, v0.normalized()).toRotationMatrix().adjoint();
124
+ VERIFY_IS_APPROX(Matrix3::Identity(), m * AngleAxisx(a, v0.normalized()));
125
+ VERIFY_IS_APPROX(Matrix3::Identity(), AngleAxisx(a, v0.normalized()) * m);
126
+
127
+ Quaternionx q1, q2;
128
+ q1 = AngleAxisx(a, v0.normalized());
129
+ q2 = AngleAxisx(a, v1.normalized());
130
+
131
+ // rotation matrix conversion
132
+ matrot1 = AngleAxisx(Scalar(0.1), Vector3::UnitX())
133
+ * AngleAxisx(Scalar(0.2), Vector3::UnitY())
134
+ * AngleAxisx(Scalar(0.3), Vector3::UnitZ());
135
+ VERIFY_IS_APPROX(matrot1 * v1,
136
+ AngleAxisx(Scalar(0.1), Vector3(1,0,0)).toRotationMatrix()
137
+ * (AngleAxisx(Scalar(0.2), Vector3(0,1,0)).toRotationMatrix()
138
+ * (AngleAxisx(Scalar(0.3), Vector3(0,0,1)).toRotationMatrix() * v1)));
139
+
140
+ // angle-axis conversion
141
+ AngleAxisx aa = AngleAxisx(q1);
142
+ VERIFY_IS_APPROX(q1 * v1, Quaternionx(aa) * v1);
143
+
144
+ // The following test is stable only if 2*angle != angle and v1 is not colinear with axis
145
+ if( (abs(aa.angle()) > test_precision<Scalar>()) && (abs(aa.axis().dot(v1.normalized()))<(Scalar(1)-Scalar(4)*test_precision<Scalar>())) )
146
+ {
147
+ VERIFY( !(q1 * v1).isApprox(Quaternionx(AngleAxisx(aa.angle()*2,aa.axis())) * v1) );
148
+ }
149
+
150
+ aa.fromRotationMatrix(aa.toRotationMatrix());
151
+ VERIFY_IS_APPROX(q1 * v1, Quaternionx(aa) * v1);
152
+ // The following test is stable only if 2*angle != angle and v1 is not colinear with axis
153
+ if( (abs(aa.angle()) > test_precision<Scalar>()) && (abs(aa.axis().dot(v1.normalized()))<(Scalar(1)-Scalar(4)*test_precision<Scalar>())) )
154
+ {
155
+ VERIFY( !(q1 * v1).isApprox(Quaternionx(AngleAxisx(aa.angle()*2,aa.axis())) * v1) );
156
+ }
157
+
158
+ // AngleAxis
159
+ VERIFY_IS_APPROX(AngleAxisx(a,v1.normalized()).toRotationMatrix(),
160
+ Quaternionx(AngleAxisx(a,v1.normalized())).toRotationMatrix());
161
+
162
+ AngleAxisx aa1;
163
+ m = q1.toRotationMatrix();
164
+ aa1 = m;
165
+ VERIFY_IS_APPROX(AngleAxisx(m).toRotationMatrix(),
166
+ Quaternionx(m).toRotationMatrix());
167
+
168
+ // Transform
169
+ // TODO complete the tests !
170
+ a = 0;
171
+ while (abs(a)<Scalar(0.1))
172
+ a = internal::random<Scalar>(-Scalar(0.4)*Scalar(EIGEN_PI), Scalar(0.4)*Scalar(EIGEN_PI));
173
+ q1 = AngleAxisx(a, v0.normalized());
174
+ Transform3 t0, t1, t2;
175
+
176
+ // first test setIdentity() and Identity()
177
+ t0.setIdentity();
178
+ VERIFY_IS_APPROX(t0.matrix(), Transform3::MatrixType::Identity());
179
+ t0.matrix().setZero();
180
+ t0 = Transform3::Identity();
181
+ VERIFY_IS_APPROX(t0.matrix(), Transform3::MatrixType::Identity());
182
+
183
+ t0.setIdentity();
184
+ t1.setIdentity();
185
+ v1 << 1, 2, 3;
186
+ t0.linear() = q1.toRotationMatrix();
187
+ t0.pretranslate(v0);
188
+ t0.scale(v1);
189
+ t1.linear() = q1.conjugate().toRotationMatrix();
190
+ t1.prescale(v1.cwiseInverse());
191
+ t1.translate(-v0);
192
+
193
+ VERIFY((t0 * t1).matrix().isIdentity(test_precision<Scalar>()));
194
+
195
+ t1.fromPositionOrientationScale(v0, q1, v1);
196
+ VERIFY_IS_APPROX(t1.matrix(), t0.matrix());
197
+
198
+ t0.setIdentity(); t0.scale(v0).rotate(q1.toRotationMatrix());
199
+ t1.setIdentity(); t1.scale(v0).rotate(q1);
200
+ VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
201
+
202
+ t0.setIdentity(); t0.scale(v0).rotate(AngleAxisx(q1));
203
+ VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
204
+
205
+ VERIFY_IS_APPROX(t0.scale(a).matrix(), t1.scale(Vector3::Constant(a)).matrix());
206
+ VERIFY_IS_APPROX(t0.prescale(a).matrix(), t1.prescale(Vector3::Constant(a)).matrix());
207
+
208
+ // More transform constructors, operator=, operator*=
209
+
210
+ Matrix3 mat3 = Matrix3::Random();
211
+ Matrix4 mat4;
212
+ mat4 << mat3 , Vector3::Zero() , Vector4::Zero().transpose();
213
+ Transform3 tmat3(mat3), tmat4(mat4);
214
+ if(Mode!=int(AffineCompact))
215
+ tmat4.matrix()(3,3) = Scalar(1);
216
+ VERIFY_IS_APPROX(tmat3.matrix(), tmat4.matrix());
217
+
218
+ Scalar a3 = internal::random<Scalar>(-Scalar(EIGEN_PI), Scalar(EIGEN_PI));
219
+ Vector3 v3 = Vector3::Random().normalized();
220
+ AngleAxisx aa3(a3, v3);
221
+ Transform3 t3(aa3);
222
+ Transform3 t4;
223
+ t4 = aa3;
224
+ VERIFY_IS_APPROX(t3.matrix(), t4.matrix());
225
+ t4.rotate(AngleAxisx(-a3,v3));
226
+ VERIFY_IS_APPROX(t4.matrix(), MatrixType::Identity());
227
+ t4 *= aa3;
228
+ VERIFY_IS_APPROX(t3.matrix(), t4.matrix());
229
+
230
+ do {
231
+ v3 = Vector3::Random();
232
+ dont_over_optimize(v3);
233
+ } while (v3.cwiseAbs().minCoeff()<NumTraits<Scalar>::epsilon());
234
+ Translation3 tv3(v3);
235
+ Transform3 t5(tv3);
236
+ t4 = tv3;
237
+ VERIFY_IS_APPROX(t5.matrix(), t4.matrix());
238
+ t4.translate((-v3).eval());
239
+ VERIFY_IS_APPROX(t4.matrix(), MatrixType::Identity());
240
+ t4 *= tv3;
241
+ VERIFY_IS_APPROX(t5.matrix(), t4.matrix());
242
+
243
+ AlignedScaling3 sv3(v3);
244
+ Transform3 t6(sv3);
245
+ t4 = sv3;
246
+ VERIFY_IS_APPROX(t6.matrix(), t4.matrix());
247
+ t4.scale(v3.cwiseInverse());
248
+ VERIFY_IS_APPROX(t4.matrix(), MatrixType::Identity());
249
+ t4 *= sv3;
250
+ VERIFY_IS_APPROX(t6.matrix(), t4.matrix());
251
+
252
+ // matrix * transform
253
+ VERIFY_IS_APPROX((t3.matrix()*t4).matrix(), (t3*t4).matrix());
254
+
255
+ // chained Transform product
256
+ VERIFY_IS_APPROX(((t3*t4)*t5).matrix(), (t3*(t4*t5)).matrix());
257
+
258
+ // check that Transform product doesn't have aliasing problems
259
+ t5 = t4;
260
+ t5 = t5*t5;
261
+ VERIFY_IS_APPROX(t5, t4*t4);
262
+
263
+ // 2D transformation
264
+ Transform2 t20, t21;
265
+ Vector2 v20 = Vector2::Random();
266
+ Vector2 v21 = Vector2::Random();
267
+ for (int k=0; k<2; ++k)
268
+ if (abs(v21[k])<Scalar(1e-3)) v21[k] = Scalar(1e-3);
269
+ t21.setIdentity();
270
+ t21.linear() = Rotation2D<Scalar>(a).toRotationMatrix();
271
+ VERIFY_IS_APPROX(t20.fromPositionOrientationScale(v20,a,v21).matrix(),
272
+ t21.pretranslate(v20).scale(v21).matrix());
273
+
274
+ t21.setIdentity();
275
+ t21.linear() = Rotation2D<Scalar>(-a).toRotationMatrix();
276
+ VERIFY( (t20.fromPositionOrientationScale(v20,a,v21)
277
+ * (t21.prescale(v21.cwiseInverse()).translate(-v20))).matrix().isIdentity(test_precision<Scalar>()) );
278
+
279
+ t20.setIdentity();
280
+ t20.shear(Scalar(2), Scalar(3));
281
+ Transform2 t23 = t20 * t21;
282
+ t21.preshear(Scalar(2), Scalar(3));
283
+ VERIFY_IS_APPROX(t21, t23);
284
+
285
+ // Transform - new API
286
+ // 3D
287
+ t0.setIdentity();
288
+ t0.rotate(q1).scale(v0).translate(v0);
289
+ // mat * aligned scaling and mat * translation
290
+ t1 = (Matrix3(q1) * AlignedScaling3(v0)) * Translation3(v0);
291
+ VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
292
+ t1 = (Matrix3(q1) * Eigen::Scaling(v0)) * Translation3(v0);
293
+ VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
294
+ t1 = (q1 * Eigen::Scaling(v0)) * Translation3(v0);
295
+ VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
296
+ // mat * transformation and aligned scaling * translation
297
+ t1 = Matrix3(q1) * (AlignedScaling3(v0) * Translation3(v0));
298
+ VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
299
+
300
+
301
+ t0.setIdentity();
302
+ t0.scale(s0).translate(v0);
303
+ t1 = Eigen::Scaling(s0) * Translation3(v0);
304
+ VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
305
+ t0.prescale(s0);
306
+ t1 = Eigen::Scaling(s0) * t1;
307
+ VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
308
+
309
+ t0 = t3;
310
+ t0.scale(s0);
311
+ t1 = t3 * Eigen::Scaling(s0,s0,s0);
312
+ VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
313
+ t0.prescale(s0);
314
+ t1 = Eigen::Scaling(s0,s0,s0) * t1;
315
+ VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
316
+
317
+ t0 = t3;
318
+ t0.scale(s0);
319
+ t1 = t3 * Eigen::Scaling(s0);
320
+ VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
321
+ t0.prescale(s0);
322
+ t1 = Eigen::Scaling(s0) * t1;
323
+ VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
324
+
325
+ t0.setIdentity();
326
+ t0.prerotate(q1).prescale(v0).pretranslate(v0);
327
+ // translation * aligned scaling and transformation * mat
328
+ t1 = (Translation3(v0) * AlignedScaling3(v0)) * Transform3(q1);
329
+ VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
330
+ // scaling * mat and translation * mat
331
+ t1 = Translation3(v0) * (AlignedScaling3(v0) * Transform3(q1));
332
+ VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
333
+
334
+ t0.setIdentity();
335
+ t0.scale(v0).translate(v0).rotate(q1);
336
+ // translation * mat and aligned scaling * transformation
337
+ t1 = AlignedScaling3(v0) * (Translation3(v0) * Transform3(q1));
338
+ VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
339
+ // transformation * aligned scaling
340
+ t0.scale(v0);
341
+ t1 *= AlignedScaling3(v0);
342
+ VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
343
+ t1 = AlignedScaling3(v0) * (Translation3(v0) * Transform3(q1));
344
+ t1 = t1 * v0.asDiagonal();
345
+ VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
346
+ // transformation * translation
347
+ t0.translate(v0);
348
+ t1 = t1 * Translation3(v0);
349
+ VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
350
+ // translation * transformation
351
+ t0.pretranslate(v0);
352
+ t1 = Translation3(v0) * t1;
353
+ VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
354
+
355
+ // transform * quaternion
356
+ t0.rotate(q1);
357
+ t1 = t1 * q1;
358
+ VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
359
+
360
+ // translation * quaternion
361
+ t0.translate(v1).rotate(q1);
362
+ t1 = t1 * (Translation3(v1) * q1);
363
+ VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
364
+
365
+ // aligned scaling * quaternion
366
+ t0.scale(v1).rotate(q1);
367
+ t1 = t1 * (AlignedScaling3(v1) * q1);
368
+ VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
369
+
370
+ // quaternion * transform
371
+ t0.prerotate(q1);
372
+ t1 = q1 * t1;
373
+ VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
374
+
375
+ // quaternion * translation
376
+ t0.rotate(q1).translate(v1);
377
+ t1 = t1 * (q1 * Translation3(v1));
378
+ VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
379
+
380
+ // quaternion * aligned scaling
381
+ t0.rotate(q1).scale(v1);
382
+ t1 = t1 * (q1 * AlignedScaling3(v1));
383
+ VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
384
+
385
+ // test transform inversion
386
+ t0.setIdentity();
387
+ t0.translate(v0);
388
+ do {
389
+ t0.linear().setRandom();
390
+ } while(t0.linear().jacobiSvd().singularValues()(2)<test_precision<Scalar>());
391
+ Matrix4 t044 = Matrix4::Zero();
392
+ t044(3,3) = 1;
393
+ t044.block(0,0,t0.matrix().rows(),4) = t0.matrix();
394
+ VERIFY_IS_APPROX(t0.inverse(Affine).matrix(), t044.inverse().block(0,0,t0.matrix().rows(),4));
395
+ t0.setIdentity();
396
+ t0.translate(v0).rotate(q1);
397
+ t044 = Matrix4::Zero();
398
+ t044(3,3) = 1;
399
+ t044.block(0,0,t0.matrix().rows(),4) = t0.matrix();
400
+ VERIFY_IS_APPROX(t0.inverse(Isometry).matrix(), t044.inverse().block(0,0,t0.matrix().rows(),4));
401
+
402
+ Matrix3 mat_rotation, mat_scaling;
403
+ t0.setIdentity();
404
+ t0.translate(v0).rotate(q1).scale(v1);
405
+ t0.computeRotationScaling(&mat_rotation, &mat_scaling);
406
+ VERIFY_IS_APPROX(t0.linear(), mat_rotation * mat_scaling);
407
+ VERIFY_IS_APPROX(mat_rotation*mat_rotation.adjoint(), Matrix3::Identity());
408
+ VERIFY_IS_APPROX(mat_rotation.determinant(), Scalar(1));
409
+ t0.computeScalingRotation(&mat_scaling, &mat_rotation);
410
+ VERIFY_IS_APPROX(t0.linear(), mat_scaling * mat_rotation);
411
+ VERIFY_IS_APPROX(mat_rotation*mat_rotation.adjoint(), Matrix3::Identity());
412
+ VERIFY_IS_APPROX(mat_rotation.determinant(), Scalar(1));
413
+
414
+ // test casting
415
+ Transform<float,3,Mode> t1f = t1.template cast<float>();
416
+ VERIFY_IS_APPROX(t1f.template cast<Scalar>(),t1);
417
+ Transform<double,3,Mode> t1d = t1.template cast<double>();
418
+ VERIFY_IS_APPROX(t1d.template cast<Scalar>(),t1);
419
+
420
+ Translation3 tr1(v0);
421
+ Translation<float,3> tr1f = tr1.template cast<float>();
422
+ VERIFY_IS_APPROX(tr1f.template cast<Scalar>(),tr1);
423
+ Translation<double,3> tr1d = tr1.template cast<double>();
424
+ VERIFY_IS_APPROX(tr1d.template cast<Scalar>(),tr1);
425
+
426
+ AngleAxis<float> aa1f = aa1.template cast<float>();
427
+ VERIFY_IS_APPROX(aa1f.template cast<Scalar>(),aa1);
428
+ AngleAxis<double> aa1d = aa1.template cast<double>();
429
+ VERIFY_IS_APPROX(aa1d.template cast<Scalar>(),aa1);
430
+
431
+ Rotation2D<Scalar> r2d1(internal::random<Scalar>());
432
+ Rotation2D<float> r2d1f = r2d1.template cast<float>();
433
+ VERIFY_IS_APPROX(r2d1f.template cast<Scalar>(),r2d1);
434
+ Rotation2D<double> r2d1d = r2d1.template cast<double>();
435
+ VERIFY_IS_APPROX(r2d1d.template cast<Scalar>(),r2d1);
436
+
437
+ for(int k=0; k<100; ++k)
438
+ {
439
+ Scalar angle = internal::random<Scalar>(-100,100);
440
+ Rotation2D<Scalar> rot2(angle);
441
+ VERIFY( rot2.smallestPositiveAngle() >= 0 );
442
+ VERIFY( rot2.smallestPositiveAngle() <= Scalar(2)*Scalar(EIGEN_PI) );
443
+ VERIFY_IS_APPROX( angleToVec(rot2.smallestPositiveAngle()), angleToVec(rot2.angle()) );
444
+
445
+ VERIFY( rot2.smallestAngle() >= -Scalar(EIGEN_PI) );
446
+ VERIFY( rot2.smallestAngle() <= Scalar(EIGEN_PI) );
447
+ VERIFY_IS_APPROX( angleToVec(rot2.smallestAngle()), angleToVec(rot2.angle()) );
448
+
449
+ Matrix<Scalar,2,2> rot2_as_mat(rot2);
450
+ Rotation2D<Scalar> rot3(rot2_as_mat);
451
+ VERIFY_IS_APPROX( angleToVec(rot2.smallestAngle()), angleToVec(rot3.angle()) );
452
+ }
453
+
454
+ s0 = internal::random<Scalar>(-100,100);
455
+ s1 = internal::random<Scalar>(-100,100);
456
+ Rotation2D<Scalar> R0(s0), R1(s1);
457
+
458
+ t20 = Translation2(v20) * (R0 * Eigen::Scaling(s0));
459
+ t21 = Translation2(v20) * R0 * Eigen::Scaling(s0);
460
+ VERIFY_IS_APPROX(t20,t21);
461
+
462
+ t20 = Translation2(v20) * (R0 * R0.inverse() * Eigen::Scaling(s0));
463
+ t21 = Translation2(v20) * Eigen::Scaling(s0);
464
+ VERIFY_IS_APPROX(t20,t21);
465
+
466
+ VERIFY_IS_APPROX(s0, (R0.slerp(0, R1)).angle());
467
+ VERIFY_IS_APPROX( angleToVec(R1.smallestPositiveAngle()), angleToVec((R0.slerp(1, R1)).smallestPositiveAngle()) );
468
+ VERIFY_IS_APPROX(R0.smallestPositiveAngle(), (R0.slerp(0.5, R0)).smallestPositiveAngle());
469
+
470
+ if(std::cos(s0)>0)
471
+ VERIFY_IS_MUCH_SMALLER_THAN((R0.slerp(0.5, R0.inverse())).smallestAngle(), Scalar(1));
472
+ else
473
+ VERIFY_IS_APPROX(Scalar(EIGEN_PI), (R0.slerp(0.5, R0.inverse())).smallestPositiveAngle());
474
+
475
+ // Check path length
476
+ Scalar l = 0;
477
+ int path_steps = 100;
478
+ for(int k=0; k<path_steps; ++k)
479
+ {
480
+ Scalar a1 = R0.slerp(Scalar(k)/Scalar(path_steps), R1).angle();
481
+ Scalar a2 = R0.slerp(Scalar(k+1)/Scalar(path_steps), R1).angle();
482
+ l += std::abs(a2-a1);
483
+ }
484
+ VERIFY(l<=Scalar(EIGEN_PI)*(Scalar(1)+NumTraits<Scalar>::epsilon()*Scalar(path_steps/2)));
485
+
486
+ // check basic features
487
+ {
488
+ Rotation2D<Scalar> r1; // default ctor
489
+ r1 = Rotation2D<Scalar>(s0); // copy assignment
490
+ VERIFY_IS_APPROX(r1.angle(),s0);
491
+ Rotation2D<Scalar> r2(r1); // copy ctor
492
+ VERIFY_IS_APPROX(r2.angle(),s0);
493
+ }
494
+
495
+ {
496
+ Transform3 t32(Matrix4::Random()), t33, t34;
497
+ t34 = t33 = t32;
498
+ t32.scale(v0);
499
+ t33*=AlignedScaling3(v0);
500
+ VERIFY_IS_APPROX(t32.matrix(), t33.matrix());
501
+ t33 = t34 * AlignedScaling3(v0);
502
+ VERIFY_IS_APPROX(t32.matrix(), t33.matrix());
503
+ }
504
+
505
+ }
506
+
507
+ template<typename A1, typename A2, typename P, typename Q, typename V, typename H>
508
+ void transform_associativity_left(const A1& a1, const A2& a2, const P& p, const Q& q, const V& v, const H& h)
509
+ {
510
+ VERIFY_IS_APPROX( q*(a1*v), (q*a1)*v );
511
+ VERIFY_IS_APPROX( q*(a2*v), (q*a2)*v );
512
+ VERIFY_IS_APPROX( q*(p*h).hnormalized(), ((q*p)*h).hnormalized() );
513
+ }
514
+
515
+ template<typename A1, typename A2, typename P, typename Q, typename V, typename H>
516
+ void transform_associativity2(const A1& a1, const A2& a2, const P& p, const Q& q, const V& v, const H& h)
517
+ {
518
+ VERIFY_IS_APPROX( a1*(q*v), (a1*q)*v );
519
+ VERIFY_IS_APPROX( a2*(q*v), (a2*q)*v );
520
+ VERIFY_IS_APPROX( p *(q*v).homogeneous(), (p *q)*v.homogeneous() );
521
+
522
+ transform_associativity_left(a1, a2,p, q, v, h);
523
+ }
524
+
525
+ template<typename Scalar, int Dim, int Options,typename RotationType>
526
+ void transform_associativity(const RotationType& R)
527
+ {
528
+ typedef Matrix<Scalar,Dim,1> VectorType;
529
+ typedef Matrix<Scalar,Dim+1,1> HVectorType;
530
+ typedef Matrix<Scalar,Dim,Dim> LinearType;
531
+ typedef Matrix<Scalar,Dim+1,Dim+1> MatrixType;
532
+ typedef Transform<Scalar,Dim,AffineCompact,Options> AffineCompactType;
533
+ typedef Transform<Scalar,Dim,Affine,Options> AffineType;
534
+ typedef Transform<Scalar,Dim,Projective,Options> ProjectiveType;
535
+ typedef DiagonalMatrix<Scalar,Dim> ScalingType;
536
+ typedef Translation<Scalar,Dim> TranslationType;
537
+
538
+ AffineCompactType A1c; A1c.matrix().setRandom();
539
+ AffineCompactType A2c; A2c.matrix().setRandom();
540
+ AffineType A1(A1c);
541
+ AffineType A2(A2c);
542
+ ProjectiveType P1; P1.matrix().setRandom();
543
+ VectorType v1 = VectorType::Random();
544
+ VectorType v2 = VectorType::Random();
545
+ HVectorType h1 = HVectorType::Random();
546
+ Scalar s1 = internal::random<Scalar>();
547
+ LinearType L = LinearType::Random();
548
+ MatrixType M = MatrixType::Random();
549
+
550
+ CALL_SUBTEST( transform_associativity2(A1c, A1, P1, A2, v2, h1) );
551
+ CALL_SUBTEST( transform_associativity2(A1c, A1, P1, A2c, v2, h1) );
552
+ CALL_SUBTEST( transform_associativity2(A1c, A1, P1, v1.asDiagonal(), v2, h1) );
553
+ CALL_SUBTEST( transform_associativity2(A1c, A1, P1, ScalingType(v1), v2, h1) );
554
+ CALL_SUBTEST( transform_associativity2(A1c, A1, P1, Scaling(v1), v2, h1) );
555
+ CALL_SUBTEST( transform_associativity2(A1c, A1, P1, Scaling(s1), v2, h1) );
556
+ CALL_SUBTEST( transform_associativity2(A1c, A1, P1, TranslationType(v1), v2, h1) );
557
+ CALL_SUBTEST( transform_associativity_left(A1c, A1, P1, L, v2, h1) );
558
+ CALL_SUBTEST( transform_associativity2(A1c, A1, P1, R, v2, h1) );
559
+
560
+ VERIFY_IS_APPROX( A1*(M*h1), (A1*M)*h1 );
561
+ VERIFY_IS_APPROX( A1c*(M*h1), (A1c*M)*h1 );
562
+ VERIFY_IS_APPROX( P1*(M*h1), (P1*M)*h1 );
563
+
564
+ VERIFY_IS_APPROX( M*(A1*h1), (M*A1)*h1 );
565
+ VERIFY_IS_APPROX( M*(A1c*h1), (M*A1c)*h1 );
566
+ VERIFY_IS_APPROX( M*(P1*h1), ((M*P1)*h1) );
567
+ }
568
+
569
+ template<typename Scalar> void transform_alignment()
570
+ {
571
+ typedef Transform<Scalar,3,Projective,AutoAlign> Projective3a;
572
+ typedef Transform<Scalar,3,Projective,DontAlign> Projective3u;
573
+
574
+ EIGEN_ALIGN_MAX Scalar array1[16];
575
+ EIGEN_ALIGN_MAX Scalar array2[16];
576
+ EIGEN_ALIGN_MAX Scalar array3[16+1];
577
+ Scalar* array3u = array3+1;
578
+
579
+ Projective3a *p1 = ::new(reinterpret_cast<void*>(array1)) Projective3a;
580
+ Projective3u *p2 = ::new(reinterpret_cast<void*>(array2)) Projective3u;
581
+ Projective3u *p3 = ::new(reinterpret_cast<void*>(array3u)) Projective3u;
582
+
583
+ p1->matrix().setRandom();
584
+ *p2 = *p1;
585
+ *p3 = *p1;
586
+
587
+ VERIFY_IS_APPROX(p1->matrix(), p2->matrix());
588
+ VERIFY_IS_APPROX(p1->matrix(), p3->matrix());
589
+
590
+ VERIFY_IS_APPROX( (*p1) * (*p1), (*p2)*(*p3));
591
+ }
592
+
593
+ template<typename Scalar, int Dim, int Options> void transform_products()
594
+ {
595
+ typedef Matrix<Scalar,Dim+1,Dim+1> Mat;
596
+ typedef Transform<Scalar,Dim,Projective,Options> Proj;
597
+ typedef Transform<Scalar,Dim,Affine,Options> Aff;
598
+ typedef Transform<Scalar,Dim,AffineCompact,Options> AffC;
599
+
600
+ Proj p; p.matrix().setRandom();
601
+ Aff a; a.linear().setRandom(); a.translation().setRandom();
602
+ AffC ac = a;
603
+
604
+ Mat p_m(p.matrix()), a_m(a.matrix());
605
+
606
+ VERIFY_IS_APPROX((p*p).matrix(), p_m*p_m);
607
+ VERIFY_IS_APPROX((a*a).matrix(), a_m*a_m);
608
+ VERIFY_IS_APPROX((p*a).matrix(), p_m*a_m);
609
+ VERIFY_IS_APPROX((a*p).matrix(), a_m*p_m);
610
+ VERIFY_IS_APPROX((ac*a).matrix(), a_m*a_m);
611
+ VERIFY_IS_APPROX((a*ac).matrix(), a_m*a_m);
612
+ VERIFY_IS_APPROX((p*ac).matrix(), p_m*a_m);
613
+ VERIFY_IS_APPROX((ac*p).matrix(), a_m*p_m);
614
+ }
615
+
616
+ template<typename Scalar, int Mode, int Options> void transformations_no_scale()
617
+ {
618
+ /* this test covers the following files:
619
+ Cross.h Quaternion.h, Transform.h
620
+ */
621
+ typedef Matrix<Scalar,3,1> Vector3;
622
+ typedef Matrix<Scalar,4,1> Vector4;
623
+ typedef Quaternion<Scalar> Quaternionx;
624
+ typedef AngleAxis<Scalar> AngleAxisx;
625
+ typedef Transform<Scalar,3,Mode,Options> Transform3;
626
+ typedef Translation<Scalar,3> Translation3;
627
+ typedef Matrix<Scalar,4,4> Matrix4;
628
+
629
+ Vector3 v0 = Vector3::Random(),
630
+ v1 = Vector3::Random();
631
+
632
+ Transform3 t0, t1, t2;
633
+
634
+ Scalar a = internal::random<Scalar>(-Scalar(EIGEN_PI), Scalar(EIGEN_PI));
635
+
636
+ Quaternionx q1, q2;
637
+
638
+ q1 = AngleAxisx(a, v0.normalized());
639
+
640
+ t0 = Transform3::Identity();
641
+ VERIFY_IS_APPROX(t0.matrix(), Transform3::MatrixType::Identity());
642
+
643
+ t0.setIdentity();
644
+ t1.setIdentity();
645
+ v1 = Vector3::Ones();
646
+ t0.linear() = q1.toRotationMatrix();
647
+ t0.pretranslate(v0);
648
+ t1.linear() = q1.conjugate().toRotationMatrix();
649
+ t1.translate(-v0);
650
+
651
+ VERIFY((t0 * t1).matrix().isIdentity(test_precision<Scalar>()));
652
+
653
+ t1.fromPositionOrientationScale(v0, q1, v1);
654
+ VERIFY_IS_APPROX(t1.matrix(), t0.matrix());
655
+ VERIFY_IS_APPROX(t1*v1, t0*v1);
656
+
657
+ // translation * vector
658
+ t0.setIdentity();
659
+ t0.translate(v0);
660
+ VERIFY_IS_APPROX((t0 * v1).template head<3>(), Translation3(v0) * v1);
661
+
662
+ // Conversion to matrix.
663
+ Transform3 t3;
664
+ t3.linear() = q1.toRotationMatrix();
665
+ t3.translation() = v1;
666
+ Matrix4 m3 = t3.matrix();
667
+ VERIFY((m3 * m3.inverse()).isIdentity(test_precision<Scalar>()));
668
+ // Verify implicit last row is initialized.
669
+ VERIFY_IS_APPROX(Vector4(m3.row(3)), Vector4(0.0, 0.0, 0.0, 1.0));
670
+
671
+ VERIFY_IS_APPROX(t3.rotation(), t3.linear());
672
+ if(Mode==Isometry)
673
+ VERIFY(t3.rotation().data()==t3.linear().data());
674
+ }
675
+
676
+ template<typename Scalar, int Mode, int Options> void transformations_computed_scaling_continuity()
677
+ {
678
+ typedef Matrix<Scalar, 3, 1> Vector3;
679
+ typedef Transform<Scalar, 3, Mode, Options> Transform3;
680
+ typedef Matrix<Scalar, 3, 3> Matrix3;
681
+
682
+ // Given: two transforms that differ by '2*eps'.
683
+ Scalar eps(1e-3);
684
+ Vector3 v0 = Vector3::Random().normalized(),
685
+ v1 = Vector3::Random().normalized(),
686
+ v3 = Vector3::Random().normalized();
687
+ Transform3 t0, t1;
688
+ // The interesting case is when their determinants have different signs.
689
+ Matrix3 rank2 = 50 * v0 * v0.adjoint() + 20 * v1 * v1.adjoint();
690
+ t0.linear() = rank2 + eps * v3 * v3.adjoint();
691
+ t1.linear() = rank2 - eps * v3 * v3.adjoint();
692
+
693
+ // When: computing the rotation-scaling parts
694
+ Matrix3 r0, s0, r1, s1;
695
+ t0.computeRotationScaling(&r0, &s0);
696
+ t1.computeRotationScaling(&r1, &s1);
697
+
698
+ // Then: the scaling parts should differ by no more than '2*eps'.
699
+ const Scalar c(2.1); // 2 + room for rounding errors
700
+ VERIFY((s0 - s1).norm() < c * eps);
701
+ }
702
+
703
+ EIGEN_DECLARE_TEST(geo_transformations)
704
+ {
705
+ for(int i = 0; i < g_repeat; i++) {
706
+ CALL_SUBTEST_1(( transformations<double,Affine,AutoAlign>() ));
707
+ CALL_SUBTEST_1(( non_projective_only<double,Affine,AutoAlign>() ));
708
+ CALL_SUBTEST_1(( transformations_computed_scaling_continuity<double,Affine,AutoAlign>() ));
709
+
710
+ CALL_SUBTEST_2(( transformations<float,AffineCompact,AutoAlign>() ));
711
+ CALL_SUBTEST_2(( non_projective_only<float,AffineCompact,AutoAlign>() ));
712
+ CALL_SUBTEST_2(( transform_alignment<float>() ));
713
+
714
+ CALL_SUBTEST_3(( transformations<double,Projective,AutoAlign>() ));
715
+ CALL_SUBTEST_3(( transformations<double,Projective,DontAlign>() ));
716
+ CALL_SUBTEST_3(( transform_alignment<double>() ));
717
+
718
+ CALL_SUBTEST_4(( transformations<float,Affine,RowMajor|AutoAlign>() ));
719
+ CALL_SUBTEST_4(( non_projective_only<float,Affine,RowMajor>() ));
720
+
721
+ CALL_SUBTEST_5(( transformations<double,AffineCompact,RowMajor|AutoAlign>() ));
722
+ CALL_SUBTEST_5(( non_projective_only<double,AffineCompact,RowMajor>() ));
723
+
724
+ CALL_SUBTEST_6(( transformations<double,Projective,RowMajor|AutoAlign>() ));
725
+ CALL_SUBTEST_6(( transformations<double,Projective,RowMajor|DontAlign>() ));
726
+
727
+
728
+ CALL_SUBTEST_7(( transform_products<double,3,RowMajor|AutoAlign>() ));
729
+ CALL_SUBTEST_7(( transform_products<float,2,AutoAlign>() ));
730
+
731
+ CALL_SUBTEST_8(( transform_associativity<double,2,ColMajor>(Rotation2D<double>(internal::random<double>()*double(EIGEN_PI))) ));
732
+ CALL_SUBTEST_8(( transform_associativity<double,3,ColMajor>(Quaterniond::UnitRandom()) ));
733
+
734
+ CALL_SUBTEST_9(( transformations_no_scale<double,Affine,AutoAlign>() ));
735
+ CALL_SUBTEST_9(( transformations_no_scale<double,Isometry,AutoAlign>() ));
736
+ }
737
+ }
include/eigen/test/half_float.cpp ADDED
@@ -0,0 +1,352 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // This file is part of Eigen, a lightweight C++ template library
2
+ // for linear algebra.
3
+ //
4
+ // This Source Code Form is subject to the terms of the Mozilla
5
+ // Public License v. 2.0. If a copy of the MPL was not distributed
6
+ // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
7
+
8
+ #include <sstream>
9
+
10
+ #include "main.h"
11
+
12
+ #include <Eigen/src/Core/arch/Default/Half.h>
13
+
14
+ #define VERIFY_HALF_BITS_EQUAL(h, bits) \
15
+ VERIFY_IS_EQUAL((numext::bit_cast<numext::uint16_t>(h)), (static_cast<numext::uint16_t>(bits)))
16
+
17
+ // Make sure it's possible to forward declare Eigen::half
18
+ namespace Eigen {
19
+ struct half;
20
+ }
21
+
22
+ using Eigen::half;
23
+
24
+ void test_conversion()
25
+ {
26
+ using Eigen::half_impl::__half_raw;
27
+
28
+ // Round-trip bit-cast with uint16.
29
+ VERIFY_IS_EQUAL(
30
+ numext::bit_cast<half>(numext::bit_cast<numext::uint16_t>(half(1.0f))),
31
+ half(1.0f));
32
+ VERIFY_IS_EQUAL(
33
+ numext::bit_cast<half>(numext::bit_cast<numext::uint16_t>(half(0.5f))),
34
+ half(0.5f));
35
+ VERIFY_IS_EQUAL(
36
+ numext::bit_cast<half>(numext::bit_cast<numext::uint16_t>(half(-0.33333f))),
37
+ half(-0.33333f));
38
+ VERIFY_IS_EQUAL(
39
+ numext::bit_cast<half>(numext::bit_cast<numext::uint16_t>(half(0.0f))),
40
+ half(0.0f));
41
+
42
+ // Conversion from float.
43
+ VERIFY_HALF_BITS_EQUAL(half(1.0f), 0x3c00);
44
+ VERIFY_HALF_BITS_EQUAL(half(0.5f), 0x3800);
45
+ VERIFY_HALF_BITS_EQUAL(half(0.33333f), 0x3555);
46
+ VERIFY_HALF_BITS_EQUAL(half(0.0f), 0x0000);
47
+ VERIFY_HALF_BITS_EQUAL(half(-0.0f), 0x8000);
48
+ VERIFY_HALF_BITS_EQUAL(half(65504.0f), 0x7bff);
49
+ VERIFY_HALF_BITS_EQUAL(half(65536.0f), 0x7c00); // Becomes infinity.
50
+
51
+ // Denormals.
52
+ VERIFY_HALF_BITS_EQUAL(half(-5.96046e-08f), 0x8001);
53
+ VERIFY_HALF_BITS_EQUAL(half(5.96046e-08f), 0x0001);
54
+ VERIFY_HALF_BITS_EQUAL(half(1.19209e-07f), 0x0002);
55
+
56
+ // Verify round-to-nearest-even behavior.
57
+ float val1 = float(half(__half_raw(0x3c00)));
58
+ float val2 = float(half(__half_raw(0x3c01)));
59
+ float val3 = float(half(__half_raw(0x3c02)));
60
+ VERIFY_HALF_BITS_EQUAL(half(0.5f * (val1 + val2)), 0x3c00);
61
+ VERIFY_HALF_BITS_EQUAL(half(0.5f * (val2 + val3)), 0x3c02);
62
+
63
+ // Conversion from int.
64
+ VERIFY_HALF_BITS_EQUAL(half(-1), 0xbc00);
65
+ VERIFY_HALF_BITS_EQUAL(half(0), 0x0000);
66
+ VERIFY_HALF_BITS_EQUAL(half(1), 0x3c00);
67
+ VERIFY_HALF_BITS_EQUAL(half(2), 0x4000);
68
+ VERIFY_HALF_BITS_EQUAL(half(3), 0x4200);
69
+
70
+ // Conversion from bool.
71
+ VERIFY_HALF_BITS_EQUAL(half(false), 0x0000);
72
+ VERIFY_HALF_BITS_EQUAL(half(true), 0x3c00);
73
+
74
+ // Conversion to float.
75
+ VERIFY_IS_EQUAL(float(half(__half_raw(0x0000))), 0.0f);
76
+ VERIFY_IS_EQUAL(float(half(__half_raw(0x3c00))), 1.0f);
77
+
78
+ // Denormals.
79
+ VERIFY_IS_APPROX(float(half(__half_raw(0x8001))), -5.96046e-08f);
80
+ VERIFY_IS_APPROX(float(half(__half_raw(0x0001))), 5.96046e-08f);
81
+ VERIFY_IS_APPROX(float(half(__half_raw(0x0002))), 1.19209e-07f);
82
+
83
+ // NaNs and infinities.
84
+ VERIFY(!(numext::isinf)(float(half(65504.0f)))); // Largest finite number.
85
+ VERIFY(!(numext::isnan)(float(half(0.0f))));
86
+ VERIFY((numext::isinf)(float(half(__half_raw(0xfc00)))));
87
+ VERIFY((numext::isnan)(float(half(__half_raw(0xfc01)))));
88
+ VERIFY((numext::isinf)(float(half(__half_raw(0x7c00)))));
89
+ VERIFY((numext::isnan)(float(half(__half_raw(0x7c01)))));
90
+
91
+ #if !EIGEN_COMP_MSVC
92
+ // Visual Studio errors out on divisions by 0
93
+ VERIFY((numext::isnan)(float(half(0.0 / 0.0))));
94
+ VERIFY((numext::isinf)(float(half(1.0 / 0.0))));
95
+ VERIFY((numext::isinf)(float(half(-1.0 / 0.0))));
96
+ #endif
97
+
98
+ // Exactly same checks as above, just directly on the half representation.
99
+ VERIFY(!(numext::isinf)(half(__half_raw(0x7bff))));
100
+ VERIFY(!(numext::isnan)(half(__half_raw(0x0000))));
101
+ VERIFY((numext::isinf)(half(__half_raw(0xfc00))));
102
+ VERIFY((numext::isnan)(half(__half_raw(0xfc01))));
103
+ VERIFY((numext::isinf)(half(__half_raw(0x7c00))));
104
+ VERIFY((numext::isnan)(half(__half_raw(0x7c01))));
105
+
106
+ #if !EIGEN_COMP_MSVC
107
+ // Visual Studio errors out on divisions by 0
108
+ VERIFY((numext::isnan)(half(0.0 / 0.0)));
109
+ VERIFY((numext::isinf)(half(1.0 / 0.0)));
110
+ VERIFY((numext::isinf)(half(-1.0 / 0.0)));
111
+ #endif
112
+
113
+ // Conversion to bool
114
+ VERIFY(!static_cast<bool>(half(0.0)));
115
+ VERIFY(!static_cast<bool>(half(-0.0)));
116
+ VERIFY(static_cast<bool>(half(__half_raw(0x7bff))));
117
+ VERIFY(static_cast<bool>(half(-0.33333)));
118
+ VERIFY(static_cast<bool>(half(1.0)));
119
+ VERIFY(static_cast<bool>(half(-1.0)));
120
+ VERIFY(static_cast<bool>(half(-5.96046e-08f)));
121
+ }
122
+
123
+ void test_numtraits()
124
+ {
125
+ std::cout << "epsilon = " << NumTraits<half>::epsilon() << " (0x" << std::hex << numext::bit_cast<numext::uint16_t>(NumTraits<half>::epsilon()) << ")" << std::endl;
126
+ std::cout << "highest = " << NumTraits<half>::highest() << " (0x" << std::hex << numext::bit_cast<numext::uint16_t>(NumTraits<half>::highest()) << ")" << std::endl;
127
+ std::cout << "lowest = " << NumTraits<half>::lowest() << " (0x" << std::hex << numext::bit_cast<numext::uint16_t>(NumTraits<half>::lowest()) << ")" << std::endl;
128
+ std::cout << "min = " << (std::numeric_limits<half>::min)() << " (0x" << std::hex << numext::bit_cast<numext::uint16_t>(half((std::numeric_limits<half>::min)())) << ")" << std::endl;
129
+ std::cout << "denorm min = " << (std::numeric_limits<half>::denorm_min)() << " (0x" << std::hex << numext::bit_cast<numext::uint16_t>(half((std::numeric_limits<half>::denorm_min)())) << ")" << std::endl;
130
+ std::cout << "infinity = " << NumTraits<half>::infinity() << " (0x" << std::hex << numext::bit_cast<numext::uint16_t>(NumTraits<half>::infinity()) << ")" << std::endl;
131
+ std::cout << "quiet nan = " << NumTraits<half>::quiet_NaN() << " (0x" << std::hex << numext::bit_cast<numext::uint16_t>(NumTraits<half>::quiet_NaN()) << ")" << std::endl;
132
+ std::cout << "signaling nan = " << std::numeric_limits<half>::signaling_NaN() << " (0x" << std::hex << numext::bit_cast<numext::uint16_t>(std::numeric_limits<half>::signaling_NaN()) << ")" << std::endl;
133
+
134
+ VERIFY(NumTraits<half>::IsSigned);
135
+
136
+ VERIFY_IS_EQUAL(
137
+ numext::bit_cast<numext::uint16_t>(std::numeric_limits<half>::infinity()),
138
+ numext::bit_cast<numext::uint16_t>(half(std::numeric_limits<float>::infinity())) );
139
+ // There is no guarantee that casting a 32-bit NaN to 16-bit has a precise
140
+ // bit pattern. We test that it is in fact a NaN, then test the signaling
141
+ // bit (msb of significand is 1 for quiet, 0 for signaling).
142
+ const numext::uint16_t HALF_QUIET_BIT = 0x0200;
143
+ VERIFY(
144
+ (numext::isnan)(std::numeric_limits<half>::quiet_NaN())
145
+ && (numext::isnan)(half(std::numeric_limits<float>::quiet_NaN()))
146
+ && ((numext::bit_cast<numext::uint16_t>(std::numeric_limits<half>::quiet_NaN()) & HALF_QUIET_BIT) > 0)
147
+ && ((numext::bit_cast<numext::uint16_t>(half(std::numeric_limits<float>::quiet_NaN())) & HALF_QUIET_BIT) > 0) );
148
+ // After a cast to half, a signaling NaN may become non-signaling
149
+ // (e.g. in the case of casting float to native __fp16). Thus, we check that
150
+ // both are NaN, and that only the `numeric_limits` version is signaling.
151
+ VERIFY(
152
+ (numext::isnan)(std::numeric_limits<half>::signaling_NaN())
153
+ && (numext::isnan)(half(std::numeric_limits<float>::signaling_NaN()))
154
+ && ((numext::bit_cast<numext::uint16_t>(std::numeric_limits<half>::signaling_NaN()) & HALF_QUIET_BIT) == 0) );
155
+
156
+ VERIFY( (std::numeric_limits<half>::min)() > half(0.f) );
157
+ VERIFY( (std::numeric_limits<half>::denorm_min)() > half(0.f) );
158
+ VERIFY( (std::numeric_limits<half>::min)()/half(2) > half(0.f) );
159
+ VERIFY_IS_EQUAL( (std::numeric_limits<half>::denorm_min)()/half(2), half(0.f) );
160
+ }
161
+
162
+ void test_arithmetic()
163
+ {
164
+ VERIFY_IS_EQUAL(float(half(2) + half(2)), 4);
165
+ VERIFY_IS_EQUAL(float(half(2) + half(-2)), 0);
166
+ VERIFY_IS_APPROX(float(half(0.33333f) + half(0.66667f)), 1.0f);
167
+ VERIFY_IS_EQUAL(float(half(2.0f) * half(-5.5f)), -11.0f);
168
+ VERIFY_IS_APPROX(float(half(1.0f) / half(3.0f)), 0.33333f);
169
+ VERIFY_IS_EQUAL(float(-half(4096.0f)), -4096.0f);
170
+ VERIFY_IS_EQUAL(float(-half(-4096.0f)), 4096.0f);
171
+
172
+ half x(3);
173
+ half y = ++x;
174
+ VERIFY_IS_EQUAL(x, half(4));
175
+ VERIFY_IS_EQUAL(y, half(4));
176
+ y = --x;
177
+ VERIFY_IS_EQUAL(x, half(3));
178
+ VERIFY_IS_EQUAL(y, half(3));
179
+ y = x++;
180
+ VERIFY_IS_EQUAL(x, half(4));
181
+ VERIFY_IS_EQUAL(y, half(3));
182
+ y = x--;
183
+ VERIFY_IS_EQUAL(x, half(3));
184
+ VERIFY_IS_EQUAL(y, half(4));
185
+ }
186
+
187
+ void test_comparison()
188
+ {
189
+ VERIFY(half(1.0f) > half(0.5f));
190
+ VERIFY(half(0.5f) < half(1.0f));
191
+ VERIFY(!(half(1.0f) < half(0.5f)));
192
+ VERIFY(!(half(0.5f) > half(1.0f)));
193
+
194
+ VERIFY(!(half(4.0f) > half(4.0f)));
195
+ VERIFY(!(half(4.0f) < half(4.0f)));
196
+
197
+ VERIFY(!(half(0.0f) < half(-0.0f)));
198
+ VERIFY(!(half(-0.0f) < half(0.0f)));
199
+ VERIFY(!(half(0.0f) > half(-0.0f)));
200
+ VERIFY(!(half(-0.0f) > half(0.0f)));
201
+
202
+ VERIFY(half(0.2f) > half(-1.0f));
203
+ VERIFY(half(-1.0f) < half(0.2f));
204
+ VERIFY(half(-16.0f) < half(-15.0f));
205
+
206
+ VERIFY(half(1.0f) == half(1.0f));
207
+ VERIFY(half(1.0f) != half(2.0f));
208
+
209
+ // Comparisons with NaNs and infinities.
210
+ #if !EIGEN_COMP_MSVC
211
+ // Visual Studio errors out on divisions by 0
212
+ VERIFY(!(half(0.0 / 0.0) == half(0.0 / 0.0)));
213
+ VERIFY(half(0.0 / 0.0) != half(0.0 / 0.0));
214
+
215
+ VERIFY(!(half(1.0) == half(0.0 / 0.0)));
216
+ VERIFY(!(half(1.0) < half(0.0 / 0.0)));
217
+ VERIFY(!(half(1.0) > half(0.0 / 0.0)));
218
+ VERIFY(half(1.0) != half(0.0 / 0.0));
219
+
220
+ VERIFY(half(1.0) < half(1.0 / 0.0));
221
+ VERIFY(half(1.0) > half(-1.0 / 0.0));
222
+ #endif
223
+ }
224
+
225
+ void test_basic_functions()
226
+ {
227
+ const float PI = static_cast<float>(EIGEN_PI);
228
+
229
+ VERIFY_IS_EQUAL(float(numext::abs(half(3.5f))), 3.5f);
230
+ VERIFY_IS_EQUAL(float(abs(half(3.5f))), 3.5f);
231
+ VERIFY_IS_EQUAL(float(numext::abs(half(-3.5f))), 3.5f);
232
+ VERIFY_IS_EQUAL(float(abs(half(-3.5f))), 3.5f);
233
+
234
+ VERIFY_IS_EQUAL(float(numext::floor(half(3.5f))), 3.0f);
235
+ VERIFY_IS_EQUAL(float(floor(half(3.5f))), 3.0f);
236
+ VERIFY_IS_EQUAL(float(numext::floor(half(-3.5f))), -4.0f);
237
+ VERIFY_IS_EQUAL(float(floor(half(-3.5f))), -4.0f);
238
+
239
+ VERIFY_IS_EQUAL(float(numext::ceil(half(3.5f))), 4.0f);
240
+ VERIFY_IS_EQUAL(float(ceil(half(3.5f))), 4.0f);
241
+ VERIFY_IS_EQUAL(float(numext::ceil(half(-3.5f))), -3.0f);
242
+ VERIFY_IS_EQUAL(float(ceil(half(-3.5f))), -3.0f);
243
+
244
+ VERIFY_IS_APPROX(float(numext::sqrt(half(0.0f))), 0.0f);
245
+ VERIFY_IS_APPROX(float(sqrt(half(0.0f))), 0.0f);
246
+ VERIFY_IS_APPROX(float(numext::sqrt(half(4.0f))), 2.0f);
247
+ VERIFY_IS_APPROX(float(sqrt(half(4.0f))), 2.0f);
248
+
249
+ VERIFY_IS_APPROX(float(numext::pow(half(0.0f), half(1.0f))), 0.0f);
250
+ VERIFY_IS_APPROX(float(pow(half(0.0f), half(1.0f))), 0.0f);
251
+ VERIFY_IS_APPROX(float(numext::pow(half(2.0f), half(2.0f))), 4.0f);
252
+ VERIFY_IS_APPROX(float(pow(half(2.0f), half(2.0f))), 4.0f);
253
+
254
+ VERIFY_IS_EQUAL(float(numext::exp(half(0.0f))), 1.0f);
255
+ VERIFY_IS_EQUAL(float(exp(half(0.0f))), 1.0f);
256
+ VERIFY_IS_APPROX(float(numext::exp(half(PI))), 20.f + PI);
257
+ VERIFY_IS_APPROX(float(exp(half(PI))), 20.f + PI);
258
+
259
+ VERIFY_IS_EQUAL(float(numext::expm1(half(0.0f))), 0.0f);
260
+ VERIFY_IS_EQUAL(float(expm1(half(0.0f))), 0.0f);
261
+ VERIFY_IS_APPROX(float(numext::expm1(half(2.0f))), 6.3890561f);
262
+ VERIFY_IS_APPROX(float(expm1(half(2.0f))), 6.3890561f);
263
+
264
+ VERIFY_IS_EQUAL(float(numext::log(half(1.0f))), 0.0f);
265
+ VERIFY_IS_EQUAL(float(log(half(1.0f))), 0.0f);
266
+ VERIFY_IS_APPROX(float(numext::log(half(10.0f))), 2.30273f);
267
+ VERIFY_IS_APPROX(float(log(half(10.0f))), 2.30273f);
268
+
269
+ VERIFY_IS_EQUAL(float(numext::log1p(half(0.0f))), 0.0f);
270
+ VERIFY_IS_EQUAL(float(log1p(half(0.0f))), 0.0f);
271
+ VERIFY_IS_APPROX(float(numext::log1p(half(10.0f))), 2.3978953f);
272
+ VERIFY_IS_APPROX(float(log1p(half(10.0f))), 2.3978953f);
273
+
274
+ VERIFY_IS_APPROX(numext::fmod(half(5.3f), half(2.0f)), half(1.3f));
275
+ VERIFY_IS_APPROX(fmod(half(5.3f), half(2.0f)), half(1.3f));
276
+ VERIFY_IS_APPROX(numext::fmod(half(-18.5f), half(-4.2f)), half(-1.7f));
277
+ VERIFY_IS_APPROX(fmod(half(-18.5f), half(-4.2f)), half(-1.7f));
278
+ }
279
+
280
+ void test_trigonometric_functions()
281
+ {
282
+ const float PI = static_cast<float>(EIGEN_PI);
283
+ VERIFY_IS_APPROX(numext::cos(half(0.0f)), half(cosf(0.0f)));
284
+ VERIFY_IS_APPROX(cos(half(0.0f)), half(cosf(0.0f)));
285
+ VERIFY_IS_APPROX(numext::cos(half(PI)), half(cosf(PI)));
286
+ // VERIFY_IS_APPROX(numext::cos(half(PI/2)), half(cosf(PI/2)));
287
+ // VERIFY_IS_APPROX(numext::cos(half(3*PI/2)), half(cosf(3*PI/2)));
288
+ VERIFY_IS_APPROX(numext::cos(half(3.5f)), half(cosf(3.5f)));
289
+
290
+ VERIFY_IS_APPROX(numext::sin(half(0.0f)), half(sinf(0.0f)));
291
+ VERIFY_IS_APPROX(sin(half(0.0f)), half(sinf(0.0f)));
292
+ // VERIFY_IS_APPROX(numext::sin(half(PI)), half(sinf(PI)));
293
+ VERIFY_IS_APPROX(numext::sin(half(PI/2)), half(sinf(PI/2)));
294
+ VERIFY_IS_APPROX(numext::sin(half(3*PI/2)), half(sinf(3*PI/2)));
295
+ VERIFY_IS_APPROX(numext::sin(half(3.5f)), half(sinf(3.5f)));
296
+
297
+ VERIFY_IS_APPROX(numext::tan(half(0.0f)), half(tanf(0.0f)));
298
+ VERIFY_IS_APPROX(tan(half(0.0f)), half(tanf(0.0f)));
299
+ // VERIFY_IS_APPROX(numext::tan(half(PI)), half(tanf(PI)));
300
+ // VERIFY_IS_APPROX(numext::tan(half(PI/2)), half(tanf(PI/2)));
301
+ //VERIFY_IS_APPROX(numext::tan(half(3*PI/2)), half(tanf(3*PI/2)));
302
+ VERIFY_IS_APPROX(numext::tan(half(3.5f)), half(tanf(3.5f)));
303
+ }
304
+
305
+ void test_array()
306
+ {
307
+ typedef Array<half,1,Dynamic> ArrayXh;
308
+ Index size = internal::random<Index>(1,10);
309
+ Index i = internal::random<Index>(0,size-1);
310
+ ArrayXh a1 = ArrayXh::Random(size), a2 = ArrayXh::Random(size);
311
+ VERIFY_IS_APPROX( a1+a1, half(2)*a1 );
312
+ VERIFY( (a1.abs() >= half(0)).all() );
313
+ VERIFY_IS_APPROX( (a1*a1).sqrt(), a1.abs() );
314
+
315
+ VERIFY( ((a1.min)(a2) <= (a1.max)(a2)).all() );
316
+ a1(i) = half(-10.);
317
+ VERIFY_IS_EQUAL( a1.minCoeff(), half(-10.) );
318
+ a1(i) = half(10.);
319
+ VERIFY_IS_EQUAL( a1.maxCoeff(), half(10.) );
320
+
321
+ std::stringstream ss;
322
+ ss << a1;
323
+ }
324
+
325
+ void test_product()
326
+ {
327
+ typedef Matrix<half,Dynamic,Dynamic> MatrixXh;
328
+ Index rows = internal::random<Index>(1,EIGEN_TEST_MAX_SIZE);
329
+ Index cols = internal::random<Index>(1,EIGEN_TEST_MAX_SIZE);
330
+ Index depth = internal::random<Index>(1,EIGEN_TEST_MAX_SIZE);
331
+ MatrixXh Ah = MatrixXh::Random(rows,depth);
332
+ MatrixXh Bh = MatrixXh::Random(depth,cols);
333
+ MatrixXh Ch = MatrixXh::Random(rows,cols);
334
+ MatrixXf Af = Ah.cast<float>();
335
+ MatrixXf Bf = Bh.cast<float>();
336
+ MatrixXf Cf = Ch.cast<float>();
337
+ VERIFY_IS_APPROX(Ch.noalias()+=Ah*Bh, (Cf.noalias()+=Af*Bf).cast<half>());
338
+ }
339
+
340
+ EIGEN_DECLARE_TEST(half_float)
341
+ {
342
+ CALL_SUBTEST(test_numtraits());
343
+ for(int i = 0; i < g_repeat; i++) {
344
+ CALL_SUBTEST(test_conversion());
345
+ CALL_SUBTEST(test_arithmetic());
346
+ CALL_SUBTEST(test_comparison());
347
+ CALL_SUBTEST(test_basic_functions());
348
+ CALL_SUBTEST(test_trigonometric_functions());
349
+ CALL_SUBTEST(test_array());
350
+ CALL_SUBTEST(test_product());
351
+ }
352
+ }
include/eigen/test/householder.cpp ADDED
@@ -0,0 +1,148 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // This file is part of Eigen, a lightweight C++ template library
2
+ // for linear algebra.
3
+ //
4
+ // Copyright (C) 2009-2010 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
+ #include <Eigen/QR>
12
+
13
+ template<typename MatrixType> void householder(const MatrixType& m)
14
+ {
15
+ static bool even = true;
16
+ even = !even;
17
+ /* this test covers the following files:
18
+ Householder.h
19
+ */
20
+ Index rows = m.rows();
21
+ Index cols = m.cols();
22
+
23
+ typedef typename MatrixType::Scalar Scalar;
24
+ typedef typename NumTraits<Scalar>::Real RealScalar;
25
+ typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
26
+ typedef Matrix<Scalar, internal::decrement_size<MatrixType::RowsAtCompileTime>::ret, 1> EssentialVectorType;
27
+ typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime> SquareMatrixType;
28
+ typedef Matrix<Scalar, Dynamic, MatrixType::ColsAtCompileTime> HBlockMatrixType;
29
+ typedef Matrix<Scalar, Dynamic, 1> HCoeffsVectorType;
30
+
31
+ typedef Matrix<Scalar, MatrixType::ColsAtCompileTime, MatrixType::RowsAtCompileTime> TMatrixType;
32
+
33
+ Matrix<Scalar, EIGEN_SIZE_MAX(MatrixType::RowsAtCompileTime,MatrixType::ColsAtCompileTime), 1> _tmp((std::max)(rows,cols));
34
+ Scalar* tmp = &_tmp.coeffRef(0,0);
35
+
36
+ Scalar beta;
37
+ RealScalar alpha;
38
+ EssentialVectorType essential;
39
+
40
+ VectorType v1 = VectorType::Random(rows), v2;
41
+ v2 = v1;
42
+ v1.makeHouseholder(essential, beta, alpha);
43
+ v1.applyHouseholderOnTheLeft(essential,beta,tmp);
44
+ VERIFY_IS_APPROX(v1.norm(), v2.norm());
45
+ if(rows>=2) VERIFY_IS_MUCH_SMALLER_THAN(v1.tail(rows-1).norm(), v1.norm());
46
+ v1 = VectorType::Random(rows);
47
+ v2 = v1;
48
+ v1.applyHouseholderOnTheLeft(essential,beta,tmp);
49
+ VERIFY_IS_APPROX(v1.norm(), v2.norm());
50
+
51
+ // reconstruct householder matrix:
52
+ SquareMatrixType id, H1, H2;
53
+ id.setIdentity(rows, rows);
54
+ H1 = H2 = id;
55
+ VectorType vv(rows);
56
+ vv << Scalar(1), essential;
57
+ H1.applyHouseholderOnTheLeft(essential, beta, tmp);
58
+ H2.applyHouseholderOnTheRight(essential, beta, tmp);
59
+ VERIFY_IS_APPROX(H1, H2);
60
+ VERIFY_IS_APPROX(H1, id - beta * vv*vv.adjoint());
61
+
62
+ MatrixType m1(rows, cols),
63
+ m2(rows, cols);
64
+
65
+ v1 = VectorType::Random(rows);
66
+ if(even) v1.tail(rows-1).setZero();
67
+ m1.colwise() = v1;
68
+ m2 = m1;
69
+ m1.col(0).makeHouseholder(essential, beta, alpha);
70
+ m1.applyHouseholderOnTheLeft(essential,beta,tmp);
71
+ VERIFY_IS_APPROX(m1.norm(), m2.norm());
72
+ if(rows>=2) VERIFY_IS_MUCH_SMALLER_THAN(m1.block(1,0,rows-1,cols).norm(), m1.norm());
73
+ VERIFY_IS_MUCH_SMALLER_THAN(numext::imag(m1(0,0)), numext::real(m1(0,0)));
74
+ VERIFY_IS_APPROX(numext::real(m1(0,0)), alpha);
75
+
76
+ v1 = VectorType::Random(rows);
77
+ if(even) v1.tail(rows-1).setZero();
78
+ SquareMatrixType m3(rows,rows), m4(rows,rows);
79
+ m3.rowwise() = v1.transpose();
80
+ m4 = m3;
81
+ m3.row(0).makeHouseholder(essential, beta, alpha);
82
+ m3.applyHouseholderOnTheRight(essential.conjugate(),beta,tmp);
83
+ VERIFY_IS_APPROX(m3.norm(), m4.norm());
84
+ if(rows>=2) VERIFY_IS_MUCH_SMALLER_THAN(m3.block(0,1,rows,rows-1).norm(), m3.norm());
85
+ VERIFY_IS_MUCH_SMALLER_THAN(numext::imag(m3(0,0)), numext::real(m3(0,0)));
86
+ VERIFY_IS_APPROX(numext::real(m3(0,0)), alpha);
87
+
88
+ // test householder sequence on the left with a shift
89
+
90
+ Index shift = internal::random<Index>(0, std::max<Index>(rows-2,0));
91
+ Index brows = rows - shift;
92
+ m1.setRandom(rows, cols);
93
+ HBlockMatrixType hbm = m1.block(shift,0,brows,cols);
94
+ HouseholderQR<HBlockMatrixType> qr(hbm);
95
+ m2 = m1;
96
+ m2.block(shift,0,brows,cols) = qr.matrixQR();
97
+ HCoeffsVectorType hc = qr.hCoeffs().conjugate();
98
+ HouseholderSequence<MatrixType, HCoeffsVectorType> hseq(m2, hc);
99
+ hseq.setLength(hc.size()).setShift(shift);
100
+ VERIFY(hseq.length() == hc.size());
101
+ VERIFY(hseq.shift() == shift);
102
+
103
+ MatrixType m5 = m2;
104
+ m5.block(shift,0,brows,cols).template triangularView<StrictlyLower>().setZero();
105
+ VERIFY_IS_APPROX(hseq * m5, m1); // test applying hseq directly
106
+ m3 = hseq;
107
+ VERIFY_IS_APPROX(m3 * m5, m1); // test evaluating hseq to a dense matrix, then applying
108
+
109
+ SquareMatrixType hseq_mat = hseq;
110
+ SquareMatrixType hseq_mat_conj = hseq.conjugate();
111
+ SquareMatrixType hseq_mat_adj = hseq.adjoint();
112
+ SquareMatrixType hseq_mat_trans = hseq.transpose();
113
+ SquareMatrixType m6 = SquareMatrixType::Random(rows, rows);
114
+ VERIFY_IS_APPROX(hseq_mat.adjoint(), hseq_mat_adj);
115
+ VERIFY_IS_APPROX(hseq_mat.conjugate(), hseq_mat_conj);
116
+ VERIFY_IS_APPROX(hseq_mat.transpose(), hseq_mat_trans);
117
+ VERIFY_IS_APPROX(hseq * m6, hseq_mat * m6);
118
+ VERIFY_IS_APPROX(hseq.adjoint() * m6, hseq_mat_adj * m6);
119
+ VERIFY_IS_APPROX(hseq.conjugate() * m6, hseq_mat_conj * m6);
120
+ VERIFY_IS_APPROX(hseq.transpose() * m6, hseq_mat_trans * m6);
121
+ VERIFY_IS_APPROX(m6 * hseq, m6 * hseq_mat);
122
+ VERIFY_IS_APPROX(m6 * hseq.adjoint(), m6 * hseq_mat_adj);
123
+ VERIFY_IS_APPROX(m6 * hseq.conjugate(), m6 * hseq_mat_conj);
124
+ VERIFY_IS_APPROX(m6 * hseq.transpose(), m6 * hseq_mat_trans);
125
+
126
+ // test householder sequence on the right with a shift
127
+
128
+ TMatrixType tm2 = m2.transpose();
129
+ HouseholderSequence<TMatrixType, HCoeffsVectorType, OnTheRight> rhseq(tm2, hc);
130
+ rhseq.setLength(hc.size()).setShift(shift);
131
+ VERIFY_IS_APPROX(rhseq * m5, m1); // test applying rhseq directly
132
+ m3 = rhseq;
133
+ VERIFY_IS_APPROX(m3 * m5, m1); // test evaluating rhseq to a dense matrix, then applying
134
+ }
135
+
136
+ EIGEN_DECLARE_TEST(householder)
137
+ {
138
+ for(int i = 0; i < g_repeat; i++) {
139
+ CALL_SUBTEST_1( householder(Matrix<double,2,2>()) );
140
+ CALL_SUBTEST_2( householder(Matrix<float,2,3>()) );
141
+ CALL_SUBTEST_3( householder(Matrix<double,3,5>()) );
142
+ CALL_SUBTEST_4( householder(Matrix<float,4,4>()) );
143
+ CALL_SUBTEST_5( householder(MatrixXd(internal::random<int>(1,EIGEN_TEST_MAX_SIZE),internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
144
+ CALL_SUBTEST_6( householder(MatrixXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE),internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
145
+ CALL_SUBTEST_7( householder(MatrixXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE),internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
146
+ CALL_SUBTEST_8( householder(Matrix<double,1,1>()) );
147
+ }
148
+ }
include/eigen/test/incomplete_cholesky.cpp ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // This file is part of Eigen, a lightweight C++ template library
2
+ // for linear algebra.
3
+ //
4
+ // Copyright (C) 2015-2016 Gael Guennebaud <gael.guennebaud@inria.fr>
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
+ // #define EIGEN_DONT_VECTORIZE
10
+ // #define EIGEN_MAX_ALIGN_BYTES 0
11
+ #include "sparse_solver.h"
12
+ #include <Eigen/IterativeLinearSolvers>
13
+ #include <unsupported/Eigen/IterativeSolvers>
14
+
15
+ template<typename T, typename I_> void test_incomplete_cholesky_T()
16
+ {
17
+ typedef SparseMatrix<T,0,I_> SparseMatrixType;
18
+ ConjugateGradient<SparseMatrixType, Lower, IncompleteCholesky<T, Lower, AMDOrdering<I_> > > cg_illt_lower_amd;
19
+ ConjugateGradient<SparseMatrixType, Lower, IncompleteCholesky<T, Lower, NaturalOrdering<I_> > > cg_illt_lower_nat;
20
+ ConjugateGradient<SparseMatrixType, Upper, IncompleteCholesky<T, Upper, AMDOrdering<I_> > > cg_illt_upper_amd;
21
+ ConjugateGradient<SparseMatrixType, Upper, IncompleteCholesky<T, Upper, NaturalOrdering<I_> > > cg_illt_upper_nat;
22
+ ConjugateGradient<SparseMatrixType, Upper|Lower, IncompleteCholesky<T, Lower, AMDOrdering<I_> > > cg_illt_uplo_amd;
23
+
24
+
25
+ CALL_SUBTEST( check_sparse_spd_solving(cg_illt_lower_amd) );
26
+ CALL_SUBTEST( check_sparse_spd_solving(cg_illt_lower_nat) );
27
+ CALL_SUBTEST( check_sparse_spd_solving(cg_illt_upper_amd) );
28
+ CALL_SUBTEST( check_sparse_spd_solving(cg_illt_upper_nat) );
29
+ CALL_SUBTEST( check_sparse_spd_solving(cg_illt_uplo_amd) );
30
+ }
31
+
32
+ template<int>
33
+ void bug1150()
34
+ {
35
+ // regression for bug 1150
36
+ for(int N = 1; N<20; ++N)
37
+ {
38
+ Eigen::MatrixXd b( N, N );
39
+ b.setOnes();
40
+
41
+ Eigen::SparseMatrix<double> m( N, N );
42
+ m.reserve(Eigen::VectorXi::Constant(N,4));
43
+ for( int i = 0; i < N; ++i )
44
+ {
45
+ m.insert( i, i ) = 1;
46
+ m.coeffRef( i, i / 2 ) = 2;
47
+ m.coeffRef( i, i / 3 ) = 2;
48
+ m.coeffRef( i, i / 4 ) = 2;
49
+ }
50
+
51
+ Eigen::SparseMatrix<double> A;
52
+ A = m * m.transpose();
53
+
54
+ Eigen::ConjugateGradient<Eigen::SparseMatrix<double>,
55
+ Eigen::Lower | Eigen::Upper,
56
+ Eigen::IncompleteCholesky<double> > solver( A );
57
+ VERIFY(solver.preconditioner().info() == Eigen::Success);
58
+ VERIFY(solver.info() == Eigen::Success);
59
+ }
60
+ }
61
+
62
+ EIGEN_DECLARE_TEST(incomplete_cholesky)
63
+ {
64
+ CALL_SUBTEST_1(( test_incomplete_cholesky_T<double,int>() ));
65
+ CALL_SUBTEST_2(( test_incomplete_cholesky_T<std::complex<double>, int>() ));
66
+ CALL_SUBTEST_3(( test_incomplete_cholesky_T<double,long int>() ));
67
+
68
+ CALL_SUBTEST_1(( bug1150<0>() ));
69
+ }
include/eigen/test/indexed_view.cpp ADDED
@@ -0,0 +1,473 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // This file is part of Eigen, a lightweight C++ template library
2
+ // for linear algebra.
3
+ //
4
+ // Copyright (C) 2017 Gael Guennebaud <gael.guennebaud@inria.fr>
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
+ #ifdef EIGEN_TEST_PART_2
11
+ // Make sure we also check c++11 max implementation
12
+ #define EIGEN_MAX_CPP_VER 11
13
+ #endif
14
+
15
+ #ifdef EIGEN_TEST_PART_3
16
+ // Make sure we also check c++98 max implementation
17
+ #define EIGEN_MAX_CPP_VER 03
18
+
19
+ // We need to disable this warning when compiling with c++11 while limiting Eigen to c++98
20
+ // Ideally we would rather configure the compiler to build in c++98 mode but this needs
21
+ // to be done at the CMakeLists.txt level.
22
+ #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8))
23
+ #pragma GCC diagnostic ignored "-Wdeprecated"
24
+ #endif
25
+
26
+ #if defined(__GNUC__) && (__GNUC__ >=9)
27
+ #pragma GCC diagnostic ignored "-Wdeprecated-copy"
28
+ #endif
29
+ #if defined(__clang__) && (__clang_major__ >= 10)
30
+ #pragma clang diagnostic ignored "-Wdeprecated-copy"
31
+ #endif
32
+
33
+ #endif
34
+
35
+ #include <valarray>
36
+ #include <vector>
37
+ #include "main.h"
38
+
39
+ #if EIGEN_HAS_CXX11
40
+ #include <array>
41
+ #endif
42
+
43
+ typedef std::pair<Index,Index> IndexPair;
44
+
45
+ int encode(Index i, Index j) {
46
+ return int(i*100 + j);
47
+ }
48
+
49
+ IndexPair decode(Index ij) {
50
+ return IndexPair(ij / 100, ij % 100);
51
+ }
52
+
53
+ template<typename T>
54
+ bool match(const T& xpr, std::string ref, std::string str_xpr = "") {
55
+ EIGEN_UNUSED_VARIABLE(str_xpr);
56
+ std::stringstream str;
57
+ str << xpr;
58
+ if(!(str.str() == ref))
59
+ std::cout << str_xpr << "\n" << xpr << "\n\n";
60
+ return str.str() == ref;
61
+ }
62
+
63
+ #define MATCH(X,R) match(X, R, #X)
64
+
65
+ template<typename T1,typename T2>
66
+ typename internal::enable_if<internal::is_same<T1,T2>::value,bool>::type
67
+ is_same_eq(const T1& a, const T2& b)
68
+ {
69
+ return (a == b).all();
70
+ }
71
+
72
+ template<typename T1,typename T2>
73
+ bool is_same_seq(const T1& a, const T2& b)
74
+ {
75
+ bool ok = a.first()==b.first() && a.size() == b.size() && Index(a.incrObject())==Index(b.incrObject());;
76
+ if(!ok)
77
+ {
78
+ std::cerr << "seqN(" << a.first() << ", " << a.size() << ", " << Index(a.incrObject()) << ") != ";
79
+ std::cerr << "seqN(" << b.first() << ", " << b.size() << ", " << Index(b.incrObject()) << ")\n";
80
+ }
81
+ return ok;
82
+ }
83
+
84
+ template<typename T1,typename T2>
85
+ typename internal::enable_if<internal::is_same<T1,T2>::value,bool>::type
86
+ is_same_seq_type(const T1& a, const T2& b)
87
+ {
88
+ return is_same_seq(a,b);
89
+ }
90
+
91
+
92
+
93
+ #define VERIFY_EQ_INT(A,B) VERIFY_IS_APPROX(int(A),int(B))
94
+
95
+ // C++03 does not allow local or unnamed enums as index
96
+ enum DummyEnum { XX=0, YY=1 };
97
+
98
+ void check_indexed_view()
99
+ {
100
+ Index n = 10;
101
+
102
+ ArrayXd a = ArrayXd::LinSpaced(n,0,n-1);
103
+ Array<double,1,Dynamic> b = a.transpose();
104
+
105
+ #if EIGEN_COMP_CXXVER>=14
106
+ ArrayXXi A = ArrayXXi::NullaryExpr(n,n, std::ref(encode));
107
+ #else
108
+ ArrayXXi A = ArrayXXi::NullaryExpr(n,n, std::ptr_fun(&encode));
109
+ #endif
110
+
111
+ for(Index i=0; i<n; ++i)
112
+ for(Index j=0; j<n; ++j)
113
+ VERIFY( decode(A(i,j)) == IndexPair(i,j) );
114
+
115
+ Array4i eii(4); eii << 3, 1, 6, 5;
116
+ std::valarray<int> vali(4); Map<ArrayXi>(&vali[0],4) = eii;
117
+ std::vector<int> veci(4); Map<ArrayXi>(veci.data(),4) = eii;
118
+
119
+ VERIFY( MATCH( A(3, seq(9,3,-1)),
120
+ "309 308 307 306 305 304 303")
121
+ );
122
+
123
+ VERIFY( MATCH( A(seqN(2,5), seq(9,3,-1)),
124
+ "209 208 207 206 205 204 203\n"
125
+ "309 308 307 306 305 304 303\n"
126
+ "409 408 407 406 405 404 403\n"
127
+ "509 508 507 506 505 504 503\n"
128
+ "609 608 607 606 605 604 603")
129
+ );
130
+
131
+ VERIFY( MATCH( A(seqN(2,5), 5),
132
+ "205\n"
133
+ "305\n"
134
+ "405\n"
135
+ "505\n"
136
+ "605")
137
+ );
138
+
139
+ VERIFY( MATCH( A(seqN(last,5,-1), seq(2,last)),
140
+ "902 903 904 905 906 907 908 909\n"
141
+ "802 803 804 805 806 807 808 809\n"
142
+ "702 703 704 705 706 707 708 709\n"
143
+ "602 603 604 605 606 607 608 609\n"
144
+ "502 503 504 505 506 507 508 509")
145
+ );
146
+
147
+ VERIFY( MATCH( A(eii, veci),
148
+ "303 301 306 305\n"
149
+ "103 101 106 105\n"
150
+ "603 601 606 605\n"
151
+ "503 501 506 505")
152
+ );
153
+
154
+ VERIFY( MATCH( A(eii, all),
155
+ "300 301 302 303 304 305 306 307 308 309\n"
156
+ "100 101 102 103 104 105 106 107 108 109\n"
157
+ "600 601 602 603 604 605 606 607 608 609\n"
158
+ "500 501 502 503 504 505 506 507 508 509")
159
+ );
160
+
161
+ // take row number 3, and repeat it 5 times
162
+ VERIFY( MATCH( A(seqN(3,5,0), all),
163
+ "300 301 302 303 304 305 306 307 308 309\n"
164
+ "300 301 302 303 304 305 306 307 308 309\n"
165
+ "300 301 302 303 304 305 306 307 308 309\n"
166
+ "300 301 302 303 304 305 306 307 308 309\n"
167
+ "300 301 302 303 304 305 306 307 308 309")
168
+ );
169
+
170
+ VERIFY( MATCH( a(seqN(3,3),0), "3\n4\n5" ) );
171
+ VERIFY( MATCH( a(seq(3,5)), "3\n4\n5" ) );
172
+ VERIFY( MATCH( a(seqN(3,3,1)), "3\n4\n5" ) );
173
+ VERIFY( MATCH( a(seqN(5,3,-1)), "5\n4\n3" ) );
174
+
175
+ VERIFY( MATCH( b(0,seqN(3,3)), "3 4 5" ) );
176
+ VERIFY( MATCH( b(seq(3,5)), "3 4 5" ) );
177
+ VERIFY( MATCH( b(seqN(3,3,1)), "3 4 5" ) );
178
+ VERIFY( MATCH( b(seqN(5,3,-1)), "5 4 3" ) );
179
+
180
+ VERIFY( MATCH( b(all), "0 1 2 3 4 5 6 7 8 9" ) );
181
+ VERIFY( MATCH( b(eii), "3 1 6 5" ) );
182
+
183
+ Array44i B;
184
+ B.setRandom();
185
+ VERIFY( (A(seqN(2,5), 5)).ColsAtCompileTime == 1);
186
+ VERIFY( (A(seqN(2,5), 5)).RowsAtCompileTime == Dynamic);
187
+ VERIFY_EQ_INT( (A(seqN(2,5), 5)).InnerStrideAtCompileTime , A.InnerStrideAtCompileTime);
188
+ VERIFY_EQ_INT( (A(seqN(2,5), 5)).OuterStrideAtCompileTime , A.col(5).OuterStrideAtCompileTime);
189
+
190
+ VERIFY_EQ_INT( (A(5,seqN(2,5))).InnerStrideAtCompileTime , A.row(5).InnerStrideAtCompileTime);
191
+ VERIFY_EQ_INT( (A(5,seqN(2,5))).OuterStrideAtCompileTime , A.row(5).OuterStrideAtCompileTime);
192
+ VERIFY_EQ_INT( (B(1,seqN(1,2))).InnerStrideAtCompileTime , B.row(1).InnerStrideAtCompileTime);
193
+ VERIFY_EQ_INT( (B(1,seqN(1,2))).OuterStrideAtCompileTime , B.row(1).OuterStrideAtCompileTime);
194
+
195
+ VERIFY_EQ_INT( (A(seqN(2,5), seq(1,3))).InnerStrideAtCompileTime , A.InnerStrideAtCompileTime);
196
+ VERIFY_EQ_INT( (A(seqN(2,5), seq(1,3))).OuterStrideAtCompileTime , A.OuterStrideAtCompileTime);
197
+ VERIFY_EQ_INT( (B(seqN(1,2), seq(1,3))).InnerStrideAtCompileTime , B.InnerStrideAtCompileTime);
198
+ VERIFY_EQ_INT( (B(seqN(1,2), seq(1,3))).OuterStrideAtCompileTime , B.OuterStrideAtCompileTime);
199
+ VERIFY_EQ_INT( (A(seqN(2,5,2), seq(1,3,2))).InnerStrideAtCompileTime , Dynamic);
200
+ VERIFY_EQ_INT( (A(seqN(2,5,2), seq(1,3,2))).OuterStrideAtCompileTime , Dynamic);
201
+ VERIFY_EQ_INT( (A(seqN(2,5,fix<2>), seq(1,3,fix<3>))).InnerStrideAtCompileTime , 2);
202
+ VERIFY_EQ_INT( (A(seqN(2,5,fix<2>), seq(1,3,fix<3>))).OuterStrideAtCompileTime , Dynamic);
203
+ VERIFY_EQ_INT( (B(seqN(1,2,fix<2>), seq(1,3,fix<3>))).InnerStrideAtCompileTime , 2);
204
+ VERIFY_EQ_INT( (B(seqN(1,2,fix<2>), seq(1,3,fix<3>))).OuterStrideAtCompileTime , 3*4);
205
+
206
+ VERIFY_EQ_INT( (A(seqN(2,fix<5>), seqN(1,fix<3>))).RowsAtCompileTime, 5);
207
+ VERIFY_EQ_INT( (A(seqN(2,fix<5>), seqN(1,fix<3>))).ColsAtCompileTime, 3);
208
+ VERIFY_EQ_INT( (A(seqN(2,fix<5>(5)), seqN(1,fix<3>(3)))).RowsAtCompileTime, 5);
209
+ VERIFY_EQ_INT( (A(seqN(2,fix<5>(5)), seqN(1,fix<3>(3)))).ColsAtCompileTime, 3);
210
+ VERIFY_EQ_INT( (A(seqN(2,fix<Dynamic>(5)), seqN(1,fix<Dynamic>(3)))).RowsAtCompileTime, Dynamic);
211
+ VERIFY_EQ_INT( (A(seqN(2,fix<Dynamic>(5)), seqN(1,fix<Dynamic>(3)))).ColsAtCompileTime, Dynamic);
212
+ VERIFY_EQ_INT( (A(seqN(2,fix<Dynamic>(5)), seqN(1,fix<Dynamic>(3)))).rows(), 5);
213
+ VERIFY_EQ_INT( (A(seqN(2,fix<Dynamic>(5)), seqN(1,fix<Dynamic>(3)))).cols(), 3);
214
+
215
+ VERIFY( is_same_seq_type( seqN(2,5,fix<-1>), seqN(2,5,fix<-1>(-1)) ) );
216
+ VERIFY( is_same_seq_type( seqN(2,5), seqN(2,5,fix<1>(1)) ) );
217
+ VERIFY( is_same_seq_type( seqN(2,5,3), seqN(2,5,fix<DynamicIndex>(3)) ) );
218
+ VERIFY( is_same_seq_type( seq(2,7,fix<3>), seqN(2,2,fix<3>) ) );
219
+ VERIFY( is_same_seq_type( seqN(2,fix<Dynamic>(5),3), seqN(2,5,fix<DynamicIndex>(3)) ) );
220
+ VERIFY( is_same_seq_type( seqN(2,fix<5>(5),fix<-2>), seqN(2,fix<5>,fix<-2>()) ) );
221
+
222
+ VERIFY( is_same_seq_type( seq(2,fix<5>), seqN(2,4) ) );
223
+ #if EIGEN_HAS_CXX11
224
+ VERIFY( is_same_seq_type( seq(fix<2>,fix<5>), seqN(fix<2>,fix<4>) ) );
225
+ VERIFY( is_same_seq( seqN(2,std::integral_constant<int,5>(),std::integral_constant<int,-2>()), seqN(2,fix<5>,fix<-2>()) ) );
226
+ VERIFY( is_same_seq( seq(std::integral_constant<int,1>(),std::integral_constant<int,5>(),std::integral_constant<int,2>()),
227
+ seq(fix<1>,fix<5>,fix<2>()) ) );
228
+ VERIFY( is_same_seq_type( seqN(2,std::integral_constant<int,5>(),std::integral_constant<int,-2>()), seqN(2,fix<5>,fix<-2>()) ) );
229
+ VERIFY( is_same_seq_type( seq(std::integral_constant<int,1>(),std::integral_constant<int,5>(),std::integral_constant<int,2>()),
230
+ seq(fix<1>,fix<5>,fix<2>()) ) );
231
+
232
+ VERIFY( is_same_seq_type( seqN(2,std::integral_constant<int,5>()), seqN(2,fix<5>) ) );
233
+ VERIFY( is_same_seq_type( seq(std::integral_constant<int,1>(),std::integral_constant<int,5>()), seq(fix<1>,fix<5>) ) );
234
+ #else
235
+ // sorry, no compile-time size recovery in c++98/03
236
+ VERIFY( is_same_seq( seq(fix<2>,fix<5>), seqN(fix<2>,fix<4>) ) );
237
+ #endif
238
+
239
+ VERIFY( (A(seqN(2,fix<5>), 5)).RowsAtCompileTime == 5);
240
+ VERIFY( (A(4, all)).ColsAtCompileTime == Dynamic);
241
+ VERIFY( (A(4, all)).RowsAtCompileTime == 1);
242
+ VERIFY( (B(1, all)).ColsAtCompileTime == 4);
243
+ VERIFY( (B(1, all)).RowsAtCompileTime == 1);
244
+ VERIFY( (B(all,1)).ColsAtCompileTime == 1);
245
+ VERIFY( (B(all,1)).RowsAtCompileTime == 4);
246
+
247
+ VERIFY(int( (A(all, eii)).ColsAtCompileTime) == int(eii.SizeAtCompileTime));
248
+ VERIFY_EQ_INT( (A(eii, eii)).Flags&DirectAccessBit, (unsigned int)(0));
249
+ VERIFY_EQ_INT( (A(eii, eii)).InnerStrideAtCompileTime, 0);
250
+ VERIFY_EQ_INT( (A(eii, eii)).OuterStrideAtCompileTime, 0);
251
+
252
+ VERIFY_IS_APPROX( A(seq(n-1,2,-2), seqN(n-1-6,3,-1)), A(seq(last,2,fix<-2>), seqN(last-6,3,fix<-1>)) );
253
+
254
+ VERIFY_IS_APPROX( A(seq(n-1,2,-2), seqN(n-1-6,4)), A(seq(last,2,-2), seqN(last-6,4)) );
255
+ VERIFY_IS_APPROX( A(seq(n-1-6,n-1-2), seqN(n-1-6,4)), A(seq(last-6,last-2), seqN(6+last-6-6,4)) );
256
+ VERIFY_IS_APPROX( A(seq((n-1)/2,(n)/2+3), seqN(2,4)), A(seq(last/2,(last+1)/2+3), seqN(last+2-last,4)) );
257
+ VERIFY_IS_APPROX( A(seq(n-2,2,-2), seqN(n-8,4)), A(seq(lastp1-2,2,-2), seqN(lastp1-8,4)) );
258
+
259
+ // Check all combinations of seq:
260
+ VERIFY_IS_APPROX( A(seq(1,n-1-2,2), seq(1,n-1-2,2)), A(seq(1,last-2,2), seq(1,last-2,fix<2>)) );
261
+ VERIFY_IS_APPROX( A(seq(n-1-5,n-1-2,2), seq(n-1-5,n-1-2,2)), A(seq(last-5,last-2,2), seq(last-5,last-2,fix<2>)) );
262
+ VERIFY_IS_APPROX( A(seq(n-1-5,7,2), seq(n-1-5,7,2)), A(seq(last-5,7,2), seq(last-5,7,fix<2>)) );
263
+ VERIFY_IS_APPROX( A(seq(1,n-1-2), seq(n-1-5,7)), A(seq(1,last-2), seq(last-5,7)) );
264
+ VERIFY_IS_APPROX( A(seq(n-1-5,n-1-2), seq(n-1-5,n-1-2)), A(seq(last-5,last-2), seq(last-5,last-2)) );
265
+
266
+ VERIFY_IS_APPROX( A.col(A.cols()-1), A(all,last) );
267
+ VERIFY_IS_APPROX( A(A.rows()-2, A.cols()/2), A(last-1, lastp1/2) );
268
+ VERIFY_IS_APPROX( a(a.size()-2), a(last-1) );
269
+ VERIFY_IS_APPROX( a(a.size()/2), a((last+1)/2) );
270
+
271
+ // Check fall-back to Block
272
+ {
273
+ VERIFY( is_same_eq(A.col(0), A(all,0)) );
274
+ VERIFY( is_same_eq(A.row(0), A(0,all)) );
275
+ VERIFY( is_same_eq(A.block(0,0,2,2), A(seqN(0,2),seq(0,1))) );
276
+ VERIFY( is_same_eq(A.middleRows(2,4), A(seqN(2,4),all)) );
277
+ VERIFY( is_same_eq(A.middleCols(2,4), A(all,seqN(2,4))) );
278
+
279
+ VERIFY( is_same_eq(A.col(A.cols()-1), A(all,last)) );
280
+
281
+ const ArrayXXi& cA(A);
282
+ VERIFY( is_same_eq(cA.col(0), cA(all,0)) );
283
+ VERIFY( is_same_eq(cA.row(0), cA(0,all)) );
284
+ VERIFY( is_same_eq(cA.block(0,0,2,2), cA(seqN(0,2),seq(0,1))) );
285
+ VERIFY( is_same_eq(cA.middleRows(2,4), cA(seqN(2,4),all)) );
286
+ VERIFY( is_same_eq(cA.middleCols(2,4), cA(all,seqN(2,4))) );
287
+
288
+ VERIFY( is_same_eq(a.head(4), a(seq(0,3))) );
289
+ VERIFY( is_same_eq(a.tail(4), a(seqN(last-3,4))) );
290
+ VERIFY( is_same_eq(a.tail(4), a(seq(lastp1-4,last))) );
291
+ VERIFY( is_same_eq(a.segment<4>(3), a(seqN(3,fix<4>))) );
292
+ }
293
+
294
+ ArrayXXi A1=A, A2 = ArrayXXi::Random(4,4);
295
+ ArrayXi range25(4); range25 << 3,2,4,5;
296
+ A1(seqN(3,4),seq(2,5)) = A2;
297
+ VERIFY_IS_APPROX( A1.block(3,2,4,4), A2 );
298
+ A1 = A;
299
+ A2.setOnes();
300
+ A1(seq(6,3,-1),range25) = A2;
301
+ VERIFY_IS_APPROX( A1.block(3,2,4,4), A2 );
302
+
303
+ // check reverse
304
+ {
305
+ VERIFY( is_same_seq_type( seq(3,7).reverse(), seqN(7,5,fix<-1>) ) );
306
+ VERIFY( is_same_seq_type( seq(7,3,fix<-2>).reverse(), seqN(3,3,fix<2>) ) );
307
+ VERIFY_IS_APPROX( a(seqN(2,last/2).reverse()), a(seqN(2+(last/2-1)*1,last/2,fix<-1>)) );
308
+ VERIFY_IS_APPROX( a(seqN(last/2,fix<4>).reverse()),a(seqN(last/2,fix<4>)).reverse() );
309
+ VERIFY_IS_APPROX( A(seq(last-5,last-1,2).reverse(), seqN(last-3,3,fix<-2>).reverse()),
310
+ A(seq(last-5,last-1,2), seqN(last-3,3,fix<-2>)).reverse() );
311
+ }
312
+
313
+ #if EIGEN_HAS_CXX11
314
+ // check lastN
315
+ VERIFY_IS_APPROX( a(lastN(3)), a.tail(3) );
316
+ VERIFY( MATCH( a(lastN(3)), "7\n8\n9" ) );
317
+ VERIFY_IS_APPROX( a(lastN(fix<3>())), a.tail<3>() );
318
+ VERIFY( MATCH( a(lastN(3,2)), "5\n7\n9" ) );
319
+ VERIFY( MATCH( a(lastN(3,fix<2>())), "5\n7\n9" ) );
320
+ VERIFY( a(lastN(fix<3>())).SizeAtCompileTime == 3 );
321
+
322
+ VERIFY( (A(all, std::array<int,4>{{1,3,2,4}})).ColsAtCompileTime == 4);
323
+
324
+ VERIFY_IS_APPROX( (A(std::array<int,3>{{1,3,5}}, std::array<int,4>{{9,6,3,0}})), A(seqN(1,3,2), seqN(9,4,-3)) );
325
+
326
+ #if EIGEN_HAS_STATIC_ARRAY_TEMPLATE
327
+ VERIFY_IS_APPROX( A({3, 1, 6, 5}, all), A(std::array<int,4>{{3, 1, 6, 5}}, all) );
328
+ VERIFY_IS_APPROX( A(all,{3, 1, 6, 5}), A(all,std::array<int,4>{{3, 1, 6, 5}}) );
329
+ VERIFY_IS_APPROX( A({1,3,5},{3, 1, 6, 5}), A(std::array<int,3>{{1,3,5}},std::array<int,4>{{3, 1, 6, 5}}) );
330
+
331
+ VERIFY_IS_EQUAL( A({1,3,5},{3, 1, 6, 5}).RowsAtCompileTime, 3 );
332
+ VERIFY_IS_EQUAL( A({1,3,5},{3, 1, 6, 5}).ColsAtCompileTime, 4 );
333
+
334
+ VERIFY_IS_APPROX( a({3, 1, 6, 5}), a(std::array<int,4>{{3, 1, 6, 5}}) );
335
+ VERIFY_IS_EQUAL( a({1,3,5}).SizeAtCompileTime, 3 );
336
+
337
+ VERIFY_IS_APPROX( b({3, 1, 6, 5}), b(std::array<int,4>{{3, 1, 6, 5}}) );
338
+ VERIFY_IS_EQUAL( b({1,3,5}).SizeAtCompileTime, 3 );
339
+ #endif
340
+
341
+ #endif
342
+
343
+ // check mat(i,j) with weird types for i and j
344
+ {
345
+ VERIFY_IS_APPROX( A(B.RowsAtCompileTime-1, 1), A(3,1) );
346
+ VERIFY_IS_APPROX( A(B.RowsAtCompileTime, 1), A(4,1) );
347
+ VERIFY_IS_APPROX( A(B.RowsAtCompileTime-1, B.ColsAtCompileTime-1), A(3,3) );
348
+ VERIFY_IS_APPROX( A(B.RowsAtCompileTime, B.ColsAtCompileTime), A(4,4) );
349
+ const Index I_ = 3, J_ = 4;
350
+ VERIFY_IS_APPROX( A(I_,J_), A(3,4) );
351
+ }
352
+
353
+ // check extended block API
354
+ {
355
+ VERIFY( is_same_eq( A.block<3,4>(1,1), A.block(1,1,fix<3>,fix<4>)) );
356
+ VERIFY( is_same_eq( A.block<3,4>(1,1,3,4), A.block(1,1,fix<3>(),fix<4>(4))) );
357
+ VERIFY( is_same_eq( A.block<3,Dynamic>(1,1,3,4), A.block(1,1,fix<3>,4)) );
358
+ VERIFY( is_same_eq( A.block<Dynamic,4>(1,1,3,4), A.block(1,1,fix<Dynamic>(3),fix<4>)) );
359
+ VERIFY( is_same_eq( A.block(1,1,3,4), A.block(1,1,fix<Dynamic>(3),fix<Dynamic>(4))) );
360
+
361
+ VERIFY( is_same_eq( A.topLeftCorner<3,4>(), A.topLeftCorner(fix<3>,fix<4>)) );
362
+ VERIFY( is_same_eq( A.bottomLeftCorner<3,4>(), A.bottomLeftCorner(fix<3>,fix<4>)) );
363
+ VERIFY( is_same_eq( A.bottomRightCorner<3,4>(), A.bottomRightCorner(fix<3>,fix<4>)) );
364
+ VERIFY( is_same_eq( A.topRightCorner<3,4>(), A.topRightCorner(fix<3>,fix<4>)) );
365
+
366
+ VERIFY( is_same_eq( A.leftCols<3>(), A.leftCols(fix<3>)) );
367
+ VERIFY( is_same_eq( A.rightCols<3>(), A.rightCols(fix<3>)) );
368
+ VERIFY( is_same_eq( A.middleCols<3>(1), A.middleCols(1,fix<3>)) );
369
+
370
+ VERIFY( is_same_eq( A.topRows<3>(), A.topRows(fix<3>)) );
371
+ VERIFY( is_same_eq( A.bottomRows<3>(), A.bottomRows(fix<3>)) );
372
+ VERIFY( is_same_eq( A.middleRows<3>(1), A.middleRows(1,fix<3>)) );
373
+
374
+ VERIFY( is_same_eq( a.segment<3>(1), a.segment(1,fix<3>)) );
375
+ VERIFY( is_same_eq( a.head<3>(), a.head(fix<3>)) );
376
+ VERIFY( is_same_eq( a.tail<3>(), a.tail(fix<3>)) );
377
+
378
+ const ArrayXXi& cA(A);
379
+ VERIFY( is_same_eq( cA.block<Dynamic,4>(1,1,3,4), cA.block(1,1,fix<Dynamic>(3),fix<4>)) );
380
+
381
+ VERIFY( is_same_eq( cA.topLeftCorner<3,4>(), cA.topLeftCorner(fix<3>,fix<4>)) );
382
+ VERIFY( is_same_eq( cA.bottomLeftCorner<3,4>(), cA.bottomLeftCorner(fix<3>,fix<4>)) );
383
+ VERIFY( is_same_eq( cA.bottomRightCorner<3,4>(), cA.bottomRightCorner(fix<3>,fix<4>)) );
384
+ VERIFY( is_same_eq( cA.topRightCorner<3,4>(), cA.topRightCorner(fix<3>,fix<4>)) );
385
+
386
+ VERIFY( is_same_eq( cA.leftCols<3>(), cA.leftCols(fix<3>)) );
387
+ VERIFY( is_same_eq( cA.rightCols<3>(), cA.rightCols(fix<3>)) );
388
+ VERIFY( is_same_eq( cA.middleCols<3>(1), cA.middleCols(1,fix<3>)) );
389
+
390
+ VERIFY( is_same_eq( cA.topRows<3>(), cA.topRows(fix<3>)) );
391
+ VERIFY( is_same_eq( cA.bottomRows<3>(), cA.bottomRows(fix<3>)) );
392
+ VERIFY( is_same_eq( cA.middleRows<3>(1), cA.middleRows(1,fix<3>)) );
393
+ }
394
+
395
+ // Check compilation of enums as index type:
396
+ a(XX) = 1;
397
+ A(XX,YY) = 1;
398
+ // Anonymous enums only work with C++11
399
+ #if EIGEN_HAS_CXX11
400
+ enum { X=0, Y=1 };
401
+ a(X) = 1;
402
+ A(X,Y) = 1;
403
+ A(XX,Y) = 1;
404
+ A(X,YY) = 1;
405
+ #endif
406
+
407
+ // Check compilation of varying integer types as index types:
408
+ Index i = n/2;
409
+ short i_short(i);
410
+ std::size_t i_sizet(i);
411
+ VERIFY_IS_EQUAL( a(i), a.coeff(i_short) );
412
+ VERIFY_IS_EQUAL( a(i), a.coeff(i_sizet) );
413
+
414
+ VERIFY_IS_EQUAL( A(i,i), A.coeff(i_short, i_short) );
415
+ VERIFY_IS_EQUAL( A(i,i), A.coeff(i_short, i) );
416
+ VERIFY_IS_EQUAL( A(i,i), A.coeff(i, i_short) );
417
+ VERIFY_IS_EQUAL( A(i,i), A.coeff(i, i_sizet) );
418
+ VERIFY_IS_EQUAL( A(i,i), A.coeff(i_sizet, i) );
419
+ VERIFY_IS_EQUAL( A(i,i), A.coeff(i_sizet, i_short) );
420
+ VERIFY_IS_EQUAL( A(i,i), A.coeff(5, i_sizet) );
421
+
422
+ // Regression test for Max{Rows,Cols}AtCompileTime
423
+ {
424
+ Matrix3i A3 = Matrix3i::Random();
425
+ ArrayXi ind(5); ind << 1,1,1,1,1;
426
+ VERIFY_IS_EQUAL( A3(ind,ind).eval(), MatrixXi::Constant(5,5,A3(1,1)) );
427
+ }
428
+
429
+ // Regression for bug 1736
430
+ {
431
+ VERIFY_IS_APPROX(A(all, eii).col(0).eval(), A.col(eii(0)));
432
+ A(all, eii).col(0) = A.col(eii(0));
433
+ }
434
+
435
+ // bug 1815: IndexedView should allow linear access
436
+ {
437
+ VERIFY( MATCH( b(eii)(0), "3" ) );
438
+ VERIFY( MATCH( a(eii)(0), "3" ) );
439
+ VERIFY( MATCH( A(1,eii)(0), "103"));
440
+ VERIFY( MATCH( A(eii,1)(0), "301"));
441
+ VERIFY( MATCH( A(1,all)(1), "101"));
442
+ VERIFY( MATCH( A(all,1)(1), "101"));
443
+ }
444
+
445
+ #if EIGEN_HAS_CXX11
446
+ //Bug IndexView with a single static row should be RowMajor:
447
+ {
448
+ // A(1, seq(0,2,1)).cwiseAbs().colwise().replicate(2).eval();
449
+ STATIC_CHECK(( (internal::evaluator<decltype( A(1,seq(0,2,1)) )>::Flags & RowMajorBit) == RowMajorBit ));
450
+ }
451
+ #endif
452
+
453
+ }
454
+
455
+ EIGEN_DECLARE_TEST(indexed_view)
456
+ {
457
+ // for(int i = 0; i < g_repeat; i++) {
458
+ CALL_SUBTEST_1( check_indexed_view() );
459
+ CALL_SUBTEST_2( check_indexed_view() );
460
+ CALL_SUBTEST_3( check_indexed_view() );
461
+ // }
462
+
463
+ // static checks of some internals:
464
+ STATIC_CHECK(( internal::is_valid_index_type<int>::value ));
465
+ STATIC_CHECK(( internal::is_valid_index_type<unsigned int>::value ));
466
+ STATIC_CHECK(( internal::is_valid_index_type<short>::value ));
467
+ STATIC_CHECK(( internal::is_valid_index_type<std::ptrdiff_t>::value ));
468
+ STATIC_CHECK(( internal::is_valid_index_type<std::size_t>::value ));
469
+ STATIC_CHECK(( !internal::valid_indexed_view_overload<int,int>::value ));
470
+ STATIC_CHECK(( !internal::valid_indexed_view_overload<int,std::ptrdiff_t>::value ));
471
+ STATIC_CHECK(( !internal::valid_indexed_view_overload<std::ptrdiff_t,int>::value ));
472
+ STATIC_CHECK(( !internal::valid_indexed_view_overload<std::size_t,int>::value ));
473
+ }
include/eigen/test/initializer_list_construction.cpp ADDED
@@ -0,0 +1,385 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // This file is part of Eigen, a lightweight C++ template library
2
+ // for linear algebra.
3
+ //
4
+ // Copyright (C) 2019 David Tellenbach <david.tellenbach@tellnotes.org>
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
+ #define EIGEN_NO_STATIC_ASSERT
11
+
12
+ #include "main.h"
13
+
14
+ template<typename Scalar, bool is_integer = NumTraits<Scalar>::IsInteger>
15
+ struct TestMethodDispatching {
16
+ static void run() {}
17
+ };
18
+
19
+ template<typename Scalar>
20
+ struct TestMethodDispatching<Scalar, 1> {
21
+ static void run()
22
+ {
23
+ {
24
+ Matrix<Scalar, Dynamic, Dynamic> m {3, 4};
25
+ Array<Scalar, Dynamic, Dynamic> a {3, 4};
26
+ VERIFY(m.rows() == 3);
27
+ VERIFY(m.cols() == 4);
28
+ VERIFY(a.rows() == 3);
29
+ VERIFY(a.cols() == 4);
30
+ }
31
+ {
32
+ Matrix<Scalar, 1, 2> m {3, 4};
33
+ Array<Scalar, 1, 2> a {3, 4};
34
+ VERIFY(m(0) == 3);
35
+ VERIFY(m(1) == 4);
36
+ VERIFY(a(0) == 3);
37
+ VERIFY(a(1) == 4);
38
+ }
39
+ {
40
+ Matrix<Scalar, 2, 1> m {3, 4};
41
+ Array<Scalar, 2, 1> a {3, 4};
42
+ VERIFY(m(0) == 3);
43
+ VERIFY(m(1) == 4);
44
+ VERIFY(a(0) == 3);
45
+ VERIFY(a(1) == 4);
46
+ }
47
+ }
48
+ };
49
+
50
+ template<typename Vec4, typename Vec5> void fixedsizeVariadicVectorConstruction2()
51
+ {
52
+ {
53
+ Vec4 ref = Vec4::Random();
54
+ Vec4 v{ ref[0], ref[1], ref[2], ref[3] };
55
+ VERIFY_IS_APPROX(v, ref);
56
+ VERIFY_IS_APPROX(v, (Vec4( ref[0], ref[1], ref[2], ref[3] )));
57
+ VERIFY_IS_APPROX(v, (Vec4({ref[0], ref[1], ref[2], ref[3]})));
58
+
59
+ Vec4 v2 = { ref[0], ref[1], ref[2], ref[3] };
60
+ VERIFY_IS_APPROX(v2, ref);
61
+ }
62
+ {
63
+ Vec5 ref = Vec5::Random();
64
+ Vec5 v{ ref[0], ref[1], ref[2], ref[3], ref[4] };
65
+ VERIFY_IS_APPROX(v, ref);
66
+ VERIFY_IS_APPROX(v, (Vec5( ref[0], ref[1], ref[2], ref[3], ref[4] )));
67
+ VERIFY_IS_APPROX(v, (Vec5({ref[0], ref[1], ref[2], ref[3], ref[4]})));
68
+
69
+ Vec5 v2 = { ref[0], ref[1], ref[2], ref[3], ref[4] };
70
+ VERIFY_IS_APPROX(v2, ref);
71
+ }
72
+ }
73
+
74
+ #define CHECK_MIXSCALAR_V5_APPROX(V, A0, A1, A2, A3, A4) { \
75
+ VERIFY_IS_APPROX(V[0], Scalar(A0) ); \
76
+ VERIFY_IS_APPROX(V[1], Scalar(A1) ); \
77
+ VERIFY_IS_APPROX(V[2], Scalar(A2) ); \
78
+ VERIFY_IS_APPROX(V[3], Scalar(A3) ); \
79
+ VERIFY_IS_APPROX(V[4], Scalar(A4) ); \
80
+ }
81
+
82
+ #define CHECK_MIXSCALAR_V5(VEC5, A0, A1, A2, A3, A4) { \
83
+ typedef VEC5::Scalar Scalar; \
84
+ VEC5 v = { A0 , A1 , A2 , A3 , A4 }; \
85
+ CHECK_MIXSCALAR_V5_APPROX(v, A0 , A1 , A2 , A3 , A4); \
86
+ }
87
+
88
+ template<int> void fixedsizeVariadicVectorConstruction3()
89
+ {
90
+ typedef Matrix<double,5,1> Vec5;
91
+ typedef Array<float,5,1> Arr5;
92
+ CHECK_MIXSCALAR_V5(Vec5, 1, 2., -3, 4.121, 5.53252);
93
+ CHECK_MIXSCALAR_V5(Arr5, 1, 2., 3.12f, 4.121, 5.53252);
94
+ }
95
+
96
+ template<typename Scalar> void fixedsizeVariadicVectorConstruction()
97
+ {
98
+ CALL_SUBTEST(( fixedsizeVariadicVectorConstruction2<Matrix<Scalar,4,1>, Matrix<Scalar,5,1> >() ));
99
+ CALL_SUBTEST(( fixedsizeVariadicVectorConstruction2<Matrix<Scalar,1,4>, Matrix<Scalar,1,5> >() ));
100
+ CALL_SUBTEST(( fixedsizeVariadicVectorConstruction2<Array<Scalar,4,1>, Array<Scalar,5,1> >() ));
101
+ CALL_SUBTEST(( fixedsizeVariadicVectorConstruction2<Array<Scalar,1,4>, Array<Scalar,1,5> >() ));
102
+ }
103
+
104
+
105
+ template<typename Scalar> void initializerListVectorConstruction()
106
+ {
107
+ Scalar raw[4];
108
+ for(int k = 0; k < 4; ++k) {
109
+ raw[k] = internal::random<Scalar>();
110
+ }
111
+ {
112
+ Matrix<Scalar, 4, 1> m { {raw[0]}, {raw[1]},{raw[2]},{raw[3]} };
113
+ Array<Scalar, 4, 1> a { {raw[0]}, {raw[1]}, {raw[2]}, {raw[3]} };
114
+ for(int k = 0; k < 4; ++k) {
115
+ VERIFY(m(k) == raw[k]);
116
+ }
117
+ for(int k = 0; k < 4; ++k) {
118
+ VERIFY(a(k) == raw[k]);
119
+ }
120
+ VERIFY_IS_EQUAL(m, (Matrix<Scalar,4,1>({ {raw[0]}, {raw[1]}, {raw[2]}, {raw[3]} })));
121
+ VERIFY((a == (Array<Scalar,4,1>({ {raw[0]}, {raw[1]}, {raw[2]}, {raw[3]} }))).all());
122
+ }
123
+ {
124
+ Matrix<Scalar, 1, 4> m { {raw[0], raw[1], raw[2], raw[3]} };
125
+ Array<Scalar, 1, 4> a { {raw[0], raw[1], raw[2], raw[3]} };
126
+ for(int k = 0; k < 4; ++k) {
127
+ VERIFY(m(k) == raw[k]);
128
+ }
129
+ for(int k = 0; k < 4; ++k) {
130
+ VERIFY(a(k) == raw[k]);
131
+ }
132
+ VERIFY_IS_EQUAL(m, (Matrix<Scalar, 1, 4>({{raw[0],raw[1],raw[2],raw[3]}})));
133
+ VERIFY((a == (Array<Scalar, 1, 4>({{raw[0],raw[1],raw[2],raw[3]}}))).all());
134
+ }
135
+ {
136
+ Matrix<Scalar, 4, Dynamic> m { {raw[0]}, {raw[1]}, {raw[2]}, {raw[3]} };
137
+ Array<Scalar, 4, Dynamic> a { {raw[0]}, {raw[1]}, {raw[2]}, {raw[3]} };
138
+ for(int k=0; k < 4; ++k) {
139
+ VERIFY(m(k) == raw[k]);
140
+ }
141
+ for(int k=0; k < 4; ++k) {
142
+ VERIFY(a(k) == raw[k]);
143
+ }
144
+ VERIFY_IS_EQUAL(m, (Matrix<Scalar, 4, Dynamic>({ {raw[0]}, {raw[1]}, {raw[2]}, {raw[3]} })));
145
+ VERIFY((a == (Array<Scalar, 4, Dynamic>({ {raw[0]}, {raw[1]}, {raw[2]}, {raw[3]} }))).all());
146
+ }
147
+ {
148
+ Matrix<Scalar, Dynamic, 4> m {{raw[0],raw[1],raw[2],raw[3]}};
149
+ Array<Scalar, Dynamic, 4> a {{raw[0],raw[1],raw[2],raw[3]}};
150
+ for(int k=0; k < 4; ++k) {
151
+ VERIFY(m(k) == raw[k]);
152
+ }
153
+ for(int k=0; k < 4; ++k) {
154
+ VERIFY(a(k) == raw[k]);
155
+ }
156
+ VERIFY_IS_EQUAL(m, (Matrix<Scalar, Dynamic, 4>({{raw[0],raw[1],raw[2],raw[3]}})));
157
+ VERIFY((a == (Array<Scalar, Dynamic, 4>({{raw[0],raw[1],raw[2],raw[3]}}))).all());
158
+ }
159
+ }
160
+
161
+ template<typename Scalar> void initializerListMatrixConstruction()
162
+ {
163
+ const Index RowsAtCompileTime = 5;
164
+ const Index ColsAtCompileTime = 4;
165
+ const Index SizeAtCompileTime = RowsAtCompileTime * ColsAtCompileTime;
166
+
167
+ Scalar raw[SizeAtCompileTime];
168
+ for (int i = 0; i < SizeAtCompileTime; ++i) {
169
+ raw[i] = internal::random<Scalar>();
170
+ }
171
+ {
172
+ Matrix<Scalar, Dynamic, Dynamic> m {};
173
+ VERIFY(m.cols() == 0);
174
+ VERIFY(m.rows() == 0);
175
+ VERIFY_IS_EQUAL(m, (Matrix<Scalar, Dynamic, Dynamic>()));
176
+ }
177
+ {
178
+ Matrix<Scalar, 5, 4> m {
179
+ {raw[0], raw[1], raw[2], raw[3]},
180
+ {raw[4], raw[5], raw[6], raw[7]},
181
+ {raw[8], raw[9], raw[10], raw[11]},
182
+ {raw[12], raw[13], raw[14], raw[15]},
183
+ {raw[16], raw[17], raw[18], raw[19]}
184
+ };
185
+
186
+ Matrix<Scalar, 5, 4> m2;
187
+ m2 << raw[0], raw[1], raw[2], raw[3],
188
+ raw[4], raw[5], raw[6], raw[7],
189
+ raw[8], raw[9], raw[10], raw[11],
190
+ raw[12], raw[13], raw[14], raw[15],
191
+ raw[16], raw[17], raw[18], raw[19];
192
+
193
+ int k = 0;
194
+ for(int i = 0; i < RowsAtCompileTime; ++i) {
195
+ for (int j = 0; j < ColsAtCompileTime; ++j) {
196
+ VERIFY(m(i, j) == raw[k]);
197
+ ++k;
198
+ }
199
+ }
200
+ VERIFY_IS_EQUAL(m, m2);
201
+ }
202
+ {
203
+ Matrix<Scalar, Dynamic, Dynamic> m{
204
+ {raw[0], raw[1], raw[2], raw[3]},
205
+ {raw[4], raw[5], raw[6], raw[7]},
206
+ {raw[8], raw[9], raw[10], raw[11]},
207
+ {raw[12], raw[13], raw[14], raw[15]},
208
+ {raw[16], raw[17], raw[18], raw[19]}
209
+ };
210
+
211
+ VERIFY(m.cols() == 4);
212
+ VERIFY(m.rows() == 5);
213
+ int k = 0;
214
+ for(int i = 0; i < RowsAtCompileTime; ++i) {
215
+ for (int j = 0; j < ColsAtCompileTime; ++j) {
216
+ VERIFY(m(i, j) == raw[k]);
217
+ ++k;
218
+ }
219
+ }
220
+
221
+ Matrix<Scalar, Dynamic, Dynamic> m2(RowsAtCompileTime, ColsAtCompileTime);
222
+ k = 0;
223
+ for(int i = 0; i < RowsAtCompileTime; ++i) {
224
+ for (int j = 0; j < ColsAtCompileTime; ++j) {
225
+ m2(i, j) = raw[k];
226
+ ++k;
227
+ }
228
+ }
229
+ VERIFY_IS_EQUAL(m, m2);
230
+ }
231
+ }
232
+
233
+ template<typename Scalar> void initializerListArrayConstruction()
234
+ {
235
+ const Index RowsAtCompileTime = 5;
236
+ const Index ColsAtCompileTime = 4;
237
+ const Index SizeAtCompileTime = RowsAtCompileTime * ColsAtCompileTime;
238
+
239
+ Scalar raw[SizeAtCompileTime];
240
+ for (int i = 0; i < SizeAtCompileTime; ++i) {
241
+ raw[i] = internal::random<Scalar>();
242
+ }
243
+ {
244
+ Array<Scalar, Dynamic, Dynamic> a {};
245
+ VERIFY(a.cols() == 0);
246
+ VERIFY(a.rows() == 0);
247
+ }
248
+ {
249
+ Array<Scalar, 5, 4> m {
250
+ {raw[0], raw[1], raw[2], raw[3]},
251
+ {raw[4], raw[5], raw[6], raw[7]},
252
+ {raw[8], raw[9], raw[10], raw[11]},
253
+ {raw[12], raw[13], raw[14], raw[15]},
254
+ {raw[16], raw[17], raw[18], raw[19]}
255
+ };
256
+
257
+ Array<Scalar, 5, 4> m2;
258
+ m2 << raw[0], raw[1], raw[2], raw[3],
259
+ raw[4], raw[5], raw[6], raw[7],
260
+ raw[8], raw[9], raw[10], raw[11],
261
+ raw[12], raw[13], raw[14], raw[15],
262
+ raw[16], raw[17], raw[18], raw[19];
263
+
264
+ int k = 0;
265
+ for(int i = 0; i < RowsAtCompileTime; ++i) {
266
+ for (int j = 0; j < ColsAtCompileTime; ++j) {
267
+ VERIFY(m(i, j) == raw[k]);
268
+ ++k;
269
+ }
270
+ }
271
+ VERIFY_IS_APPROX(m, m2);
272
+ }
273
+ {
274
+ Array<Scalar, Dynamic, Dynamic> m {
275
+ {raw[0], raw[1], raw[2], raw[3]},
276
+ {raw[4], raw[5], raw[6], raw[7]},
277
+ {raw[8], raw[9], raw[10], raw[11]},
278
+ {raw[12], raw[13], raw[14], raw[15]},
279
+ {raw[16], raw[17], raw[18], raw[19]}
280
+ };
281
+
282
+ VERIFY(m.cols() == 4);
283
+ VERIFY(m.rows() == 5);
284
+ int k = 0;
285
+ for(int i = 0; i < RowsAtCompileTime; ++i) {
286
+ for (int j = 0; j < ColsAtCompileTime; ++j) {
287
+ VERIFY(m(i, j) == raw[k]);
288
+ ++k;
289
+ }
290
+ }
291
+
292
+ Array<Scalar, Dynamic, Dynamic> m2(RowsAtCompileTime, ColsAtCompileTime);
293
+ k = 0;
294
+ for(int i = 0; i < RowsAtCompileTime; ++i) {
295
+ for (int j = 0; j < ColsAtCompileTime; ++j) {
296
+ m2(i, j) = raw[k];
297
+ ++k;
298
+ }
299
+ }
300
+ VERIFY_IS_APPROX(m, m2);
301
+ }
302
+ }
303
+
304
+ template<typename Scalar> void dynamicVectorConstruction()
305
+ {
306
+ const Index size = 4;
307
+ Scalar raw[size];
308
+ for (int i = 0; i < size; ++i) {
309
+ raw[i] = internal::random<Scalar>();
310
+ }
311
+
312
+ typedef Matrix<Scalar, Dynamic, 1> VectorX;
313
+
314
+ {
315
+ VectorX v {{raw[0], raw[1], raw[2], raw[3]}};
316
+ for (int i = 0; i < size; ++i) {
317
+ VERIFY(v(i) == raw[i]);
318
+ }
319
+ VERIFY(v.rows() == size);
320
+ VERIFY(v.cols() == 1);
321
+ VERIFY_IS_EQUAL(v, (VectorX {{raw[0], raw[1], raw[2], raw[3]}}));
322
+ }
323
+
324
+ {
325
+ VERIFY_RAISES_ASSERT((VectorX {raw[0], raw[1], raw[2], raw[3]}));
326
+ }
327
+ {
328
+ VERIFY_RAISES_ASSERT((VectorX {
329
+ {raw[0], raw[1], raw[2], raw[3]},
330
+ {raw[0], raw[1], raw[2], raw[3]},
331
+ }));
332
+ }
333
+ }
334
+
335
+ EIGEN_DECLARE_TEST(initializer_list_construction)
336
+ {
337
+ CALL_SUBTEST_1(initializerListVectorConstruction<unsigned char>());
338
+ CALL_SUBTEST_1(initializerListVectorConstruction<float>());
339
+ CALL_SUBTEST_1(initializerListVectorConstruction<double>());
340
+ CALL_SUBTEST_1(initializerListVectorConstruction<int>());
341
+ CALL_SUBTEST_1(initializerListVectorConstruction<long int>());
342
+ CALL_SUBTEST_1(initializerListVectorConstruction<std::ptrdiff_t>());
343
+ CALL_SUBTEST_1(initializerListVectorConstruction<std::complex<double>>());
344
+ CALL_SUBTEST_1(initializerListVectorConstruction<std::complex<float>>());
345
+
346
+ CALL_SUBTEST_2(initializerListMatrixConstruction<unsigned char>());
347
+ CALL_SUBTEST_2(initializerListMatrixConstruction<float>());
348
+ CALL_SUBTEST_2(initializerListMatrixConstruction<double>());
349
+ CALL_SUBTEST_2(initializerListMatrixConstruction<int>());
350
+ CALL_SUBTEST_2(initializerListMatrixConstruction<long int>());
351
+ CALL_SUBTEST_2(initializerListMatrixConstruction<std::ptrdiff_t>());
352
+ CALL_SUBTEST_2(initializerListMatrixConstruction<std::complex<double>>());
353
+ CALL_SUBTEST_2(initializerListMatrixConstruction<std::complex<float>>());
354
+
355
+ CALL_SUBTEST_3(initializerListArrayConstruction<unsigned char>());
356
+ CALL_SUBTEST_3(initializerListArrayConstruction<float>());
357
+ CALL_SUBTEST_3(initializerListArrayConstruction<double>());
358
+ CALL_SUBTEST_3(initializerListArrayConstruction<int>());
359
+ CALL_SUBTEST_3(initializerListArrayConstruction<long int>());
360
+ CALL_SUBTEST_3(initializerListArrayConstruction<std::ptrdiff_t>());
361
+ CALL_SUBTEST_3(initializerListArrayConstruction<std::complex<double>>());
362
+ CALL_SUBTEST_3(initializerListArrayConstruction<std::complex<float>>());
363
+
364
+ CALL_SUBTEST_4(fixedsizeVariadicVectorConstruction<unsigned char>());
365
+ CALL_SUBTEST_4(fixedsizeVariadicVectorConstruction<float>());
366
+ CALL_SUBTEST_4(fixedsizeVariadicVectorConstruction<double>());
367
+ CALL_SUBTEST_4(fixedsizeVariadicVectorConstruction<int>());
368
+ CALL_SUBTEST_4(fixedsizeVariadicVectorConstruction<long int>());
369
+ CALL_SUBTEST_4(fixedsizeVariadicVectorConstruction<std::ptrdiff_t>());
370
+ CALL_SUBTEST_4(fixedsizeVariadicVectorConstruction<std::complex<double>>());
371
+ CALL_SUBTEST_4(fixedsizeVariadicVectorConstruction<std::complex<float>>());
372
+ CALL_SUBTEST_4(fixedsizeVariadicVectorConstruction3<0>());
373
+
374
+ CALL_SUBTEST_5(TestMethodDispatching<int>::run());
375
+ CALL_SUBTEST_5(TestMethodDispatching<long int>::run());
376
+
377
+ CALL_SUBTEST_6(dynamicVectorConstruction<unsigned char>());
378
+ CALL_SUBTEST_6(dynamicVectorConstruction<float>());
379
+ CALL_SUBTEST_6(dynamicVectorConstruction<double>());
380
+ CALL_SUBTEST_6(dynamicVectorConstruction<int>());
381
+ CALL_SUBTEST_6(dynamicVectorConstruction<long int>());
382
+ CALL_SUBTEST_6(dynamicVectorConstruction<std::ptrdiff_t>());
383
+ CALL_SUBTEST_6(dynamicVectorConstruction<std::complex<double>>());
384
+ CALL_SUBTEST_6(dynamicVectorConstruction<std::complex<float>>());
385
+ }
include/eigen/test/inplace_decomposition.cpp ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // This file is part of Eigen, a lightweight C++ template library
2
+ // for linear algebra.
3
+ //
4
+ // Copyright (C) 2016 Gael Guennebaud <gael.guennebaud@inria.fr>
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
+ #include <Eigen/LU>
12
+ #include <Eigen/Cholesky>
13
+ #include <Eigen/QR>
14
+
15
+ // This file test inplace decomposition through Ref<>, as supported by Cholesky, LU, and QR decompositions.
16
+
17
+ template<typename DecType,typename MatrixType> void inplace(bool square = false, bool SPD = false)
18
+ {
19
+ typedef typename MatrixType::Scalar Scalar;
20
+ typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> RhsType;
21
+ typedef Matrix<Scalar, MatrixType::ColsAtCompileTime, 1> ResType;
22
+
23
+ Index rows = MatrixType::RowsAtCompileTime==Dynamic ? internal::random<Index>(2,EIGEN_TEST_MAX_SIZE/2) : Index(MatrixType::RowsAtCompileTime);
24
+ Index cols = MatrixType::ColsAtCompileTime==Dynamic ? (square?rows:internal::random<Index>(2,rows)) : Index(MatrixType::ColsAtCompileTime);
25
+
26
+ MatrixType A = MatrixType::Random(rows,cols);
27
+ RhsType b = RhsType::Random(rows);
28
+ ResType x(cols);
29
+
30
+ if(SPD)
31
+ {
32
+ assert(square);
33
+ A.topRows(cols) = A.topRows(cols).adjoint() * A.topRows(cols);
34
+ A.diagonal().array() += 1e-3;
35
+ }
36
+
37
+ MatrixType A0 = A;
38
+ MatrixType A1 = A;
39
+
40
+ DecType dec(A);
41
+
42
+ // Check that the content of A has been modified
43
+ VERIFY_IS_NOT_APPROX( A, A0 );
44
+
45
+ // Check that the decomposition is correct:
46
+ if(rows==cols)
47
+ {
48
+ VERIFY_IS_APPROX( A0 * (x = dec.solve(b)), b );
49
+ }
50
+ else
51
+ {
52
+ VERIFY_IS_APPROX( A0.transpose() * A0 * (x = dec.solve(b)), A0.transpose() * b );
53
+ }
54
+
55
+ // Check that modifying A breaks the current dec:
56
+ A.setRandom();
57
+ if(rows==cols)
58
+ {
59
+ VERIFY_IS_NOT_APPROX( A0 * (x = dec.solve(b)), b );
60
+ }
61
+ else
62
+ {
63
+ VERIFY_IS_NOT_APPROX( A0.transpose() * A0 * (x = dec.solve(b)), A0.transpose() * b );
64
+ }
65
+
66
+ // Check that calling compute(A1) does not modify A1:
67
+ A = A0;
68
+ dec.compute(A1);
69
+ VERIFY_IS_EQUAL(A0,A1);
70
+ VERIFY_IS_NOT_APPROX( A, A0 );
71
+ if(rows==cols)
72
+ {
73
+ VERIFY_IS_APPROX( A0 * (x = dec.solve(b)), b );
74
+ }
75
+ else
76
+ {
77
+ VERIFY_IS_APPROX( A0.transpose() * A0 * (x = dec.solve(b)), A0.transpose() * b );
78
+ }
79
+ }
80
+
81
+
82
+ EIGEN_DECLARE_TEST(inplace_decomposition)
83
+ {
84
+ EIGEN_UNUSED typedef Matrix<double,4,3> Matrix43d;
85
+ for(int i = 0; i < g_repeat; i++) {
86
+ CALL_SUBTEST_1(( inplace<LLT<Ref<MatrixXd> >, MatrixXd>(true,true) ));
87
+ CALL_SUBTEST_1(( inplace<LLT<Ref<Matrix4d> >, Matrix4d>(true,true) ));
88
+
89
+ CALL_SUBTEST_2(( inplace<LDLT<Ref<MatrixXd> >, MatrixXd>(true,true) ));
90
+ CALL_SUBTEST_2(( inplace<LDLT<Ref<Matrix4d> >, Matrix4d>(true,true) ));
91
+
92
+ CALL_SUBTEST_3(( inplace<PartialPivLU<Ref<MatrixXd> >, MatrixXd>(true,false) ));
93
+ CALL_SUBTEST_3(( inplace<PartialPivLU<Ref<Matrix4d> >, Matrix4d>(true,false) ));
94
+
95
+ CALL_SUBTEST_4(( inplace<FullPivLU<Ref<MatrixXd> >, MatrixXd>(true,false) ));
96
+ CALL_SUBTEST_4(( inplace<FullPivLU<Ref<Matrix4d> >, Matrix4d>(true,false) ));
97
+
98
+ CALL_SUBTEST_5(( inplace<HouseholderQR<Ref<MatrixXd> >, MatrixXd>(false,false) ));
99
+ CALL_SUBTEST_5(( inplace<HouseholderQR<Ref<Matrix43d> >, Matrix43d>(false,false) ));
100
+
101
+ CALL_SUBTEST_6(( inplace<ColPivHouseholderQR<Ref<MatrixXd> >, MatrixXd>(false,false) ));
102
+ CALL_SUBTEST_6(( inplace<ColPivHouseholderQR<Ref<Matrix43d> >, Matrix43d>(false,false) ));
103
+
104
+ CALL_SUBTEST_7(( inplace<FullPivHouseholderQR<Ref<MatrixXd> >, MatrixXd>(false,false) ));
105
+ CALL_SUBTEST_7(( inplace<FullPivHouseholderQR<Ref<Matrix43d> >, Matrix43d>(false,false) ));
106
+
107
+ CALL_SUBTEST_8(( inplace<CompleteOrthogonalDecomposition<Ref<MatrixXd> >, MatrixXd>(false,false) ));
108
+ CALL_SUBTEST_8(( inplace<CompleteOrthogonalDecomposition<Ref<Matrix43d> >, Matrix43d>(false,false) ));
109
+ }
110
+ }
include/eigen/test/integer_types.cpp ADDED
@@ -0,0 +1,173 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // This file is part of Eigen, a lightweight C++ template library
2
+ // for linear algebra.
3
+ //
4
+ // Copyright (C) 2010 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
+ #define EIGEN_NO_STATIC_ASSERT
11
+
12
+ #include "main.h"
13
+
14
+ #undef VERIFY_IS_APPROX
15
+ #define VERIFY_IS_APPROX(a, b) VERIFY((a)==(b));
16
+ #undef VERIFY_IS_NOT_APPROX
17
+ #define VERIFY_IS_NOT_APPROX(a, b) VERIFY((a)!=(b));
18
+
19
+ template<typename MatrixType> void signed_integer_type_tests(const MatrixType& m)
20
+ {
21
+ typedef typename MatrixType::Scalar Scalar;
22
+
23
+ enum { is_signed = (Scalar(-1) > Scalar(0)) ? 0 : 1 };
24
+ VERIFY(is_signed == 1);
25
+
26
+ Index rows = m.rows();
27
+ Index cols = m.cols();
28
+
29
+ MatrixType m1(rows, cols),
30
+ m2 = MatrixType::Random(rows, cols),
31
+ mzero = MatrixType::Zero(rows, cols);
32
+
33
+ do {
34
+ m1 = MatrixType::Random(rows, cols);
35
+ } while(m1 == mzero || m1 == m2);
36
+
37
+ // check linear structure
38
+
39
+ Scalar s1;
40
+ do {
41
+ s1 = internal::random<Scalar>();
42
+ } while(s1 == 0);
43
+
44
+ VERIFY_IS_EQUAL(-(-m1), m1);
45
+ VERIFY_IS_EQUAL(-m2+m1+m2, m1);
46
+ VERIFY_IS_EQUAL((-m1+m2)*s1, -s1*m1+s1*m2);
47
+ }
48
+
49
+ template<typename MatrixType> void integer_type_tests(const MatrixType& m)
50
+ {
51
+ typedef typename MatrixType::Scalar Scalar;
52
+
53
+ VERIFY(NumTraits<Scalar>::IsInteger);
54
+ enum { is_signed = (Scalar(-1) > Scalar(0)) ? 0 : 1 };
55
+ VERIFY(int(NumTraits<Scalar>::IsSigned) == is_signed);
56
+
57
+ typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
58
+
59
+ Index rows = m.rows();
60
+ Index cols = m.cols();
61
+
62
+ // this test relies a lot on Random.h, and there's not much more that we can do
63
+ // to test it, hence I consider that we will have tested Random.h
64
+ MatrixType m1(rows, cols),
65
+ m2 = MatrixType::Random(rows, cols),
66
+ m3(rows, cols),
67
+ mzero = MatrixType::Zero(rows, cols);
68
+
69
+ typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime> SquareMatrixType;
70
+ SquareMatrixType identity = SquareMatrixType::Identity(rows, rows),
71
+ square = SquareMatrixType::Random(rows, rows);
72
+ VectorType v1(rows),
73
+ v2 = VectorType::Random(rows),
74
+ vzero = VectorType::Zero(rows);
75
+
76
+ do {
77
+ m1 = MatrixType::Random(rows, cols);
78
+ } while(m1 == mzero || m1 == m2);
79
+
80
+ do {
81
+ v1 = VectorType::Random(rows);
82
+ } while(v1 == vzero || v1 == v2);
83
+
84
+ VERIFY_IS_APPROX( v1, v1);
85
+ VERIFY_IS_NOT_APPROX( v1, 2*v1);
86
+ VERIFY_IS_APPROX( vzero, v1-v1);
87
+ VERIFY_IS_APPROX( m1, m1);
88
+ VERIFY_IS_NOT_APPROX( m1, 2*m1);
89
+ VERIFY_IS_APPROX( mzero, m1-m1);
90
+
91
+ VERIFY_IS_APPROX(m3 = m1,m1);
92
+ MatrixType m4;
93
+ VERIFY_IS_APPROX(m4 = m1,m1);
94
+
95
+ m3.real() = m1.real();
96
+ VERIFY_IS_APPROX(static_cast<const MatrixType&>(m3).real(), static_cast<const MatrixType&>(m1).real());
97
+ VERIFY_IS_APPROX(static_cast<const MatrixType&>(m3).real(), m1.real());
98
+
99
+ // check == / != operators
100
+ VERIFY(m1==m1);
101
+ VERIFY(m1!=m2);
102
+ VERIFY(!(m1==m2));
103
+ VERIFY(!(m1!=m1));
104
+ m1 = m2;
105
+ VERIFY(m1==m2);
106
+ VERIFY(!(m1!=m2));
107
+
108
+ // check linear structure
109
+
110
+ Scalar s1;
111
+ do {
112
+ s1 = internal::random<Scalar>();
113
+ } while(s1 == 0);
114
+
115
+ VERIFY_IS_EQUAL(m1+m1, 2*m1);
116
+ VERIFY_IS_EQUAL(m1+m2-m1, m2);
117
+ VERIFY_IS_EQUAL(m1*s1, s1*m1);
118
+ VERIFY_IS_EQUAL((m1+m2)*s1, s1*m1+s1*m2);
119
+ m3 = m2; m3 += m1;
120
+ VERIFY_IS_EQUAL(m3, m1+m2);
121
+ m3 = m2; m3 -= m1;
122
+ VERIFY_IS_EQUAL(m3, m2-m1);
123
+ m3 = m2; m3 *= s1;
124
+ VERIFY_IS_EQUAL(m3, s1*m2);
125
+
126
+ // check matrix product.
127
+
128
+ VERIFY_IS_APPROX(identity * m1, m1);
129
+ VERIFY_IS_APPROX(square * (m1 + m2), square * m1 + square * m2);
130
+ VERIFY_IS_APPROX((m1 + m2).transpose() * square, m1.transpose() * square + m2.transpose() * square);
131
+ VERIFY_IS_APPROX((m1 * m2.transpose()) * m1, m1 * (m2.transpose() * m1));
132
+ }
133
+
134
+ template<int>
135
+ void integer_types_extra()
136
+ {
137
+ VERIFY_IS_EQUAL(int(internal::scalar_div_cost<int>::value), 8);
138
+ VERIFY_IS_EQUAL(int(internal::scalar_div_cost<unsigned int>::value), 8);
139
+ if(sizeof(long)>sizeof(int)) {
140
+ VERIFY(int(internal::scalar_div_cost<long>::value) > int(internal::scalar_div_cost<int>::value));
141
+ VERIFY(int(internal::scalar_div_cost<unsigned long>::value) > int(internal::scalar_div_cost<int>::value));
142
+ }
143
+ }
144
+
145
+ EIGEN_DECLARE_TEST(integer_types)
146
+ {
147
+ for(int i = 0; i < g_repeat; i++) {
148
+ CALL_SUBTEST_1( integer_type_tests(Matrix<unsigned int, 1, 1>()) );
149
+ CALL_SUBTEST_1( integer_type_tests(Matrix<unsigned long, 3, 4>()) );
150
+
151
+ CALL_SUBTEST_2( integer_type_tests(Matrix<long, 2, 2>()) );
152
+ CALL_SUBTEST_2( signed_integer_type_tests(Matrix<long, 2, 2>()) );
153
+
154
+ CALL_SUBTEST_3( integer_type_tests(Matrix<char, 2, Dynamic>(2, 10)) );
155
+ CALL_SUBTEST_3( signed_integer_type_tests(Matrix<signed char, 2, Dynamic>(2, 10)) );
156
+
157
+ CALL_SUBTEST_4( integer_type_tests(Matrix<unsigned char, 3, 3>()) );
158
+ CALL_SUBTEST_4( integer_type_tests(Matrix<unsigned char, Dynamic, Dynamic>(20, 20)) );
159
+
160
+ CALL_SUBTEST_5( integer_type_tests(Matrix<short, Dynamic, 4>(7, 4)) );
161
+ CALL_SUBTEST_5( signed_integer_type_tests(Matrix<short, Dynamic, 4>(7, 4)) );
162
+
163
+ CALL_SUBTEST_6( integer_type_tests(Matrix<unsigned short, 4, 4>()) );
164
+
165
+ #if EIGEN_HAS_CXX11
166
+ CALL_SUBTEST_7( integer_type_tests(Matrix<long long, 11, 13>()) );
167
+ CALL_SUBTEST_7( signed_integer_type_tests(Matrix<long long, 11, 13>()) );
168
+
169
+ CALL_SUBTEST_8( integer_type_tests(Matrix<unsigned long long, Dynamic, 5>(1, 5)) );
170
+ #endif
171
+ }
172
+ CALL_SUBTEST_9( integer_types_extra<0>() );
173
+ }
include/eigen/test/io.cpp ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // This file is part of Eigen, a lightweight C++ template library
2
+ // for linear algebra.
3
+ //
4
+ // Copyright (C) 2019 Joel Holdsworth <joel.holdsworth@vcatechnology.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 <sstream>
11
+
12
+ #include "main.h"
13
+
14
+ template<typename Scalar>
15
+ struct check_ostream_impl
16
+ {
17
+ static void run()
18
+ {
19
+ const Array<Scalar,1,1> array(123);
20
+ std::ostringstream ss;
21
+ ss << array;
22
+ VERIFY(ss.str() == "123");
23
+
24
+ check_ostream_impl< std::complex<Scalar> >::run();
25
+ };
26
+ };
27
+
28
+ template<>
29
+ struct check_ostream_impl<bool>
30
+ {
31
+ static void run()
32
+ {
33
+ const Array<bool,1,2> array(1, 0);
34
+ std::ostringstream ss;
35
+ ss << array;
36
+ VERIFY(ss.str() == "1 0");
37
+ };
38
+ };
39
+
40
+ template<typename Scalar>
41
+ struct check_ostream_impl< std::complex<Scalar> >
42
+ {
43
+ static void run()
44
+ {
45
+ const Array<std::complex<Scalar>,1,1> array(std::complex<Scalar>(12, 34));
46
+ std::ostringstream ss;
47
+ ss << array;
48
+ VERIFY(ss.str() == "(12,34)");
49
+ };
50
+ };
51
+
52
+ template<typename Scalar>
53
+ static void check_ostream()
54
+ {
55
+ check_ostream_impl<Scalar>::run();
56
+ }
57
+
58
+ EIGEN_DECLARE_TEST(rand)
59
+ {
60
+ CALL_SUBTEST(check_ostream<bool>());
61
+ CALL_SUBTEST(check_ostream<float>());
62
+ CALL_SUBTEST(check_ostream<double>());
63
+ CALL_SUBTEST(check_ostream<Eigen::numext::int8_t>());
64
+ CALL_SUBTEST(check_ostream<Eigen::numext::uint8_t>());
65
+ CALL_SUBTEST(check_ostream<Eigen::numext::int16_t>());
66
+ CALL_SUBTEST(check_ostream<Eigen::numext::uint16_t>());
67
+ CALL_SUBTEST(check_ostream<Eigen::numext::int32_t>());
68
+ CALL_SUBTEST(check_ostream<Eigen::numext::uint32_t>());
69
+ CALL_SUBTEST(check_ostream<Eigen::numext::int64_t>());
70
+ CALL_SUBTEST(check_ostream<Eigen::numext::uint64_t>());
71
+ }
include/eigen/test/is_same_dense.cpp ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // This file is part of Eigen, a lightweight C++ template library
2
+ // for linear algebra.
3
+ //
4
+ // Copyright (C) 2015 Gael Guennebaud <gael.guennebaud@inria.fr>
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
+ using internal::is_same_dense;
13
+
14
+ EIGEN_DECLARE_TEST(is_same_dense)
15
+ {
16
+ typedef Matrix<double,Dynamic,Dynamic,ColMajor> ColMatrixXd;
17
+ typedef Matrix<std::complex<double>,Dynamic,Dynamic,ColMajor> ColMatrixXcd;
18
+ ColMatrixXd m1(10,10);
19
+ ColMatrixXcd m2(10,10);
20
+ Ref<ColMatrixXd> ref_m1(m1);
21
+ Ref<ColMatrixXd,0, Stride<Dynamic,Dynamic> > ref_m2_real(m2.real());
22
+ Ref<const ColMatrixXd> const_ref_m1(m1);
23
+
24
+ VERIFY(is_same_dense(m1,m1));
25
+ VERIFY(is_same_dense(m1,ref_m1));
26
+ VERIFY(is_same_dense(const_ref_m1,m1));
27
+ VERIFY(is_same_dense(const_ref_m1,ref_m1));
28
+
29
+ VERIFY(is_same_dense(m1.block(0,0,m1.rows(),m1.cols()),m1));
30
+ VERIFY(!is_same_dense(m1.row(0),m1.col(0)));
31
+
32
+ Ref<const ColMatrixXd> const_ref_m1_row(m1.row(1));
33
+ VERIFY(!is_same_dense(m1.row(1),const_ref_m1_row));
34
+
35
+ Ref<const ColMatrixXd> const_ref_m1_col(m1.col(1));
36
+ VERIFY(is_same_dense(m1.col(1),const_ref_m1_col));
37
+
38
+
39
+ VERIFY(!is_same_dense(m1, ref_m2_real));
40
+ VERIFY(!is_same_dense(m2, ref_m2_real));
41
+ }
include/eigen/test/jacobi.cpp ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // This file is part of Eigen, a lightweight C++ template library
2
+ // for linear algebra.
3
+ //
4
+ // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
5
+ // Copyright (C) 2009 Benoit Jacob <jacob.benoit.1@gmail.com>
6
+ //
7
+ // This Source Code Form is subject to the terms of the Mozilla
8
+ // Public License v. 2.0. If a copy of the MPL was not distributed
9
+ // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10
+
11
+ #include "main.h"
12
+ #include <Eigen/SVD>
13
+
14
+ template<typename MatrixType, typename JacobiScalar>
15
+ void jacobi(const MatrixType& m = MatrixType())
16
+ {
17
+ Index rows = m.rows();
18
+ Index cols = m.cols();
19
+
20
+ enum {
21
+ RowsAtCompileTime = MatrixType::RowsAtCompileTime,
22
+ ColsAtCompileTime = MatrixType::ColsAtCompileTime
23
+ };
24
+
25
+ typedef Matrix<JacobiScalar, 2, 1> JacobiVector;
26
+
27
+ const MatrixType a(MatrixType::Random(rows, cols));
28
+
29
+ JacobiVector v = JacobiVector::Random().normalized();
30
+ JacobiScalar c = v.x(), s = v.y();
31
+ JacobiRotation<JacobiScalar> rot(c, s);
32
+
33
+ {
34
+ Index p = internal::random<Index>(0, rows-1);
35
+ Index q;
36
+ do {
37
+ q = internal::random<Index>(0, rows-1);
38
+ } while (q == p);
39
+
40
+ MatrixType b = a;
41
+ b.applyOnTheLeft(p, q, rot);
42
+ VERIFY_IS_APPROX(b.row(p), c * a.row(p) + numext::conj(s) * a.row(q));
43
+ VERIFY_IS_APPROX(b.row(q), -s * a.row(p) + numext::conj(c) * a.row(q));
44
+ }
45
+
46
+ {
47
+ Index p = internal::random<Index>(0, cols-1);
48
+ Index q;
49
+ do {
50
+ q = internal::random<Index>(0, cols-1);
51
+ } while (q == p);
52
+
53
+ MatrixType b = a;
54
+ b.applyOnTheRight(p, q, rot);
55
+ VERIFY_IS_APPROX(b.col(p), c * a.col(p) - s * a.col(q));
56
+ VERIFY_IS_APPROX(b.col(q), numext::conj(s) * a.col(p) + numext::conj(c) * a.col(q));
57
+ }
58
+ }
59
+
60
+ EIGEN_DECLARE_TEST(jacobi)
61
+ {
62
+ for(int i = 0; i < g_repeat; i++) {
63
+ CALL_SUBTEST_1(( jacobi<Matrix3f, float>() ));
64
+ CALL_SUBTEST_2(( jacobi<Matrix4d, double>() ));
65
+ CALL_SUBTEST_3(( jacobi<Matrix4cf, float>() ));
66
+ CALL_SUBTEST_3(( jacobi<Matrix4cf, std::complex<float> >() ));
67
+
68
+ int r = internal::random<int>(2, internal::random<int>(1,EIGEN_TEST_MAX_SIZE)/2),
69
+ c = internal::random<int>(2, internal::random<int>(1,EIGEN_TEST_MAX_SIZE)/2);
70
+ CALL_SUBTEST_4(( jacobi<MatrixXf, float>(MatrixXf(r,c)) ));
71
+ CALL_SUBTEST_5(( jacobi<MatrixXcd, double>(MatrixXcd(r,c)) ));
72
+ CALL_SUBTEST_5(( jacobi<MatrixXcd, std::complex<double> >(MatrixXcd(r,c)) ));
73
+ // complex<float> is really important to test as it is the only way to cover conjugation issues in certain unaligned paths
74
+ CALL_SUBTEST_6(( jacobi<MatrixXcf, float>(MatrixXcf(r,c)) ));
75
+ CALL_SUBTEST_6(( jacobi<MatrixXcf, std::complex<float> >(MatrixXcf(r,c)) ));
76
+
77
+ TEST_SET_BUT_UNUSED_VARIABLE(r);
78
+ TEST_SET_BUT_UNUSED_VARIABLE(c);
79
+ }
80
+ }
include/eigen/test/lscg.cpp ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // This file is part of Eigen, a lightweight C++ template library
2
+ // for linear algebra.
3
+ //
4
+ // Copyright (C) 2011 Gael Guennebaud <g.gael@free.fr>
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 "sparse_solver.h"
11
+ #include <Eigen/IterativeLinearSolvers>
12
+
13
+ template<typename T> void test_lscg_T()
14
+ {
15
+ LeastSquaresConjugateGradient<SparseMatrix<T> > lscg_colmajor_diag;
16
+ LeastSquaresConjugateGradient<SparseMatrix<T>, IdentityPreconditioner> lscg_colmajor_I;
17
+ LeastSquaresConjugateGradient<SparseMatrix<T,RowMajor> > lscg_rowmajor_diag;
18
+ LeastSquaresConjugateGradient<SparseMatrix<T,RowMajor>, IdentityPreconditioner> lscg_rowmajor_I;
19
+
20
+ CALL_SUBTEST( check_sparse_square_solving(lscg_colmajor_diag) );
21
+ CALL_SUBTEST( check_sparse_square_solving(lscg_colmajor_I) );
22
+
23
+ CALL_SUBTEST( check_sparse_leastsquare_solving(lscg_colmajor_diag) );
24
+ CALL_SUBTEST( check_sparse_leastsquare_solving(lscg_colmajor_I) );
25
+
26
+ CALL_SUBTEST( check_sparse_square_solving(lscg_rowmajor_diag) );
27
+ CALL_SUBTEST( check_sparse_square_solving(lscg_rowmajor_I) );
28
+
29
+ CALL_SUBTEST( check_sparse_leastsquare_solving(lscg_rowmajor_diag) );
30
+ CALL_SUBTEST( check_sparse_leastsquare_solving(lscg_rowmajor_I) );
31
+ }
32
+
33
+ EIGEN_DECLARE_TEST(lscg)
34
+ {
35
+ CALL_SUBTEST_1(test_lscg_T<double>());
36
+ CALL_SUBTEST_2(test_lscg_T<std::complex<double> >());
37
+ }
include/eigen/test/main.h ADDED
@@ -0,0 +1,884 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ // This file is part of Eigen, a lightweight C++ template library
3
+ // for linear algebra.
4
+ //
5
+ // Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
6
+ // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
7
+ //
8
+ // This Source Code Form is subject to the terms of the Mozilla
9
+ // Public License v. 2.0. If a copy of the MPL was not distributed
10
+ // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
11
+
12
+ #include <cstdlib>
13
+ #include <cerrno>
14
+ #include <ctime>
15
+ #include <iostream>
16
+ #include <fstream>
17
+ #include <string>
18
+ #include <sstream>
19
+ #include <vector>
20
+ #include <typeinfo>
21
+ #include <functional>
22
+
23
+ // The following includes of STL headers have to be done _before_ the
24
+ // definition of macros min() and max(). The reason is that many STL
25
+ // implementations will not work properly as the min and max symbols collide
26
+ // with the STL functions std:min() and std::max(). The STL headers may check
27
+ // for the macro definition of min/max and issue a warning or undefine the
28
+ // macros.
29
+ //
30
+ // Still, Windows defines min() and max() in windef.h as part of the regular
31
+ // Windows system interfaces and many other Windows APIs depend on these
32
+ // macros being available. To prevent the macro expansion of min/max and to
33
+ // make Eigen compatible with the Windows environment all function calls of
34
+ // std::min() and std::max() have to be written with parenthesis around the
35
+ // function name.
36
+ //
37
+ // All STL headers used by Eigen should be included here. Because main.h is
38
+ // included before any Eigen header and because the STL headers are guarded
39
+ // against multiple inclusions, no STL header will see our own min/max macro
40
+ // definitions.
41
+ #include <limits>
42
+ #include <algorithm>
43
+ // Disable ICC's std::complex operator specializations so we can use our own.
44
+ #define _OVERRIDE_COMPLEX_SPECIALIZATION_ 1
45
+ #include <complex>
46
+ #include <deque>
47
+ #include <queue>
48
+ #include <cassert>
49
+ #include <list>
50
+ #if __cplusplus >= 201103L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201103L)
51
+ #include <random>
52
+ #include <chrono>
53
+ #ifdef EIGEN_USE_THREADS
54
+ #include <future>
55
+ #endif
56
+ #endif
57
+
58
+ // Same for cuda_fp16.h
59
+ #if defined(__CUDACC__) && !defined(EIGEN_NO_CUDA)
60
+ // Means the compiler is either nvcc or clang with CUDA enabled
61
+ #define EIGEN_CUDACC __CUDACC__
62
+ #endif
63
+ #if defined(EIGEN_CUDACC)
64
+ #include <cuda.h>
65
+ #define EIGEN_CUDA_SDK_VER (CUDA_VERSION * 10)
66
+ #else
67
+ #define EIGEN_CUDA_SDK_VER 0
68
+ #endif
69
+ #if EIGEN_CUDA_SDK_VER >= 70500
70
+ #include <cuda_fp16.h>
71
+ #endif
72
+
73
+ // To test that all calls from Eigen code to std::min() and std::max() are
74
+ // protected by parenthesis against macro expansion, the min()/max() macros
75
+ // are defined here and any not-parenthesized min/max call will cause a
76
+ // compiler error.
77
+ #if !defined(__HIPCC__) && !defined(EIGEN_USE_SYCL)
78
+ //
79
+ // HIP header files include the following files
80
+ // <thread>
81
+ // <regex>
82
+ // <unordered_map>
83
+ // which seem to contain not-parenthesized calls to "max"/"min", triggering the following check and causing the compile to fail
84
+ //
85
+ // Including those header files before the following macro definition for "min" / "max", only partially resolves the issue
86
+ // This is because other HIP header files also define "isnan" / "isinf" / "isfinite" functions, which are needed in other
87
+ // headers.
88
+ //
89
+ // So instead choosing to simply disable this check for HIP
90
+ //
91
+ #define min(A,B) please_protect_your_min_with_parentheses
92
+ #define max(A,B) please_protect_your_max_with_parentheses
93
+ #define isnan(X) please_protect_your_isnan_with_parentheses
94
+ #define isinf(X) please_protect_your_isinf_with_parentheses
95
+ #define isfinite(X) please_protect_your_isfinite_with_parentheses
96
+ #endif
97
+
98
+
99
+ // test possible conflicts
100
+ struct real {};
101
+ struct imag {};
102
+
103
+ #ifdef M_PI
104
+ #undef M_PI
105
+ #endif
106
+ #define M_PI please_use_EIGEN_PI_instead_of_M_PI
107
+
108
+ #define FORBIDDEN_IDENTIFIER (this_identifier_is_forbidden_to_avoid_clashes) this_identifier_is_forbidden_to_avoid_clashes
109
+ // B0 is defined in POSIX header termios.h
110
+ #define B0 FORBIDDEN_IDENTIFIER
111
+ // `I` may be defined by complex.h:
112
+ #define I FORBIDDEN_IDENTIFIER
113
+
114
+ // _res is defined by resolv.h
115
+ #define _res FORBIDDEN_IDENTIFIER
116
+
117
+ // Unit tests calling Eigen's blas library must preserve the default blocking size
118
+ // to avoid troubles.
119
+ #ifndef EIGEN_NO_DEBUG_SMALL_PRODUCT_BLOCKS
120
+ #define EIGEN_DEBUG_SMALL_PRODUCT_BLOCKS
121
+ #endif
122
+
123
+ // shuts down ICC's remark #593: variable "XXX" was set but never used
124
+ #define TEST_SET_BUT_UNUSED_VARIABLE(X) EIGEN_UNUSED_VARIABLE(X)
125
+
126
+ #ifdef TEST_ENABLE_TEMPORARY_TRACKING
127
+
128
+ static long int nb_temporaries;
129
+ static long int nb_temporaries_on_assert = -1;
130
+
131
+ inline void on_temporary_creation(long int size) {
132
+ // here's a great place to set a breakpoint when debugging failures in this test!
133
+ if(size!=0) nb_temporaries++;
134
+ if(nb_temporaries_on_assert>0) assert(nb_temporaries<nb_temporaries_on_assert);
135
+ }
136
+
137
+ #define EIGEN_DENSE_STORAGE_CTOR_PLUGIN { on_temporary_creation(size); }
138
+
139
+ #define VERIFY_EVALUATION_COUNT(XPR,N) {\
140
+ nb_temporaries = 0; \
141
+ XPR; \
142
+ if(nb_temporaries!=(N)) { std::cerr << "nb_temporaries == " << nb_temporaries << "\n"; }\
143
+ VERIFY( (#XPR) && nb_temporaries==(N) ); \
144
+ }
145
+
146
+ #endif
147
+
148
+ #include "split_test_helper.h"
149
+
150
+ #ifdef NDEBUG
151
+ #undef NDEBUG
152
+ #endif
153
+
154
+ // On windows CE, NDEBUG is automatically defined <assert.h> if NDEBUG is not defined.
155
+ #ifndef DEBUG
156
+ #define DEBUG
157
+ #endif
158
+
159
+ // bounds integer values for AltiVec
160
+ #if defined(__ALTIVEC__) || defined(__VSX__)
161
+ #define EIGEN_MAKING_DOCS
162
+ #endif
163
+
164
+ #define DEFAULT_REPEAT 10
165
+
166
+ namespace Eigen
167
+ {
168
+ static std::vector<std::string> g_test_stack;
169
+ // level == 0 <=> abort if test fail
170
+ // level >= 1 <=> warning message to std::cerr if test fail
171
+ static int g_test_level = 0;
172
+ static int g_repeat = 1;
173
+ static unsigned int g_seed = 0;
174
+ static bool g_has_set_repeat = false, g_has_set_seed = false;
175
+
176
+ class EigenTest
177
+ {
178
+ public:
179
+ EigenTest() : m_func(0) {}
180
+ EigenTest(const char* a_name, void (*func)(void))
181
+ : m_name(a_name), m_func(func)
182
+ {
183
+ get_registered_tests().push_back(this);
184
+ }
185
+ const std::string& name() const { return m_name; }
186
+ void operator()() const { m_func(); }
187
+
188
+ static const std::vector<EigenTest*>& all() { return get_registered_tests(); }
189
+ protected:
190
+ static std::vector<EigenTest*>& get_registered_tests()
191
+ {
192
+ static std::vector<EigenTest*>* ms_registered_tests = new std::vector<EigenTest*>();
193
+ return *ms_registered_tests;
194
+ }
195
+ std::string m_name;
196
+ void (*m_func)(void);
197
+ };
198
+
199
+ // Declare and register a test, e.g.:
200
+ // EIGEN_DECLARE_TEST(mytest) { ... }
201
+ // will create a function:
202
+ // void test_mytest() { ... }
203
+ // that will be automatically called.
204
+ #define EIGEN_DECLARE_TEST(X) \
205
+ void EIGEN_CAT(test_,X) (); \
206
+ static EigenTest EIGEN_CAT(test_handler_,X) (EIGEN_MAKESTRING(X), & EIGEN_CAT(test_,X)); \
207
+ void EIGEN_CAT(test_,X) ()
208
+ }
209
+
210
+ #define TRACK std::cerr << __FILE__ << " " << __LINE__ << std::endl
211
+ // #define TRACK while()
212
+
213
+ #define EIGEN_DEFAULT_IO_FORMAT IOFormat(4, 0, " ", "\n", "", "", "", "")
214
+
215
+ #if (defined(_CPPUNWIND) || defined(__EXCEPTIONS)) && !defined(__CUDA_ARCH__) && !defined(__HIP_DEVICE_COMPILE__) && !defined(__SYCL_DEVICE_ONLY__)
216
+ #define EIGEN_EXCEPTIONS
217
+ #endif
218
+
219
+ #ifndef EIGEN_NO_ASSERTION_CHECKING
220
+
221
+ namespace Eigen
222
+ {
223
+ static const bool should_raise_an_assert = false;
224
+
225
+ // Used to avoid to raise two exceptions at a time in which
226
+ // case the exception is not properly caught.
227
+ // This may happen when a second exceptions is triggered in a destructor.
228
+ static bool no_more_assert = false;
229
+ static bool report_on_cerr_on_assert_failure = true;
230
+
231
+ struct eigen_assert_exception
232
+ {
233
+ eigen_assert_exception(void) {}
234
+ ~eigen_assert_exception() { Eigen::no_more_assert = false; }
235
+ };
236
+
237
+ struct eigen_static_assert_exception
238
+ {
239
+ eigen_static_assert_exception(void) {}
240
+ ~eigen_static_assert_exception() { Eigen::no_more_assert = false; }
241
+ };
242
+ }
243
+ // If EIGEN_DEBUG_ASSERTS is defined and if no assertion is triggered while
244
+ // one should have been, then the list of executed assertions is printed out.
245
+ //
246
+ // EIGEN_DEBUG_ASSERTS is not enabled by default as it
247
+ // significantly increases the compilation time
248
+ // and might even introduce side effects that would hide
249
+ // some memory errors.
250
+ #ifdef EIGEN_DEBUG_ASSERTS
251
+
252
+ namespace Eigen
253
+ {
254
+ namespace internal
255
+ {
256
+ static bool push_assert = false;
257
+ }
258
+ static std::vector<std::string> eigen_assert_list;
259
+ }
260
+ #define eigen_assert(a) \
261
+ if( (!(a)) && (!no_more_assert) ) \
262
+ { \
263
+ if(report_on_cerr_on_assert_failure) \
264
+ std::cerr << #a << " " __FILE__ << "(" << __LINE__ << ")\n"; \
265
+ Eigen::no_more_assert = true; \
266
+ EIGEN_THROW_X(Eigen::eigen_assert_exception()); \
267
+ } \
268
+ else if (Eigen::internal::push_assert) \
269
+ { \
270
+ eigen_assert_list.push_back(std::string(EIGEN_MAKESTRING(__FILE__) " (" EIGEN_MAKESTRING(__LINE__) ") : " #a) ); \
271
+ }
272
+
273
+ #ifdef EIGEN_EXCEPTIONS
274
+ #define VERIFY_RAISES_ASSERT(a) \
275
+ { \
276
+ Eigen::no_more_assert = false; \
277
+ Eigen::eigen_assert_list.clear(); \
278
+ Eigen::internal::push_assert = true; \
279
+ Eigen::report_on_cerr_on_assert_failure = false; \
280
+ try { \
281
+ a; \
282
+ std::cerr << "One of the following asserts should have been triggered:\n"; \
283
+ for (uint ai=0 ; ai<eigen_assert_list.size() ; ++ai) \
284
+ std::cerr << " " << eigen_assert_list[ai] << "\n"; \
285
+ VERIFY(Eigen::should_raise_an_assert && # a); \
286
+ } catch (Eigen::eigen_assert_exception) { \
287
+ Eigen::internal::push_assert = false; VERIFY(true); \
288
+ } \
289
+ Eigen::report_on_cerr_on_assert_failure = true; \
290
+ Eigen::internal::push_assert = false; \
291
+ }
292
+ #endif //EIGEN_EXCEPTIONS
293
+
294
+ #elif !defined(__CUDACC__) && !defined(__HIPCC__) && !defined(SYCL_DEVICE_ONLY) // EIGEN_DEBUG_ASSERTS
295
+ // see bug 89. The copy_bool here is working around a bug in gcc <= 4.3
296
+ #define eigen_assert(a) \
297
+ if( (!Eigen::internal::copy_bool(a)) && (!no_more_assert) )\
298
+ { \
299
+ Eigen::no_more_assert = true; \
300
+ if(report_on_cerr_on_assert_failure) \
301
+ eigen_plain_assert(a); \
302
+ else \
303
+ EIGEN_THROW_X(Eigen::eigen_assert_exception()); \
304
+ }
305
+
306
+ #ifdef EIGEN_EXCEPTIONS
307
+ #define VERIFY_RAISES_ASSERT(a) { \
308
+ Eigen::no_more_assert = false; \
309
+ Eigen::report_on_cerr_on_assert_failure = false; \
310
+ try { \
311
+ a; \
312
+ VERIFY(Eigen::should_raise_an_assert && # a); \
313
+ } \
314
+ catch (Eigen::eigen_assert_exception&) { VERIFY(true); } \
315
+ Eigen::report_on_cerr_on_assert_failure = true; \
316
+ }
317
+ #endif // EIGEN_EXCEPTIONS
318
+ #endif // EIGEN_DEBUG_ASSERTS
319
+
320
+ #if defined(TEST_CHECK_STATIC_ASSERTIONS) && defined(EIGEN_EXCEPTIONS)
321
+ #define EIGEN_STATIC_ASSERT(a,MSG) \
322
+ if( (!Eigen::internal::copy_bool(a)) && (!no_more_assert) )\
323
+ { \
324
+ Eigen::no_more_assert = true; \
325
+ if(report_on_cerr_on_assert_failure) \
326
+ eigen_plain_assert((a) && #MSG); \
327
+ else \
328
+ EIGEN_THROW_X(Eigen::eigen_static_assert_exception()); \
329
+ }
330
+ #define VERIFY_RAISES_STATIC_ASSERT(a) { \
331
+ Eigen::no_more_assert = false; \
332
+ Eigen::report_on_cerr_on_assert_failure = false; \
333
+ try { \
334
+ a; \
335
+ VERIFY(Eigen::should_raise_an_assert && # a); \
336
+ } \
337
+ catch (Eigen::eigen_static_assert_exception&) { VERIFY(true); } \
338
+ Eigen::report_on_cerr_on_assert_failure = true; \
339
+ }
340
+ #endif // TEST_CHECK_STATIC_ASSERTIONS
341
+
342
+ #ifndef VERIFY_RAISES_ASSERT
343
+ #define VERIFY_RAISES_ASSERT(a) \
344
+ std::cout << "Can't VERIFY_RAISES_ASSERT( " #a " ) with exceptions disabled\n";
345
+ #endif
346
+ #ifndef VERIFY_RAISES_STATIC_ASSERT
347
+ #define VERIFY_RAISES_STATIC_ASSERT(a) \
348
+ std::cout << "Can't VERIFY_RAISES_STATIC_ASSERT( " #a " ) with exceptions disabled\n";
349
+ #endif
350
+
351
+ #if !defined(__CUDACC__) && !defined(__HIPCC__) && !defined(SYCL_DEVICE_ONLY)
352
+ #define EIGEN_USE_CUSTOM_ASSERT
353
+ #endif
354
+
355
+ #else // EIGEN_NO_ASSERTION_CHECKING
356
+
357
+ #define VERIFY_RAISES_ASSERT(a) {}
358
+ #define VERIFY_RAISES_STATIC_ASSERT(a) {}
359
+
360
+ #endif // EIGEN_NO_ASSERTION_CHECKING
361
+
362
+ #define EIGEN_INTERNAL_DEBUGGING
363
+ #include <Eigen/QR> // required for createRandomPIMatrixOfRank
364
+
365
+ inline void verify_impl(bool condition, const char *testname, const char *file, int line, const char *condition_as_string)
366
+ {
367
+ if (!condition)
368
+ {
369
+ if(Eigen::g_test_level>0)
370
+ std::cerr << "WARNING: ";
371
+ std::cerr << "Test " << testname << " failed in " << file << " (" << line << ")"
372
+ << std::endl << " " << condition_as_string << std::endl;
373
+ std::cerr << "Stack:\n";
374
+ const int test_stack_size = static_cast<int>(Eigen::g_test_stack.size());
375
+ for(int i=test_stack_size-1; i>=0; --i)
376
+ std::cerr << " - " << Eigen::g_test_stack[i] << "\n";
377
+ std::cerr << "\n";
378
+ if(Eigen::g_test_level==0)
379
+ abort();
380
+ }
381
+ }
382
+
383
+ #define VERIFY(a) ::verify_impl(a, g_test_stack.back().c_str(), __FILE__, __LINE__, EIGEN_MAKESTRING(a))
384
+
385
+ #define VERIFY_GE(a, b) ::verify_impl(a >= b, g_test_stack.back().c_str(), __FILE__, __LINE__, EIGEN_MAKESTRING(a >= b))
386
+ #define VERIFY_LE(a, b) ::verify_impl(a <= b, g_test_stack.back().c_str(), __FILE__, __LINE__, EIGEN_MAKESTRING(a <= b))
387
+
388
+
389
+ #define VERIFY_IS_EQUAL(a, b) VERIFY(test_is_equal(a, b, true))
390
+ #define VERIFY_IS_NOT_EQUAL(a, b) VERIFY(test_is_equal(a, b, false))
391
+ #define VERIFY_IS_APPROX(a, b) VERIFY(verifyIsApprox(a, b))
392
+ #define VERIFY_IS_NOT_APPROX(a, b) VERIFY(!test_isApprox(a, b))
393
+ #define VERIFY_IS_MUCH_SMALLER_THAN(a, b) VERIFY(test_isMuchSmallerThan(a, b))
394
+ #define VERIFY_IS_NOT_MUCH_SMALLER_THAN(a, b) VERIFY(!test_isMuchSmallerThan(a, b))
395
+ #define VERIFY_IS_APPROX_OR_LESS_THAN(a, b) VERIFY(test_isApproxOrLessThan(a, b))
396
+ #define VERIFY_IS_NOT_APPROX_OR_LESS_THAN(a, b) VERIFY(!test_isApproxOrLessThan(a, b))
397
+ #define VERIFY_IS_CWISE_EQUAL(a, b) VERIFY(verifyIsCwiseApprox(a, b, true))
398
+ #define VERIFY_IS_CWISE_APPROX(a, b) VERIFY(verifyIsCwiseApprox(a, b, false))
399
+
400
+ #define VERIFY_IS_UNITARY(a) VERIFY(test_isUnitary(a))
401
+
402
+ #define STATIC_CHECK(COND) EIGEN_STATIC_ASSERT( (COND) , EIGEN_INTERNAL_ERROR_PLEASE_FILE_A_BUG_REPORT )
403
+
404
+ #define CALL_SUBTEST(FUNC) do { \
405
+ g_test_stack.push_back(EIGEN_MAKESTRING(FUNC)); \
406
+ FUNC; \
407
+ g_test_stack.pop_back(); \
408
+ } while (0)
409
+
410
+
411
+ namespace Eigen {
412
+
413
+ template<typename T1,typename T2>
414
+ typename internal::enable_if<internal::is_same<T1,T2>::value,bool>::type
415
+ is_same_type(const T1&, const T2&)
416
+ {
417
+ return true;
418
+ }
419
+
420
+ template<typename T> inline typename NumTraits<T>::Real test_precision() { return NumTraits<T>::dummy_precision(); }
421
+ template<> inline float test_precision<float>() { return 1e-3f; }
422
+ template<> inline double test_precision<double>() { return 1e-6; }
423
+ template<> inline long double test_precision<long double>() { return 1e-6l; }
424
+ template<> inline float test_precision<std::complex<float> >() { return test_precision<float>(); }
425
+ template<> inline double test_precision<std::complex<double> >() { return test_precision<double>(); }
426
+ template<> inline long double test_precision<std::complex<long double> >() { return test_precision<long double>(); }
427
+
428
+ #define EIGEN_TEST_SCALAR_TEST_OVERLOAD(TYPE) \
429
+ inline bool test_isApprox(TYPE a, TYPE b) \
430
+ { return numext::equal_strict(a, b) || \
431
+ ((numext::isnan)(a) && (numext::isnan)(b)) || \
432
+ (internal::isApprox(a, b, test_precision<TYPE>())); } \
433
+ inline bool test_isCwiseApprox(TYPE a, TYPE b, bool exact) \
434
+ { return numext::equal_strict(a, b) || \
435
+ ((numext::isnan)(a) && (numext::isnan)(b)) || \
436
+ (!exact && internal::isApprox(a, b, test_precision<TYPE>())); } \
437
+ inline bool test_isMuchSmallerThan(TYPE a, TYPE b) \
438
+ { return internal::isMuchSmallerThan(a, b, test_precision<TYPE>()); } \
439
+ inline bool test_isApproxOrLessThan(TYPE a, TYPE b) \
440
+ { return internal::isApproxOrLessThan(a, b, test_precision<TYPE>()); }
441
+
442
+ EIGEN_TEST_SCALAR_TEST_OVERLOAD(short)
443
+ EIGEN_TEST_SCALAR_TEST_OVERLOAD(unsigned short)
444
+ EIGEN_TEST_SCALAR_TEST_OVERLOAD(int)
445
+ EIGEN_TEST_SCALAR_TEST_OVERLOAD(unsigned int)
446
+ EIGEN_TEST_SCALAR_TEST_OVERLOAD(long)
447
+ EIGEN_TEST_SCALAR_TEST_OVERLOAD(unsigned long)
448
+ #if EIGEN_HAS_CXX11
449
+ EIGEN_TEST_SCALAR_TEST_OVERLOAD(long long)
450
+ EIGEN_TEST_SCALAR_TEST_OVERLOAD(unsigned long long)
451
+ #endif
452
+ EIGEN_TEST_SCALAR_TEST_OVERLOAD(float)
453
+ EIGEN_TEST_SCALAR_TEST_OVERLOAD(double)
454
+ EIGEN_TEST_SCALAR_TEST_OVERLOAD(half)
455
+ EIGEN_TEST_SCALAR_TEST_OVERLOAD(bfloat16)
456
+
457
+ #undef EIGEN_TEST_SCALAR_TEST_OVERLOAD
458
+
459
+ #ifndef EIGEN_TEST_NO_COMPLEX
460
+ inline bool test_isApprox(const std::complex<float>& a, const std::complex<float>& b)
461
+ { return internal::isApprox(a, b, test_precision<std::complex<float> >()); }
462
+ inline bool test_isMuchSmallerThan(const std::complex<float>& a, const std::complex<float>& b)
463
+ { return internal::isMuchSmallerThan(a, b, test_precision<std::complex<float> >()); }
464
+
465
+ inline bool test_isApprox(const std::complex<double>& a, const std::complex<double>& b)
466
+ { return internal::isApprox(a, b, test_precision<std::complex<double> >()); }
467
+ inline bool test_isMuchSmallerThan(const std::complex<double>& a, const std::complex<double>& b)
468
+ { return internal::isMuchSmallerThan(a, b, test_precision<std::complex<double> >()); }
469
+
470
+ #ifndef EIGEN_TEST_NO_LONGDOUBLE
471
+ inline bool test_isApprox(const std::complex<long double>& a, const std::complex<long double>& b)
472
+ { return internal::isApprox(a, b, test_precision<std::complex<long double> >()); }
473
+ inline bool test_isMuchSmallerThan(const std::complex<long double>& a, const std::complex<long double>& b)
474
+ { return internal::isMuchSmallerThan(a, b, test_precision<std::complex<long double> >()); }
475
+ #endif
476
+ #endif
477
+
478
+ #ifndef EIGEN_TEST_NO_LONGDOUBLE
479
+ inline bool test_isApprox(const long double& a, const long double& b)
480
+ {
481
+ bool ret = internal::isApprox(a, b, test_precision<long double>());
482
+ if (!ret) std::cerr
483
+ << std::endl << " actual = " << a
484
+ << std::endl << " expected = " << b << std::endl << std::endl;
485
+ return ret;
486
+ }
487
+
488
+ inline bool test_isMuchSmallerThan(const long double& a, const long double& b)
489
+ { return internal::isMuchSmallerThan(a, b, test_precision<long double>()); }
490
+ inline bool test_isApproxOrLessThan(const long double& a, const long double& b)
491
+ { return internal::isApproxOrLessThan(a, b, test_precision<long double>()); }
492
+ #endif // EIGEN_TEST_NO_LONGDOUBLE
493
+
494
+ // test_relative_error returns the relative difference between a and b as a real scalar as used in isApprox.
495
+ template<typename T1,typename T2>
496
+ typename NumTraits<typename T1::RealScalar>::NonInteger test_relative_error(const EigenBase<T1> &a, const EigenBase<T2> &b)
497
+ {
498
+ using std::sqrt;
499
+ typedef typename NumTraits<typename T1::RealScalar>::NonInteger RealScalar;
500
+ typename internal::nested_eval<T1,2>::type ea(a.derived());
501
+ typename internal::nested_eval<T2,2>::type eb(b.derived());
502
+ return sqrt(RealScalar((ea-eb).cwiseAbs2().sum()) / RealScalar((std::min)(eb.cwiseAbs2().sum(),ea.cwiseAbs2().sum())));
503
+ }
504
+
505
+ template<typename T1,typename T2>
506
+ typename T1::RealScalar test_relative_error(const T1 &a, const T2 &b, const typename T1::Coefficients* = 0)
507
+ {
508
+ return test_relative_error(a.coeffs(), b.coeffs());
509
+ }
510
+
511
+ template<typename T1,typename T2>
512
+ typename T1::Scalar test_relative_error(const T1 &a, const T2 &b, const typename T1::MatrixType* = 0)
513
+ {
514
+ return test_relative_error(a.matrix(), b.matrix());
515
+ }
516
+
517
+ template<typename S, int D>
518
+ S test_relative_error(const Translation<S,D> &a, const Translation<S,D> &b)
519
+ {
520
+ return test_relative_error(a.vector(), b.vector());
521
+ }
522
+
523
+ template <typename S, int D, int O>
524
+ S test_relative_error(const ParametrizedLine<S,D,O> &a, const ParametrizedLine<S,D,O> &b)
525
+ {
526
+ return (std::max)(test_relative_error(a.origin(), b.origin()), test_relative_error(a.origin(), b.origin()));
527
+ }
528
+
529
+ template <typename S, int D>
530
+ S test_relative_error(const AlignedBox<S,D> &a, const AlignedBox<S,D> &b)
531
+ {
532
+ return (std::max)(test_relative_error((a.min)(), (b.min)()), test_relative_error((a.max)(), (b.max)()));
533
+ }
534
+
535
+ template<typename Derived> class SparseMatrixBase;
536
+ template<typename T1,typename T2>
537
+ typename T1::RealScalar test_relative_error(const MatrixBase<T1> &a, const SparseMatrixBase<T2> &b)
538
+ {
539
+ return test_relative_error(a,b.toDense());
540
+ }
541
+
542
+ template<typename Derived> class SparseMatrixBase;
543
+ template<typename T1,typename T2>
544
+ typename T1::RealScalar test_relative_error(const SparseMatrixBase<T1> &a, const MatrixBase<T2> &b)
545
+ {
546
+ return test_relative_error(a.toDense(),b);
547
+ }
548
+
549
+ template<typename Derived> class SparseMatrixBase;
550
+ template<typename T1,typename T2>
551
+ typename T1::RealScalar test_relative_error(const SparseMatrixBase<T1> &a, const SparseMatrixBase<T2> &b)
552
+ {
553
+ return test_relative_error(a.toDense(),b.toDense());
554
+ }
555
+
556
+ template<typename T1,typename T2>
557
+ typename NumTraits<typename NumTraits<T1>::Real>::NonInteger test_relative_error(const T1 &a, const T2 &b, typename internal::enable_if<internal::is_arithmetic<typename NumTraits<T1>::Real>::value, T1>::type* = 0)
558
+ {
559
+ typedef typename NumTraits<typename NumTraits<T1>::Real>::NonInteger RealScalar;
560
+ return numext::sqrt(RealScalar(numext::abs2(a-b))/(numext::mini)(RealScalar(numext::abs2(a)),RealScalar(numext::abs2(b))));
561
+ }
562
+
563
+ template<typename T>
564
+ T test_relative_error(const Rotation2D<T> &a, const Rotation2D<T> &b)
565
+ {
566
+ return test_relative_error(a.angle(), b.angle());
567
+ }
568
+
569
+ template<typename T>
570
+ T test_relative_error(const AngleAxis<T> &a, const AngleAxis<T> &b)
571
+ {
572
+ return (std::max)(test_relative_error(a.angle(), b.angle()), test_relative_error(a.axis(), b.axis()));
573
+ }
574
+
575
+ template<typename Type1, typename Type2>
576
+ inline bool test_isApprox(const Type1& a, const Type2& b, typename Type1::Scalar* = 0) // Enabled for Eigen's type only
577
+ {
578
+ return a.isApprox(b, test_precision<typename Type1::Scalar>());
579
+ }
580
+
581
+ // get_test_precision is a small wrapper to test_precision allowing to return the scalar precision for either scalars or expressions
582
+ template<typename T>
583
+ typename NumTraits<typename T::Scalar>::Real get_test_precision(const T&, const typename T::Scalar* = 0)
584
+ {
585
+ return test_precision<typename NumTraits<typename T::Scalar>::Real>();
586
+ }
587
+
588
+ template<typename T>
589
+ typename NumTraits<T>::Real get_test_precision(const T&,typename internal::enable_if<internal::is_arithmetic<typename NumTraits<T>::Real>::value, T>::type* = 0)
590
+ {
591
+ return test_precision<typename NumTraits<T>::Real>();
592
+ }
593
+
594
+ // verifyIsApprox is a wrapper to test_isApprox that outputs the relative difference magnitude if the test fails.
595
+ template<typename Type1, typename Type2>
596
+ inline bool verifyIsApprox(const Type1& a, const Type2& b)
597
+ {
598
+ bool ret = test_isApprox(a,b);
599
+ if(!ret)
600
+ {
601
+ std::cerr << "Difference too large wrt tolerance " << get_test_precision(a) << ", relative error is: " << test_relative_error(a,b) << std::endl;
602
+ }
603
+ return ret;
604
+ }
605
+
606
+ // verifyIsCwiseApprox is a wrapper to test_isCwiseApprox that outputs the relative difference magnitude if the test fails.
607
+ template<typename Type1, typename Type2>
608
+ inline bool verifyIsCwiseApprox(const Type1& a, const Type2& b, bool exact)
609
+ {
610
+ bool ret = test_isCwiseApprox(a,b,exact);
611
+ if(!ret) {
612
+ if (exact) {
613
+ std::cerr << "Values are not an exact match";
614
+ } else {
615
+ std::cerr << "Difference too large wrt tolerance " << get_test_precision(a);
616
+ }
617
+ std::cerr << ", relative error is: " << test_relative_error(a,b) << std::endl;
618
+ }
619
+ return ret;
620
+ }
621
+
622
+ // The idea behind this function is to compare the two scalars a and b where
623
+ // the scalar ref is a hint about the expected order of magnitude of a and b.
624
+ // WARNING: the scalar a and b must be positive
625
+ // Therefore, if for some reason a and b are very small compared to ref,
626
+ // we won't issue a false negative.
627
+ // This test could be: abs(a-b) <= eps * ref
628
+ // However, it seems that simply comparing a+ref and b+ref is more sensitive to true error.
629
+ template<typename Scalar,typename ScalarRef>
630
+ inline bool test_isApproxWithRef(const Scalar& a, const Scalar& b, const ScalarRef& ref)
631
+ {
632
+ return test_isApprox(a+ref, b+ref);
633
+ }
634
+
635
+ template<typename Derived1, typename Derived2>
636
+ inline bool test_isMuchSmallerThan(const MatrixBase<Derived1>& m1,
637
+ const MatrixBase<Derived2>& m2)
638
+ {
639
+ return m1.isMuchSmallerThan(m2, test_precision<typename internal::traits<Derived1>::Scalar>());
640
+ }
641
+
642
+ template<typename Derived>
643
+ inline bool test_isMuchSmallerThan(const MatrixBase<Derived>& m,
644
+ const typename NumTraits<typename internal::traits<Derived>::Scalar>::Real& s)
645
+ {
646
+ return m.isMuchSmallerThan(s, test_precision<typename internal::traits<Derived>::Scalar>());
647
+ }
648
+
649
+ template<typename Derived>
650
+ inline bool test_isUnitary(const MatrixBase<Derived>& m)
651
+ {
652
+ return m.isUnitary(test_precision<typename internal::traits<Derived>::Scalar>());
653
+ }
654
+
655
+ // Forward declaration to avoid ICC warning
656
+ template<typename T, typename U>
657
+ bool test_is_equal(const T& actual, const U& expected, bool expect_equal=true);
658
+
659
+ template<typename T, typename U>
660
+ bool test_is_equal(const T& actual, const U& expected, bool expect_equal)
661
+ {
662
+ if ((actual==expected) == expect_equal)
663
+ return true;
664
+ // false:
665
+ std::cerr
666
+ << "\n actual = " << actual
667
+ << "\n expected " << (expect_equal ? "= " : "!=") << expected << "\n\n";
668
+ return false;
669
+ }
670
+
671
+ /** Creates a random Partial Isometry matrix of given rank.
672
+ *
673
+ * A partial isometry is a matrix all of whose singular values are either 0 or 1.
674
+ * This is very useful to test rank-revealing algorithms.
675
+ */
676
+ // Forward declaration to avoid ICC warning
677
+ template<typename MatrixType>
678
+ void createRandomPIMatrixOfRank(Index desired_rank, Index rows, Index cols, MatrixType& m);
679
+ template<typename MatrixType>
680
+ void createRandomPIMatrixOfRank(Index desired_rank, Index rows, Index cols, MatrixType& m)
681
+ {
682
+ typedef typename internal::traits<MatrixType>::Scalar Scalar;
683
+ enum { Rows = MatrixType::RowsAtCompileTime, Cols = MatrixType::ColsAtCompileTime };
684
+
685
+ typedef Matrix<Scalar, Dynamic, 1> VectorType;
686
+ typedef Matrix<Scalar, Rows, Rows> MatrixAType;
687
+ typedef Matrix<Scalar, Cols, Cols> MatrixBType;
688
+
689
+ if(desired_rank == 0)
690
+ {
691
+ m.setZero(rows,cols);
692
+ return;
693
+ }
694
+
695
+ if(desired_rank == 1)
696
+ {
697
+ // here we normalize the vectors to get a partial isometry
698
+ m = VectorType::Random(rows).normalized() * VectorType::Random(cols).normalized().transpose();
699
+ return;
700
+ }
701
+
702
+ MatrixAType a = MatrixAType::Random(rows,rows);
703
+ MatrixType d = MatrixType::Identity(rows,cols);
704
+ MatrixBType b = MatrixBType::Random(cols,cols);
705
+
706
+ // set the diagonal such that only desired_rank non-zero entries reamain
707
+ const Index diag_size = (std::min)(d.rows(),d.cols());
708
+ if(diag_size != desired_rank)
709
+ d.diagonal().segment(desired_rank, diag_size-desired_rank) = VectorType::Zero(diag_size-desired_rank);
710
+
711
+ HouseholderQR<MatrixAType> qra(a);
712
+ HouseholderQR<MatrixBType> qrb(b);
713
+ m = qra.householderQ() * d * qrb.householderQ();
714
+ }
715
+
716
+ // Forward declaration to avoid ICC warning
717
+ template<typename PermutationVectorType>
718
+ void randomPermutationVector(PermutationVectorType& v, Index size);
719
+ template<typename PermutationVectorType>
720
+ void randomPermutationVector(PermutationVectorType& v, Index size)
721
+ {
722
+ typedef typename PermutationVectorType::Scalar Scalar;
723
+ v.resize(size);
724
+ for(Index i = 0; i < size; ++i) v(i) = Scalar(i);
725
+ if(size == 1) return;
726
+ for(Index n = 0; n < 3 * size; ++n)
727
+ {
728
+ Index i = internal::random<Index>(0, size-1);
729
+ Index j;
730
+ do j = internal::random<Index>(0, size-1); while(j==i);
731
+ std::swap(v(i), v(j));
732
+ }
733
+ }
734
+
735
+ template<typename T> bool isNotNaN(const T& x)
736
+ {
737
+ return x==x;
738
+ }
739
+
740
+ template<typename T> bool isPlusInf(const T& x)
741
+ {
742
+ return x > NumTraits<T>::highest();
743
+ }
744
+
745
+ template<typename T> bool isMinusInf(const T& x)
746
+ {
747
+ return x < NumTraits<T>::lowest();
748
+ }
749
+
750
+ } // end namespace Eigen
751
+
752
+ template<typename T> struct GetDifferentType;
753
+
754
+ template<> struct GetDifferentType<float> { typedef double type; };
755
+ template<> struct GetDifferentType<double> { typedef float type; };
756
+ template<typename T> struct GetDifferentType<std::complex<T> >
757
+ { typedef std::complex<typename GetDifferentType<T>::type> type; };
758
+
759
+ // Forward declaration to avoid ICC warning
760
+ template<typename T> std::string type_name();
761
+ template<typename T> std::string type_name() { return "other"; }
762
+ template<> std::string type_name<float>() { return "float"; }
763
+ template<> std::string type_name<double>() { return "double"; }
764
+ template<> std::string type_name<long double>() { return "long double"; }
765
+ template<> std::string type_name<int>() { return "int"; }
766
+ template<> std::string type_name<std::complex<float> >() { return "complex<float>"; }
767
+ template<> std::string type_name<std::complex<double> >() { return "complex<double>"; }
768
+ template<> std::string type_name<std::complex<long double> >() { return "complex<long double>"; }
769
+ template<> std::string type_name<std::complex<int> >() { return "complex<int>"; }
770
+
771
+ using namespace Eigen;
772
+
773
+ inline void set_repeat_from_string(const char *str)
774
+ {
775
+ errno = 0;
776
+ g_repeat = int(strtoul(str, 0, 10));
777
+ if(errno || g_repeat <= 0)
778
+ {
779
+ std::cout << "Invalid repeat value " << str << std::endl;
780
+ exit(EXIT_FAILURE);
781
+ }
782
+ g_has_set_repeat = true;
783
+ }
784
+
785
+ inline void set_seed_from_string(const char *str)
786
+ {
787
+ errno = 0;
788
+ g_seed = int(strtoul(str, 0, 10));
789
+ if(errno || g_seed == 0)
790
+ {
791
+ std::cout << "Invalid seed value " << str << std::endl;
792
+ exit(EXIT_FAILURE);
793
+ }
794
+ g_has_set_seed = true;
795
+ }
796
+
797
+ int main(int argc, char *argv[])
798
+ {
799
+ g_has_set_repeat = false;
800
+ g_has_set_seed = false;
801
+ bool need_help = false;
802
+
803
+ for(int i = 1; i < argc; i++)
804
+ {
805
+ if(argv[i][0] == 'r')
806
+ {
807
+ if(g_has_set_repeat)
808
+ {
809
+ std::cout << "Argument " << argv[i] << " conflicting with a former argument" << std::endl;
810
+ return 1;
811
+ }
812
+ set_repeat_from_string(argv[i]+1);
813
+ }
814
+ else if(argv[i][0] == 's')
815
+ {
816
+ if(g_has_set_seed)
817
+ {
818
+ std::cout << "Argument " << argv[i] << " conflicting with a former argument" << std::endl;
819
+ return 1;
820
+ }
821
+ set_seed_from_string(argv[i]+1);
822
+ }
823
+ else
824
+ {
825
+ need_help = true;
826
+ }
827
+ }
828
+
829
+ if(need_help)
830
+ {
831
+ std::cout << "This test application takes the following optional arguments:" << std::endl;
832
+ std::cout << " rN Repeat each test N times (default: " << DEFAULT_REPEAT << ")" << std::endl;
833
+ std::cout << " sN Use N as seed for random numbers (default: based on current time)" << std::endl;
834
+ std::cout << std::endl;
835
+ std::cout << "If defined, the environment variables EIGEN_REPEAT and EIGEN_SEED" << std::endl;
836
+ std::cout << "will be used as default values for these parameters." << std::endl;
837
+ return 1;
838
+ }
839
+
840
+ char *env_EIGEN_REPEAT = getenv("EIGEN_REPEAT");
841
+ if(!g_has_set_repeat && env_EIGEN_REPEAT)
842
+ set_repeat_from_string(env_EIGEN_REPEAT);
843
+ char *env_EIGEN_SEED = getenv("EIGEN_SEED");
844
+ if(!g_has_set_seed && env_EIGEN_SEED)
845
+ set_seed_from_string(env_EIGEN_SEED);
846
+
847
+ if(!g_has_set_seed) g_seed = (unsigned int) time(NULL);
848
+ if(!g_has_set_repeat) g_repeat = DEFAULT_REPEAT;
849
+
850
+ std::cout << "Initializing random number generator with seed " << g_seed << std::endl;
851
+ std::stringstream ss;
852
+ ss << "Seed: " << g_seed;
853
+ g_test_stack.push_back(ss.str());
854
+ srand(g_seed);
855
+ std::cout << "Repeating each test " << g_repeat << " times" << std::endl;
856
+
857
+ VERIFY(EigenTest::all().size()>0);
858
+
859
+ for(std::size_t i=0; i<EigenTest::all().size(); ++i)
860
+ {
861
+ const EigenTest& current_test = *EigenTest::all()[i];
862
+ Eigen::g_test_stack.push_back(current_test.name());
863
+ current_test();
864
+ Eigen::g_test_stack.pop_back();
865
+ }
866
+
867
+ return 0;
868
+ }
869
+
870
+ // These warning are disabled here such that they are still ON when parsing Eigen's header files.
871
+ #if defined __INTEL_COMPILER
872
+ // remark #383: value copied to temporary, reference to temporary used
873
+ // -> this warning is raised even for legal usage as: g_test_stack.push_back("foo"); where g_test_stack is a std::vector<std::string>
874
+ // remark #1418: external function definition with no prior declaration
875
+ // -> this warning is raised for all our test functions. Declaring them static would fix the issue.
876
+ // warning #279: controlling expression is constant
877
+ // remark #1572: floating-point equality and inequality comparisons are unreliable
878
+ #pragma warning disable 279 383 1418 1572
879
+ #endif
880
+
881
+ #ifdef _MSC_VER
882
+ // 4503 - decorated name length exceeded, name was truncated
883
+ #pragma warning( disable : 4503)
884
+ #endif