initializer_list_construction.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, bool is_integer = NumTraits<Scalar>::IsInteger>
16  static void run() {}
17 };
18 
19 template<typename Scalar>
21  static void run()
22  {
23  {
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] };
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] };
61  }
62  {
63  Vec5 ref = Vec5::Random();
64  Vec5 v{ ref[0], ref[1], ref[2], ref[3], ref[4] };
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] };
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 
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 {
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  {
173  VERIFY(m.cols() == 0);
174  VERIFY(m.rows() == 0);
176  }
177  {
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 
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  {
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  {
245  VERIFY(a.cols() == 0);
246  VERIFY(a.rows() == 0);
247  }
248  {
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 
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  }
272  }
273  {
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  }
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 
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  {
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 
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 }
VERIFY_IS_EQUAL
#define VERIFY_IS_EQUAL(a, b)
Definition: main.h:386
CHECK_MIXSCALAR_V5
#define CHECK_MIXSCALAR_V5(VEC5, A0, A1, A2, A3, A4)
Definition: initializer_list_construction.cpp:82
initializerListVectorConstruction
void initializerListVectorConstruction()
Definition: initializer_list_construction.cpp:105
Eigen::Array
General-purpose arrays with easy API for coefficient-wise operations.
Definition: Array.h:45
VERIFY_RAISES_ASSERT
#define VERIFY_RAISES_ASSERT(a)
Definition: main.h:340
size
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
CALL_SUBTEST_4
#define CALL_SUBTEST_4(FUNC)
Definition: split_test_helper.h:22
m2
MatrixType m2(n_dims)
CALL_SUBTEST_3
#define CALL_SUBTEST_3(FUNC)
Definition: split_test_helper.h:16
CALL_SUBTEST_1
#define CALL_SUBTEST_1(FUNC)
Definition: split_test_helper.h:4
j
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2
fixedsizeVariadicVectorConstruction3
void fixedsizeVariadicVectorConstruction3()
Definition: initializer_list_construction.cpp:88
Eigen::all
static const Eigen::internal::all_t all
Definition: IndexedViewHelper.h:171
CALL_SUBTEST_5
#define CALL_SUBTEST_5(FUNC)
Definition: split_test_helper.h:28
m
Matrix3f m
Definition: AngleAxis_mimic_euler.cpp:1
dynamicVectorConstruction
void dynamicVectorConstruction()
Definition: initializer_list_construction.cpp:304
CALL_SUBTEST_6
#define CALL_SUBTEST_6(FUNC)
Definition: split_test_helper.h:34
CALL_SUBTEST_2
#define CALL_SUBTEST_2(FUNC)
Definition: split_test_helper.h:10
fixedsizeVariadicVectorConstruction2
void fixedsizeVariadicVectorConstruction2()
Definition: initializer_list_construction.cpp:50
tree::f
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
Definition: testExpression.cpp:218
VERIFY_IS_APPROX
#define VERIFY_IS_APPROX(a, b)
Definition: integer_types.cpp:15
a
ArrayXXi a
Definition: Array_initializer_list_23_cxx11.cpp:1
main.h
initializerListMatrixConstruction
void initializerListMatrixConstruction()
Definition: initializer_list_construction.cpp:161
initializerListArrayConstruction
void initializerListArrayConstruction()
Definition: initializer_list_construction.cpp:233
EIGEN_DECLARE_TEST
EIGEN_DECLARE_TEST(initializer_list_construction)
Definition: initializer_list_construction.cpp:335
v2
Vector v2
Definition: testSerializationBase.cpp:39
TestMethodDispatching
Definition: initializer_list_construction.cpp:15
ref
Reference counting helper.
Definition: object.h:67
v
Array< int, Dynamic, 1 > v
Definition: Array_initializer_list_vector_cxx11.cpp:1
TestMethodDispatching::run
static void run()
Definition: initializer_list_construction.cpp:16
VectorX
Matrix< Scalar, Dynamic, 1 > VectorX
Definition: sparse_lu.cpp:41
Eigen::Matrix< Scalar, Dynamic, Dynamic >
TestMethodDispatching< Scalar, 1 >::run
static void run()
Definition: initializer_list_construction.cpp:21
fixedsizeVariadicVectorConstruction
void fixedsizeVariadicVectorConstruction()
Definition: initializer_list_construction.cpp:96
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
Scalar
SCALAR Scalar
Definition: bench_gemm.cpp:46
CALL_SUBTEST
#define CALL_SUBTEST(FUNC)
Definition: main.h:399
VERIFY
#define VERIFY(a)
Definition: main.h:380
Eigen::Index
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74


gtsam
Author(s):
autogenerated on Tue Jun 25 2024 03:01:02