michel paul wrote:
On Sat, Jan 9, 2010 at 7:37 AM, <[email protected] <mailto:[email protected]>> wrote:


    Then in Analysis class we hit matrices this week, and Sage was
    perfect for this.  I was able to show the kids how a matrix is
    just a list of lists, and I showed how simple it is to create a
    'dumb' matrix in Python and then how to make it a 'smart' matrix
    in Sage.
    <<

Could you give an example of this?
Sure -  here's a 'dumb' matrix in pure Python:

M = [[2, 7, 6], [9, 5, 1], [4, 3, 8]]

It is quite literally just a list of lists. Structurally it's a matrix, but it doesn't yet know how to act like a matrix. However, with just this much you can illustrate indexing: M[0] returns [2, 7, 6]. M[1][1] returns 5, etc.

Now we can make it a 'smart' matrix:

M = matrix(M)

Magic! M can now do all kinds of useful matrixy things. Indexing works just as before.


This is a great way to approach this subject.  Thanks for sharing it!

A small technical note about indexing. While M[1][1] works (i.e., it gives you the entry), M[1,1] is much, much more powerful. For example, M[1, (1,2)] gives you a submatrix, M[1,:] gives you row 1; M[:, 1] gives you column 1, etc. It's even more powerful than that, because you can easily do operations involving submatrices. For example,

M[:, (0,1)] = M[:, (1,0)]

swaps columns 0 and 1. Do you see what is happening here? The right hand side is giving you a matrix consisting of columns 1 and 0 (in that order). You are assigning those to whatever is on the left side of the equals, which is a placeholder for columns 0 and 1 (in that order). So you've swapped the columns! You can't do this sort of thing with the M[][]-type indexing.

Also, M[:, -1] gives you the last column.

For documentation, see M.__getitem__? and M.__setitem__?

We should make the indexing stuff examples in the reference manual (I don't believe it's there now since it's for a "__" function). I've made http://trac.sagemath.org/sage_trac/ticket/7877 to address this problem.

Thanks,

Jason

-- 
You received this message because you are subscribed to the Google Groups 
"sage-edu" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sage-edu?hl=en.


Reply via email to