I am trying to solve a system of linear equations in Sage. However, I want to print the solution only if it is unique. I have tried using A.solve_right(y)
where A is coefficient matrix
, y is the right hand side (Ax = y)
. However, this returns a solution when there are multiple solutions. I tried checking if the determinant is 0, but this only works when having square matrix.
I have been thinking of 2 ways, but could not find suitable way to implement my thoughts in Sage:
- Method which solves system of
n
equations ofm
variables. Whenever I call A.solve(x) where A is n x m matrix and x is vector of length n, I should get either exception "No unique solution"
(if no or more than one solution) or the solution if it is unique. - Method which allows me adding rows to the matrix
A
. When I add new row I check if it is linear combination of the previous rows and if so I ignore that row. Otherwise, I add the new row. When I obtain an x n matrix A
, then I check if thedeterminant is not zero
and print the solution. Otherwise I throw exception"No unique solution"
.
I have been looking for solution online, but without success. I guess there must be some easy way to achieve this, but I am having short deadline and could not go deeply into the Sage documentation. Any suggestion will be very welcome!