1
votes

I have 2 MatrixXd that I want to multiply. But I get a runtime error.

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

I've checked the size of both matrices and I should be able to multiply them together, or maybe my maths skills are wrong. Here are the content of my two MatrixXd:

Matrix A:

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

Matrix B:

 1
 1
-1
-1
-1
-1

Here is the code to reproduce. W and YTrain are 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;
1
@JeJo Code has been updated to reproduce the error. - BeGreen
Yes sure, I've released my mistake, sorry. - BeGreen
By my math, mult_trans should be a 6x2 matrix. mat_Y is a 6x1. Multiplying a 6x2 matrix by a 6x1 matrix should produce the error you've received. - Matthew M.
I've updated the code to reproduce. @MatthewM. So you are telleming that mat_Y is a 6x1 matrix? I thought it was a 1x6 matrix... So I should redefine Eigen::MatrixXd mat_Y(1, 6); and insert at proper index after? - BeGreen
@BeGreen Your code is telling me that mat_Y is a 6x1 matrix. MatrixXd mat_Y(6, 1) is a 6x1 matrix. Making mat_Y a 1x6 matrix still wouldn't resolve your issue because mult_trans is a 6x2 matrix. - Matthew M.

1 Answers

0
votes

Indeed the maths was impossible to multiply a 6x2 matrix with a 1x6.

With help of Matthew M. I've released my algorithm was bad. I've added a row to XTrain, but I didn't need it.

To recap, XTrain was wrong dimension.