giliev
giliev

Reputation: 3058

Solving Matrix equation over GF(2) in Sage

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!

Upvotes: 3

Views: 1257

Answers (1)

nuemlouno
nuemlouno

Reputation: 308

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()

Upvotes: 2

Related Questions