I am using Armadillo 5.200 with Visual Studio 2013 to solve a system of linear equations xA=b. My code calculates this by solving x=(A'\b')', and I am using Armadillo's solve() function to solve the system of linear equations.
My problem is that the solution returned to me is incorrect-- it is different from both the Eigen and Matlab solutions to the same problem, which match up. The reason I'm not just using Eigen in the first place is that it performs too slowly (I have to make the calculation several million times) and I want to see if using Armadillo speeds it up at all. I'm also just curious at this point why this error is occurring.
Here is a piece of sample code which illustrates the problem:
#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;
}
My output is:
world coords arma: 5.3599e-002 4.242e-001 1.3120e-001 8.8313e-005
world coors eig: .0978826 .439301 0 0.00010165
The eigen solution here is the correct one (the one Matlab gives me, and makes logical sense with the calculation I'm performing). Why would Armadillo give me the incorrect solution?