简体   繁体   English

断言失败乘以本征矩阵

[英]Assertion failure multiplying Eigen matrices

I am writing a c++ program for least square leaner regression problem in interpolation. 我正在为插值中的最小二乘精益回归问题编写C ++程序。 I use Eigen for matrix operations. 我使用Eigen进行矩阵运算。 The problem I am getting is when I run the program it shows an error displaying an assertion error. 我遇到的问题是,当我运行程序时,它显示一个错误,显示一个断言错误。 Here is my code: 这是我的代码:

#include <iostream>
#include <Eigen/Dense>
using Eigen::MatrixXd;
using namespace std;
int main()
{
    int i;
    int nmbrOfPoints;
    cout<<" Enter the number of data points : ";
    cin>>nmbrOfPoints;

    MatrixXd matY(nmbrOfPoints,1);       //initialize matrix Y
    MatrixXd matX(nmbrOfPoints,2);       //initialize matrix X
    MatrixXd matXdup(nmbrOfPoints,2);      //initialize matrix X duplicate
    MatrixXd matAns(2,1);


    for(i=0;i<nmbrOfPoints;i++)
    {
        matX(i,0)=1;                    // storing the 1 st column of the matrix x, all 1s.
        matXdup(i,0)=1;
    }

    cout<<"Enter all sample points (x and y values ): "<<endl;

    for(i=0;i<nmbrOfPoints;i++)
    {
        cin>>matX(i,1)>>matY(i,0); // read both (x,f(x)) ,, store x values to matrix x and y values to matrix y
    }

    for(i=0;i<nmbrOfPoints;i++)
    {
        matXdup(i,1)=matX(i,1);    //copying matrix x to its duplicate
    }

    cout<<"\n \n";
    cout << matX << endl;
    cout<<"\n \n";
    cout << matY << endl;
    cout<<"\n \n";
    cout << matXdup << endl;

    // find the transpose of matrix x

    cout << "\nHere is the transposed matrix x duplicate:\n" << endl;
    matXdup.transposeInPlace();


    cout << matXdup << endl;
    cout<<"\n \n";
    cout << matX << endl;

    //find the multiplication of x and transpose of x

    matX = matX* matXdup;   // now the matrix x holds the multiplication of transpose of x and x

    cout << "\nmultiplication of x and xdup:\n" << endl;
    cout << matX << endl;
    cout<<"\n \n";

    //find the inverse of x

    double q,a,b,c,d;

    a=matX(0,0);
    b=matX(0,1);
    c=matX(1,0);
    d=matX(1,1);

    q=1/((a*d)-(b*c));

    matX(0,0) = d*q;
    matX(0,1) = b*-1*q;             //now matrix x holds the inverse of x
    matX(1,0) = c*-1*q;
    matX(1,1) = a*q;

    cout<<"\n \n";
    cout << "\n inverse of x:\n" << endl;
    cout << matX << endl;

    //find the multiplication of transpose of x(x duplicate matrix) and y

     matY = matXdup* matY;   // now the matrix x duplicate holds the multiplication of y and x transpose

    //find the multiplication of x(inverse of xt*x) and matXdup (xt*y)

    // matAns = matY* matX;

     cout << "\nfinal answers :\n" << endl;
     cout << "\n *********************:\n" << endl;

     cout << matY << endl;
     cout<<"\n \n";
     cout << matX << endl;

     cout << "\nfinal answer FINAL :\n" << endl;
     cout << "\n *********************:\n" << endl;
     matAns = matY* matX;
     cout << matAns << endl;

     /*cout<<"\n matx dup = \n";
     cout << matXdup << endl;
     cout<<"\n maty =  \n";
     cout << matY << endl;
     cout<<"\n \n";*/

     return 0;


}

I am getting the error from the final multiplication part which is matAns = matY* matX : 我从最后的乘法部分matAns = matY* matX得到错误:

Assertion failed: a_lhs.cols() == a_rhs.rows() && "invalid matrix product" && "if you wanted a coeff-wise or a dot product use the respective explicit functions"

When I remove that statement code works. 当我删除该语句代码工作。 Up to that point the code works fine. 到那时为止,代码可以正常工作。 Can someone explain me what is assertion problem and how to fix it here? 有人可以向我解释什么是断言问题以及如何在此处解决它吗?

matY is a 2x1 vector and matX is a NxN matrix, so the product matY * matX is invalid. matY是2x1向量,而matX是NxN矩阵,因此乘积matY * matX无效。 Are you sure you don't want to compute matX as: 您确定不想将matX计算为:

matX = matXdup * matX;

and matAns as: 和matAns为:

matAns = matX * matY;

?

BTW, no need to explicitly transpose matXdup with transposeInPlace , you can directly do: 顺便说一句,无需使用transposeInPlace显式转置matXdup ,您可以直接执行以下操作:

matX = matXdup.transpose() * matX;

Moreover, when a dimension is known at compiletime and that this dimension is very small, better specify it. 此外,当在编译时知道某个尺寸并且该尺寸很小时,最好指定它。 For instance, matY should rather be a VectorXd. 例如,matY应该是VectorXd。 The result of matXdup.transpose() * matX should rather be stored in a Matrix2d object. matXdup.transpose() * matX的结果应该存储在Matrix2d对象中。 Then call inverse() instead of writing your own inverse routine (you need to include <Eigen/LU> : 然后调用inverse()而不是编写自己的反例程(您需要包含<Eigen/LU>

Matrix2d XX = matXdup.transpose() * matX; 
Vector2d Y = matXdup * matY;
Vector2d ans = XX.inverse() * Y;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM