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

Uniform Initialization Support?

Tags: None
(comma "," separated)
jbauer
Registered Member
Posts
25
Karma
0

Uniform Initialization Support?

Sat Nov 29, 2014 5:31 am
Hi,

I'm curious if there are plans to support uniform initialization in future versions of Eigen? For example, in C++11 one can initialize a std::vector with the code:
Code: Select all
std::vector test_vector{ 0.1, 0.2, 0.3, 0.4 };

It would be very convenient if one could do the same for a dynamic sized Eigen variable, for example:
Code: Select all
Eigen::VectorXd test_eigen{ 0.1, 0.2, 0.3, 0.4 };

Are there plans for such a feature, does uniform initialization somehow conflict with Eigen's internal design, or is this simply a feature that is not in high demand?

Cheers!
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: Uniform Initialization Support?

Mon Dec 01, 2014 2:19 pm
Generally, we want to keep Eigen C++03 compatible as much as possible, so C++11 initializer lists is not a priority. Moreover, we support more powerful initialization mechanisms, e.g.:

Code: Select all
VectorXd vec(4);
vec << 0.1, 0.2, 0.3, 0.4;

VectorXd vec = VectorXd::LinSpaced(4,0.1,0.4);

VectorXd vec = Vector4f(0.1, 0.2, 0.3, 0.4);
jbauer
Registered Member
Posts
25
Karma
0

Re: Uniform Initialization Support?

Mon Dec 01, 2014 2:49 pm
Right, that makes sense, thank you! The current initialization mechanisms do not support sparse matrices, though, no? While not essential at all, it would be convenient to be able to set the triplets from an initializer list.
eacousineau
Registered Member
Posts
15
Karma
0
OS

Re: Uniform Initialization Support?

Sun Apr 23, 2017 5:25 am
EDIT 2: Made a more expression template-friendly version, using NumPy's hstack / vstack style. Added a new topic:
viewtopic.php?f=74&t=140012#p375247

EDIT 1: Made a very sheepish attempt at expression templates, but ultimately failed. Attempt may be found here:
https://github.com/EricCousineau-TRI/re ... xpr_tpl.cc

ORIGINAL:
Reviving an old thread, but I was tinkering around with initializer_list stuff. I saw there were some other examples here:
https://github.com/hauptmech/eigen-initializer_list (has a fork with a plugin for Arrays)
These work well with scalars, and I wanted to see if something really rough could be done for initializer_lists.

Tinkered around, and tried out a quick proof of concept using composed initializer_lists, and it works; probably not performant in this case, but mayhaps it could leverage expression template wizardry that the present initializer expressions use?

I do see that Eigen has some CXX11 guards, and I'd be interested if this could be incorporated as an optional interface.

AFAIK, the present initializer mechanisms may not permit ragged initialization across rows, without the need to use either block accessors or using pre-concatenated temporary matrices. This may provide a little more expressiveness, if it turns out to be viable.

Code overview, from https://github.com/EricCousineau-TRI/repro/blob/29fe69e/eigen_scratch/matrix_stack.cc
Code: Select all
/*
Goal: Support ragged matrix concatenation such as:
  -------------
  | A |   | D |
  |---| C |---|
  | B |   | E |
  |-----------|
  |     F     |
  -------------

Using inheritance as proof of concept. Kept to dynamic case for simplicity
*/
template<typename Scalar>
class MatrixX : public Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> {
public:
    using Base = Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic>;
    using Base::Base;

    using col_initializer_list = std::initializer_list<MatrixX>;
    using row_initializer_list = std::initializer_list<col_initializer_list>;

    MatrixX(row_initializer_list row_list) {
 ...
        for (const auto& col_list : row_list)
...
            for (const auto& item : col_list)
...
    }

    // Permit scalars
    MatrixX(const Scalar& s)
...
};

int main() {
...
    MatrixXc X = {
            { {{A}, {B}}, C, {{D}, {E}} },
            { F, {{s1, s2}, {s3, s4}} }
        };
...
}


Output, from https://github.com/EricCousineau-TRI/repro/blob/29fe69e/eigen_scratch/matrix_stack.output.txt
Code: Select all
A:
A[0] A[1]

B:
B[0] B[1]

C:
C[0]
C[1]

D:
D[0] D[1] D[2]

E:
E[0] E[1] E[2]

F:
F[0] F[2] F[4] F[6]
F[1] F[3] F[5] F[7]

Scalars: s1, s2, s3, s4

X:
A[0] A[1] C[0] D[0] D[1] D[2]
B[0] B[1] C[1] E[0] E[1] E[2]
F[0] F[2] F[4] F[6]   s1   s2
F[1] F[3] F[5] F[7]   s3   s4


Bookmarks



Who is online

Registered users: Bing [Bot], claydoh, gfielding, Google [Bot], markhm, rblackwell, sethaaaa, Sogou [Bot], Yahoo [Bot]