断言失败乘以特征矩阵 我编写了一个C++程序,用于插值最小二乘回归问题。我使用本征矩阵运算。我遇到的问题是,当我运行程序时,它显示一个错误,显示一个断言错误。这是我的密码: #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; }

断言失败乘以特征矩阵 我编写了一个C++程序,用于插值最小二乘回归问题。我使用本征矩阵运算。我遇到的问题是,当我运行程序时,它显示一个错误,显示一个断言错误。这是我的密码: #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; },c++,codeblocks,matrix-multiplication,eigen,least-squares,C++,Codeblocks,Matrix Multiplication,Eigen,Least Squares,当我删除该语句时,代码就起作用了。到目前为止,代码运行良好。有人能解释一下什么是断言问题以及如何解决它吗?matY是2x1向量,matX是NxN矩阵,因此产品matY*matX无效。您确定不想将matX计算为: matX = matXdup * matX; 以及马坦斯: matAns = matX * matY; ? 顺便说一句,无需使用transposeInPlace显式转置matXdup,您可以直接执行以下操作: matX = matXdup.transpose() * matX; 此

当我删除该语句时,代码就起作用了。到目前为止,代码运行良好。有人能解释一下什么是断言问题以及如何解决它吗?

matY
是2x1向量,
matX
是NxN矩阵,因此产品
matY*matX
无效。您确定不想将
matX
计算为:

matX = matXdup * matX;
以及马坦斯:

matAns = matX * matY;
?

顺便说一句,无需使用
transposeInPlace
显式转置
matXdup
,您可以直接执行以下操作:

matX = matXdup.transpose() * matX;
此外,当一个维度在compiletime已知并且该维度非常小时,最好指定它。例如,matY应该是一个向量xd。
matXdup.transpose()*matX
的结果应该存储在
Matrix2d
对象中。然后调用
inverse()


错误是什么?特征断言通常包含有用的信息。请使用所有警告和调试信息(例如
g++-Wall-g
)进行编译,并学习如何使用调试器(例如Linux上的
gdb
)。通常,断言错误表示您违反了以某种方式调用的函数的“约定”。您没有提供太多详细信息,但由于您正在执行矩阵乘法,我想您已经选择了两个矩阵,其大小意味着它们无法相乘。我使用的是代码块,因此如何在编译时发出警告。错误说明s断言失败:a_lhs.cols()==a_lhs.rows()&&“无效矩阵积”&“如果需要系数积或点积,请使用相应的显式函数
matX = matXdup.transpose() * matX;
Matrix2d XX = matXdup.transpose() * matX; 
Vector2d Y = matXdup * matY;
Vector2d ans = XX.inverse() * Y;