Search code examples
c++eigensqrt

Why does std:sqrt on Eigen's diagonal().row() fail with "no instance of overloaded function matches argument list"


I am trying to calculate the square root of each element of the .diagonal() of a Eigen::Matrix3d. Using

std::sqrt(matrix.diagonal().row(i))

will give me a compile error:

no instance of overloaded function "std::sqrt" matches the argument list -- argument types are: (Eigen::Block<Eigen::Diagonal<Eigen::Matrix<double, 3, 3, 0, 3, 3>, 0>, 1, 1, false>)C/C++(304)

I am using .row() in a for-loop to access each row of the diagonal vector. Each element of the .diagonal() vector should be type double.

I am using a pointer dereference - but when I just print the .row() it works.

I guess the problem is within sqrt and the returned value from .row(). What am I doing wrong?

EDIT: .diagonal().array().sqrt() does the trick.


Solution

  • It's because the result type of matrix.diagonal().row(n) is a one by one matrix. You can convert this to a flat type with the .value() member function:

    #include <iostream>
    #include <Eigen/Dense>
    
    int main()
    {
        Eigen::Matrix3f m;
        m << 1, 2, 3,
             4, 5, 6,
             7, 8, 9;
    
        std::cout << std::sqrt( m.diagonal().row(1).value() ) << "\n";
    
        return 0;
    }