C++ Eigen initialize static matrix

Matt Stokes picture Matt Stokes · Jul 21, 2015 · Viewed 11.9k times · Source

Is it possible to initialize a static eigen matrix4d in a header file? I want to use it as a global variable.

I'd like to do something along the lines of:

static Eigen::Matrix4d foo = Eigen::Matrix4d(1, 2 ... 16);

Or similar to vectors:

static Eigen::Matrix4d foo = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}; 

Here is a link to the eigen matrix docs. I can't seem to find how to do this from there.

Answer

Frik picture Frik · Feb 3, 2016

A more elegant solution might include the use of finished(). The function returns 'the built matrix once all its coefficients have been set.'

E.g:

static Eigen::Matrix4d foo = (Eigen::Matrix4d() << 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16).finished();