Sage is a powerful system for studying and exploring many different areas of mathematics. In the next section, and the majority of the remaining section, we will include short descriptions and examples using Sage. You can read a bit more about Sage in the Preface. If you are not already reading this in a capable format, you may want to investigate the online version of the book where the examples are “live” and editable. Most of your interaction with Sage will be by typing commands into a compute cell. We have placed a compute cell just below this paragraph. Online this is an instance of the Sage Cell Server at work. In a PDF or on paper or in an EPUB, it is likely to quite boring, since it is empty.
If you can, place your cursor inside the cell and type 2+2
and then click on the evaluate link. Did a 4
appear below the cell? If so, you have successfully sent a command off for Sage to evaluate and you have received back the (correct) answer.
Here is another compute cell. Try evaluating the command factorial(300)
.
Hmmmmm. That is quite a big integer! And it possibly continues onto more than one line, or off the edge of your screen, since there are 615 digits in the result.
Online you cannot save your results, nor make new cells. But you can reload a page if your editing makes a real mess of a cell.
Each cell knows about values of variables stored in other cells, and we assume you evaluate all the cells in a section, in the order presented. In truth, the variables are self-contained within each Sage discussion. Note that reloading a page destroys all the intermediate results and you will need to start over.
Each compute cell will show output due to only the very last command in the cell. Try to predict the following output before evaluating the cell.
The following compute cell will not print anything since the one command does not create output. But it will have an effect, as you can see when you execute the subsequent cell. Notice how this uses the value of b
from above. Execute this compute cell once. Exactly once. Even if it appears to do nothing. If you execute the cell twice, your credit card may be charged twice.
Now execute this cell, which will produce some output.
So b
came into existence as 6
. Then a cell added 50
. This assumes you only executed this cell once! In the last cell we create b+20
(but do not save it) and it is this value that is output.
You can combine several commands on one line with a semi-colon. This is a great way to get multiple outputs from a compute cell.
Some commands in Sage are functions, an example is factorial()
above. Other commands are methods of an object and are like characteristics of objects, examples are .factor()
and .derivative()
as methods of a function.
There are other ways to compute with Sage, besides Sage cells embedded in web pages. A good option is the
SageMathCloud at
cloud.sagemath.com
. You can experiment with a free account, and then you can save your work in
Sage worksheets between sessions. There are several ways of additionally documenting your results, using Markdown,
HTML, or
LaTeX. SageMathCloud also supports Jupyter notebooks with a Sage kernel.
Much of our interaction with sets will be through Sage lists. These are not really sets — they allow duplicates, and order matters. But they are so close to sets, and so easy and powerful to use that we will use them regularly. We will use a fun made-up list for practice, the quote marks mean the items are just text, with no special mathematical meaning. Execute these compute cells as we work through them.
So the square brackets define the boundaries of our list, commas separate items, and we can give the list a name. To work with just one element of the list, we use the name and a pair of brackets with an index. Notice that lists have indices that begin counting at zero. This will seem odd at first and will seem very natural later.
We can add a new creature to the zoo, it is joined up at the far right end.
We can remove a creature.
We can extract a sublist. Here we start with element 1 (the elephant) and go all the way up to, but not including, element 3 (the beetle). Again a bit odd, but it will feel natural later. For now, notice that we are extracting two elements of the lists, exactly \(3-1=2\) elements.
Often we will want to see if two lists are equal. To do that we will need to sort a list first. A function creates a new, sorted list, leaving the original alone. So we need to save the new one with a new name.
Notice that if you run this last compute cell your zoo has changed and some commands above will not necessarily execute the same way. If you want to experiment, go all the way back to the first creation of the zoo and start executing cells again from there with a fresh zoo.
A construction called a “list comprehension” is especially powerful, especially since it almost exactly mirrors notation we use to describe sets. Suppose we want to form the plural of the names of the creatures in our zoo. We build a new list, based on all of the elements of our old list.
Almost like it says: we add an “s” to each animal name, for each animal in the zoo, and place them in a new list. Perfect. (Except for getting the plural of “ostrich” wrong.)
One final type of list, with numbers this time. The range()
function will create an object that can generate a lists of integer. In its simplest form an invocation like range(12)
does not seem to do much. But when want to, we can make teh object generate a list.
The next example will create a list of 12 integers, starting at zero and working up to, but not including, 12. Does this sound familiar?
Here are two other forms, that you should be able to understand by studying the examples. In the second one, we use a shorthand of sorts (the “starred” form) to produce a list.
We have covered a lot here in this section, so come back later to pick up tidbits you might have missed. There are also many more features in SageMathCloud, including easy and powerful collaboration, that we have not covered.