Sage Quick Reference

William Stein (based on P. Jipsen’s quickref)

latest version at http://wiki.sagemath.org/quickref

GNU Free Document License, extend for your own use

Notebook Evaluate cell: ⟨shift-enter⟩ Evaluate cell creating new cell: ⟨alt-enter⟩ Split cell: ⟨control-;⟩ Joins cells: ⟨control-backspace⟩ lprint() toggle LaTeX output mode Insert cell: click on blue line between cells Insert text cell: shift-click on line between cells Delete cell: delete content then backspace

Command line com⟨tab⟩ complete command command?⟨tab⟩ shows documentation command??⟨tab⟩ shows source code a.⟨tab⟩ shows methods for object a (more: dir(a)) a._⟨tab⟩ shows hidden methods for object a search_doc('string or regexp') fulltext search of docs search_src('string or regexp') search all source code

Numbers and Rings Integers: ℤ = ZZ e.g. -2 -1 0 1 10^100

Rationals: ℚ = QQ e.g. 1/2  1/1000  314/100  -2/1
Reals: ℝ ≈ RR e.g. .5  0.001  3.14  -42.

Complex: ℂ ≈ CC e.g. 1+i 2.5-3*i Double precision: RDF and CDF

Finite fields: 𝔽_{q} = GF e.g. GF(3)(2)   GF(9,'a').0
Mod n: ℤ/nℤ = Zmod e.g. Mod(2,3)  Zmod(3)(2)

Polynomials: R[x, y] e.g. R.<x,y> = QQ[] x+2*y^3

Series: R[[t]] e.g. R.<t>=QQ[[]]    1/2 + O(t^2)

p-adic numbers: ℚ_{p}: Zp, Zq, Qp, Qq Algebraic closure: \overline{ℚ} = QQbar e.g. QQbar(2^(1/5))

Interval arithmetic: RIF e.g. sage: RIF((1,1.00001))
Number field: R.<x>=QQ[];K.<a>=NumberField(x^3+x+1)

Constants and functions Constants: π = pi e = e i = i ∞ = oo Approximate: pi.n(digits=18) = 3.14159265358979324

Functions: sin cos tan sec csc cot sinh cosh tanh
sech csch coth log ln exp
ab = a*b   \frac{a}{b} = a/b a^{b} = a^b   \sqrt{x} = sqrt(x)
\sqrt[n]{x} = x^(1/n) |x| = abs(x) log_{b}(x) = log(x,b)
Symbolic variables: e.g. t,u,v,y,z = var('t u v y z')

Define function: e.g. f(x) = x^{2} f(x)=x^2

or f=lambda x: x^2 or def f(x): return x^2

Operations on expressions

factor(...)    expand(...)    (...).simplify_...

Symbolic equations: f(x)==g(x) _is previous output _+a _-a _*a _/a manipulates equation

Solve f(x) = g(x): solve(f(x)==g(x), x)
solve([f(x,y)==0, g(x,y)==0], x,y)

find_root(f(x), a, b) find x ∈ [a, b] s.t. f(x) ≈ 0

∑_{i=k}^{n} f(i) = sum(f(i) for i in [k..n])
∏_{i=k}^{n} f(i) = prod(f(i) for i in [k..n])

Calculus

lim_{x→a} f(x) = limit(f(x), x=a)
\frac{d}{dx}(f(x)) = diff(f(x),x)
\frac{∂}{∂x}(f(x, y)) = diff(f(x,y),x)
diff = differentiate = derivative
∫ f(x)dx = integral(f(x),x)
integral = integrate
∫_{a}^{b} f(x)dx = integral(f(x),x,a,b)

Taylor polynomial, deg n about a: taylor(f(x),x,a,n)

Interactive Put @interact on line before defining function Variables defaults determine controls. E.g.,

@interact
def f(n=[1..10], s=(1..10)):
print n^s

Type interact? for details.

2d graphics line([(x_{1},y_{1}),…,(x_{n},y_{n})],options)

polygon([(x_{1},y_{1}),. . .,(x_{n},y_{n})],options)
circle((x,y),r,options)
text("txt",(x,y),options)
options as in plot.options, e.g. thickness=pixel,

rgbcolor=(r,g,b), hue=h where 0 ≤ r, b, g, h ≤ 1 show(graphic, options) use option figsize=[w,h] to adjust size use option aspect_ratio=... to adjust aspect ratio plot(f(x),(x, x_{min}, x_{max}),options)

parametric plot((f(t),g(t)),(t, t_{min}, t_{max}),options)

polar plot(f(t),(t, t_{min}, t_{max}),options)

combine graphs: circle((1,1),1)+line([(0,0),(2,2)])

animate(list of graphics objects, options).show(delay=20)

3d graphics line3d([(x_{1},y_{1},z_{1}),…,(x_{n},y_{n},z_{n})],options)

text3d('string', (x,y,z), options)
sphere((x,y,z),r,options)
tetrahedron((x,y,z),size,options)

cube((x,y,z),size,options)

octahedron((x,y,z),size,options)
dodecahedron((x,y,z),size,options)
icosahedron((x,y,z),size,options)
options e.g. aspect ratio=[1, 1, 1] color='red' opacity
plot3d(f(x, y),[x_{b},x_{e}],[y_{b},y_{e}],options)
add option plot_points=[m, n] or use plot3d adaptive
parametric plot3d((f(t),g(t),h(t)),[t_{b},t_{e}],options)
parametric plot3d((f(u, v),g(u, v),h(u, v)),

[u_{b},u_{e}],[v_{b},v_{e}],options)

Discrete math

⌊x⌋ = floor(x) ⌈x⌉ = ceil(x)

Remainder of n divided by k = n%k k|n iff n%k==0

n! = factorial(n)    \binom{x}{m} = binomial(x,m)
ϕ = golden_ratio  ϕ(n) = euler phi(n)
Strings: e.g. s = 'Hello' = "Hello" = ""+"He"+'llo'
s[0]='H'   s[-1]='o'   s[1:3]='el'   s[3:]='lo'
Lists: e.g. [1,'Hello',x] = []+[1,'Hello']+[x]

Tuples: e.g. (1,'Hello',x) (immutable) Sets: e.g. {1, 2, 1, a} = Set([1,2,1,'a']) (= {1, 2, a})

List comprehension ≈ set builder notation, e.g.

{f(x) : x ∈ X, x > 0} = Set([f(x) for x in X if x>0])

Matrix algebra

(\begin{matrix}1 \\ 2 \end{matrix}) = vector([1,2])

(\begin{matrix}1 & 2 \\ 3 & 4 \end{matrix}) = matrix(QQ,[[1,2],[3,4]])
(\begin{matrix}1 & 2 \\ 3 & 4 \end{matrix}) = matrix(QQ,2,[1,2, 3,4])
|\begin{matrix}1 & 2 \\ 3 & 4 \end{matrix}| = det(matrix(QQ,[[1,2],[3,4]]))
Av = A*v A^{−1} = A^-1 A^{t} = A.transpose()
Solve Ax = v: A\v or A.solve_right(v)
Solve xA = v: A.solve_left(v)

Reduced row echelon form: A.echelon_form()

Hessenberg form: A.hessenberg_form()

Characteristic polynomial: A.charpoly()

Gram-Schmidt: A.gram_schmidt()

Visualize: plot(A)

Methods: nrows() ncols() nullity() rank() ...

Linear algebra Vector space K^{n} = K^n e.g. QQ^3 RR^2 CC^4 Subspace: span(vectors, [field])

E.g., span([[1,2,3], [2,3,5]], QQ)

Kernel as subspace: A.kernel() Sum and intersection: V + W and V.intersection(W)

Basis: V.basis()

Restrict matrix to subspace: A.restrict(V) Vector in terms of basis: A.coordinates(v)

Number theory

Primes: prime_range(5,20), is_prime(2009)

Kronecker symbol: (\frac{a}{b}) = kronecker symbol(a,b)

Sage modules and packages import module name

module_name.⟨tab⟩ and help(module_name)
e.g.  calculus coding combinat crypto functions
games geometry graphs groups logic matrix
numerical plot probability rings sets stats

sage.module name.all.⟨tab⟩ shows exported commands Std packages: Maxima GP/PARI GAP Singular R Shell ... Opt packages: Biopython Gnuplot Kash ... %package name then use package command syntax

Timing and profiling

time command to show timing information timeit('command'): accurately time command %pdb: turn on interactive debugger (command line only) %prun command: profile command (command line only)

                                                  t = cputime(); cputime(t): elapsed CPU time
                                                  t = walltime(); walltime(t): elapsed wall time