Sage Quick Reference (Basic Math) Peter Jipsen, version 1.1 (w/modification by nu)

latest version at wiki.sagemath.org/quickref

GNU Free Document License, extend for your own use Aim: map standard math notation to Sage commands

Notebook (and commandline) Evaluate cell: ⟨shift-enter⟩ com⟨tab⟩ tries to complete command command?⟨tab⟩ shows documentation command??⟨tab⟩ shows source a.⟨tab⟩ shows all methods for object a (more: dir(a)) search doc('string or regexp') shows links to docs search src('string or regexp') shows links to source lprint() toggle L^{A}TEX output mode version() print version of Sage Insert cell: click on blue line between cells Delete cell: delete content then backspace

Numerical types Integers: ℤ = ZZ e.g. -2 -1 0 1 10^100

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

Complex: ℂ ≈ CC e.g. 1+i 2.5-3*i

Basic 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 = var('t u v y') Define function: e.g. f(x) = x^{2}

As symbolic function (can integrate, etc): f(x)=x^2 or As Python function: 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)
lim_{x→a^{−}} f(x) = limit(f(x), x=a, dir='minus')
lim_{x→a^{+}} f(x) = limit(f(x), x=a, dir='plus')
\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)

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

use option figsize=[w,h] to adjust aspect ratio plot(f(x),x_{min},x_{max},options)

parametric plot((f(t),g(t)),t_{min},t_{max},options)
polar plot(f(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)

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)

use + to combine graphics objects

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

Linear algebra

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

                                                     (\begin{matrix}1 & 2 \\ 3 & 4 \end{matrix}) = matrix([[1,2],[3,4]])

                                                     |\begin{matrix}1 & 2 \\ 3 & 4 \end{matrix}| = det(matrix([[1,2],[3,4]]))
                                                     Av = A*v A^{−1} = A^-1 A^{t} = A.transpose()
                                                     methods: nrows() ncols() nullity() rank() trace()...

Sage modules and packages

from module name import * (many preloaded)

                                                     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 Fricas(Axiom) Gnuplot Kash... %package name then use package command syntax time command to show timing information