Search code examples
c++vectorizationeigen

Eigen: Operating on Vectors of Different Types


I have many expressions that look like

auto result = vec3f.cwiseProduct( vec3ui );

where vec3f is from type Eigen::Matrix< float, 3, 1 > and vec3ui is from Eigen::Matrix< unsigned int, 3, 1 >. These doesn't seem to be allowed, at least the compiler complains about it.

Hence I need to write the above like

Eigen::Matrix< float, 3, 1 > result( vec3f.x() * vec3ui.x(), /*...*/ );

which leads to very long, less-readable code.

Is it possible to vectorize the above expression using Eigen 3?


Solution

  • You need to cast the second matrix to the form of the first, like this:

    Eigen::Matrix< float, 3, 1 > mf;
    Eigen::Matrix< unsigned int, 3, 1 > mi;
    mf.dot(mi.cast<float>());
    

    Also, Eigen gives ready to use types for vectors, like Eigen::Vector3f for floats and Eigen::Vector3i for ints. (but none for unsigned int)