c++ - Eigen 乘法断言失败

标签 c++ c++17 eigen

我有 2 个 MatrixXd 我想相乘。但是我遇到运行时错误。

Assertion failed: lhs.cols() == rhs.rows() && "invalid matrix product" && "if you wanted a coeff-wise or a dot product use the respective explicit functions",
 file C:\Users\<myPathToProject>\packages\Eigen.3.3.3\build\native\include\Eigen\src\Core\Product.h, line 97

我已经检查了两个矩阵的大小,我应该可以将它们相乘,或者我的数学技能有问题。以下是我的两个 MatrixXd 的内容:

矩阵A:

        1         1         1         1         1         1
0.0196078         0         1         1  0.184314  0.329412

矩阵 B:

 1
 1
-1
-1
-1
-1

这是要重现的代码。 WYTraindouble*:

    double* W = (double*)malloc(sizeof(double) * 2);
    double* YTrain = (double*)malloc(sizeof(double) * 6);
    double* XTrain = (double*)malloc(sizeof(double) * 6);

    W[0] = -0.527407;
    W[1] = -0.0828247;

    XTrain[0] = 0.0196078;
    XTrain[1] = 0;
    XTrain[2] = 1;
    XTrain[3] = 1;
    XTrain[4] = 0.184314;
    XTrain[5] = 0.329412;

    YTrain[0] = 1;
    YTrain[1] = 1;
    YTrain[2] = -1;
    YTrain[3] = -1;
    YTrain[4] = -1;
    YTrain[5] = -1;

Eigen::MatrixXd mat_Y(6, 1);
for (int i = 0; i < 6; i++)
    mat_Y(i) = YTrain[i];

Eigen::MatrixXd mat_XTrain(2, 6);
int pos = 0;
for (int x = 0; x < 6; x++)
{
    for (int y = 0; y < 1; y++)
    {
        if (y == 0)
            mat_XTrain(y, x) = 1;
        else
         {
            mat_XTrain(y, x) = XTrain[pos];
            pos++;
        }
    }
}

Eigen::MatrixXd mult = mat_XTrain.transpose() * mat_XTrain;
auto pseudo_inv = mult.completeOrthogonalDecomposition().pseudoInverse();
Eigen::MatrixXd mult_trans = pseudo_inv * mat_XTrain.transpose();
auto final_res = mult_trans * mat_Y;

最佳答案

事实上,用 1x6 乘以 6x2 矩阵是不可能的。

在 Matthew M 的帮助下,我发布了我的算法是错误的。我已经向 XTrain 添加了一行,但我不需要它。

回顾一下,XTrain 是错误的维度。

关于c++ - Eigen 乘法断言失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55856351/

相关文章:

c++ - 内存管理 : scope and local pointer variable

c++ - 我可以使用reduce代替for循环吗?

c++ - 从相机投影线

c++ - 使用 Eigen 动态类型进行统一初始化

c++ - 访问冲突写入位置未处理的异常

c++ - 我需要一种流式传输字符串的替代方法。 C++

c++ - 并发 TS:std::future<...>::then,如何在不存储返回的 future 的情况下保持链条事件?

c++ - 是否有可能让 "named constructor"返回一个私有(private)构造的、不可移动的、不可复制的 std::optional<T>?

c++ - 如何在 C++ 中针对 x86 优化这三个矩阵的乘积?

c++ - 我可以遍历 C++ 类的(公共(public))属性吗?