Compare Eigen matrices in Google Test or Google Mock

后端 未结 3 2023
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-14 13:03

I was wondering if there is a good way to test two Eigen matrices for approximate equality using Google Test, or Google Mock.

Take the following test-case as a

3条回答
  •  一向
    一向 (楼主)
    2021-02-14 13:25

    A simplified solution would be to compare the norm of the difference with some epsilon, i.e.

    (C_expect - C_actual).norm() < 1e-6 
    

    In a vector space || X - Y || == 0 if and only if X == Y, and the norm is always non-negative (real). This way, you won't have to manually do the loop and compare element-wise (of course the norm will perform more calculations in the background than simple element-wise comparisons)

    PS: the Matrix::norm() implemented in Eigen is the Frobenius norm, which is computationally very fast to evaluate, see http://mathworld.wolfram.com/FrobeniusNorm.html

提交回复
热议问题