MAA Northeastern Section Spring 2011 Conference
Welcome to the Sage workshop! Special thanks to Rob P. for inviting me. We should have a lot of fun and learn a lot.
Click here to go straight to the main content. Some of this content was originally developed for the MAA PREP Workshop "Sage: Using Open-Source Mathematics Software with Undergraduates" (funding provided by NSF DUE 0817071).
The first part of this workshop is to learn how to open a Sage worksheet. You'll need your own copy of the worksheet on the server. It should look like this at the top:

Except, of course, your username will appear! If you already have a live copy, please skip below.
If you don't see this, take another look at the top of the screen. Does it look like this?

If so, you need to log in! Click the link at the upper left and follow usual website login creation/login procedures. Then return to this worksheet page.
Otherwise, it should look like this:

It should also look like this once you log in. In that case, just click 'Edit this'.
Then the worksheet should look like this!

Then you can continue with the worksheet.
Because you self-selected into this workshop, we're going to assume that you know some of the point of mathematics software in the classroom. So we are going to have two goals.
The first goal will be addressed through some brief introduction to how Sage works, how to find help interactively, and how to annotate in Sage.
The second goal will be addressed after this.
Let's get started!
Below, and throughout this worksheet, are little boxes called input cells or code cells. They should be about the width of your browser.
Evaluating the content of an input cell is very easy.
Try evaluating the following cell.
4 4 |
Sage prints out its response just below the cell (that's the "4" above, so Sage confirms that 2+2=4). Note also that Sage has automatically made the next cell active after you evaluated your first cell.
You can also evaluate a cell using a keyboard shortcut.
2 * 3 * 5 * 67 2 * 3 * 5 * 67 |
An input cell isn't much use if it can only do one thing, so you can edit a cell and evaluate it again. Just click inside, and then make any changes you wish by typing as usual.
Try changing the number "2010" above to "2011" and evaluate the cell to find its factorization (surprised?); then try your own favorite number.
To do more math, we'll need to be able to create new input cells. This is also easy.
If for some reason you need to remove or delete an input cell, just delete all the text inside of it, and then press backspace in the now-empty cell.
Try creating a few new input cells below, doing some arithmetic in those cells, and then deleting one of the input cells.
|
|
|
|
There are lots of commands in Sage, but it wouldn't be productive to use all our time exploring them. Instead, I want to illustrate an important fact about how to use Sage by creating some matrices and doing things to them.
[1 2 3] [3 2 1] [1 1 1] [1 2 3] [3 2 1] [1 1 1] |
I can get the determinant of this matrix fairly easily. Many Sage commands look like this.
0 0 |
However, it might be tedious even to cut and paste this definition all the time. So instead we assign the matrix to a new variable name.
[1 2 3] [3 2 1] [1 1 1] [1 2 3] [3 2 1] [1 1 1] |
This shows two things.
Now let's do something to this matrix.
[ 1 0 -1] [ 0 1 2] [ 0 0 0] [ 1 0 -1] [ 0 1 2] [ 0 0 0] |
This has given us the row-reduced echelon form of the matrix A. The last row is all zeros because this matrix doesn't have full rank, as we saw from the determinant.
Question: What's up with that syntax?
For now, we won't go in great depth explaining the reasons behind this syntax, which may be new to you. For those who are interested, Sage often uses this type of syntax (known as "object-oriented") because...
For example, the following numerically evaluates ('n') the constant \pi ('pi') to twenty digits ('digits=20').
3.1415926535897932385 3.1415926535897932385 |
There are various ways to get help for doing things in Sage. Here are several common ways to get help as you are working in a Sage worksheet.
Sage includes extensive documentation covering thousands of functions, with many examples, tutorials, and other helps.
Our main focus here, though, is help you can immediately access from within a worksheet, where you don't have to do any of those things.
The most useful help available in the notebook is "tab completion". The idea is that even if you aren't one hundred percent sure of the name of a command, the first few letters should still be enough to help find it. Here's an example.
Try tabbing after the "pl" in the following cell to see all the commands that start with the letters "pl". You should see that "plot_slope_field" is one of them.
|
|
And here is the slope field I wanted.
|
|
(Parenthetically, it's important that I defined my function with the usual mathematical notation f(x,y), but we won't delve into this now.)
You can also use tab-completion to see what you can do to an expression or mathematical object.
You will see a list pop up of all the things you can do to the expression.
To try this, evaluate the following cell, just to make sure our matrix A is defined.
|
|
Now put your cursor after the period and press your tab key.
|
|
Because Sage starts counting at zero (like many programming environments), it's the middle column that is rescaled.
In the previous example, because you have to pick a column and a scaling factor, it makes sense that there would be some extra things to input. But how would you know what order they go in?
To find out, there is another help tool one can use from right inside the notebook. Almost all documentation in Sage has extensive examples that can illustrate how to use the function.
To see how this help works, evaluate the first cell, then move your cursor after the question mark in the second one and press tab.
[ 1 10 3] [ 3 10 1] [ 1 5 1] [ 1 10 3] [ 3 10 1] [ 1 5 1] |
In the previous example, you might have wondered why I needed to put "f.integrate(x)" rather than just "f.integrate()", by analogy with "sqrt(2).n()".
To find out, there is another help tool one can use from right inside the notebook. Almost all documentation in Sage has extensive examples that can illustrate how to use the function.
To see how this help works, move your cursor after the question mark below and press tab.
|
|
|
File: /home/sage/sage_install/sage-4.6/devel/sage/sage/symbolic/expression.pyx Type: <type ‘builtin_function_or_method’> Definition: f.integrate(*args, **kwds) Docstring:
File: /home/sage/sage_install/sage-4.6/devel/sage/sage/symbolic/expression.pyx Type: <type ‘builtin_function_or_method’> Definition: f.integrate(*args, **kwds) Docstring:
|
The examples illustrate that the syntax requires "f.integrate(x)" and not just "f.integrate()". (After all, the latter could be ambiguous if several variables had already been defined).
To stop viewing the documentation after pressing tab, you can press the Escape key, just like with the completion of options.
|
|
There is one more source of help you may find useful in the long run, though perhaps not immediately.
The ability to see the code (the underlying instructions to the computer) is one of Sage's great strengths. You can see all the code to everything.
This means:
|
|
Whether one uses Sage in the classroom or in research, it is usually helpful to describe to the reader what is being done, such as in the description you are now reading.
Thanks to the mini-word processor TinyMCE and a TeX rendering engine called jsmath, you can type much more in Sage than just Sage commands. This math-aware setup makes Sage perfect for annotating computations.
To use the word processor, we create a text cell (as opposed to a input cell that contains Sage commands that Sage evaluates).
To create a text cell, do the following.
Try inserting a text cell between the input cells below.
|
|
|
|
TinyMCE makes it easy for format text in many ways. Try experimenting with the usual bold button, underline button, different text fonts and colors, ordered and unordered lists, centering, and so on. Some of the shortcut keys you are familiar with from other word processors may also work, depending on your system.
There are two other things you can do which take advantage of the worksheet being on the web.
Here is a simpler example.
81 81 |
If f(x)=x^2, then f(9)=81.
It is simple to edit a text cell; simply double-click on the text.
Try double-clicking on this text to edit this text cell (or any text cell) to see how we typed the mathematics!
We just have an hour together, so I would rather have you explore.
So in the spirit of Grätzer's LaTeX book, here is a gallery of some of my favorite examples of Sage functionality.
I particularly encourage you to take small pieces of these, make a new cell, and try to discover their use on their own.
3/50*sqrt(25*x^2 - 3)*x + 9/250*log(50*x + 10*sqrt(25*x^2 - 3)) 3/50*sqrt(25*x^2 - 3)*x + 9/250*log(50*x + 10*sqrt(25*x^2 - 3)) |
|
|
|
|
|
2 2 |
6 6 |
Number Field in a with defining polynomial z^2 - 2 True Number Field in a with defining polynomial z^2 - 2 True |
False False |
|
|
|
|
[[4, 0, 0], [3, 1, 0], [3, 0, 1], [2, 2, 0], [2, 1, 1], [2, 0, 2], [1, 3, 0], [1, 2, 1], [1, 1, 2], [1, 0, 3], [0, 4, 0], [0, 3, 1], [0, 2, 2], [0, 1, 3], [0, 0, 4]] [[4, 0, 0], [3, 1, 0], [3, 0, 1], [2, 2, 0], [2, 1, 1], [2, 0, 2], [1, 3, 0], [1, 2, 1], [1, 1, 2], [1, 0, 3], [0, 4, 0], [0, 3, 1], [0, 2, 2], [0, 1, 3], [0, 0, 4]] |
|
|
The following cell finds a maximal stable set of a graph using linear programming.
|
|
There are many ways to do statistics in Sage. Here we use the very powerful program R, which is part of Sage.
[1] 2.9 3.0 2.5 2.6 3.2 3.8 2.7 4.0 2.4 2.8 3.4 3.7 2.2 2.0 [1] 1 1 1 1 1 2 2 2 2 3 3 3 3 3 Levels: 1 2 3 [1] 2.9 3.0 2.5 2.6 3.2 3.8 2.7 4.0 2.4 2.8 3.4 3.7 2.2 2.0 [1] 1 1 1 1 1 2 2 2 2 3 3 3 3 3 Levels: 1 2 3 |
Kruskal-Wallis rank sum test data: sage17 and sage33 Kruskal-Wallis chi-squared = 0.7714, df = 2, p-value = 0.68 Kruskal-Wallis rank sum test data: sage17 and sage33 Kruskal-Wallis chi-squared = 0.7714, df = 2, p-value = 0.68 |
|
|
Click to the left again to hide and once more to show the dynamic interactive window |
|
|
Part of our gallery of functionality is pedagogical. Here are many things one can do with Sage (or any other such software) - but in Sage's case, from the convenience of students' own rooms. For instance:
And of course, for everything else, please go to
for all things Sage. Thanks for participating!
|
|