Created
April 27, 2020 04:25
-
-
Save juliusmh/34dbecc9d61ade7e6da9a8e637aac277 to your computer and use it in GitHub Desktop.
example SageMath script solving underdetermined matrix
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def solve_right(A, b): | |
try: a = A.solve_right(b) | |
except: return "no solution" | |
if A.rank() == len(b): return a | |
return (a, matrix(A.right_kernel().basis())) | |
# Solve matrix A with rank 2 | |
F = IntegerModRing(5) | |
A = matrix(F, [[0,2,1,3],[4,0,1,0],[2,4,0,1]]) | |
b = vector([4,2,4]) | |
sol = solve_right(A, b) | |
# solution has more degrees of freedom (in this example 2) | |
if len(sol) == 2: | |
c, x = sol | |
print "x = %s + F^%s * [%s]" % (c, x.rank(), str(x).replace("\n", "")) | |
# x = (3, 2, 0, 0) + F^2 * [[1 0 1 3][0 1 0 1]] | |
else: | |
print "x = %s" % sol | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment