-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matrix_initializer_list.h
74 lines (57 loc) · 1.77 KB
/
Matrix_initializer_list.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/* Author: hauptmech <[email protected]>, Nov 2013 */
#include <initializer_list>
/** Constructs a matrix from a C++11 initializer_list
MatrixXd a {1,2,3,4} //4x1 matrix
VectorXd b {6,5,8,6} // 4 element vector
Vector3d c {1,2,3}
The constructor will have an assertion failure if a fixed size vector or
matrix given a initializer list of the wrong size.
**/
EIGEN_STRONG_INLINE Matrix(std::initializer_list<double > initlist) : Base()
{
Base::_check_template_params();
int size = initlist.size();
if (base().size() == 0) { //Empty matrix, gotta resize it
this->resize(size,1 );
}
int initializer_list_size = initlist.size();
int Matrix_size = base().size();
eigen_assert(initializer_list_size == Matrix_size);
if (initlist.size() == base().size()){
int i = 0;
for (auto x: initlist){
coeffRef(i)=Scalar(x);
i++;
}
}
}
/** Constructs a matrix from a C++11 initializer_list
MatrixXd a {1,2,3,4} //4x1 matrix
VectorXd b {6,5,8,6} // 4 element vector
Vector3d c {1,2,3}
The constructor will have an assertion failure if a fixed size vector or
matrix given a initializer list of the wrong size.
**/
EIGEN_STRONG_INLINE Matrix(std::initializer_list<std::initializer_list<double> > initlist) : Base()
{
Base::_check_template_params();
int rows = initlist.size();
int cols = initlist.begin()->size();
if (base().size() == 0) { //Empty matrix, gotta resize it
this->resize(rows, cols);
}
int initializer_list_size = rows*cols;
int Matrix_size = base().size();
eigen_assert(initializer_list_size == Matrix_size);
if (rows*cols == base().size()){
int i = 0;
for (auto x: initlist){
int j=0;
for (auto y: x){
coeffRef(rows*j+i)=Scalar(y);
j++;
}
i++;
}
}
}