Example GS Getting Started from Rob Beezer FCLA
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 an electronic version, you may want to investigate obtaining the worksheet version of this book, where the examples are "live" and editable. Most of your interaction with Sage will be by typing commands into a compute cell. That's a compute cell just below this paragraph. Click once inside the compute cell and you will get a more distinctive border around it, a blinking cursor inside, plus a cute little "evaluate" link below it.



At the cursor, type 2+2 and then click on the evaluate link. Did a 4 appear below the cell? If so, you've successfully sent a command off for Sage to evaluate and you've received back the (correct) answer.

Here's another compute cell. Try evaluating the command factorial(300).

Hmmmmm. That is quite a big integer! The slashes you see at the end of each line mean the result is continued onto the next line, since there are 615 digits in the result.

To make new compute cells, hover your mouse just above another compute cell, or just below some output from a compute cell. When you see a skinny blue bar across the width of your worksheet, click and you will open up a new compute cell, ready for input. Note that your worksheet will remember any calculations you make, in the order you make them, no matter where you put the cells, so it is best to stay organized and add new cells at the bottom.

Try placing your cursor just below the monstrous value of $300!$ that you have. Click on the blue bar and try another factorial computation in the new compute cell.

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. The syntax for building a matrix should be somewhat obvious when you see the output, but if not, it is not particularly important to understand now.





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. To comment on your work, you can open up a small word-processor. Hover your mouse until you get the skinny blue bar again, but now when you click, also hold the SHIFT key at the same time. Experiment with fonts, colors, bullet lists, etc and then click the "Save changes" button to exit. Double-click on your text if you need to go back and edit it later.

Open the word-processor again to create a new bit of text (maybe next to the empty compute cell just below). Type all of the following exactly, but do not include any backslashes that might precede the dollar signs in the print version: Pythagorean Theorem: \$c^2=a^2+b^2\$ and save your changes. The symbols between the dollar signs are written according to the mathematical typesetting language known as TeX -- cruise the internet to learn more about this very popular tool. (Well, it is extremely popular among mathematicians and physical scientists.)



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 lists of integers. In its simplest form an invocation like range(12) 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.



There is a "Save" button in the upper-right corner of your worksheet. This will save a current copy of your worksheet that you can retrieve from within your notebook again later, though you have to re-execute all the cells when you re-open the worksheet later.

There is also a "File" drop-down list, on the left, just above your very top compute cell (not be confused with your browser's File menu item!). You will see a choice here labeled "Save worksheet to a file..." When you do this, you are creating a copy of your worksheet in the "sws" format (short for "Sage WorkSheet"). You can email this file, or post it on a website, for other Sage users and they can use the "Upload"link on their main notebook page to incorporate a copy of your worksheet into their notebook.

There are other ways to share worksheets that you can experiment with, but this gives you one way to share any worksheet with anybody almost anywhere.

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 the notebook that we have not covered.