diagonal_matrix_variadic_ctor.cpp
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 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>
16 {
17  typedef DiagonalMatrix<Scalar, 5> DiagMatrix5;
18  typedef DiagonalMatrix<Scalar, 7> DiagMatrix7;
19  typedef DiagonalMatrix<Scalar, Dynamic> DiagMatrixX;
20 
21  Scalar raw[6];
22  for (int i = 0; i < 6; ++i) {
23  raw[i] = internal::random<Scalar>();
24  }
25 
26  VERIFY_RAISES_ASSERT((DiagMatrix5{raw[0], raw[1], raw[2], raw[3]}));
27  VERIFY_RAISES_ASSERT((DiagMatrix5{raw[0], raw[1], raw[3]}));
28  VERIFY_RAISES_ASSERT((DiagMatrix7{raw[0], raw[1], raw[2], raw[3]}));
29 
30  VERIFY_RAISES_ASSERT((DiagMatrixX {
31  {raw[0], raw[1], raw[2]},
32  {raw[3], raw[4], raw[5]}
33  }));
34 }
35 
36 #define VERIFY_IMPLICIT_CONVERSION_3(DIAGTYPE, V0, V1, V2) \
37  DIAGTYPE d(V0, V1, V2); \
38  DIAGTYPE::DenseMatrixType Dense = d.toDenseMatrix(); \
39  VERIFY_IS_APPROX(Dense(0, 0), (Scalar)V0); \
40  VERIFY_IS_APPROX(Dense(1, 1), (Scalar)V1); \
41  VERIFY_IS_APPROX(Dense(2, 2), (Scalar)V2);
42 
43 #define VERIFY_IMPLICIT_CONVERSION_4(DIAGTYPE, V0, V1, V2, V3) \
44  DIAGTYPE d(V0, V1, V2, V3); \
45  DIAGTYPE::DenseMatrixType Dense = d.toDenseMatrix(); \
46  VERIFY_IS_APPROX(Dense(0, 0), (Scalar)V0); \
47  VERIFY_IS_APPROX(Dense(1, 1), (Scalar)V1); \
48  VERIFY_IS_APPROX(Dense(2, 2), (Scalar)V2); \
49  VERIFY_IS_APPROX(Dense(3, 3), (Scalar)V3);
50 
51 #define VERIFY_IMPLICIT_CONVERSION_5(DIAGTYPE, V0, V1, V2, V3, V4) \
52  DIAGTYPE d(V0, V1, V2, V3, V4); \
53  DIAGTYPE::DenseMatrixType Dense = d.toDenseMatrix(); \
54  VERIFY_IS_APPROX(Dense(0, 0), (Scalar)V0); \
55  VERIFY_IS_APPROX(Dense(1, 1), (Scalar)V1); \
56  VERIFY_IS_APPROX(Dense(2, 2), (Scalar)V2); \
57  VERIFY_IS_APPROX(Dense(3, 3), (Scalar)V3); \
58  VERIFY_IS_APPROX(Dense(4, 4), (Scalar)V4);
59 
60 template<typename Scalar>
62 {
63  typedef DiagonalMatrix<Scalar, 0> DiagonalMatrix0;
64  typedef DiagonalMatrix<Scalar, 3> DiagonalMatrix3;
65  typedef DiagonalMatrix<Scalar, 4> DiagonalMatrix4;
66  typedef DiagonalMatrix<Scalar, Dynamic> DiagonalMatrixX;
67 
68  Scalar raw[7];
69  for (int k = 0; k < 7; ++k) raw[k] = internal::random<Scalar>();
70 
71  // Fixed-sized matrices
72  {
73  DiagonalMatrix0 a {{}};
74  VERIFY(a.rows() == 0);
75  VERIFY(a.cols() == 0);
76  typename DiagonalMatrix0::DenseMatrixType m = a.toDenseMatrix();
77  for (Index k = 0; k < a.rows(); ++k) VERIFY(m(k, k) == raw[k]);
78  }
79  {
80  DiagonalMatrix3 a {{raw[0], raw[1], raw[2]}};
81  VERIFY(a.rows() == 3);
82  VERIFY(a.cols() == 3);
83  typename DiagonalMatrix3::DenseMatrixType m = a.toDenseMatrix();
84  for (Index k = 0; k < a.rows(); ++k) VERIFY(m(k, k) == raw[k]);
85  }
86  {
87  DiagonalMatrix4 a {{raw[0], raw[1], raw[2], raw[3]}};
88  VERIFY(a.rows() == 4);
89  VERIFY(a.cols() == 4);
90  typename DiagonalMatrix4::DenseMatrixType m = a.toDenseMatrix();
91  for (Index k = 0; k < a.rows(); ++k) VERIFY(m(k, k) == raw[k]);
92  }
93 
94  // dynamically sized matrices
95  {
96  DiagonalMatrixX a{{}};
97  VERIFY(a.rows() == 0);
98  VERIFY(a.rows() == 0);
99  typename DiagonalMatrixX::DenseMatrixType m = a.toDenseMatrix();
100  for (Index k = 0; k < a.rows(); ++k) VERIFY(m(k, k) == raw[k]);
101  }
102  {
103  DiagonalMatrixX a{{raw[0], raw[1], raw[2], raw[3], raw[4], raw[5], raw[6]}};
104  VERIFY(a.rows() == 7);
105  VERIFY(a.rows() == 7);
106  typename DiagonalMatrixX::DenseMatrixType m = a.toDenseMatrix();
107  for (Index k = 0; k < a.rows(); ++k) VERIFY(m(k, k) == raw[k]);
108  }
109 }
110 
111 template<>
113 {
114  typedef float Scalar;
115 
116  typedef DiagonalMatrix<Scalar, 0> DiagonalMatrix0;
117  typedef DiagonalMatrix<Scalar, 3> DiagonalMatrix3;
118  typedef DiagonalMatrix<Scalar, 4> DiagonalMatrix4;
119  typedef DiagonalMatrix<Scalar, 5> DiagonalMatrix5;
120  typedef DiagonalMatrix<Scalar, Dynamic> DiagonalMatrixX;
121 
122  Scalar raw[7];
123  for (int k = 0; k < 7; ++k) raw[k] = internal::random<Scalar>();
124 
125  // Fixed-sized matrices
126  {
127  DiagonalMatrix0 a {{}};
128  VERIFY(a.rows() == 0);
129  VERIFY(a.cols() == 0);
130  typename DiagonalMatrix0::DenseMatrixType m = a.toDenseMatrix();
131  for (Index k = 0; k < a.rows(); ++k) VERIFY(m(k, k) == raw[k]);
132  }
133  {
134  DiagonalMatrix3 a {{raw[0], raw[1], raw[2]}};
135  VERIFY(a.rows() == 3);
136  VERIFY(a.cols() == 3);
137  typename DiagonalMatrix3::DenseMatrixType m = a.toDenseMatrix();
138  for (Index k = 0; k < a.rows(); ++k) VERIFY(m(k, k) == raw[k]);
139  }
140  {
141  DiagonalMatrix4 a {{raw[0], raw[1], raw[2], raw[3]}};
142  VERIFY(a.rows() == 4);
143  VERIFY(a.cols() == 4);
144  typename DiagonalMatrix4::DenseMatrixType m = a.toDenseMatrix();
145  for (Index k = 0; k < a.rows(); ++k) VERIFY(m(k, k) == raw[k]);
146  }
147 
148  // dynamically sized matrices
149  {
150  DiagonalMatrixX a{{}};
151  VERIFY(a.rows() == 0);
152  VERIFY(a.rows() == 0);
153  typename DiagonalMatrixX::DenseMatrixType m = a.toDenseMatrix();
154  for (Index k = 0; k < a.rows(); ++k) VERIFY(m(k, k) == raw[k]);
155  }
156  {
157  DiagonalMatrixX a{{raw[0], raw[1], raw[2], raw[3], raw[4], raw[5], raw[6]}};
158  VERIFY(a.rows() == 7);
159  VERIFY(a.rows() == 7);
160  typename DiagonalMatrixX::DenseMatrixType m = a.toDenseMatrix();
161  for (Index k = 0; k < a.rows(); ++k) VERIFY(m(k, k) == raw[k]);
162  }
163  { VERIFY_IMPLICIT_CONVERSION_3(DiagonalMatrix3, 1.2647, 2.56f, -3); }
164  { VERIFY_IMPLICIT_CONVERSION_4(DiagonalMatrix4, 1.2647, 2.56f, -3, 3.23f); }
165  { VERIFY_IMPLICIT_CONVERSION_5(DiagonalMatrix5, 1.2647, 2.56f, -3, 3.23f, 2); }
166 }
167 
168 EIGEN_DECLARE_TEST(diagonal_matrix_variadic_ctor)
169 {
170  CALL_SUBTEST_1(assertionTest<unsigned char>());
171  CALL_SUBTEST_1(assertionTest<float>());
172  CALL_SUBTEST_1(assertionTest<Index>());
173  CALL_SUBTEST_1(assertionTest<int>());
174  CALL_SUBTEST_1(assertionTest<long int>());
175  CALL_SUBTEST_1(assertionTest<std::ptrdiff_t>());
176  CALL_SUBTEST_1(assertionTest<std::complex<double>>());
177 
178  CALL_SUBTEST_2(constructorTest<unsigned char>());
180  CALL_SUBTEST_2(constructorTest<Index>());
181  CALL_SUBTEST_2(constructorTest<int>());
182  CALL_SUBTEST_2(constructorTest<long int>());
183  CALL_SUBTEST_2(constructorTest<std::ptrdiff_t>());
184  CALL_SUBTEST_2(constructorTest<std::complex<double>>());
185 }
constructorTest
void constructorTest()
Definition: diagonal_matrix_variadic_ctor.cpp:61
Eigen::DiagonalMatrix
Represents a diagonal matrix with its storage.
Definition: DiagonalMatrix.h:140
assertionTest
void assertionTest()
Definition: diagonal_matrix_variadic_ctor.cpp:15
VERIFY_RAISES_ASSERT
#define VERIFY_RAISES_ASSERT(a)
Definition: main.h:340
CALL_SUBTEST_1
#define CALL_SUBTEST_1(FUNC)
Definition: split_test_helper.h:4
m
Matrix3f m
Definition: AngleAxis_mimic_euler.cpp:1
VERIFY_IMPLICIT_CONVERSION_4
#define VERIFY_IMPLICIT_CONVERSION_4(DIAGTYPE, V0, V1, V2, V3)
Definition: diagonal_matrix_variadic_ctor.cpp:43
CALL_SUBTEST_2
#define CALL_SUBTEST_2(FUNC)
Definition: split_test_helper.h:10
VERIFY_IMPLICIT_CONVERSION_3
#define VERIFY_IMPLICIT_CONVERSION_3(DIAGTYPE, V0, V1, V2)
Definition: diagonal_matrix_variadic_ctor.cpp:36
tree::f
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
Definition: testExpression.cpp:218
a
ArrayXXi a
Definition: Array_initializer_list_23_cxx11.cpp:1
main.h
constructorTest< float >
void constructorTest< float >()
Definition: diagonal_matrix_variadic_ctor.cpp:112
VERIFY_IMPLICIT_CONVERSION_5
#define VERIFY_IMPLICIT_CONVERSION_5(DIAGTYPE, V0, V1, V2, V3, V4)
Definition: diagonal_matrix_variadic_ctor.cpp:51
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
Scalar
SCALAR Scalar
Definition: bench_gemm.cpp:46
VERIFY
#define VERIFY(a)
Definition: main.h:380
Eigen::Index
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74
EIGEN_DECLARE_TEST
EIGEN_DECLARE_TEST(diagonal_matrix_variadic_ctor)
Definition: diagonal_matrix_variadic_ctor.cpp:168


gtsam
Author(s):
autogenerated on Fri Nov 1 2024 03:32:26