#PUT CURSOR ANYWHERE IN THIS BOX THEN HIT SHIFT+ENTER to start the applet @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 ): """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(). """ 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," swap 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 C=1 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",row+1,"+R",changed_row+1 M.add_multiple_of_row(changed_row,row,factor) C +=1 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] if factor != 0: print "",factor," R",row+1,"+R",changed_row+1 M.add_multiple_of_row(row,changed_row,factor) show(M)
Interact: please open in CoCalc
#Example with variables in entries a= var('a') b= var('b') c= var('c') h= var('h') M = matrix(SR,[ [-1,4,1], [h,-2,-7], [7,5,2] ]) B= matrix(SR,[[a],[b],[c]]) M=M.augment(B,subdivide = True) print "Starting M" show(M) print "" print "Reduced M" show(M.echelon_form())
Starting M
−1h74−251−72abc
Reduced M
100010001−a+3(2h−111(h−7)−6)(14a+2c−2h−133(ah+b))(2h−12(h−7)−1)+2h−12(ah+b)2(2h−1)ah+b+6(2h−1)(2h−111(h−7)−6)(14a+2c−2h−133(ah+b))(h−7)−3(2h−111(h−7)−6)14a+2c−2h−133(ah+b)
(-a + 1/3*(14*a + 2*c - 33*(a*h + b)/(2*h - 1))*(2*(h - 7)/(2*h - 1) - 1)/(11*(h - 7)/(2*h - 1) - 6) + 2*(a*h + b)/(2*h - 1), 1/2*(a*h + b)/(2*h - 1) + 1/6*(14*a + 2*c - 33*(a*h + b)/(2*h - 1))*(h - 7)/((2*h - 1)*(11*(h - 7)/(2*h - 1) - 6)), -1/3*(14*a + 2*c - 33*(a*h + b)/(2*h - 1))/(11*(h - 7)/(2*h - 1) - 6))
a= var('k') b= var('h') c= var('c') M = matrix(SR,[ [3,2], [h,k] ]) B= matrix(SR,[[-1],[7]]) M=M.augment(B,subdivide = True) print "Starting M" show(M) print "" print "Reduced M" show(M.echelon_form())
Starting M
(3h2k−17)
Reduced M
(10013(2h−3k)2(h+21)−31−2h−3kh+21)
#PUT CURSOR ANYWHERE IN THIS BOX THEN HIT SHIFT+ENTER to start the applet @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 ): """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(). """ 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," swap 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 C=1 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",row+1,"+R",changed_row+1 M.add_multiple_of_row(changed_row,row,factor) C +=1 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] if factor != 0: print "",factor," R",row+1,"+R",changed_row+1 M.add_multiple_of_row(row,changed_row,factor) show(M)
Interact: please open in CoCalc
︠2e1ea1f8-b719-43cd-8651-1e547ae09ce9︠
#PUT CURSOR ANYWHERE IN THIS BOX THEN HIT SHIFT+ENTER to start the applet @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 ): """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(). """ 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," swap 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 C=1 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",row+1,"+R",changed_row+1 M.add_multiple_of_row(changed_row,row,factor) C +=1 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] if factor != 0: print "",factor," R",row+1,"+R",changed_row+1 M.add_multiple_of_row(row,changed_row,factor) show(M)
Interact: please open in CoCalc
#Example with variables in entries a= var('a') b= var('b') c= var('c') h= var('h') M = matrix(SR,[ [-1,4,1], [h,-2,-7], [7,5,2] ]) B= matrix(SR,[[a],[b],[c]]) M=M.augment(B,subdivide = True) print "Starting M" show(M) print "" print "Reduced M" show(M.echelon_form())
Starting M
−1h74−251−72abc
Reduced M
100010001−a+3(2h−111(h−7)−6)(14a+2c−2h−133(ah+b))(2h−12(h−7)−1)+2h−12(ah+b)2(2h−1)ah+b+6(2h−1)(2h−111(h−7)−6)(14a+2c−2h−133(ah+b))(h−7)−3(2h−111(h−7)−6)14a+2c−2h−133(ah+b)
(-a + 1/3*(14*a + 2*c - 33*(a*h + b)/(2*h - 1))*(2*(h - 7)/(2*h - 1) - 1)/(11*(h - 7)/(2*h - 1) - 6) + 2*(a*h + b)/(2*h - 1), 1/2*(a*h + b)/(2*h - 1) + 1/6*(14*a + 2*c - 33*(a*h + b)/(2*h - 1))*(h - 7)/((2*h - 1)*(11*(h - 7)/(2*h - 1) - 6)), -1/3*(14*a + 2*c - 33*(a*h + b)/(2*h - 1))/(11*(h - 7)/(2*h - 1) - 6))
a= var('k') b= var('h') c= var('c') M = matrix(SR,[ [3,2], [h,k] ]) B= matrix(SR,[[-1],[7]]) M=M.augment(B,subdivide = True) print "Starting M" show(M) print "" print "Reduced M" show(M.echelon_form())
Starting M
(3h2k−17)
Reduced M
(10013(2h−3k)2(h+21)−31−2h−3kh+21)
#PUT CURSOR ANYWHERE IN THIS BOX THEN HIT SHIFT+ENTER to start the applet @interact def _(row = slider([1..10], label='rows', default = 3) , col = slider([1..10], label='columns', default = 3)): print "If you want a variables to appear use the syntax var('x'), var('y'), ... in the entries." @interact(layout=dict(top=[['M', 'b']])) def gauss_method(b= matrix(SR,[ [0] for i in range(row)]), M=matrix(SR,[ [0 for i in range(col)] for i in range(row)]),rescale_leading_entry=True ): """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(). """ 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," swap 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 C=1 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",row+1,"+R",changed_row+1 M.add_multiple_of_row(changed_row,row,factor) C +=1 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] if factor != 0: print "",factor," R",row+1,"+R",changed_row+1 M.add_multiple_of_row(row,changed_row,factor) show(M)
Interact: please open in CoCalc
row = 4 col = 4 b= matrix(SR,[ [0] for i in range(row)]) M=matrix(SR,[ [0 for i in range(col)] for i in range(row)]) show(M,b)
0000000000000000 0000