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] };
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 
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  }
271  VERIFY_IS_APPROX(m, m2);
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  }
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 
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 
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 }
Matrix3f m
SCALAR Scalar
Definition: bench_gemm.cpp:46
#define VERIFY_RAISES_ASSERT(a)
Definition: main.h:340
#define CALL_SUBTEST_6(FUNC)
#define CALL_SUBTEST_4(FUNC)
EIGEN_DECLARE_TEST(initializer_list_construction)
static const Eigen::internal::all_t all
Vector v2
void initializerListVectorConstruction()
void fixedsizeVariadicVectorConstruction2()
#define CALL_SUBTEST_3(FUNC)
MatrixType m2(n_dims)
Matrix< Scalar, Dynamic, 1 > VectorX
Definition: sparse_lu.cpp:41
#define CHECK_MIXSCALAR_V5(VEC5, A0, A1, A2, A3, A4)
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
#define VERIFY_IS_APPROX(a, b)
#define VERIFY_IS_EQUAL(a, b)
Definition: main.h:386
#define CALL_SUBTEST_1(FUNC)
void initializerListArrayConstruction()
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74
Array< int, Dynamic, 1 > v
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
Reference counting helper.
Definition: object.h:67
void fixedsizeVariadicVectorConstruction3()
#define CALL_SUBTEST_5(FUNC)
void initializerListMatrixConstruction()
#define CALL_SUBTEST(FUNC)
Definition: main.h:399
#define VERIFY(a)
Definition: main.h:380
General-purpose arrays with easy API for coefficient-wise operations.
Definition: Array.h:45
#define CALL_SUBTEST_2(FUNC)
void fixedsizeVariadicVectorConstruction()
void dynamicVectorConstruction()
std::ptrdiff_t j


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:34:22