I gave an introduction to sage tutorial at the University of Warwick Computational Group seminar today, 2 November 2017. Below is a conversion of the sage/jupyter notebook I based the rest of the tutorial on. I said many things which are not included in the notebook, and during the seminar we added a few additional examples and took extra consideration to a few different calls. But for reference, the notebook is here.

The notebook itself (as a jupyter notebook) can be found and viewed on my github (link to jupyter notebook). When written, this notebook used a Sage 8.0.0.rc1 backend kernel and ran fine on the standard Sage 8.0 release , though I expect it to work fine with any recent official version of sage. The last cell requires an active notebook to be seen (or some way to export jupyter widgets to standalone javascript or something; this either doesn't yet exist, or I am not aware of it).

I will also note that I converted the notebook for display on this website using jupyter's nbconvert package. I have some CSS and syntax coloring set up that affects the display.

Good luck learning sage, and happy hacking.

# Sage¶

Sage (also known as SageMath) is a general purpose computer algebra system written on top of the python language. In Mathematica, Magma, and Maple, one writes code in the mathematica-language, the magma-language, or the maple-language. Sage is python. But no python background is necessary for the rest of today's guided tutorial. The purpose of today's tutorial is to give an indication about how one really*uses*sage, and what might be available to you if you want to try it out. I will spoil the surprise by telling you upfront the two main points I hope you'll take away from this tutorial.

- With tab-completion and documentation, you can do many things in sage without ever having done them before.
- The ecosystem of libraries and functionality available in sage is tremendous, and (usually) pretty easy to use.

## Lightning Preview¶

Let's first get a small feel for sage by seeing some standard operations and what typical use looks like through a series of trivial, mostly unconnected examples.```
# Fundamental manipulations work as you hope
2+3
```

```
>>> 3-2
1
>>> 2*3
6
>>> 2^3
8
>>> 2**3 # (also exponentiation)
8
```

There is an order of operations, but these things work pretty much as you want them to work. You might try out several different operations.
```
factor(-1008)
```

```
list(factor(1008))
```

```
sin(pi)
```

```
exp(2)
```

`N()`

.
```
N(exp(2))
```

```
pi
```

```
N(pi)
```

`digits`

keyword to `N()`

.
```
N(pi, digits=60)
```

```
sqrt(2)
```

```
sqrt(2)**2
```

`range`

function in python counts up to a given number, starting at 0.
```
range(16)
```

```
A = matrix(4,4, range(16))
A
```

```
B = matrix(4,4, range(-5, 11))
B
```

```
A*B
```

`.`

, and then call the function.
```
A.charpoly()
```

```
factor(A.charpoly())
```

```
A.
```

and hit `<Tab>`

. Sage should display a list of things it thinks it can do to the matrix A.
### Warning¶

Note that on CoCalc or external servers, tab completion sometimes has a small delay.```
A.
```

`A.eigenvalues()`

, then type
```
A.eigenvalues?
```

and hit enter.
```
A.eigenvalues?
```

```
A.eigenvalues()
```

```
A.eigenvalues??
```

which will also show you the implementation of that functionality. (You usually don't need this).
```
A.eigenvalues??
```

```
E = EllipticCurve([1,2,3,4,5])
E
```

```
# Tab complete me to see what's available
E.
```

```
E.conductor()
```

```
E.rank()
```

`i`

or `I`

to mean a $\sqrt{-1}$.
```
(1+2*I) * (pi - sqrt(5)*I)
```

```
c = 1/(sqrt(3)*I + 3/4 + sqrt(29)*2/3)
```

`c`

is stored with perfect representations of square roots.
```
c
```

`N()`

on them.
```
N(c)
```

```
N(c, 20) # Keep 20 "bits" of information
```

`latex(<object>)`

to give a latex representation.
```
latex(c)
```

```
latex(E)
```

```
latex(A)
```

`pretty_print`

```
pretty_print(A)
```

## Basic Algebra¶

```
H = DihedralGroup(6)
H.list()
```

```
a = H[1]
a
```

```
a.order()
```

```
b = H[2]
b
```

```
a*b
```

```
for elem in H:
if elem.order() == 2:
print elem
```

```
# Or, in the "pythonic" way
elements_of_order_2 = [elem for elem in H if elem.order() == 2]
elements_of_order_2
```

```
rand_elem = H.random_element()
rand_elem
```

```
G_sub = H.subgroup([rand_elem])
G_sub
```

```
# Explicitly using elements of a group
H("(1,2,3,4,5,6)") * H("(1,5)(2,4)")
```

### Exercises¶

The real purpose of these exercises are to show you that it's often possible to use tab-completion to quickly find out what is and isn't possible to do within sage.- What things does sage know about this subgroup? Can you find the cardinality of the subgroup? (Note that the subgroup is generated by a random element, and your subgroup might be different than your neighbor's). Can you list all subgroups of the dihedral group in sage?
- Sage knows other groups as well. Create a symmetric group on 5 elements. What does sage know about that? Can you verify that S5 isn't simple? Create some cyclic groups?

## Changing the Field¶

It's pretty easy to work over different fields in sage as well. I show a few examples of this```
# It may be necessary to use `reset('x')` if x has otherwise been defined
K.<alpha> = NumberField(x**3 - 5)
```

```
K
```

```
alpha
```

```
alpha**3
```

```
(alpha+1)**3
```

```
GF?
```

```
F7 = GF(7)
```

```
a = 2/5
a
```

```
F7(a)
```

```
var('x')
```

```
eqn = x**3 + sqrt(2)*x + 5 == 0
a = solve(eqn, x)[0].rhs()
```

```
a
```

```
latex(a)
```

```
pretty_print(a)
```

```
# Also RR, CC
QQ
```

```
K.<b> = QQ[a]
```

```
K
```

```
a.minpoly()
```

```
K.class_number()
```

### Exercise¶

Sage tries to keep the same syntax even across different applications. Above, we factored a few integers. But we may also try to factor over a number field. You can factor 2 over the Gaussian integers by:- Create the Gaussian integers. The constructor CC[I] works.
- Get the Gaussian integer 2 (which is programmatically different than the typical integer 2), by something like
`CC[I](2)`

. `factor`

that 2.

## Some Calculus And Symbolic Manipulation¶

```
# Let's declare that we want x and y to mean symbolic variables
x = 1
y = 2
print(x+y)
reset('x')
reset('y')
var('x')
var('y')
print(x+y)
```

```
solve(x^2 + 3*x + 2, x)
```

```
solve(x^2 + y*x + 2 == 0, x)
```

```
# Nonlinear systems with complicated solutions can be solved as well
var('p,q')
eq1 = p+1==9
eq2 = q*y+p*x==-6
eq3 = q*y**2+p*x**2==24
s = solve([eq1, eq2, eq3, y==1], p,q,x,y)
s
```

```
s[0]
```

```
latex(s[0])
```

```
# We can also do some symbolic calculus
f = x**2 + 2*x + 1
f
```

```
diff(f, x)
```

```
integral(f, x)
```

```
F = integral(f, x)
F(x=1)
```

```
diff(sin(x**3), x)
```

```
# Compute the 4th derivative
diff(sin(x**3), x, 4)
```

```
# We can try to foil sage by giving it a hard integral
integral(sin(x)/x, x)
```

```
f = sin(x**2)
f
```

```
# And sage can give Taylor expansions
f.taylor(x, 0, 20)
```

```
f(x,y)=y^2+1-x^3-x
contour_plot(f, (x,-pi,pi), (y,-pi,pi))
```

```
contour_plot(f, (x,-pi,pi), (y,-pi,pi), colorbar=True, labels=True)
```

```
# Implicit plots
f(x,y) = -x**3 + y**2 - y + x + 1
implicit_plot(f(x,y)==0,(x,0,2*pi),(y,-pi,pi))
```

### Exercises¶

- Experiment with the above examples by trying out different functions and plots.
- Sage can do partial fractions for you as well. To do this, you first define your function you want to split up. Suppose you call it
`f`

. Then you use`f`

.partial_fraction(x). Try this out - Sage can also create 3d plots. Create one. Start by looking at the documentation for
`plot3d`

.

```
@interact
def g(f=sin(x), c=0, n=(1..30),
xinterval=range_slider(-10, 10, 1, default=(-8,8), label="x-interval"),
yinterval=range_slider(-50, 50, 1, default=(-3,3), label="y-interval")):
x0 = c
degree = n
xmin,xmax = xinterval
ymin,ymax = yinterval
p = plot(f, xmin, xmax, thickness=4)
dot = point((x0,f(x=x0)),pointsize=80,rgbcolor=(1,0,0))
ft = f.taylor(x,x0,degree)
pt = plot(ft, xmin, xmax, color='red', thickness=2, fill=f)
show(dot + p + pt, ymin=ymin, ymax=ymax, xmin=xmin, xmax=xmax)
html('$f(x)\;=\;%s$'%latex(f))
html('$P_{%s}(x)\;=\;%s+R_{%s}(x)$'%(degree,latex(ft),degree))
```

### Additional Resources and Comments¶

There are a variety of tutorials and resources for learning more about sage. I list several here.- Sage provides some tutorials of its own. These include its Guided Tour and the Standard Sage Tutorial. The Standard Sage Tutorial is designed to take 2-4 hours to work through, and afterwards you should have a pretty good sense of the sage environment.
- PREP Tutorials are a set of tutorials created in a program sponsored by the Mathematics Association of America, aimed at working with university students with sage. These tutorials are designed for people both new to sage and to programming.

## Info on how to comment

To make a comment, please send an email using the button below. Your email address

won't be shared(unless you include it in the body of your comment). If you don't want your real name to be used next to your comment, please specify the name you would like to use. If you want your name to link to a particular url, include that as well.bold,italics, and plain text are allowed in comments. A reasonable subset of markdown is supported, including lists, links, and fenced code blocks. In addition, math can be formatted using`$(inline math)$`

or`$$(your display equation)$$`

.Please use plaintext emailwhen commenting. See Plaintext Email and Comments on this site for more. Note also thatcomments are expected to be open, considerate, and respectful.