Search code examples
c++eigen

`a,b,c` are both type `Eigen::ArrayXd`. What's the shape of `{a,b,c}`?


from others' codes in the codebase, there is an assignment like z = {a,b,c} where z has type Eigen::Matrix<Eigen::ArrayXd, 3, 1>, but I have no idea how it forms this, and how to access a member of a such as a[1] for example in this Matrix.

I have to say it is much easier to understand with numpy.vstack in Python.

the docs of Eigen https://eigen.tuxfamily.org/dox/group__TutorialMatrixClass.html says it should be a scalar type in template Matrix<typename Scalar, int RowsAtCompileTime, int ColsAtCompileTime> so how is Eigen::Matrix<Eigen::ArrayXd,3,1> even possible since Eigen::ArrayXd is not a scalar type?


Solution

  • The shape of Eigen::Matrix<Eigen::ArrayXd, 3, 1> is 3 rows x 1 column. Each element contains an array (not a matrix or vector, from math POV) of size N (dynamic).

    That's it, all the operations will be defined like usual. In particular, given:

    Eigen::Matrix<Eigen::ArrayXd, 3, 1> m = {a, b, c};
    

    the transposition of this matrix will have a shape (1,3), and the matrix multiplication result

    auto prod = m * m.transpose();
    

    will be 3x3 matrix with elements

    aa ab ac
    ba bb bc
    ca cb cc
    

    And since your elements are of type Eigen::ArrayXd, they will be multiplied element-wise.

    Small demo