# How to show the steps of Gauss' method

I am new to Sage, so I apologize if this is dopey. I can get a matrix in Sage and I can get its reduced echelon form. I can get Sage to show the result of a single Gauss' method row operation. But is there a way to get Sage do Gauss' method out to reduced echelon form, while showing the steps? It would be especially sweet if it showed the intermediate matrices.

edit retag close merge delete

Not except by hand. We have many requests for steps in basic algebra and linear algebra and calculus, but it's unlikely that anyone will step up and implement this from scratch. You can do the steps yourself, of course, for pedagogical purposes, but Sage won't show those intermediate ones a priori. Unless you want to implement this :) which would be welcomed, for sure.

Thanks for the reply. It is for pedagogical purposes. I'll think about implementing it.

Sort by » oldest newest most voted

In case it saves anyone some time, I am using the following routine. Since this is for pedagogical purposes only, I restricted the matrices to rational entries. Bug reports welcome.

# Naive Gaussian reduction
"""Describe the reduction to echelon form of the given matrix of rationals.

M  matrix of rationals   e.g., M = matrix(QQ, [[..], [..], ..])

Returns: None.  Side effect: M is reduced.  Note: this is echelon form,
not reduced echelon form; this routine does not end the same way as does
M.echelon_form().

"""
num_rows=M.nrows()
num_cols=M.ncols()
print M

col = 0   # all cols before this are already done
for row in range(0,num_rows):
# ?Need to swap in a nonzero entry from below
while (col < num_cols
and M[row][col] == 0):
for i in M.nonzero_positions_in_column(col):
if i > row:
print " swap row",row+1,"with row",i+1
M.swap_rows(row,i)
print M
break
else:
col += 1

if col >= num_cols:
break

# Now guaranteed M[row][col] != 0
and M[row][col] != 1):
print " take",1/M[row][col],"times row",row+1
M.rescale_row(row,1/M[row][col])
print M
change_flag=False
for changed_row in range(row+1,num_rows):
if M[changed_row][col] != 0:
change_flag=True
factor=-1*M[changed_row][col]/M[row][col]
print " take",factor,"times row",row+1,"plus row",changed_row+1
if change_flag:
print M
col +=1

more

2

Maybe we should add it to the library this summer - isn't that part of your UTMOST project anyway?

That *is* a nice interact. BTW, in case you can use it and need an explicit statement, license is pd.

1

Hi, thanks for the nice code, I've added a little bits to it and changed some of the small parts. Take a look at the updated version here: http://sagecell.sagemath.org/?z=eJytV... This post is a wiki. Anyone with karma >750 is welcome to improve it.

I added to the code linked above and made it into an @interact that takes one all the way to reduced echelon form: (a formatting issue is having one of the @interacts not in the code box, and messing with the indentation a bit. Be sure to copy-paste the hanging @interact and indent in the nested one.)

@interact def _(row = slider([1..10], label='rows', default = 3) , col = slider([1..10], label='columns', default = 3)):

@interact(layout=dict(top=[['M', 'b']]))

def gauss_method(b= random_matrix(QQ,nrows = row, ncols = 1, algorithm='echelonizable', rank = 1), M=random_matrix(QQ,nrows = row, ncols = col, algorithm='echelonizable', rank = min(row,col)),rescale_leading_entry=True ):

M=M.augment(b, subdivide = True)
N=copy(M)
N=N.rref()

num_rows=M.nrows()
num_cols=M.ncols()
print("Reduced echelon form:")
show(N)

print("Steps")
show(M)

col = 0   # all cols before this are already done
for row in range(0,num_rows):
# ?Need to swap in a nonzero entry from below
while (col < num_cols
and M[row][col] == 0):
for i in M.nonzero_positions_in_column(col):
if i > row:
print " R",row+1,"\;\circlearrowright \; R",i+1
M.swap_rows(row,i)
show(M)
break
else:
col += 1

if col >= num_cols:
break

# Now guaranteed M[row][col] != 0
and M[row][col] != 1):
print "",1/M[row][col],"R",row+1
M.rescale_row(row,1/M[row][col])
show(M)
change_flag=False
for changed_row in range(row+1,num_rows):
if M[changed_row][col] != 0:
change_flag=True
factor=-1*M[changed_row][col]/M[row][col]
print "",factor," R",changed_row+1,"+R",changed_row
if change_flag:
show(M)
col +=1
print "Above is the echelon form, let's keep cruising to get the reduced echelon form"

for changed_row in range(1,num_rows):
for row in range(0,changed_row):
factor = -1*M[row][changed_row]
print "",factor," R",changed_row+1,"+R",row+1
show(M)

more