Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
19 views
#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
(141ah27b752c)\displaystyle \left(\begin{array}{rrr|r} -1 & 4 & 1 & a \\ h & -2 & -7 & b \\ 7 & 5 & 2 & c \end{array}\right)
Reduced M
(100a+(14a+2c33(ah+b)2h1)(2(h7)2h11)3(11(h7)2h16)+2(ah+b)2h1010ah+b2(2h1)+(14a+2c33(ah+b)2h1)(h7)6(2h1)(11(h7)2h16)00114a+2c33(ah+b)2h13(11(h7)2h16))\displaystyle \left(\begin{array}{rrr|r} 1 & 0 & 0 & -a + \frac{{\left(14 \, a + 2 \, c - \frac{33 \, {\left(a h + b\right)}}{2 \, h - 1}\right)} {\left(\frac{2 \, {\left(h - 7\right)}}{2 \, h - 1} - 1\right)}}{3 \, {\left(\frac{11 \, {\left(h - 7\right)}}{2 \, h - 1} - 6\right)}} + \frac{2 \, {\left(a h + b\right)}}{2 \, h - 1} \\ 0 & 1 & 0 & \frac{a h + b}{2 \, {\left(2 \, h - 1\right)}} + \frac{{\left(14 \, a + 2 \, c - \frac{33 \, {\left(a h + b\right)}}{2 \, h - 1}\right)} {\left(h - 7\right)}}{6 \, {\left(2 \, h - 1\right)} {\left(\frac{11 \, {\left(h - 7\right)}}{2 \, h - 1} - 6\right)}} \\ 0 & 0 & 1 & -\frac{14 \, a + 2 \, c - \frac{33 \, {\left(a h + b\right)}}{2 \, h - 1}}{3 \, {\left(\frac{11 \, {\left(h - 7\right)}}{2 \, h - 1} - 6\right)}} \end{array}\right)
(-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
(321hk7)\displaystyle \left(\begin{array}{rr|r} 3 & 2 & -1 \\ h & k & 7 \end{array}\right)
Reduced M
(102(h+21)3(2h3k)1301h+212h3k)\displaystyle \left(\begin{array}{rr|r} 1 & 0 & \frac{2 \, {\left(h + 21\right)}}{3 \, {\left(2 \, h - 3 \, k\right)}} - \frac{1}{3} \\ 0 & 1 & -\frac{h + 21}{2 \, h - 3 \, k} \end{array}\right)
#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
(141ah27b752c)\displaystyle \left(\begin{array}{rrr|r} -1 & 4 & 1 & a \\ h & -2 & -7 & b \\ 7 & 5 & 2 & c \end{array}\right)
Reduced M
(100a+(14a+2c33(ah+b)2h1)(2(h7)2h11)3(11(h7)2h16)+2(ah+b)2h1010ah+b2(2h1)+(14a+2c33(ah+b)2h1)(h7)6(2h1)(11(h7)2h16)00114a+2c33(ah+b)2h13(11(h7)2h16))\displaystyle \left(\begin{array}{rrr|r} 1 & 0 & 0 & -a + \frac{{\left(14 \, a + 2 \, c - \frac{33 \, {\left(a h + b\right)}}{2 \, h - 1}\right)} {\left(\frac{2 \, {\left(h - 7\right)}}{2 \, h - 1} - 1\right)}}{3 \, {\left(\frac{11 \, {\left(h - 7\right)}}{2 \, h - 1} - 6\right)}} + \frac{2 \, {\left(a h + b\right)}}{2 \, h - 1} \\ 0 & 1 & 0 & \frac{a h + b}{2 \, {\left(2 \, h - 1\right)}} + \frac{{\left(14 \, a + 2 \, c - \frac{33 \, {\left(a h + b\right)}}{2 \, h - 1}\right)} {\left(h - 7\right)}}{6 \, {\left(2 \, h - 1\right)} {\left(\frac{11 \, {\left(h - 7\right)}}{2 \, h - 1} - 6\right)}} \\ 0 & 0 & 1 & -\frac{14 \, a + 2 \, c - \frac{33 \, {\left(a h + b\right)}}{2 \, h - 1}}{3 \, {\left(\frac{11 \, {\left(h - 7\right)}}{2 \, h - 1} - 6\right)}} \end{array}\right)
(-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
(321hk7)\displaystyle \left(\begin{array}{rr|r} 3 & 2 & -1 \\ h & k & 7 \end{array}\right)
Reduced M
(102(h+21)3(2h3k)1301h+212h3k)\displaystyle \left(\begin{array}{rr|r} 1 & 0 & \frac{2 \, {\left(h + 21\right)}}{3 \, {\left(2 \, h - 3 \, k\right)}} - \frac{1}{3} \\ 0 & 1 & -\frac{h + 21}{2 \, h - 3 \, k} \end{array}\right)
#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)\displaystyle \left(\begin{array}{rrrr} 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \end{array}\right) (0000)\displaystyle \left(\begin{array}{r} 0 \\ 0 \\ 0 \\ 0 \end{array}\right)