【发布时间】:2019-04-26 05:01:42
【问题描述】:
我有 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
这是要重现的代码。 W 和 YTrain 是 double*:
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;
【问题讨论】:
-
@JeJo 代码已更新以重现错误。
-
是的,我已经发布了我的错误,抱歉。
-
根据我的数学计算,
mult_trans应该是一个 6x2 矩阵。 mat_Y 是 6x1。将 6x2 矩阵乘以 6x1 矩阵应该会产生您收到的错误。 -
我已更新代码以重现。 @马修M。所以你说
mat_Y是一个 6x1 矩阵?我以为它是一个 1x6 矩阵...所以我应该重新定义Eigen::MatrixXd mat_Y(1, 6);并在之后插入适当的索引? -
@BeGreen 您的代码告诉我
mat_Y是一个 6x1 矩阵。MatrixXd mat_Y(6, 1)是一个 6x1 矩阵。将mat_Y设为 1x6 矩阵仍然无法解决您的问题,因为mult_trans是 6x2 矩阵。