armadillo library c++ solve the linear equation that is inconsistent

3.3k Views Asked by At

I'm using armadillo library I try to use this code to solve a System of a linear equation I input a case that should be inconsistent but it output a solve for the equation !
I try this

x+y=3,
4 x + 4 y = 10

this the code

mat A(2,2);
vec B(2);
A << 1 << 1 << endr
  << 4 << 4 << endr;
B << 3 << endr
  << 10 << endr;
vec Ans;
Ans = solve(A,B);
cout << Ans << endl;

from the documntation it says that if a solution not found it will throws and excption

If no solution is found:
X = solve(A,B) resets X and throws a std::runtime_error exception solve(X,A,B) resets X and returns a bool set to false (exception is not thrown)

so what should I do so when I solve inconsistent it should throws and exception or return false or anything what is the right way to do that
Thanks in advance

1

There are 1 best solutions below

0
On BEST ANSWER

By default armadillo tries to find approximate solution for singular matrix A:

warning: solve(): system seems singular; attempting approx solution
   1.2647
   1.2647

You should disable this behavior with solve_opts::no_approx option:

Ans = solve(A,B,solve_opts::no_approx);

See documentation for solve().