This forum has been archived. All content is frozen. Please use KDE Discuss instead.

partial success initializing vector of length 4, fail w 5

Tags: None
(comma "," separated)
sergeist
Registered Member
Posts
1
Karma
0
Hello All,

I am new to Eigen, and not experienced in C++, though pretty experienced in "C". I have read the Eigen documentation and have successfully tried some examples and some very simple code of my own.

Now I'm stuck trying to initialize vectors. Here is the copy-pasted screen session (Eigen version is 3.3.5) :

"
sergei@amdam3:~/try_eigen > cat -n vector_initialization_problem.cxx
1 #include <iostream>
2 #include <Eigen/Dense>
3 using namespace Eigen;
4 using namespace std;
5
6 template <typename MatrixElementType, size_t vector_length>
7 using EigenColumnFixedLengthVector = Matrix<MatrixElementType, vector_length, 1>;
8
9 template <typename MatrixElementType, size_t vector_length>
10 using EigenRowFixedLengthVector = Matrix<MatrixElementType, 1, vector_length>;
11
12
13 EigenColumnFixedLengthVector<double, 4> cc4_1 = {1.0, 2.0, 3.0, 4.0};
14 EigenColumnFixedLengthVector<double, 4> cc4_2 << 1.0, 2.0, 3.0, 4.0;
15
16 EigenRowFixedLengthVector<double, 4> cr4_1 = {5.0, 6.0, 7.0, 8.0};
17 EigenRowFixedLengthVector<double, 4> cr4_2 << 5.0, 6.0, 7.0, 8.0;
18
19
20 EigenColumnFixedLengthVector<double, 5> cc5_1 = {1.0, 2.0, 4.0, 4.0, 5.0};
21 EigenColumnFixedLengthVector<double, 5> cc5_2 << 1.0, 2.0, 4.0, 4.0, 5.0;
22
23 EigenRowFixedLengthVector<double, 5> cr5_1 = {5.0, 6.0, 7.0, 8.0, 9.0};
24 EigenRowFixedLengthVector<double, 5> cr5_2 << 5.0, 6.0, 7.0, 8.0, 9.0;
25
26
27 int main()
28 {
29 return 0;
30 }
31
32
33
sergei@amdam3:~/try_eigen > time g++ -O3 -Wall -std=c++11 -I eigen-eigen-b3f3d4950030/ vector_initialization_problem.cxx -o vector_initialization_problem.o
vector_initialization_problem.cxx:14:47: error: expected initializer before ‘<<’ token
EigenColumnFixedLengthVector<double, 4> cc4_2 << 1.0, 2.0, 3.0, 4.0;
^
vector_initialization_problem.cxx:17:50: error: expected initializer before ‘<<’ token
EigenRowFixedLengthVector<double, 4> cr4_2 << 5.0, 6.0, 7.0, 8.0;
^
vector_initialization_problem.cxx:20:73: error: could not convert ‘{1.0e+0, 2.0e+0, 4.0e+0, 4.0e+0, 5.0e+0}’ from ‘<brace-enclosed initializer list>’ to ‘EigenColumnFixedLengthVector<double, 5ul> {aka Eigen::Matrix<double, 5, 1, 0, 5, 1>}’
EigenColumnFixedLengthVector<double, 5> cc5_1 = {1.0, 2.0, 4.0, 4.0, 5.0};
^
vector_initialization_problem.cxx:21:47: error: expected initializer before ‘<<’ token
EigenColumnFixedLengthVector<double, 5> cc5_2 << 1.0, 2.0, 4.0, 4.0, 5.0;
^
vector_initialization_problem.cxx:23:70: error: could not convert ‘{5.0e+0, 6.0e+0, 7.0e+0, 8.0e+0, 9.0e+0}’ from ‘<brace-enclosed initializer list>’ to ‘EigenRowFixedLengthVector<double, 5ul> {aka Eigen::Matrix<double, 1, 5, 1, 1, 5>}’
EigenRowFixedLengthVector<double, 5> cr5_1 = {5.0, 6.0, 7.0, 8.0, 9.0};
^
vector_initialization_problem.cxx:24:44: error: expected initializer before ‘<<’ token
EigenRowFixedLengthVector<double, 5> cr5_2 << 5.0, 6.0, 7.0, 8.0, 9.0;
^

real 0m1.396s
user 0m1.088s
sys 0m0.104s
sergei@amdam3:~/try_eigen > g++ --version
g++ (Ubuntu 4.8.4-2ubuntu1~14.04.4) 4.8.4
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

sergei@amdam3:~/try_eigen >
".

As one can see, I can successfully initialize both column and row vector of length 4 using the '{...}' initialization - line numbers 13, 16.

I can't initialize vectors of length 4 using the '<<' operator which is widely used in the documentation. Probably it's not overloaded for vectors (opposed to matrices) - by itself not a big deal, I quite enjoy '{...}' initialization.

However, I can't at all initialize vectors of length 5 - line numbers 20, 21, 23, 34. I.e. even the '{...}' initialization doesn't work. I find this to be very odd. I.e. I do not understand why initialization works for length 4, but doesn't work for length 5.

Am I making a stupid newbie mistake ? If yes, what is it ?


Bookmarks



Who is online

Registered users: Bing [Bot], Evergrowing, Google [Bot], rblackwell