Let's dive into the world of SymPy and see how it can be your powerful ally for mathematical operations, especially in calculus and Laplace transforms!
pip install sympy
Import SymPy: import sympy as sp
Define Symbols: You need to tell SymPy which variables are symbols (not just regular Python variables): x, y, z = sp.symbols('x y z') t, s = sp.symbols('t s') # Often used for Laplace transforms
Expressions: Create mathematical expressions using these symbols: expr = x**2 + 2*x*y + z
Basic Arithmetic: expr1 = x + y expr2 = x - y expr3 = x * y expr4 = x / y expr5 = x**y
Substitution: Replace symbols with values or other expressions: expr = x**2 + 1 result = expr.subs(x, 2) # result will be 5 new_expr = expr.subs(x, y + 1)
Simplification: SymPy can simplify expressions: expr = (x**2 - 1) / (x - 1) simplified_expr = sp.simplify(expr) # simplified_expr will be x + 1
Expansion: expr = (x + 1) * (x + 2) expanded_expr = sp.expand(expr) # expanded_expr will be x**2 + 3*x + 2
Factoring: expr = x**2 + 3*x + 2 factored_expr = sp.factor(expr) # factored_expr will be (x + 1)*(x + 2)
Solving Equations: # Solve x**2 - 4 = 0 solutions = sp.solve(x**2 - 4, x) # solutions will be [-2, 2] # Solve a system of equations: eq1 = x + y - 3 eq2 = x - y - 1 solution = sp.solve((eq1, eq2), (x, y)) # solution will be {x: 2, y: 1}
Limits: limit_expr = sp.limit(sp.sin(x) / x, x, 0) # limit_expr will be 1
Differentiation: expr = x**3 + 2*x**2 + x derivative = sp.diff(expr, x) # derivative will be 3*x**2 + 4*x + 1 second_derivative = sp.diff(expr, x, 2) # second derivative partial_derivative = sp.diff(x**2 + x*y, y) # Partial derivative w.r.t y
Integration: Indefinite Integrals: expr = 3*x**2 + 1 integral = sp.integrate(expr, x) # integral will be x**3 + x + C
Definite Integrals: integral = sp.integrate(expr, (x, 0, 2)) # integral from 0 to 2
Multiple Integrals # Double integral of x*y over a rectangle double_integral = sp.integrate(x*y, (x,0,1), (y,0,2))
Laplace Transform: f = t**2 F = sp.laplace_transform(f, t, s) # F will be (2/s**3, 0, True) # The output is a tuple: (transformed_expression, a, convergence_condition) # Usually, you only need the first element # More common use case, getting only the transformed expression: F = sp.laplace_transform(f, t, s)[0]
SymPy can handle various functions like exp, sin, cos, Heaviside (unit step), and DiracDelta (impulse). Inverse Laplace Transform: F = 2 / s**3 f = sp.inverse_laplace_transform(F, s, t) # f will be t**2
import sympy as sp
# Define symbols
t, s = sp.symbols('t s')
y = sp.Function('y')(t)
# Define the differential equation
ode = sp.diff(y, t, 2) + 4*sp.diff(y, t) + 3*y - sp.exp(-t)
# Take the Laplace transform of the ODE
ode_s = sp.laplace_transform(ode, t, s, noconds=True) # noconds=True will ignore the initial conditions.
# Substitute initial conditions using a dictionary
ics = {y.subs(t,0) : 1, sp.diff(y,t).subs(t,0) : 0}
# Laplace Transform of derivatives need to be adjusted for initial conditions
ode_s = ode_s.subs(sp.laplace_transform(sp.diff(y,t,2),t,s), s**2 * sp.laplace_transform(y,t,s) - s*ics[y.subs(t,0)] - ics[sp.diff(y,t).subs(t,0)])
ode_s = ode_s.subs(sp.laplace_transform(sp.diff(y,t),t,s), s * sp.laplace_transform(y,t,s) - ics[y.subs(t,0)])
# Solve for the Laplace transform of y(t)
Y_s = sp.solve(ode_s, sp.laplace_transform(y, t, s))[0]
# Take the inverse Laplace transform to get y(t)
y_t = sp.inverse_laplace_transform(Y_s, s, t)
# Simplify the solution
y_t = sp.simplify(y_t)
print(y_t)
sp.laplace_transform(f, t, s): Calculates the Laplace transform of f(t). sp.inverse_laplace_transform(F, s, t): Calculates the inverse Laplace transform of F(s). sp.Heaviside(t): Represents the unit step function. sp.DiracDelta(t): Represents the Dirac delta function (impulse).
Matrix Operations: M = sp.Matrix([[1, 2], [3, 4]]) M_inv = M.inv() # Inverse M_det = M.det() # Determinant M_transpose = M.transpose() eigenvalues = M.eigenvals() eigenvectors = M.eigenvects()
Assumptions: You can tell SymPy about properties of symbols, which can help with simplification and solving: x = sp.Symbol('x', real=True, positive=True) # x is a real and positive number
Series Expansion: expr = sp.sin(x) series = expr.series(x, 0, 6) # Taylor series expansion around x=0 up to order 6
Differential Equation Solving (Without Laplace): y = sp.Function('y') ode = sp.diff(y(t), t, 2) + y(t) # y'' + y = 0 solution = sp.dsolve(ode, y(t))
Plotting: SymPy has basic plotting capabilities: sp.plot(x**2, (x, -2, 2)) # Plot x**2 from x = -2 to 2 sp.plot_parametric(sp.cos(x), sp.sin(x), (x,0, 2*sp.pi)) # Parametric plot sp.plot3d(x**2 + y**2, (x,-2,2), (y,-2,2)) # 3D plot
Read the Documentation: The official SymPy documentation is excellent and comprehensive.Experiment in a Jupyter Notebook: Jupyter Notebooks are a great environment for interactive SymPy exploration.Break Down Complex Problems: Divide complicated expressions or equations into smaller, more manageable parts.Use simplify() and expand() Strategically: These functions can sometimes make expressions more or less complicated depending on what you need.Be Mindful of Assumptions: Assumptions on symbols can significantly affect the results.Combine with Other Libraries: Use SymPy alongside libraries like NumPy and Matplotlib for numerical computation and visualization.