<section xml:id="sets-sage" xml:base="sage/sets-sage.xml">
<title>Sage</title>
<introduction>
<p>
Sage is a powerful system for studying and exploring many different areas of mathematics.
In this textbook, you will study a variety of algebraic structures,
such as groups, rings and fields.
Sage does an excellent job of implementing many features of these objects as we will see in the chapters ahead.
But here and now, in this initial chapter,
we will concentrate on a few general ways of getting the most out of working with Sage.
</p>
<p>
You may use Sage several different ways.
It may be used as a command-line program when installed on your own computer.
Or it might be a web application such as the SageMathCloud.
Our writing will assume that you are reading this as a worksheet within the Sage Notebook (a web browser interface),
or this is a section of the entire book presented as web pages,
and you are employing the Sage Cell Server via those pages.
After the first few chapters the explanations should work equally well for whatever vehicle you use to execute Sage commands.
</p>
</introduction>
<subsection>
<title>Executing Sage Commands</title>
<p>
Most of your interaction will be by typing commands into a <em>compute cell</em>.
If you are reading this in the Sage Notebook or as a webpage version of the book,
then you will see a compute cell just below this paragraph.
Click once inside the compute cell and if you are in the Sage Notebook,
you will get a more distinctive border around it,
a blinking cursor inside, plus a cute little
<q>evaluate</q>
link below.
</p>
<sage />
<p>
At the cursor,
type <c>2+2</c> and then click on the evaluate link.
Did a <c>4</c> 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.
</p>
<p>
Here is another compute cell.
Try evaluating the command <c>factorial(300)</c> here.
</p>
<sage />
<p>
Hmmmmm.
That is quite a big integer!
If you see slashes at the end of each line,
this means the result is continued onto the next line,
since there are 615 total digits in the result.
</p>
<p>
To make new compute cells in the Sage Notebook (only),
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.
</p>
<p>
Try placing your cursor just below the monstrous value of <m>300!</m> that you have.
Click on the blue bar and try another factorial computation in the new compute cell.
</p>
<p>
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.
</p>
<sage>
<input>
a = 10
b = 6
b = b - 10
a = a + 20
a
</input>
<output>30</output>
</sage>
<p>
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 <c>b</c> from above.
Execute this compute cell <em>once</em>.
Exactly once.
Even if it <em>appears</em> to do nothing.
If you execute the cell twice,
your credit card may be charged twice.
</p>
<sage>
<input>b = b + 50</input>
</sage>
<p>
Now execute this cell, which will produce some output.
</p>
<sage>
<input>b + 20</input>
<output>66</output>
</sage>
<p>
So <c>b</c> came into existence as <c>6</c>.
We subtracted <m>10</m> immediately afterward.
Then a subsequent cell added <c>50</c>.
This assumes you executed this cell <em>exactly</em> once!
In the last cell we create <c>b+20</c> (but do not save it) and it is this value (<m>66</m>) that is output,
while <c>b</c> is still <m>46</m>.
</p>
<p>
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.
</p>
<sage>
<input>A = matrix([[3, 1], [5,2]]); A</input>
<output>
[3 1]
[5 2]
</output>
</sage>
<sage>
<input>print A; print ; A.inverse()</input>
<output>
[3 1]
[5 2]
<BLANKLINE>
[ 2 -1]
[-5 3]
</output>
</sage>
</subsection>
<subsection>
<title>Immediate Help</title>
<p>
Some commands in Sage are
<q>functions,</q>
an example is <c>factorial()</c> above.
Other commands are
<q>methods</q>
of an object and are like characteristics of objects,
an example is <c>.inverse()</c> as a method of a matrix.
Once you know how to create an object
(such as a matrix),
then it is easy to see all the available methods.
Write the name of the object, place a period (
<q>dot</q>
) and hit the TAB key.
If you have <c>A</c> defined from above,
then the compute cell below is ready to go,
click into it and then hit TAB
(not
<q>evaluate</q>!).
You should get a long list of possible methods.
</p>
<sage doctest="not tested">
<input>A.</input>
</sage>
<p>
To get some help on how to use a method with an object,
write its name after a dot
(with no parentheses)
and then use a question-mark and hit TAB. (Hit the escape key
<q>ESC</q>
to remove the list, or click on the text for a method.)
</p>
<sage doctest="not tested">
<input>A.inverse?</input>
</sage>
<p>
With one more question-mark and a TAB you can see the actual computer instructions that were programmed into Sage to make the method work,
once you scoll down past the documentation delimited by the triple quotes (<c>"""</c>):
</p>
<sage doctest="not tested">
<input>A.inverse??</input>
</sage>
<p>
It is worthwhile to see what Sage does when there is an error.
You will probably see a lot of these at first,
and initially they will be a bit intimidating.
But with time,
you will learn how to use them effectively and you will also become more proficient with Sage and see them less often.
Execute the compute cell below,
it asks for the inverse of a matrix that has no inverse.
Then reread the commentary.
</p>
<sage>
<input>
B = matrix([[2, 20], [5, 50]])
B.inverse()
</input>
<output>
Traceback (most recent call last):
...
ZeroDivisionError: Matrix is singular
</output>
</sage>
<p>
Click just to the left of the error message to expand it fully
(another click hides it totally,
and a third click brings back the abbreviated form).
Read the bottom of an error message first,
it is your best explanation.
Here a <c>ZeroDivisionError</c> is not 100% accurate,
but is close.
The matrix is not invertible,
not dissimilar to how we cannot divide scalars by zero.
The remainder of the message begins at the top showing were the error first happened in your code and then the various places where intermediate functions were called,
until the actual piece of Sage where the problem occurred.
Sometimes this information will give you some clues,
sometimes it is totally undecipherable.
So do not let it scare you if it seems mysterious,
but do remember to always read the last line first,
then go back and read the first few lines for something that looks like your code.
</p>
</subsection>
<subsection>
<title>Annotating Your Work</title>
<p>
It is easy to comment on your work when you use the Sage Notebook.
(The following only applies if you are reading this within a Sage Notebook.
If you are not,
then perhaps you can go open up a worksheet in the Sage Notebook and experiment there.)
You can open up a small word-processor by hovering your mouse until you get a 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
<q>Save changes</q>
button to exit.
Double-click on your text if you need to go back and edit it later.
</p>
<p>
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 <em>exactly</em>:
</p>
<blockquote>
<p>
<c>Pythagorean Theorem: $c^2=a^2+b^2$</c>
</p>
</blockquote>
<p>
and save your changes.
The symbols between the dollar signs are written according to the mathematical typesetting language known as <tex /> <mdash /> cruise the internet to learn more about this very popular tool.
(Well, it is extremely popular among mathematicians and physical scientists.)
</p>
<sage />
</subsection>
<subsection>
<title>Lists</title>
<p>
Much of our interaction with sets will be through Sage lists.
These are not really sets <mdash /> 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.
</p>
<sage>
<input>
zoo = ['snake', 'parrot', 'elephant', 'baboon', 'beetle']
zoo
</input>
<output>
['snake', 'parrot', 'elephant', 'baboon', 'beetle']
</output>
</sage>
<p>
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
<em>begin counting at zero</em>.
This will seem odd at first and will seem very natural later.
</p>
<sage>
<input>zoo[2]</input>
<output>'elephant'</output>
</sage>
<p>
We can add a new creature to the zoo,
it is joined up at the far right end.
</p>
<sage>
<input>zoo.append('ostrich'); zoo</input>
<output>['snake', 'parrot', 'elephant', 'baboon', 'beetle', 'ostrich']</output>
</sage>
<p>
We can remove a creature.
</p>
<sage>
<input>
zoo.remove('parrot')
zoo
</input>
<output>['snake', 'elephant', 'baboon', 'beetle', 'ostrich']</output>
</sage>
<p>
We can extract a sublist.
Here we start with element 1
(the elephant)
and go all the way up to, <em>but not including</em>, 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 <m>3-1=2</m> elements.
</p>
<sage>
<input>
mammals = zoo[1:3]
mammals
</input>
<output>['elephant', 'baboon']</output>
</sage>
<p>
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.
</p>
<sage>
<input>
newzoo = sorted(zoo)
newzoo
</input>
<output>['baboon', 'beetle', 'elephant', 'ostrich', 'snake']</output>
</sage>
<sage>
<input>
zoo.sort()
zoo
</input>
<output>['baboon', 'beetle', 'elephant', 'ostrich', 'snake']</output>
</sage>
<p>
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.
</p>
<p>
A construction called a <term>list comprehension</term> 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.
</p>
<sage>
<input>
plurality_zoo = [animal+'s' for animal in zoo]
plurality_zoo
</input>
<output>['baboons', 'beetles', 'elephants', 'ostrichs', 'snakes']</output>
</sage>
<p>
Almost like it says: we add an
<q>s</q>
to each animal name, for each animal in the zoo,
and place them in a new list.
Perfect.
(Except for getting the plural of
<q>ostrich</q>
wrong.)
</p>
</subsection>
<subsection>
<title>Lists of Integers</title>
<p>
One final type of list, with numbers this time.
The <c>srange()</c> function will create lists of integers.
(The
<q>s</q>
in the name stands for
<q>Sage</q>
and so will produce integers that Sage understands best.
Many early difficulties with Sage and group theory can be alleviated by using only this command to create lists of integers.)
In its simplest form an invocation like <c>srange(12)</c> will create a list of 12 integers,
<em>starting at zero</em> and working up to,
<em>but not including</em>, 12.
Does this sound familiar?
</p>
<sage>
<input>dozen = srange(12); dozen</input>
<output>[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]</output>
</sage>
<p>
Here are two other forms,
that you should be able to understand by studying the examples.
</p>
<sage>
<input>teens = srange(13, 20); teens</input>
<output>[13, 14, 15, 16, 17, 18, 19]</output>
</sage>
<sage>
<input>decades = srange(1900, 2000, 10); decades</input>
<output>[1900, 1910, 1920, 1930, 1940, 1950, 1960, 1970, 1980, 1990]</output>
</sage>
</subsection>
<subsection>
<title>Saving and Sharing Your Work</title>
<p>
There is a
<q>Save</q>
button in the upper-right corner of the Sage Notebook.
This will save a current copy of your worksheet that you can retrieve your work from within your notebook again later,
though you have to re-execute all the cells when you re-open the worksheet.
</p>
<p>
There is also a
<q>File</q>
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
<q>Save worksheet to a file...</q>
When you do this,
you are creating a copy of your worksheet in the <c>sws</c> format
(short for
<q>Sage WorkSheet</q>).
You can email this file,
or post it on a website, for other Sage users and they can use the
<q>Upload</q>
link on the homepage of their notebook to incorporate a copy of your worksheet into their notebook.
</p>
<p>
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.
</p>
<p>
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 Sage Notebook that we have not covered.
</p>
</subsection>
</section>
Section 1.3 Sage
View Source for section
Sage is a powerful system for studying and exploring many different areas of mathematics. In this textbook, you will study a variety of algebraic structures, such as groups, rings and fields. Sage does an excellent job of implementing many features of these objects as we will see in the chapters ahead. But here and now, in this initial chapter, we will concentrate on a few general ways of getting the most out of working with Sage.
You may use Sage several different ways. It may be used as a command-line program when installed on your own computer. Or it might be a web application such as the SageMathCloud. Our writing will assume that you are reading this as a worksheet within the Sage Notebook (a web browser interface), or this is a section of the entire book presented as web pages, and you are employing the Sage Cell Server via those pages. After the first few chapters the explanations should work equally well for whatever vehicle you use to execute Sage commands.
Subsection 1.3.1 Executing Sage Commands
View Source for subsection
<subsection>
<title>Executing Sage Commands</title>
<p>
Most of your interaction will be by typing commands into a <em>compute cell</em>.
If you are reading this in the Sage Notebook or as a webpage version of the book,
then you will see a compute cell just below this paragraph.
Click once inside the compute cell and if you are in the Sage Notebook,
you will get a more distinctive border around it,
a blinking cursor inside, plus a cute little
<q>evaluate</q>
link below.
</p>
<sage />
<p>
At the cursor,
type <c>2+2</c> and then click on the evaluate link.
Did a <c>4</c> 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.
</p>
<p>
Here is another compute cell.
Try evaluating the command <c>factorial(300)</c> here.
</p>
<sage />
<p>
Hmmmmm.
That is quite a big integer!
If you see slashes at the end of each line,
this means the result is continued onto the next line,
since there are 615 total digits in the result.
</p>
<p>
To make new compute cells in the Sage Notebook (only),
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.
</p>
<p>
Try placing your cursor just below the monstrous value of <m>300!</m> that you have.
Click on the blue bar and try another factorial computation in the new compute cell.
</p>
<p>
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.
</p>
<sage>
<input>
a = 10
b = 6
b = b - 10
a = a + 20
a
</input>
<output>30</output>
</sage>
<p>
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 <c>b</c> from above.
Execute this compute cell <em>once</em>.
Exactly once.
Even if it <em>appears</em> to do nothing.
If you execute the cell twice,
your credit card may be charged twice.
</p>
<sage>
<input>b = b + 50</input>
</sage>
<p>
Now execute this cell, which will produce some output.
</p>
<sage>
<input>b + 20</input>
<output>66</output>
</sage>
<p>
So <c>b</c> came into existence as <c>6</c>.
We subtracted <m>10</m> immediately afterward.
Then a subsequent cell added <c>50</c>.
This assumes you executed this cell <em>exactly</em> once!
In the last cell we create <c>b+20</c> (but do not save it) and it is this value (<m>66</m>) that is output,
while <c>b</c> is still <m>46</m>.
</p>
<p>
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.
</p>
<sage>
<input>A = matrix([[3, 1], [5,2]]); A</input>
<output>
[3 1]
[5 2]
</output>
</sage>
<sage>
<input>print A; print ; A.inverse()</input>
<output>
[3 1]
[5 2]
<BLANKLINE>
[ 2 -1]
[-5 3]
</output>
</sage>
</subsection>
Most of your interaction will be by typing commands into a compute cell. If you are reading this in the Sage Notebook or as a webpage version of the book, then you will see a compute cell just below this paragraph. Click once inside the compute cell and if you are in the Sage Notebook, you will get a more distinctive border around it, a blinking cursor inside, plus a cute little “evaluate” link below.
At the cursor, 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)
here.Hmmmmm. That is quite a big integer! If you see slashes at the end of each line, this means the result is continued onto the next line, since there are 615 total digits in the result.
To make new compute cells in the Sage Notebook (only), 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
. We subtracted \(10\) immediately afterward. Then a subsequent cell added 50
. This assumes you executed this cell exactly once! In the last cell we create b+20
(but do not save it) and it is this value (\(66\)) that is output, while b
is still \(46\text{.}\)
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.
Subsection 1.3.2 Immediate Help
View Source for subsection
<subsection>
<title>Immediate Help</title>
<p>
Some commands in Sage are
<q>functions,</q>
an example is <c>factorial()</c> above.
Other commands are
<q>methods</q>
of an object and are like characteristics of objects,
an example is <c>.inverse()</c> as a method of a matrix.
Once you know how to create an object
(such as a matrix),
then it is easy to see all the available methods.
Write the name of the object, place a period (
<q>dot</q>
) and hit the TAB key.
If you have <c>A</c> defined from above,
then the compute cell below is ready to go,
click into it and then hit TAB
(not
<q>evaluate</q>!).
You should get a long list of possible methods.
</p>
<sage doctest="not tested">
<input>A.</input>
</sage>
<p>
To get some help on how to use a method with an object,
write its name after a dot
(with no parentheses)
and then use a question-mark and hit TAB. (Hit the escape key
<q>ESC</q>
to remove the list, or click on the text for a method.)
</p>
<sage doctest="not tested">
<input>A.inverse?</input>
</sage>
<p>
With one more question-mark and a TAB you can see the actual computer instructions that were programmed into Sage to make the method work,
once you scoll down past the documentation delimited by the triple quotes (<c>"""</c>):
</p>
<sage doctest="not tested">
<input>A.inverse??</input>
</sage>
<p>
It is worthwhile to see what Sage does when there is an error.
You will probably see a lot of these at first,
and initially they will be a bit intimidating.
But with time,
you will learn how to use them effectively and you will also become more proficient with Sage and see them less often.
Execute the compute cell below,
it asks for the inverse of a matrix that has no inverse.
Then reread the commentary.
</p>
<sage>
<input>
B = matrix([[2, 20], [5, 50]])
B.inverse()
</input>
<output>
Traceback (most recent call last):
...
ZeroDivisionError: Matrix is singular
</output>
</sage>
<p>
Click just to the left of the error message to expand it fully
(another click hides it totally,
and a third click brings back the abbreviated form).
Read the bottom of an error message first,
it is your best explanation.
Here a <c>ZeroDivisionError</c> is not 100% accurate,
but is close.
The matrix is not invertible,
not dissimilar to how we cannot divide scalars by zero.
The remainder of the message begins at the top showing were the error first happened in your code and then the various places where intermediate functions were called,
until the actual piece of Sage where the problem occurred.
Sometimes this information will give you some clues,
sometimes it is totally undecipherable.
So do not let it scare you if it seems mysterious,
but do remember to always read the last line first,
then go back and read the first few lines for something that looks like your code.
</p>
</subsection>
Some commands in Sage are “functions,” an example is
factorial()
above. Other commands are “methods” of an object and are like characteristics of objects, an example is .inverse()
as a method of a matrix. Once you know how to create an object (such as a matrix), then it is easy to see all the available methods. Write the name of the object, place a period ( “dot” ) and hit the TAB key. If you have A
defined from above, then the compute cell below is ready to go, click into it and then hit TAB (not “evaluate”!). You should get a long list of possible methods.To get some help on how to use a method with an object, write its name after a dot (with no parentheses) and then use a question-mark and hit TAB. (Hit the escape key “ESC” to remove the list, or click on the text for a method.)
With one more question-mark and a TAB you can see the actual computer instructions that were programmed into Sage to make the method work, once you scoll down past the documentation delimited by the triple quotes (
"""
):It is worthwhile to see what Sage does when there is an error. You will probably see a lot of these at first, and initially they will be a bit intimidating. But with time, you will learn how to use them effectively and you will also become more proficient with Sage and see them less often. Execute the compute cell below, it asks for the inverse of a matrix that has no inverse. Then reread the commentary.
Click just to the left of the error message to expand it fully (another click hides it totally, and a third click brings back the abbreviated form). Read the bottom of an error message first, it is your best explanation. Here a
ZeroDivisionError
is not 100% accurate, but is close. The matrix is not invertible, not dissimilar to how we cannot divide scalars by zero. The remainder of the message begins at the top showing were the error first happened in your code and then the various places where intermediate functions were called, until the actual piece of Sage where the problem occurred. Sometimes this information will give you some clues, sometimes it is totally undecipherable. So do not let it scare you if it seems mysterious, but do remember to always read the last line first, then go back and read the first few lines for something that looks like your code.Subsection 1.3.3 Annotating Your Work
View Source for subsection
<subsection>
<title>Annotating Your Work</title>
<p>
It is easy to comment on your work when you use the Sage Notebook.
(The following only applies if you are reading this within a Sage Notebook.
If you are not,
then perhaps you can go open up a worksheet in the Sage Notebook and experiment there.)
You can open up a small word-processor by hovering your mouse until you get a 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
<q>Save changes</q>
button to exit.
Double-click on your text if you need to go back and edit it later.
</p>
<p>
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 <em>exactly</em>:
</p>
<blockquote>
<p>
<c>Pythagorean Theorem: $c^2=a^2+b^2$</c>
</p>
</blockquote>
<p>
and save your changes.
The symbols between the dollar signs are written according to the mathematical typesetting language known as <tex /> <mdash /> cruise the internet to learn more about this very popular tool.
(Well, it is extremely popular among mathematicians and physical scientists.)
</p>
<sage />
</subsection>
It is easy to comment on your work when you use the Sage Notebook. (The following only applies if you are reading this within a Sage Notebook. If you are not, then perhaps you can go open up a worksheet in the Sage Notebook and experiment there.) You can open up a small word-processor by hovering your mouse until you get a 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:
View Source for blockquote
<blockquote> <p> <c>Pythagorean Theorem: $c^2=a^2+b^2$</c> </p> </blockquote>
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.)
Subsection 1.3.4 Lists
View Source for subsection
<subsection>
<title>Lists</title>
<p>
Much of our interaction with sets will be through Sage lists.
These are not really sets <mdash /> 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.
</p>
<sage>
<input>
zoo = ['snake', 'parrot', 'elephant', 'baboon', 'beetle']
zoo
</input>
<output>
['snake', 'parrot', 'elephant', 'baboon', 'beetle']
</output>
</sage>
<p>
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
<em>begin counting at zero</em>.
This will seem odd at first and will seem very natural later.
</p>
<sage>
<input>zoo[2]</input>
<output>'elephant'</output>
</sage>
<p>
We can add a new creature to the zoo,
it is joined up at the far right end.
</p>
<sage>
<input>zoo.append('ostrich'); zoo</input>
<output>['snake', 'parrot', 'elephant', 'baboon', 'beetle', 'ostrich']</output>
</sage>
<p>
We can remove a creature.
</p>
<sage>
<input>
zoo.remove('parrot')
zoo
</input>
<output>['snake', 'elephant', 'baboon', 'beetle', 'ostrich']</output>
</sage>
<p>
We can extract a sublist.
Here we start with element 1
(the elephant)
and go all the way up to, <em>but not including</em>, 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 <m>3-1=2</m> elements.
</p>
<sage>
<input>
mammals = zoo[1:3]
mammals
</input>
<output>['elephant', 'baboon']</output>
</sage>
<p>
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.
</p>
<sage>
<input>
newzoo = sorted(zoo)
newzoo
</input>
<output>['baboon', 'beetle', 'elephant', 'ostrich', 'snake']</output>
</sage>
<sage>
<input>
zoo.sort()
zoo
</input>
<output>['baboon', 'beetle', 'elephant', 'ostrich', 'snake']</output>
</sage>
<p>
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.
</p>
<p>
A construction called a <term>list comprehension</term> 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.
</p>
<sage>
<input>
plurality_zoo = [animal+'s' for animal in zoo]
plurality_zoo
</input>
<output>['baboons', 'beetles', 'elephants', 'ostrichs', 'snakes']</output>
</sage>
<p>
Almost like it says: we add an
<q>s</q>
to each animal name, for each animal in the zoo,
and place them in a new list.
Perfect.
(Except for getting the plural of
<q>ostrich</q>
wrong.)
</p>
</subsection>
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.)
Subsection 1.3.5 Lists of Integers
View Source for subsection
<subsection>
<title>Lists of Integers</title>
<p>
One final type of list, with numbers this time.
The <c>srange()</c> function will create lists of integers.
(The
<q>s</q>
in the name stands for
<q>Sage</q>
and so will produce integers that Sage understands best.
Many early difficulties with Sage and group theory can be alleviated by using only this command to create lists of integers.)
In its simplest form an invocation like <c>srange(12)</c> will create a list of 12 integers,
<em>starting at zero</em> and working up to,
<em>but not including</em>, 12.
Does this sound familiar?
</p>
<sage>
<input>dozen = srange(12); dozen</input>
<output>[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]</output>
</sage>
<p>
Here are two other forms,
that you should be able to understand by studying the examples.
</p>
<sage>
<input>teens = srange(13, 20); teens</input>
<output>[13, 14, 15, 16, 17, 18, 19]</output>
</sage>
<sage>
<input>decades = srange(1900, 2000, 10); decades</input>
<output>[1900, 1910, 1920, 1930, 1940, 1950, 1960, 1970, 1980, 1990]</output>
</sage>
</subsection>
One final type of list, with numbers this time. The
srange()
function will create lists of integers. (The “s” in the name stands for “Sage” and so will produce integers that Sage understands best. Many early difficulties with Sage and group theory can be alleviated by using only this command to create lists of integers.) In its simplest form an invocation like srange(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.
Subsection 1.3.6 Saving and Sharing Your Work
View Source for subsection
<subsection>
<title>Saving and Sharing Your Work</title>
<p>
There is a
<q>Save</q>
button in the upper-right corner of the Sage Notebook.
This will save a current copy of your worksheet that you can retrieve your work from within your notebook again later,
though you have to re-execute all the cells when you re-open the worksheet.
</p>
<p>
There is also a
<q>File</q>
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
<q>Save worksheet to a file...</q>
When you do this,
you are creating a copy of your worksheet in the <c>sws</c> format
(short for
<q>Sage WorkSheet</q>).
You can email this file,
or post it on a website, for other Sage users and they can use the
<q>Upload</q>
link on the homepage of their notebook to incorporate a copy of your worksheet into their notebook.
</p>
<p>
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.
</p>
<p>
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 Sage Notebook that we have not covered.
</p>
</subsection>
There is a “Save” button in the upper-right corner of the Sage Notebook. This will save a current copy of your worksheet that you can retrieve your work from within your notebook again later, though you have to re-execute all the cells when you re-open the worksheet.
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 the homepage of their notebook 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 Sage Notebook that we have not covered.