int main() { MatrixXf A = MatrixXf::Random(3, 2); cout << "Here is the matrix A:\n" << A << endl; VectorXf b = VectorXf::Random(3); cout << "Here is the right hand side b:\n" << b << endl; cout << "The least-squares solution is:\n" << A.jacobiSvd(ComputeThinU | ComputeThinV).solve(b) << endl; }
void TestLeastSquares() { MatrixXf A = MatrixXf::Random(10, 2); VectorXf b = VectorXf::Random(10); Vector2f x; std::cout << "=============================" << std::endl; std::cout << "Testing least squares solvers" << std::endl; std::cout << "=============================" << std::endl; x = A.jacobiSvd(ComputeThinU | ComputeThinV).solve(b); std::cout << "Solution using Jacobi SVD = " << x.transpose() << std::endl; x = A.colPivHouseholderQr().solve(b); std::cout << "Solution using column pivoting Householder QR = " << x.transpose() << std::endl; // If the matrix A is ill-conditioned, then this is not a good method x = (A.transpose() * A).ldlt().solve(A.transpose() * b); std::cout << "Solution using normal equation = " << x.transpose() << std::endl; }