Skip to main content

Calculus in `SymPy`

Calculus — SymPy documentation

Setting Up SymPy​

Before diving into calculus operations, initialize SymPy with symbols and functions:

from sympy import symbols, diff, integrate, limit, series, sin, exp, ln, oo

# Define symbols
x, y, z = symbols('x y z')

Differentiation​

Differentiate functions symbolically:

# Differentiate f(x) = x^2
f_prime = diff(x**2, x) # Returns 2*x

# Partial differentiation of f(x, y) = x^2 + xy
partial_f_x = diff(x**2 + x*y, x) # Returns 2*x + y
partial_f_y = diff(x**2 + x*y, y) # Returns x

Integration​

Perform both definite and indefinite integrations:

# Indefinite integration of f(x) = x^2
integral_f = integrate(x**2, x) # Returns x**3/3

# Definite integration of f(x) = x^2 from 0 to 1
definite_integral_f = integrate(x**2, (x, 0, 1)) # Returns 1/3

Limits​

Compute the limit of functions as a variable approaches a value:

# Limit of sin(x)/x as x -> 0
lim_sin_x = limit(sin(x)/x, x, 0) # Returns 1

# Limit of (1+1/x)^x as x -> infinity
lim_exp = limit((1+1/x)**x, x, oo) # Returns E

Series Expansion​

Expand functions into their Taylor series around a point:

# Keep powers below x**5; O(x**5) is the remainder order
series_exp = series(exp(x), x, 0, 5)
# Returns 1 + x + x**2/2 + x**3/6 + x**4/24 + O(x**5)

# Series expansion of ln(1+x) around 0
series_ln = series(ln(1+x), x, 0, 4)
# Returns x - x**2/2 + x**3/3 + O(x**4)

Solving Differential Equations​

SymPy can also solve differential equations symbolically. Here's a basic example:

from sympy import Function, dsolve, Eq, Derivative

# Define an unknown function and differential equation
f = Function('f')
diffeq = Eq(Derivative(f(x), x, x) - 2*Derivative(f(x), x) + f(x), sin(x))

# Solve the differential equation
sol = dsolve(diffeq, f(x))

Assumptions, exact values, and checks​

Run the snippets in order in a Python environment where SymPy is already available. They illustrate symbolic results, not a guarantee that every integral or differential equation has a closed form. An unevaluated Integral means the integration was not carried out, not that its value is zero. The official calculus tutorial explains unevaluated objects and series order terms.

Symbols are unconstrained unless assumptions are supplied. For example, simplifying r2\sqrt{r^2} to rr is wrong for negative real rr; it equals ∣r∣|r|. Use exact SymPy numbers when exact algebra is intended:

from sympy import Rational, sqrt, simplify, Abs

r = symbols('r', real=True)
p = symbols('p', positive=True)
assert sqrt(r**2) == Abs(r)
assert sqrt(p**2) == p
assert integrate(x**2, (x, 0, 1)) == Rational(1, 3)
print(Rational(1, 3).evalf(20))

Writing Python's 1/3 first creates a floating-point approximation; increasing display precision later cannot recover missing exactness. integrate(x**2, x) returns one antiderivative: the general family also has an arbitrary additive constant. Check an antiderivative by differentiating it on its domain.

For finite-point limits, check the approach direction explicitly when the sides may disagree:

assert limit(1/x, x, 0, dir='+') == oo
assert limit(1/x, x, 0, dir='-') == -oo
assert simplify(diff(integral_f, x) - x**2) == 0

The two one-sided limits here differ, so there is no two-sided limit. A Taylor result ending in O(x**5) retains powers below five; removing the order term gives a local polynomial approximation, not a globally valid equality.

The differential-equation example above has general solution f(x)=(C1+C2x)ex+12cos⁡xf(x)=(C_1+C_2x)e^x+\tfrac12\cos x. Two initial conditions are needed to determine its two constants. Verify both the equation and any initial conditions, rather than trusting the printed form:

from sympy import cos

C1, C2 = symbols('C1 C2')
g = (C1 + C2*x)*exp(x) + cos(x)/2
assert simplify(diff(g, x, 2) - 2*diff(g, x) + g - sin(x)) == 0
sol_ivp = dsolve(diffeq, f(x), ics={f(0): 0, diff(f(x), x).subs(x, 0): 0})
assert simplify(sol_ivp.rhs - ((x - 1)*exp(x) + cos(x))/2) == 0
Explore connectionsOpen network