Sage Quick Reference: Calculus William Stein Sage Version 3.4

http://wiki.sagemath.org/quickref

GNU Free Document License, extend for your own use

Builtin constants and functions Constants: π = pi e = e i = I = i

∞ = oo = infinity NaN=NaN log(2) =log2
ϕ = golden_ratio γ = euler_gamma
0.915 ≈ catalan 2.685 ≈ khinchin
0.660 ≈ twinprime 0.261 ≈ merten 1.902 ≈ brun

Approximate: pi.n(digits=18) = 3.14159265358979324

Builtin functions:  sin cos tan sec csc cot sinh
cosh tanh sech csch coth log ln exp ...

Defining symbolic expressions Create symbolic variables:

var("t u theta") or var("t,u,theta")

Use * for multiplication and ^for exponentiation:

2x^{5} + \sqrt{2} = 2*x^5 + sqrt(2)
Typeset: show(2*theta^5 + sqrt(2)) ⟶ 2θ^{5} + \sqrt{2}

Symbolic functions Symbolic function (can integrate, differentiate, etc.):

f(a,b,theta) = a + b*theta^2

Also, a “formal” function of theta:

f = function('f',theta)

Piecewise symbolic functions:

Piecewise([[(0,pi/2),sin(1/x)],[(pi/2,pi),x^2+1]])

Python functions Defining:

def f(a, b, theta=1):
c = a + b*theta^2
return c

Inline functions:

f = lambda a, b, theta = 1: a + b*theta^2

Simplifying and expanding Below f must be symbolic (so not a Python function):

Simplify: f.simplify_exp(), f.simplify_full(),
f.simplify_log(), f.simplify_radical(),
f.simplify_rational(), f.simplify_trig()
Expand: f.expand(), f.expand_rational()

Equations Relations: f = g: f == g, f ≠ g: f != g,

f ≤ g: f <= g, f ≥ g: f >= g, f < g: f < g, f > g: f > g

Solve f = g: solve(f == g, x), and
solve([f == 0, g == 0], x,y)
solve([x^2+y^2==1, (x-1)^2+y^2==1],x,y)

Solutions:

S = solve(x^2+x+1==0, x, solution_dict=True)
S[0]["x"]   S[1]["x"] are the solutions
Exact roots: (x^3+2*x+1).roots(x)
Real roots:  (x^3+2*x+1).roots(x,ring=RR)
Complex roots: (x^3+2*x+1).roots(x,ring=CC)

Factorization

Factored form: (x^3-y^3).factor()

List of (factor, exponent) pairs:

(x^3-y^3).factor_list()

Limits

lim_{x→a} f(x) = limit(f(x), x=a)
limit(sin(x)/x, x=0)
lim_{x→a^{+}} f(x) = limit(f(x), x=a, dir='plus')
limit(1/x, x=0, dir='plus')
lim_{x→a^{−}} f(x) = limit(f(x), x=a, dir='minus')
limit(1/x, x=0, dir='minus')

Derivatives

\frac{d}{dx}(f(x)) = diff(f(x),x) = f.diff(x)
\frac{∂}{∂x}(f(x, y)) = diff(f(x,y),x)
diff = differentiate = derivative
diff(x*y + sin(x^2) + e^(-x), x)

Integrals

∫ f(x)dx = integral(f,x) = f.integrate(x)
   integral(x*cos(x^2), x)
∫_{a}^{b} f(x)dx = integral(f,x,a,b)
   integral(x*cos(x^2), x, 0, sqrt(pi))
∫_{a}^{b} f(x)dx ≈ numerical_integral(f(x),a,b)[0]
   numerical_integral(x*cos(x^2),0,1)[0]

assume(...): use if integration asks a question

   assume(x>0)

Taylor and partial fraction expansion Taylor polynomial, deg n about a: taylor(f,x,a,n)≈ c_{0} + c_{1}(x − a) + ⋯ + c_{n}(x − a)^{n}

   taylor(sqrt(x+1), x, 0, 5)

Partial fraction:

(x^2/(x+1)^3).partial_fraction()

Numerical roots and optimization

Numerical root: f.find_root(a, b, x)
   (x^2 - 2).find_root(1,2,x)

Maximize: find (m, x_{0}) with f(x_{0}) = m maximal

    f.find_maximum_on_interval(a, b, x)

Minimize: find (m, x_{0}) with f(x_{0}) = m minimal

    f.find_minimum_on_interval(a, b, x)

Minimization: minimize(f, start point)

   minimize(x^2+x*y^3+(1-z)^2-1, [1,1,1])

Multivariable calculus

Gradient: f.gradient() or f.gradient(vars)
    (x^2+y^2).gradient([x,y])
Hessian: f.hessian()
    (x^2+y^2).hessian()

Jacobian matrix: jacobian(f, vars)

    jacobian(x^2 - 2*x*y, (x,y))

Summing infinite series

∑_{n=1}^{∞} \frac{1}{n^{2}} = \frac{π^{2}}{6}

Not yet implemented, but you can use Maxima:

s = 'sum (1/n^2,n,1,inf), simpsum'
SR(sage.calculus.calculus.maxima(s)) ⟶ π^{2}/6