【发布时间】:2015-07-29 15:44:05
【问题描述】:
我正在使用带有 Visual Studio 2013 的 Armadillo 5.200 来求解线性方程组 xA=b。我的代码通过求解 x=(A'\b')' 来计算这个,我正在使用 Armadillo 的 solve() 函数来求解线性方程组。
我的问题是返回给我的解决方案不正确 - 它与相同问题的 Eigen 和 Matlab 解决方案不同,它们匹配。我不只是首先使用 Eigen 的原因是它执行得太慢(我必须进行数百万次计算),我想看看使用 Armadillo 是否能加快它的速度。我现在也很好奇为什么会发生这个错误。
这是一个说明问题的示例代码:
#include "stdafx.h"
#include <iostream>
#include <armadillo>
#include <Eigen/Dense>
// Matrix operation to find world coordinates.
arma::mat mrdivide(arma::mat A, arma::mat B){
arma::mat A_t = A.t();
arma::mat B_t = B.t();
return solve(A_t, B_t).t();
}
Eigen::MatrixXd mrdivide(Eigen::MatrixXd A, Eigen::MatrixXd B){
Eigen::MatrixXd A_t = A.transpose();
Eigen::MatrixXd B_t = B.transpose();
return ((A_t).colPivHouseholderQr().solve(B_t)).transpose();
}
int main(int argc, char* argv[])
{
Eigen::Matrix<double, 4, 3> M_eig;
M_eig << 761.544, 0, 0,
0, 761.544, 0,
639.5, 399.5, 1.0,
3.762513283904080e+06, 1.824431013104484e+06, 9.837714402800992e+03;
arma::mat M_arma;
M_arma << M_eig(0, 0) << M_eig(0, 1) << M_eig(0, 2) << arma::endr
<< M_eig(1, 0) << M_eig(1, 1) << M_eig(1, 2) << arma::endr
<< M_eig(2, 0) << M_eig(2, 1) << M_eig(2, 2) << arma::endr
<< M_eig(3, 0) << M_eig(3, 1) << M_eig(3, 2) << arma::endr;
Eigen::Matrix<double, 1, 3> pixelCoords_eig;
pixelCoords_eig << 457, 520, 1;
arma::mat pixelCoords_arma;
pixelCoords_arma << 457 << 520 << 1 << arma::endr;
Eigen::Matrix<double, 1, 4> worldCoords_eig;
arma::mat worldCoords_arma;
worldCoords_arma = mrdivide(M_arma, pixelCoords_arma);
worldCoords_eig = mrdivide(M_eig, pixelCoords_eig);
std::cout << "world coords arma: " << worldCoords_arma << std::endl;
std::cout << "world coords eig: " << worldCoords_eig << std::endl;
}
我的输出是:
world coords arma: 5.3599e-002 4.242e-001 1.3120e-001 8.8313e-005
world coors eig: .0978826 .439301 0 0.00010165
这里的特征解是正确的(Matlab 给我的,并且对我正在执行的计算具有逻辑意义)。为什么犰狳会给我错误的解决方案?
【问题讨论】:
标签: c++ visual-studio-2013 linear-algebra eigen armadillo