2

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:

  1. Method which solves system of n equations of m 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.
  2. 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 a n x n matrix A, then I check if the determinant 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!

giliev
  • 2,938
  • 4
  • 27
  • 47
  • 2
    Quick search of the sage docs shows matrix.echelon_form() . This will show you which columns are linear combinations of others. – David Maust Jan 10 '16 at 20:00
  • 1
    @DavidMaust thanks. I was thinking that maybe it was implemented already, but now I did it that way, going through the rows and checking which ones are only zeros. – giliev Jan 10 '16 at 20:11

1 Answers1

1

With galois (python), you can find the rref of your matrix with

import galois
GF = galois.GF(2)
g = GF(x=matrix.astype(int))
g.row_reduce()
nuemlouno
  • 288
  • 1
  • 10
  • is there any link with Galois in python? I just wanted to learn more, like the `import galois`. thanks and have a great corona-free week! – William Martens Sep 25 '22 at 07:24
  • Not sure what do you mean? Galois is a Library for Python and can be installed via pip install galois – nuemlouno Sep 25 '22 at 16:52