ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 29 Sep 2016 19:28:31 -0500How to show the steps of Gauss' methodhttp://ask.sagemath.org/question/8840/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.Sat, 31 Mar 2012 07:09:50 -0500http://ask.sagemath.org/question/8840/how-to-show-the-steps-of-gauss-method/Comment by kcrisman for <p>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.</p>
http://ask.sagemath.org/question/8840/how-to-show-the-steps-of-gauss-method/?comment=20005#post-id-20005Not 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.Sat, 31 Mar 2012 08:13:12 -0500http://ask.sagemath.org/question/8840/how-to-show-the-steps-of-gauss-method/?comment=20005#post-id-20005Comment by jh for <p>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.</p>
http://ask.sagemath.org/question/8840/how-to-show-the-steps-of-gauss-method/?comment=20004#post-id-20004Thanks for the reply. It is for pedagogical purposes. I'll think about implementing it.Sun, 01 Apr 2012 06:10:49 -0500http://ask.sagemath.org/question/8840/how-to-show-the-steps-of-gauss-method/?comment=20004#post-id-20004Answer by jh for <p>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.</p>
http://ask.sagemath.org/question/8840/how-to-show-the-steps-of-gauss-method/?answer=13428#post-id-13428In 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
def gauss_method(M,rescale_leading_entry=False):
"""Describe the reduction to echelon form of the given matrix of rationals.
M matrix of rationals e.g., M = matrix(QQ, [[..], [..], ..])
rescale_leading_entry=False boolean make the leading entries to 1's
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
if (rescale_leading_entry
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
M.add_multiple_of_row(changed_row,row,factor)
if change_flag:
print M
col +=1Thu, 05 Apr 2012 11:52:08 -0500http://ask.sagemath.org/question/8840/how-to-show-the-steps-of-gauss-method/?answer=13428#post-id-13428Comment by kcrisman for <p>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.</p>
<pre><code># Naive Gaussian reduction
def gauss_method(M,rescale_leading_entry=False):
"""Describe the reduction to echelon form of the given matrix of rationals.
M matrix of rationals e.g., M = matrix(QQ, [[..], [..], ..])
rescale_leading_entry=False boolean make the leading entries to 1's
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
if (rescale_leading_entry
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
M.add_multiple_of_row(changed_row,row,factor)
if change_flag:
print M
col +=1
</code></pre>
http://ask.sagemath.org/question/8840/how-to-show-the-steps-of-gauss-method/?comment=34993#post-id-34993Unfortunately, that link gives an error.Thu, 29 Sep 2016 19:28:31 -0500http://ask.sagemath.org/question/8840/how-to-show-the-steps-of-gauss-method/?comment=34993#post-id-34993Comment by k1monfared for <p>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.</p>
<pre><code># Naive Gaussian reduction
def gauss_method(M,rescale_leading_entry=False):
"""Describe the reduction to echelon form of the given matrix of rationals.
M matrix of rationals e.g., M = matrix(QQ, [[..], [..], ..])
rescale_leading_entry=False boolean make the leading entries to 1's
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
if (rescale_leading_entry
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
M.add_multiple_of_row(changed_row,row,factor)
if change_flag:
print M
col +=1
</code></pre>
http://ask.sagemath.org/question/8840/how-to-show-the-steps-of-gauss-method/?comment=34990#post-id-34990Hi, 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=eJytVt9v2zYQfg-Q_-GmPERqaM3aimEw5g4Fig0F5qBN9jDAMwRaomwiFCWIVF3vr--RlGzKloOlLR8kiveL9313pG7gnvJPDP6krVKcSmhY3maaV_L6KmcFbMx6WjK9rfJw8fbvh_f_kIapjAqWCkZzLjcpk7rZz2__okrfRrPrK8ARBME7VGv4moHesqNb0BWwbMsETouqKaEqrMIGdyGhpLrhn81aQ402FSq-vnIuXXQY00EpizcxgQXMO3n48SOB5TKOV_iyT3xEztOFBP7gDWaA9k9uz50YjJgzZXae3CrYbblgkFdG5GBTiBsTgpdcUgfdM1EsTBC-YwVthY7-ZziLH0p7MB6YbhupZk7AcotlDPDIcwasKFimZ_Ch4VIrUJrVyuLVk3DAFGnqwEXgsqredxx3QMm2TJtqp-aLWJp36K1nlbDr5t2vq221CxcxrWuxT0tah4KW65yCnum4aIVIFS9rwYt9GEVRvwd0gMGnOLsBKoT5VrBmmJDBhSugOKGiQXj2CLtkzgzlgHsCjjVL5YaFU9JvN5qB0wHr9Pd7hgAhnGpHa6NPQVbyP9ZUFus9FE1VYkRR7Y5mDvbQbO63Q75HcTeozGGxxJirJcpXMMdEBThu, 29 Sep 2016 16:16:56 -0500http://ask.sagemath.org/question/8840/how-to-show-the-steps-of-gauss-method/?comment=34990#post-id-34990Comment by kcrisman for <p>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.</p>
<pre><code># Naive Gaussian reduction
def gauss_method(M,rescale_leading_entry=False):
"""Describe the reduction to echelon form of the given matrix of rationals.
M matrix of rationals e.g., M = matrix(QQ, [[..], [..], ..])
rescale_leading_entry=False boolean make the leading entries to 1's
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
if (rescale_leading_entry
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
M.add_multiple_of_row(changed_row,row,factor)
if change_flag:
print M
col +=1
</code></pre>
http://ask.sagemath.org/question/8840/how-to-show-the-steps-of-gauss-method/?comment=19980#post-id-19980Maybe we should add it to the library this summer - isn't that part of your UTMOST project anyway?Fri, 06 Apr 2012 04:31:47 -0500http://ask.sagemath.org/question/8840/how-to-show-the-steps-of-gauss-method/?comment=19980#post-id-19980Comment by jh for <p>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.</p>
<pre><code># Naive Gaussian reduction
def gauss_method(M,rescale_leading_entry=False):
"""Describe the reduction to echelon form of the given matrix of rationals.
M matrix of rationals e.g., M = matrix(QQ, [[..], [..], ..])
rescale_leading_entry=False boolean make the leading entries to 1's
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
if (rescale_leading_entry
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
M.add_multiple_of_row(changed_row,row,factor)
if change_flag:
print M
col +=1
</code></pre>
http://ask.sagemath.org/question/8840/how-to-show-the-steps-of-gauss-method/?comment=19970#post-id-19970That *is* a nice interact. BTW, in case you can use it and need an explicit statement, license is pd. Fri, 06 Apr 2012 09:27:04 -0500http://ask.sagemath.org/question/8840/how-to-show-the-steps-of-gauss-method/?comment=19970#post-id-19970Comment by Jason Grout for <p>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.</p>
<pre><code># Naive Gaussian reduction
def gauss_method(M,rescale_leading_entry=False):
"""Describe the reduction to echelon form of the given matrix of rationals.
M matrix of rationals e.g., M = matrix(QQ, [[..], [..], ..])
rescale_leading_entry=False boolean make the leading entries to 1's
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
if (rescale_leading_entry
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
M.add_multiple_of_row(changed_row,row,factor)
if change_flag:
print M
col +=1
</code></pre>
http://ask.sagemath.org/question/8840/how-to-show-the-steps-of-gauss-method/?comment=18755#post-id-18755Updated interact link: http://aleph.sagemath.org/?q=a2b497f8-fd66-4d7e-a693-62159e85a70b&lang=sageSat, 03 Nov 2012 02:26:51 -0500http://ask.sagemath.org/question/8840/how-to-show-the-steps-of-gauss-method/?comment=18755#post-id-18755Comment by Jason Grout for <p>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.</p>
<pre><code># Naive Gaussian reduction
def gauss_method(M,rescale_leading_entry=False):
"""Describe the reduction to echelon form of the given matrix of rationals.
M matrix of rationals e.g., M = matrix(QQ, [[..], [..], ..])
rescale_leading_entry=False boolean make the leading entries to 1's
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
if (rescale_leading_entry
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
M.add_multiple_of_row(changed_row,row,factor)
if change_flag:
print M
col +=1
</code></pre>
http://ask.sagemath.org/question/8840/how-to-show-the-steps-of-gauss-method/?comment=19983#post-id-19983Cool! It makes a nice interact: http://aleph.sagemath.org/?q=58cbf7f7-6371-4a33-817f-b5bfea594056Thu, 05 Apr 2012 14:34:50 -0500http://ask.sagemath.org/question/8840/how-to-show-the-steps-of-gauss-method/?comment=19983#post-id-19983Answer by jungem for <p>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.</p>
http://ask.sagemath.org/question/8840/how-to-show-the-steps-of-gauss-method/?answer=32928#post-id-32928I 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
if (rescale_leading_entry
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
M.add_multiple_of_row(changed_row,row,factor)
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
M.add_multiple_of_row(row,changed_row,factor)
show(M)Thu, 31 Mar 2016 09:47:23 -0500http://ask.sagemath.org/question/8840/how-to-show-the-steps-of-gauss-method/?answer=32928#post-id-32928