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.

If you are browsing this document as a static web page, you can see all the
examples; however you need to copy-paste them one by one to experiment with them.
Use the `Upload worksheet` button of the notebook and copy-paste the URL of
this page to obtain an editable copy in your notebook.

If you are browsing this document as part of Sage’s live documentation, you can
play with the examples directly here; however your changes will be lost when
you close this page. Use `Copy worksheet` from the `File...` menu at the
top of this page to get an editable copy in your notebook.

Both in the live tutorial and in the notebook, you can clear all output by
selecting `Delete All Output` from the `Action...` menu next to the
`File...` menu at the top of the worksheet.

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., \(2+3\)). The first time you evaluate a cell takes longer
than subsequent times since a new Sage process is started:

```
sage: 2 + 3
5
sage: # edit here
sage: # edit here
```

To create *new input cells*, click the blue line that appears between
cells when you move your mouse around. Try it now:

```
sage: 1 + 1
2
sage: # edit here
```

You can *go back* and edit any cell by clicking in it (or using the
arrow keys on your keyboard to move up or down). Go back and change
your \(2+3\) above to \(3+3\) and re-evaluate it. An empty cell can be
*deleted* with backspace.

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$ by using dollar signs
just like in TeX or LaTeX.

There are various ways of getting help in Sage.

- navigate through the documentation (there is a link
`Help`at the top right of the worksheet), `tab`completion,- contextual help.

We detail below the latter two 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, i.e. it
differentiates upper case from lower case. Hence the `tab` completion of
`klein` won’t show you the `KleinFourGroup` command that builds the group
\(\ZZ/2 \times \ZZ/2\) as a permutation group. Try it on the next cells:

```
sage: klein<tab>
sage: Klein<tab>
```

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 in:

```
sage: KleinFourGroup?<tab>
```

```
sage: # edit here
```

Exercise A

What is the largest prime factor of \(600851475143\)?

```
sage: factor?<tab>
```

```
sage: # edit here
```

In the above manipulations we have not stored any data for
later use. This can be done in Sage with the `=` symbol as in:

```
sage: a = 3
sage: b = 2
sage: print a+b
5
```

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 left-hand side (see
the foreword of *Tutorial: Objects and Classes in Python and Sage* for
details). Multiple assignments can be done at once:

```
sage: a,b = 2,3
sage: print a,b
2 3
```

This allows us to swap the values of two variables directly:

```
sage: a,b = 2,3
sage: a,b = b,a
sage: print a,b
3 2
```

We can also assign a common value to several variables simultaneously:

```
sage: c = d = 1
sage: c, d
(1, 1)
sage: d = 2
sage: c, d
(1, 2)
```

Note that when we use the word *variable* in the computer-science sense we
mean “a label attached 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()**:

```
sage: p = 17
sage: p.is_prime()
True
```

See *Tutorial: Objects and Classes in Python and Sage* for details.
To know all methods of an object you can once more use tab-completion. Write the
name of the object followed by a dot and then press `tab`:

```
sage: a.<tab>
sage: # edit here
```

Exercise B

Create the permutation 51324 and assign it to the variable `p`.

```
sage: Permutation?<tab>
```

```
sage: # edit here
```

What is the `inverse` of `p`?

```
sage: p.inv<tab>
sage: # edit here
```

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).

```
sage: p.pat<tab>
sage: # edit here
```

Exercise C

Use the `matrix()` command to create the following matrix.

\[\begin{split}M = \left(\begin{array}{rrrr}
10 & 4 & 1 & 1 \\
4 & 6 & 5 & 1 \\
1 & 5 & 6 & 4 \\
1 & 1 & 4 & 10
\end{array}\right)\end{split}\]

```
sage: matrix?<tab>
```

```
sage: # edit here
```

Then, using methods of the matrix,

- Compute the determinant of the matrix.
- Compute the echelon form of the matrix.
- Compute the eigenvalues of the matrix.
- Compute the kernel of the matrix.
- Compute the LLL decomposition of the matrix (and lookup the documentation for what LLL is if needed!)

```
sage: # edit here
sage: # edit here
```

Now that you know how to access the different methods of matrices,

- Create the vector \(v = (1,-1,-1,1)\).
- Compute the two products: \(M\cdot v\) and \(v\cdot M\). What mathematically borderline operation is Sage doing implicitly?

```
sage: vector?<tab>
```

```
sage: # edit here
```

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>
```

```
sage: # edit here
```

Here is a simple example:

```
sage: var('x') # make sure x is a symbolic variable
x
sage: plot(sin(x^2), (x,0,10))
Graphics object consisting of 1 graphics primitive
```

Here is a more complicated plot. Try to change every single input to the plot command in some way, evaluating to see what happens:

```
sage: P = plot(sin(x^2), (x,-2,2), rgbcolor=(0.8,0,0.2), thickness=3, linestyle='--', fill='axis')
sage: show(P, gridlines=True)
```

Above we used the `show()` command to show a plot after it was created. You can
also use `P.show` instead:

```
sage: P.show(gridlines=True)
```

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.

```
sage: P.show(
```

Plotting multiple functions at once is as easy as adding them together:

```
sage: P1 = plot(sin(x), (x,0,2*pi))
sage: P2 = plot(cos(x), (x,0,2*pi), rgbcolor='red')
sage: P1 + P2
Graphics object consisting of 2 graphics primitives
```

Here is an example of a symbolic function:

```
sage: f(x) = x^4 - 8*x^2 - 3*x + 2
sage: f(x)
x^4 - 8*x^2 - 3*x + 2
sage: f(-3)
20
```

This is an example of a function in the *mathematical* variable \(x\). When Sage
starts, it defines the symbol \(x\) to be a mathematical variable. If you want
to use other symbols for variables, you must define them first:

```
sage: x^2
x^2
sage: u + v
Traceback (most recent call last):
...
NameError: name 'u' is not defined
sage: var('u v')
(u, v)
sage: u + v
u + v
```

Still, it is possible to define symbolic functions without first defining their variables:

```
sage: f(w) = w^2
sage: f(3)
9
```

In this case those variables are defined implicitly:

```
sage: w
w
```

Exercise D

Define the symbolic function \(f(x) = x \sin(x^2)\). Plot \(f\) on the
domain \([-3,3]\) and color it red. Use the `find_root()` method to
numerically approximate the root of \(f\) on the interval \([1,2]\):

```
sage: # edit here
```

Compute the tangent line to \(f\) at \(x=1\):

```
sage: # edit here
```

Plot \(f\) and the tangent line to \(f\) at \(x=1\) in one image:

```
sage: # edit here
```

Exercise E (Advanced)

Solve the following equation for \(y\):

\[y = 1 + x y^2\]

There are two solutions, take the one for which \(\lim_{x\to0}y(x)=1\). (Don’t forget to create the variables \(x\) and \(y\)!).

```
sage: # edit here
```

Expand \(y\) as a truncated Taylor series around \(0\) and containing \(n=10\) terms.

```
sage: # edit here
```

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 class `OEIS` which
queries the encyclopedia:

```
sage: oeis?<tab>
```

```
sage: # edit here
```

Congratulations for completing your first Sage tutorial!

Young’s lattice and the RSK algorithm

Enter search terms or a module, class or function name.

© Copyright 2005--2015, The Sage Development Team.
Created using Sphinx 1.2.2.