This worksheet is based on William Stein’s JPL09__intro_to_sage.sws worksheet and the Sage days 20.5_demo worksheet and aims to be an interactive introduction to Sage through exercises. You will learn how to use the notebook and call the help.
Go into the File menu at the top left of this window and click on Copy worksheet. Then, you can clear all output in the menu Action by clicking on Delete All Output.
TODO: explain how to do it from static documentation with Sage >= 4.7
To evaluate code in the Sage Notebook, type the code into an input cell and
press shift-enter or click the evaluate link. Try it now with a simple
expression (e.g., ). The first time you evaluate a cell takes longer
than subsequent times since a new Sage process is started:
Create new input cells by clicking blue line that appears between cells when you move your mouse around. Try it now:
You can go back and edit any cell by clicking in it (or using the keyboard to move up or down). Go back and change your 2+2 above to 3 + 3 and re-evaluate it.
You can also edit this text right here by double clicking on it, which will bring up the TinyMCE Javascript text editor. You can even put embedded mathematics like this $sin(x) - y^3$ just like with LaTeX.
There are various ways of getting help in Sage.
We detail in what follows the two last methods through examples.
Start typing something and press the tab key. The interface tries to
complete it with a command name. If there is more than one completion, then they
are all presented to you. Remember that Sage is case sensitive, e.g. it
differentiates lower case from lower case. Hence the tab completion of
klein won’t show you the KleinFourGroup command that build the group
as a permutation group. Try it on the next cells
To see documentation and examples for a command, type a question mark ? at the end of the command name and press the tab key as
sage: KleinFourGroup?<tab>
Exercise A: What is the largest prime factor of ?
sage: factor?<tab>
In the above exercise we do not store any mathematical data for later use. This can be done in Sage with the = symbol as in:
This can be understood as Sage evaluating the expression to the right of the = sign and creating the appropriate object, and then associating that object with a label, given by the right hand side. Multiple assignments can be done simultaneously:
This allows us to swap variables directly:
Note that when we use the word variable in the computer-science sense we mean “a label associated to some data stored by Sage”. Once an object is created, some methods apply to it. This means functions but instead of writing f(my_object) you write my_object.f().:
To know all methods of an object you can still use tab-completion. Write the name of the object followed by a dot and then press tab.:
a.<tab>
Exercise B: Create the Permutation 51324 and assign it to the variable p.
sage: Permutation?<tab>
What is the inverse of p ?
Does p have the pattern 123 ? What about 1234 ? And 312 ? (even if you don’t know what a pattern is, you should be able to find a command that does this).
Exercise C: Use the matrix command to create the following matrix.
sage: matrix?<tab>
Then using methods of the matrix:
Now you know how to access the different methods of matrices:
sage: vector?<tab>
Note
Vectors in Sage are row vectors. A method such as eigenspaces might not return what you expect, so it is best to specify eigenspaces_left or eigenspaces_right instead. Same thing for kernel (left_kernel or right_kernel), and so on.
The plot command allows you to draw plots of functions. Recall that you can access the documentation by pressing the tab key after writing plot? in a cell.:
sage: plot?<tab>
Here is a simple example:
Here is a more complicated plot. Try to change every single input to the plot command in some way, evaluating to see what happens:
Above we used the show command to show a plot after it was created. You can also use P.show instead:
Try putting the cursor right after P.show( and pressing tab to get a list of the options for how you can change the values of the given inputs.:
Plotting multiple functions at once is as easy as adding them together:
Here is an example of a symbolic function:
This is an example of a function in the mathematical variable . When Sage
starts, it defines the symbol
to be a mathematical variable. If you want
to use other symbols for variables, you must define them first.:
It is possible, though, to define symbolic functions without first defining the variables.:
Exercise D: Define the symbolic function . Plot
on the
domain
and colour it red. Use the find_root method to numerically
approximate the root of
on the interval
:
Compute the tangent line to at
:
Plot and the tangent line to
at
in one image:
Exercise E (Advanced): Solve the following equation for
There are two solutions, take the one for which . (Don’t
forget to create the variables
and
!).:
Expand as a truncated Taylor series around
and containing
terms.
Do you recognize the coefficients of the Taylor series expansion? You might want to use the On-Line Encyclopedia of Integer Sequences, or better yet, Sage’s command sloane_find which queries the encyclopedia:
sloane_find?<tab>
Enter search terms or a module, class or function name.