# REU

## 3118 days ago by jimlb

This worksheet just gives a very short introduction to SAGE.  The first thing to note is that one nice thing about SAGE is that you can type latex directly into it and read it!  For example, we have the equation $E: y^2 = x^3 + 1$.  You can also select the typeset condition above and it will display the answers in latex as well.  Finally, you can copy the latex from SAGE directly into your latex file so you don't have to retype horrible messy answers.

2+2
 $\newcommand{\Bold}[1]{\mathbf{#1}}4$
f(x) = x^2
type(f(x))
 $\newcommand{\Bold}[1]{\mathbf{#1}}\verb||$
f(2)
 $\newcommand{\Bold}[1]{\mathbf{#1}}4$
E1;
 $\newcommand{\Bold}[1]{\mathbf{#1}}y^2 = x^3 + 1$

SAGE is based on the programming language python, so if you are familiar with python SAGE is very easy to use.  You definitely don't have to know anything about python to use SAGE.  One very nice feature is that once you define an object, say a polynomial $f \in \mathbb{Q}[x]$, one can get the possible commands by typing $f$. tab.  We need to declare a variable first.

var('x'); R = QQ[x];R
f = x^3 + 1;f
 $\newcommand{\Bold}[1]{\mathbf{#1}}x^{3} + 1$
f.derivative(x)
 $\newcommand{\Bold}[1]{\mathbf{#1}}3 \, x^{2}$
f.factor()
 $\newcommand{\Bold}[1]{\mathbf{#1}}{\left(x + 1\right)} {\left(x^{2} - x + 1\right)}$
P=plot(f,(0,10));
Q=plot(800,(0,10), color='red');
P+Q
var('x,y,z')
 $\newcommand{\Bold}[1]{\mathbf{#1}}\left(x, y, z\right)$
R=QQ[x,y,z];R
 $\newcommand{\Bold}[1]{\mathbf{#1}}\Bold{Q}[x, y, z]$
I = (x^2 *y, x*(y+z), y^5 * (x-z))*R;I
 $\newcommand{\Bold}[1]{\mathbf{#1}}\left(x^{2} y, x y + x z, x y^{5} - y^{5} z\right)\Bold{Q}[x, y, z]$
I.groebner_basis()
 $\newcommand{\Bold}[1]{\mathbf{#1}}\left[x z^{6}, y^{5} z + x z^{5}, x^{2} z, x y + x z\right]$

As far as I can tell there are no algorithms for SAGBI bases implemented in SAGE, so if you work on that project it might be a good idea to put that on your list of things to do.

I know very little graph theory, so Kirsti will help you more with this (or force me to learn it.)

D = Graph( { 0: [1], 1: [2], 2: [0]} )
D.show()
g=graphs.CompleteGraph(5)
g.show()
 $\newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rrrrr} 0 & 1 & 1 & 1 & 1 \\ 1 & 0 & 1 & 1 & 1 \\ 1 & 1 & 0 & 1 & 1 \\ 1 & 1 & 1 & 0 & 1 \\ 1 & 1 & 1 & 1 & 0 \end{array}\right)$
g=graphs.CompleteBipartiteGraph(2, 3)
g.show()
g.bipartite_color()
 $\newcommand{\Bold}[1]{\mathbf{#1}}\left\{0 : 1, 1 : 1, 2 : 0, 3 : 0, 4 : 0\right\}$
D = DiGraph( { 0: [1], 1: [2], 2: [0], 3:[2]} )
D.show()

There are a lot of commands related to elliptic curves implemented in SAGE.

E1=EllipticCurve([0,1]);E1
 $\newcommand{\Bold}[1]{\mathbf{#1}}y^2 = x^3 + 1$
E1.plot()
E1.torsion_subgroup()
 $\newcommand{\Bold}[1]{\mathbf{#1}}\frac{\ZZ}{6\ZZ}$
E1.torsion_polynomial(5)
 $\newcommand{\Bold}[1]{\mathbf{#1}}5 x^{12} + 380 x^{9} - 240 x^{6} - 1600 x^{3} - 256$
E2=EllipticCurve([0,3]);E2
 $\newcommand{\Bold}[1]{\mathbf{#1}}y^2 = x^3 + 3$
E2.rank()
 $\newcommand{\Bold}[1]{\mathbf{#1}}1$
L=E2.lseries()
L(1)
 $\newcommand{\Bold}[1]{\mathbf{#1}}0.000000000000000$
rho=E1.galois_representation()
rho.image_type(2);
 $\newcommand{\Bold}[1]{\mathbf{#1}}\verb|The|\phantom{\verb!x!}\verb|image|\phantom{\verb!x!}\verb|is|\phantom{\verb!x!}\verb|cyclic|\phantom{\verb!x!}\verb|of|\phantom{\verb!x!}\verb|order|\phantom{\verb!x!}\verb|2|\phantom{\verb!x!}\verb|as|\phantom{\verb!x!}\verb|there|\phantom{\verb!x!}\verb|is|\phantom{\verb!x!}\verb|exactly|\phantom{\verb!x!}\verb|one|\phantom{\verb!x!}\verb|rational|\phantom{\verb!x!}\verb|2-torsion|\phantom{\verb!x!}\verb|point.|$
rho.image_type(6);
 Traceback (click to the left of this block for traceback) ... TypeError: p (=6) must be prime. Traceback (most recent call last): File "", line 1, in File "_sage_input_51.py", line 10, in exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("cmhvLmltYWdlX3R5cGUoNik7"),globals())+"\\n"); execfile(os.path.abspath("___code___.py")) File "", line 1, in File "/tmp/tmpCKXrkU/___code___.py", line 3, in exec compile(u'rho.image_type(_sage_const_6 ); File "", line 1, in File "/usr/local/sage-5.8/local/lib/python2.7/site-packages/sage/schemes/elliptic_curves/gal_reps.py", line 902, in image_type raise TypeError, "p (=%s) must be prime."%p TypeError: p (=6) must be prime.
K.<i>=NumberField(x^2+1);K
 $\newcommand{\Bold}[1]{\mathbf{#1}}\Bold{Q}[i]/(i^{2} + 1)$
i^2
 $\newcommand{\Bold}[1]{\mathbf{#1}}-1$
b=i^3 + 1;
b.parent()
 $\newcommand{\Bold}[1]{\mathbf{#1}}\Bold{Q}[i]/(i^{2} + 1)$
i^2 + 1
 $\newcommand{\Bold}[1]{\mathbf{#1}}0$
E=EllipticCurve([3*i,-5*i]);E
 $\newcommand{\Bold}[1]{\mathbf{#1}}y^2 = x^3 + 3 ix + \left(-5 i\right)$
E.torsion_subgroup()
 $\newcommand{\Bold}[1]{\mathbf{#1}}0$
E.rank()
 $\newcommand{\Bold}[1]{\mathbf{#1}}1$

Programming is done as it is in python.  The thing to remember is indexing starts at 0, not 1!

for i in [1..10]: i^2
for p in primes(10): p
E1;
 $\newcommand{\Bold}[1]{\mathbf{#1}}y^2 = x^3 + 1$
for p in primes(20): E1.ap(p)
for n in [2..10]: E1.an(n)
E2;
 $\newcommand{\Bold}[1]{\mathbf{#1}}y^2 = x^3 + 3$
E2.rank()
 $\newcommand{\Bold}[1]{\mathbf{#1}}1$
E2.torsion_subgroup()
 $\newcommand{\Bold}[1]{\mathbf{#1}}0$
E2.rational_points(bound=20);
 $\newcommand{\Bold}[1]{\mathbf{#1}}\left[\left(0 : 1 : 0\right), \left(1 : -2 : 1\right), \left(1 : 2 : 1\right)\right]$
T=E2.gens();T
 $\newcommand{\Bold}[1]{\mathbf{#1}}\left[\left(1 : 2 : 1\right)\right]$
P=T[0]
for j in [25..35]: j*P

Suppose you needed $5P$ for your write-up.  You can copy it directly from SAGE into latex!

5*P
 $\newcommand{\Bold}[1]{\mathbf{#1}}\left(\frac{3320340721}{4218632401} : \frac{511703877377158}{274004393077351} : 1\right)$

We can also define our own functions in SAGE.

def eucl_dist((a1,b1),(a2,b2)): return N(sqrt((a1-a2)^2 + (b1-b2)^2))
eucl_dist((1,0),(0,0))
 $\newcommand{\Bold}[1]{\mathbf{#1}}1.00000000000000$
eucl_dist((1,1),(0,0))
 $\newcommand{\Bold}[1]{\mathbf{#1}}1.41421356237310$
def eucl_dist((a1,b1,c1),(a2,b2,c2)): return N(sqrt((a1-a2)^2 + (b1-b2)^2 + (c1-c2)^2))

One last fancy example. You can also define an environment where you can interact with the output.

@interact def f(t=(1/4,(1/16,1/8,..,1))): t0 = t x,y,t=var('x,y,t') show([x==(1-t^2)/(1+t^2), y==2*t/(1+t^2)]) t = t0 (x,y) = ((1-t^2)/(1+t^2), 2*t/(1+t^2)) a = 1/3 G = circle((0,0), 1, color='blue', thickness=3) G += arrow((-1-a,-t*a), (x+a,y+t*a), head=2, color='red') G += point((0,t), pointsize=150, color='black', zorder=100) G += point((-1,0), pointsize=150, color='black', zorder=100) G += point((x,y), pointsize=190, color='lightgreen', zorder=100) G += text("$(0, %s)$"%t, (-.3, t+.2), fontsize=16, color='black', zorder=200) G += text(r"$(%s,\,%s)$"%(x,y), (x+.35, y+.25), fontsize=16, color='black', zorder=200) G.show(aspect_ratio=1, ymin=-1.1, ymax=1.6, xmax=2, xmin=-1.3, fontsize=0, figsize=6)