问题
I have a set of linear algebraic equations in matrices form, Ax=By. Where A is matrix of 36x20
and x is a vector of size 20
, B is 36x13
and y is 13x1
. Rank(A)=20. Because system is overdetermined (there are more number of equations than the variables), so least squares solution is possible, i,e; x = (A^TA)^-1A^TBy. I want the solution so that the residual error e = Ax-By should be minimized.
Using Eigen/Dense
library of C++ i have formulated all the matrices etc. I tried the method described on this page
Eigen Tutorial!
I guess the method described in this page is only for square matrices. Because when it try to run this it gives error.
x = A.jacobiSvd( ComputeThinU | ComputeThinV ).solve(B*y);
Error
/usr/include/eigen3/Eigen/src/SVD/JacobiSVD.h: In member function 'const
Eigen::internal::solve_retval<Eigen::JacobiSVD<MatrixType, QRPreconditioner>, Rhs>
Eigen::JacobiSVD<MatrixType, QRPreconditioner>::solve(const
Eigen::MatrixBase<OtherDerived>&) const [with Rhs =
Eigen::GeneralProduct<Eigen::Matrix<float, 36, 13>, Eigen::Matrix<double, -1, 1>, 4>;
_MatrixType = Eigen::Matrix<float, 36, 20>; int QRPreconditioner = 2]':
/usr/include/eigen3/Eigen/src/SVD/JacobiSVD.h:658:5: warning: control reaches end of
non-void function [-Wreturn-type]
make[2]: *** [src/CMakeFiles/spacebot_actuationKinematics.dir
/ActuationKinematics.cpp.o] Error 1
make[1]: *** [src/CMakeFiles/spacebot_actuationKinematics.dir/all] Error 2
make: *** [all] Error 2
回答1:
It seems that is having a problem with the matrix multiplication in your solve(B*y); part. Try doing B*y separately and use solve(result); instead.
Eigen::GeneralProduct<Eigen::Matrix<float, 36, 13>, Eigen::Matrix<double, -1, 1>, 4>
That line gave me this suspicion. It says that the y variable came with a size of -1x1, thus your program wont run no matter what since it can't multiply with the matrix.
Also, the tutorial says...
A.jacobiSvd(ComputeThinU | ComputeThinV).solve(b) << endl;
I don't know how Eigen works exactly, but that seems to be the problem.
回答2:
As explained in the documentation, the ComputeThin*
options are only for Dynamic
sized matrices. For fixed sizes, you must use ComputeFull*
. Nevertheless, in your case it is better to use Dynamic
size matrices, i.e., MatrixXf
. Fixed size matrices only makes sense for very small ones.
Finally, ColPivHouseholderQR is probably a better choice for least-square solving. SVD is a bit overkill.
来源:https://stackoverflow.com/questions/22101591/least-squares-solution-of-linear-algerbraic-equation-ax-by-in-eigen-c