NES MAA Section NExT

924 days ago by pub

An Introduction to Sage: Free and Open-Source Mathematics Software - Online!

Northeastern Section MAA, Fall 2009 Meeting, November 20th

Speaker: Karl-Dieter Crisman, Gordon College

Thanks to Karen and the organizing committee for inviting me!  I hope this will be a real treat for all of you.  Before we start, and while we're digesting lunch, think of a generic username and password for yourself, and send your web browser to http://demo2.sagenb.org.  

2+2; 2^2; 2*2 
       

Let's start.  We usually expect our students to be able to do calculations like these.  But what about calculations like these?

integral(3*x^2/sqrt(25*x^2-3)); factorial(21); primitive_root(991); find_root(cos(x)==sin(x),0,pi/2) 
       

Unless we had a specific reason for them doing these by hand, most of us would be happy for our students to be able to use these things in some other problem.  And doing computations like this (symbolic and numerical) is what comprehensive mathematics software does - often, very well indeed.

But there are two potential barriers to our using them.

  • Such systems can be very expensive.  Sometimes this is a relatively minor issue, but at smaller institutions, or ones with budget woes, it cannot be ignored.
  • Such systems are, unfortunately, usually not available to students when they are most likely to use them - at 11 PM in their dorm rooms!  There are ways around this, but they usually involve either a lot of hassle for faculty, or purchasing additional licenses.

This talk is about a solution which overcomes both of these barriers - the Sage mathematics software package.  In fact, the entire talk is running in Sage!

  • Sage is free and open source.  The only commitment you make is a minimal amount of time and bandwidth to download an executable binary - or not even that, see the next item.
  • One can use Sage anywhere and anytime, assuming one has access to a server running it and a (non-ancient) web browser.  

In fact, if you followed the directions earlier, you discovered you currently have access to such a server at http://demo2.sagenb.org!  Use that username and password I asked you for earlier; sign up for an account, and log in.  

You may want to click on "New Worksheet", and try entering 2+2 in the cell.  To see if it works, either click on the "Evaluate" button, or press Shift and Enter at the same time (Shift-Enter).

2+2 
       

It's as easy as that; type in the command and Shift-Enter.  This is very similar to other systems you may be familiar with, and makes it easy to experiment.  

In fact, I strongly encourage you to play around and not pay any attention to me for the remainder of the talk; the "Help" button up top should give you links to some tutorials and references, for instance if you really want to get right into computing with modular forms.  You should also be able to click "Published" at the top of the sheet to find the very same worksheet I am using right now, and to play around with it.

Getting back to the worksheet; please let me know if you did not get 4 as the answer in the previous cell.  For instance, if you got

\zeta(s)=\sum_{n=1}^{\infty}\frac{1}{n^s}=\prod_p \left(\frac{1}{1-p^{-s}}\right)
as your answer, you probably did something wrong.  

Of course, you might want to know how to get that to show up!  Let's explore that.  Move your mouse between two math cells (that's what we call these) until you see the little blue line.  Press shift, and click on the line simultaneously; now you have a WYSIWYG text editor called TinyMCE available - and it understands TeX through the magic of jsmath!  

So you can type "x" or "$x$", and the result should be different.  Note that depending on the browser, you may have to install some extra fonts for full effect, but it should still look better than nothing.  The above formula is "$$\zeta(s)=\sum_{n=1}^{\infty}\frac{1}{n^s}=\prod_p \left(\frac{1}{1-p^{-s}}\right)$$", by the way.

A = Matrix([[1,2,3],[3,2,1],[1,1,1]]) A 
       

Back to mathematics.  Here, our example is from linear algebra.  Notice how making two lines is easy, so we can separate commands.  

Above, in the first line we have assigned a specific matrix to the variable A, and the second line tells Sage to represent A for us - in this case, by nicely typesetting it, if I have clicked the "Typeset" button at the very tippy-top of the page.  Notice the difference between the next two cells.

Matrix([[1,2,3],[3,2,1],[1,1,1]]) A 
       
Matrix([[1,2,3],[3,2,1],[1,1,1]]); A 
       

Flexibility like this is very nice to have when preparing class notes.  Anyway, back to the matrices.

w = vector([1,1,-4]); w; w*A; A*w 
       

Note that it's important to assign most things to a name, in order to obviate the tedium of retyping everything.  Just don't reassign things like \pi:

pi; pi=3; pi 
       

Sage has support for a wide variety of things, and often has a couple different ways to get the same result.  Here, we are solving the linear system

  • x+2y+3z=0
  • 3x+2y+z=-4
  • x+y+z=-1
Y = vector([0,-4,-1]) X = A.solve_right(Y) A \ Y; X; A*X 
       

It will also tell me if there isn't a solution, which of course can happen!

A.solve_right(w) 
       

Okay, this is nice, but what about finding out what else Sage can do?  Here, there is a very nice set of help features.  What can we do with a matrix, for instance?  Type the dot/period after your object's name, and then [tab] to see what options you have:

A. 
       
A.echelon_form();A.determinant();A.rank() 
       

Naturally, the only options that come up are ones you can actually do to a matrix.  

Also note the "()" syntax - it betrays Sage's foundation in the Python programming language.  You don't need to know Python to use Sage, though it doesn't hurt to learn just a tad of it to be able to do cooler things.  

Many things are also accessible via normal mathematical syntax, of course:

rank(A); det(A) 
       

But not all.  This is probably a good thing, though:

echelon_form(A) 
       

The other nice feature in Sage for help is access to the documentation of each function or object.  This is done simply by appending "?" to your request.

A.eigenspaces_left? 
       

Just a few final examples, to show a few more standard things you might expect Sage to have - which indeed it does:

y = var('y') P=plot_slope_field(1-y,(x,0,3),(y,0,20)) y = function('y',x) # declare y to be a function of x f = desolve(diff(y,x) + y - 1, y, ics=[2,2]) # solve the DE, with Initial ConditionS of 2,2 Q=plot(f,0,3) html('$f=%s$'%(latex(f),)); show(P+Q) # I can use HTML in my outcomes 
       

One annoying, but crucial, point in the above is that you must use the syntax "var('y')" to define all variables other than "x".  In the long run, this is good, because it helps avoid doing silly things like the reassignment of \pi I did above, but in the short run it is annoying, I apologize.  

The only other such thing I can think of off the top of my head is that Sage doesn't like it if you pretend a symbolic expression is "callable" without explicitly saying so.  In one variable this seems pointless:

f=x^2; f(3) 
       
f(x)=x^2; f(3) 
       

But it makes lots of sense to require this in more than one variable! 

g=x^2*y h=y*x^2 g(2,1); h(2,1) 
       
g(x,y)=x^2*y h(y,x)=y*x^2 g(2,1);h(2,1) 
       

Okay, now we can continue with the eye candy.

plot([x^i for i in [1..10]],(x,0,1),fill = dict((i,[i+1]) for i in [0..9])) 
       
import pylab A_image = pylab.mean(pylab.imread(DATA + 'WNECMath.png'), 2) @interact def svd_image(i=(20,(1..100)),display_axes=True): u,s,v = pylab.linalg.svd(A_image) A = sum(s[j]*pylab.outer(u[0:,j],v[j,0:]) for j in range(i)) g = graphics_array([matrix_plot(A),matrix_plot(A_image)]) show(g,axes=display_axes, figsize=(8,3)) html('<h2>Compressed using %s eigenvalues</h2>'%i) 
       

Click to the left again to hide and once more to show the dynamic interactive window

var('x,y') plot3d(sin(pi*(x^2+y^2))/2,(x,-1,1),(y,-1,1)) 
       

Well, I could go on, but I really just wanted to give you a taste of how Sage is and isn't like similar systems; I assume you have your own experience and needs.  And naturally, there are some things that are easier to do in one system than another, and some that aren't possible in any of them (solving the Riemann Hypothesis, for instance).  

One thing I do want to point out is the variety of pedagogical techniques one can use with this (or any other) software - but from the convenience of students' own rooms.  For instance:

  • Use interactive "mathlets" or "Sagelets" to create lab experiments which don't require remembering syntax or commands - even completely GUI-driven.  I have done this a lot.
  • Fully featured lecture notes.  I have done a lot of this.
  • Sage's power only increases as you go up the mathematical food chain, as it were, so it can be incredibly useful for undergraduate (or your own!) research.  I have done some of this, as have many others across the country.
  • Create a "cheat sheet" of commands which students can use to check homework or try things out.  I can imagine this would be particularly useful in calculus or linear algebra, but haven't done a lot of this - yet.
  • Allow students to share worksheets collaboratively for a project, with you or without you.  This is very easy (see the "Share" button above), but I haven't found the need for it yet; if you do a lot of that, you may want to try it.

Okay, now let's open up the floor!  And don't forget to check out the website:

http://www.sagemath.org