Ask Your Question
2

How to show the steps of Gauss' method

asked 2012-03-31 14:09:50 +0200

jh gravatar image

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 flag offensive close merge delete

Comments

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.

kcrisman gravatar imagekcrisman ( 2012-03-31 15:13:12 +0200 )edit

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

jh gravatar imagejh ( 2012-04-01 13:10:49 +0200 )edit

2 Answers

Sort by ยป oldest newest most voted
4

answered 2012-04-05 18:52:08 +0200

jh gravatar image

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
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
edit flag offensive delete link more

Comments

2
Jason Grout gravatar imageJason Grout ( 2012-04-05 21:34:50 +0200 )edit

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

kcrisman gravatar imagekcrisman ( 2012-04-06 11:31:47 +0200 )edit

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

jh gravatar imagejh ( 2012-04-06 16:27:04 +0200 )edit
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...

k1monfared gravatar imagek1monfared ( 2016-09-29 23:16:56 +0200 )edit
2

answered 2016-03-31 16:47:23 +0200

this post is marked as community wiki

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
        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)
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2012-03-31 14:09:50 +0200

Seen: 5,878 times

Last updated: Mar 31 '16