Sage Quick Reference William Stein (based on work of P. Jipsen) GNU Free Document License, extend for your own use Notebook Evaluate cell: shift-enter Evaluate cell creating new cell: alt-enter Split cell: control-; Join cells: control-backspace Insert math cell: click blue line between cells Insert text/HTML cell: shift-click blue line between cells Delete cell: delete content then backspace Command line com tab complete command *bar*? list command names containing “bar” 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 source code _ is previous output Numbers Integers: Z = ZZ e.g. -2 -1 0 1 10^100 Rationals: Q = QQ e.g. 1/2 1/1000 314/100 -2/1 Reals: R ≈ RR e.g. .5 0.001 3.14 1.23e10000 Complex: C ≈ CC e.g. CC(1,1) CC(2.5,-3) Double precision: RDF and CDF e.g. CDF(2.1,3) Mod n: Z/nZ = Zmod e.g. Mod(2,3) Zmod(3)(2) Finite fields: Fq = GF e.g. GF(3)(2) GF(9,"a").0 Polynomials: R[x, y] e.g. S.=QQ[] x+2*y^3 Series: R[[t]] e.g. S.=QQ[[]] 1/2+2*t+O(t^2) p-adic numbers: Zp ≈Zp, Qp ≈Qp e.g. 2+3*5+O(5^2) Algebraic closure: Q = QQbar e.g. QQbar(2^(1/5)) Interval arithmetic: RIF e.g. sage: RIF((1,1.00001)) Number field: R.=QQ[];K.=NumberField(x^3+x+1) Arithmetic ab = a*b a b = a/b ab = a^b √ x = sqrt(x) n √ x = x^(1/n) |x| = abs(x) logb(x) = log(x,b) Sums: n i=k f(i) = sum(f(i) for i in (k..n)) Products: n i=k f(i) = prod(f(i) for i in (k..n)) Constants and functions Constants: π = pi e = e i = i ∞ = oo φ = golden_ratio γ = euler_gamma Approximate: pi.n(digits=18) = 3.14159265358979324 Functions: sin cos tan sec csc cot sinh cosh tanh sech csch coth log ln exp ... Python function: def f(x): return x^2 Interactive functions Put @interact before function (vars determine controls) @interact def f(n=[0..4], s=(1..5), c=Color("red")): var("x");show(plot(sin(n+x^s),-pi,pi,color=c)) Symbolic expressions Define new symbolic variables: var("t u v y z") Symbolic function: e.g. f(x) = x2 f(x)=x^2 Relations: f==g f<=g f>=g fg Solve f = g: solve(f(x)==g(x), x) solve([f(x,y)==0, g(x,y)==0], x,y) factor(...) expand(...) (...).simplify_... find_root(f(x), a, b) find x ∈ [a, b] s.t. f(x) ≈ 0 Calculus lim x→a f(x) = limit(f(x), x=a) d dx (f(x)) = diff(f(x),x) ∂ ∂x (f(x, y)) = diff(f(x,y),x) diff = differentiate = derivative f(x)dx = integral(f(x),x) b a f(x)dx = integral(f(x),x,a,b) b a f(x)dx ≈ numerical_integral(f(x),a,b) Taylor polynomial, deg n about a: taylor(f(x),x,a,n) 2D graphics -6 -4 -2 2 4 6 -1 -0.75 -0.5 -0.25 0.25 0.5 0.75 1 line([(x1,y1),. . .,(xn,yn)],options) polygon([(x1,y1),. . .,(xn,yn)],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 figsize=[w,h] to adjust size use aspect_ratio=number to adjust aspect ratio plot(f(x),(x, xmin, xmax),options) parametric plot((f(t),g(t)),(t, tmin, tmax),options) polar plot(f(t),(t, tmin, tmax),options) combine: circle((1,1),1)+line([(0,0),(2,2)]) animate(list of graphics, options).show(delay=20) 3D graphics line3d([(x1,y1,z1),. . .,(xn,yn,zn)],options) sphere((x,y,z),r,options) text3d("txt", (x,y,z), 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) plot3d(f(x, y),(x, xb, xe), (y, yb, ye),options) parametric plot3d((f,g,h),(t, tb, te),options) parametric plot3d((f(u, v),g(u, v),h(u, v)), (u, ub, ue),(v, vb, ve),options) options: aspect ratio=[1, 1, 1], color="red" opacity=0.5, figsize=6, viewer="tachyon" 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) x m = binomial(x,m) φ(n) = euler phi(n) Strings: e.g. s = "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]) Graph theory Graph: G = Graph({0:[1,2,3], 2:[4]}) Directed Graph: DiGraph(dictionary) Graph families: graphs. tab Invariants: G.chromatic polynomial(), G.is planar() Paths: G.shortest path() Visualize: G.plot(), G.plot3d() Automorphisms: G.automorphism group(), G1.is isomorphic(G2), G1.is subgraph(G2) Combinatorics Integer sequences: sloane find(list), sloane. tab Partitions: P=Partitions(n) P.count() Combinations: C=Combinations(list) C.list() Cartesian product: CartesianProduct(P,C) Tableau: Tableau([[1,2,3],[4,5]]) Words: W=Words("abc"); W("aabca") Posets: Poset([[1,2],[4],[3],[4],[]]) Root systems: RootSystem(["A",3]) Crystals: CrystalOfTableaux(["A",3], shape=[3,2]) Lattice Polytopes: A=random_matrix(ZZ,3,6,x=7) L=LatticePolytope(A) L.npoints() L.plot3d() Matrix algebra 1 2 = vector([1,2]) 1 2 3 4 = matrix(QQ,[[1,2],[3,4]], sparse=False) 1 2 3 4 5 6 = matrix(QQ,2,3,[1,2,3, 4,5,6]) 1 2 3 4 = det(matrix(QQ,[[1,2],[3,4]])) Av = A*v A−1 = A^-1 At = 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() Rank and nullity: A.rank() A.nullity() Hessenberg form: A.hessenberg_form() Characteristic polynomial: A.charpoly() Eigenvalues: A.eigenvalues() Eigenvectors: A.eigenvectors_right() (also left) Gram-Schmidt: A.gram_schmidt() Visualize: A.plot() LLL reduction: matrix(ZZ,...).LLL() Hermite form: matrix(ZZ,...).hermite_form() Linear algebra Vector space Kn = 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: A.right_kernel() (also left) Sum and intersection: V + W and V.intersection(W) Basis: V.basis() Basis matrix: V.basis_matrix() Restrict matrix to subspace: A.restrict(V) Vector in terms of basis: V.coordinates(vector) Numerical mathematics Packages: import numpy, scipy, cvxopt Minimization: var("x y z") minimize(x^2+x*y^3+(1-z)^2-1, [1,1,1]) Number theory Primes: prime_range(n,m), is_prime, next_prime Factor: factor(n), qsieve(n), ecm.factor(n) Kronecker symbol: a b = kronecker symbol(a,b) Continued fractions: continued_fraction(x) Bernoulli numbers: bernoulli(n), bernoulli mod p(p) Elliptic curves: EllipticCurve([a1, a2, a3, a4, a6]) Dirichlet characters: DirichletGroup(N) Modular forms: ModularForms(level, weight) Modular symbols: ModularSymbols(level, weight, sign) Brandt modules: BrandtModule(level, weight) Modular abelian varieties: J0(N), J1(N) Group theory G = PermutationGroup([[(1,2,3),(4,5)],[(3,4)]]) SymmetricGroup(n), AlternatingGroup(n) Abelian groups: AbelianGroup([3,15]) Matrix groups: GL, SL, Sp, SU, GU, SO, GO Functions: G.sylow subgroup(p), G.character table(), G.normal subgroups(), G.cayley graph() Noncommutative rings Quaternions: Q. = QuaternionAlgebra(a,b) Free algebra: R. = FreeAlgebra(QQ, 3) Python modules import module name module_name. tab and help(module_name) Profiling and debugging time command: show timing information timeit("command"): accurately time command t = cputime(); cputime(t): elapsed CPU time t = walltime(); walltime(t): elapsed wall time %pdb: turn on interactive debugger (command line only) %prun command: profile command (command line only)