本文介绍了比较Google Test或Google Mock中的本征矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种很好的方法来测试两个矩阵使用或。

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

将以下测试用例作为一个简化的示例:我将两个复值矩阵 A B 相乘,并期望得到一定的结果 C_expect 。我使用Eigen计算数值结果 C_actual = A * B 。现在,我想比较 C_expect C_actual 。现在,相应的代码如下:

Take the following test-case as a simplified example: I am multiplying two complex valued matrices A, and B, and expect a certain result C_expect. I calculate the numerical result C_actual = A * B, using Eigen. Now, I want to compare C_expect, and C_actual. Right now, the corresponding code looks like this:

#include <complex>
#include <Eigen/Dense>
#include <gtest/gtest.h>
#include <gmock/gmock.h>

typedef std::complex<double> Complex;
typedef Eigen::Matrix2cd Matrix;

TEST(Eigen, MatrixMultiplication) {
    Matrix A, B, C_expect, C_actual;

    A << Complex(1, 1), Complex(2, 3),
         Complex(3, 2), Complex(4, 4);
    B << Complex(4, 4), Complex(3, 2),
         Complex(2, 3), Complex(1, 1);
    C_expect << Complex(-5, 20), Complex(0, 10),
                Complex(0, 40), Complex(5, 20);

    C_actual = A * B;

    // !!! These are the lines that bother me.
    for (int j = 0; j < C_actual.cols(); ++j) {
        for (int i = 0; i < C_actual.rows(); ++i) {
            EXPECT_NEAR(C_expect(i, j).real(), C_actual(i, j).real(), 1e-7)
                << "Re(" << i << "," << j << ")";
            EXPECT_NEAR(C_expect(i, j).imag(), C_actual(i, j).imag(), 1e-7)
                << "Im(" << i << "," << j << ")";
        }
    }
}

这有什么问题?好吧,我必须手动遍历矩阵的所有索引,然后分别比较实部和虚部。我更喜欢Google Mock的 ElementsAreArray 匹配器。例如,

What's wrong with this? Well, I have to manually iterate through all indices of the matrix, and then compare the real-part and imaginary-part individually. I would much prefer something along the lines of Google Mock's ElementsAreArray matcher. E.g.

EXPECT_THAT(C_actual, ElementsAreArray(C_expect));
// or
EXPECT_THAT(C_actual, Pointwise(MyComplexNear(1e-7), C_expect));

不幸的是,Google Mock的内置功能似乎只适用于一维C样式或STL型容器。此外,我需要对矩阵的复数值进行近似比较。

Unfortunately, the built-in capabilities of Google Mock only seem to work on 1-dimensional C-style, or STL-type containers. Furthermore, I need an approximate comparison for the complex values of my matrix.

我的问题:您知道吗是否可以教Google Mock在多个维度上进行迭代,并比较复杂的浮点数以近似相等?

My question: Do you know if (and how) it is possible to teach Google Mock to iterate over multiple dimensions, and compare complex floating point numbers to approximate equality?

请注意,我不能只是将数据指针作为C样式的数组进行处理,因为在 C_expect C_actual 。而且,实际上,矩阵大于仅2x2矩阵。即肯定有必要进行某种循环。

Please note, that I cannot just handle the data-pointers as C-style arrays, because the storage layout might differ between C_expect, and C_actual. Also, in reality, the matrices are larger than just 2x2 matrices. I.e. some sort of loop is definitely necessary.

推荐答案

为什么不使用 isApprox Eigen Matrix类型的成员函数 isMuchSmallerThan

以上这些函数的文档均可用

The documentation of these above functions are available here

因此,在大多数情况下 ASSERT_TRUE(C_actual.isApprox(C_expect)); 是您所需要的。您还可以提供一个精确度参数作为的第二个论据。

So for most cases ASSERT_TRUE(C_actual.isApprox(C_expect)); is what you need. You can also provide a precision parameter as the second arguement to isApprox.

这篇关于比较Google Test或Google Mock中的本征矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-27 16:59