Sympy Studio
Powerful skill for skill, working, symbolic, mathematics. Includes structured workflows, validation checks, and reusable patterns for scientific.
Sympy Studio
Perform symbolic mathematics with SymPy, a Python library for exact computation covering algebra, calculus, linear algebra, differential equations, and number theory. This skill covers symbolic expression manipulation, equation solving, integration, series expansion, code generation, and LaTeX rendering.
When to Use This Skill
Choose Sympy Studio when you need to:
- Solve algebraic equations, systems of equations, and inequalities symbolically
- Compute derivatives, integrals, limits, and series expansions exactly
- Manipulate matrices symbolically and solve eigenvalue problems
- Generate optimized numerical code from symbolic expressions
Consider alternatives when:
- You need high-performance numerical computation (use NumPy/SciPy)
- You need computer algebra with advanced polynomial arithmetic (use Sage or Mathematica)
- You need symbolic tensor computation for physics (use cadabra or xAct)
Quick Start
pip install sympy
from sympy import * x, y, z = symbols('x y z') # Algebraic simplification expr = (x**2 - 1) / (x - 1) print(f"Simplified: {simplify(expr)}") # x + 1 # Solve equations solutions = solve(x**3 - 6*x**2 + 11*x - 6, x) print(f"Roots: {solutions}") # [1, 2, 3] # Calculus f = sin(x) * exp(-x) print(f"Derivative: {diff(f, x)}") print(f"Integral: {integrate(f, x)}") print(f"Definite integral [0,oo): {integrate(f, (x, 0, oo))}") # Limits print(f"Limit: {limit(sin(x)/x, x, 0)}") # 1 # Series expansion print(f"Taylor series: {series(cos(x), x, 0, n=6)}") # Linear algebra M = Matrix([[1, 2], [3, 4]]) print(f"Determinant: {M.det()}") print(f"Inverse:\n{M.inv()}") print(f"Eigenvalues: {M.eigenvals()}")
Core Concepts
Module Overview
| Module | Purpose | Key Functions |
|---|---|---|
sympy.core | Symbols, numbers, expressions | symbols, Rational, pi, oo |
sympy.simplify | Expression simplification | simplify, factor, expand, collect |
sympy.solvers | Equation solving | solve, solveset, linsolve, dsolve |
sympy.calculus | Calculus operations | diff, integrate, limit, series |
sympy.matrices | Linear algebra | Matrix, det, inv, eigenvals |
sympy.physics | Physics utilities | units, mechanics, quantum |
sympy.printing | Output formatting | latex, ccode, pycode |
sympy.stats | Symbolic probability | Normal, density, E, variance |
Differential Equations and Code Generation
from sympy import * # Solve ODE: y'' + 2y' + y = sin(t) t = symbols('t') y = Function('y') ode = Eq(y(t).diff(t, 2) + 2*y(t).diff(t) + y(t), sin(t)) solution = dsolve(ode) print(f"ODE solution: {solution}") # System of equations a, b, c = symbols('a b c') system = [ Eq(2*a + b - c, 1), Eq(a - b + 2*c, 3), Eq(3*a + 2*b - c, 2), ] sol = solve(system, [a, b, c]) print(f"System solution: {sol}") # Code generation: symbolic → optimized C code x, y = symbols('x y') expr = sqrt(x**2 + y**2) + sin(x*y) / (1 + exp(-x)) print("C code:") print(ccode(expr, assign_to='result')) print("\nPython/NumPy code:") from sympy.utilities.lambdify import lambdify import numpy as np f_numeric = lambdify([x, y], expr, modules=['numpy']) print(f"f(1.0, 2.0) = {f_numeric(1.0, 2.0):.6f}") # LaTeX rendering print(f"\nLaTeX: {latex(expr)}")
Configuration
| Parameter | Description | Default |
|---|---|---|
rational | Parse floats as exact rationals | False |
evaluate | Auto-simplify expressions on creation | True |
assumptions | Symbol properties (positive, real, integer) | None |
domain | Solution domain (Reals, Complexes) | Complexes |
n_digits | Precision for numerical evaluation | 15 |
order | Series expansion order | 6 |
method | ODE/integration method hint | Auto-selected |
simplify_result | Simplify solver output | True |
Best Practices
-
Declare symbol assumptions for correct results — Use
x = symbols('x', positive=True)orn = symbols('n', integer=True)when you know the domain. Without assumptions, SymPy returns general solutions that may include complex values or branches that aren't relevant to your problem. -
Use
Rationalinstead of floats for exact arithmetic —Rational(1, 3)gives exact1/3, while1/3gives0.333...and introduces floating-point errors that propagate through symbolic computation. UseS(1)/3as shorthand. Reservefloatfor final numerical evaluation only. -
Prefer
solvesetoversolvefor modern usage —solveset(expr, x, domain=S.Reals)returns a proper mathematical set and handles infinite solution sets correctly.solvereturns a list and can miss solutions.solvesetis the recommended API for SymPy 1.5+. -
Use
lambdifyto convert symbolic expressions to fast numerical functions —lambdify([x, y], expr, 'numpy')creates a function that evaluates 1000x faster than.evalf(). Use this when you need to evaluate a symbolic expression at many points (plotting, optimization, simulation). -
Simplify strategically, not blindly —
simplify()tries many strategies and can be slow. Use targeted functions:factor()for polynomials,trigsimp()for trig expressions,collect(expr, x)to group by variable. Know what form you want and use the specific transformation.
Common Issues
solve returns an empty list for valid equations — The equation may have no closed-form solution, or the solver needs a hint. Try solveset instead, or for transcendental equations, use nsolve(expr, x, initial_guess) for numerical solutions. Also check that the equation is set up correctly — solve expects expressions equal to zero, not Eq objects.
integrate hangs on complex integrals — Some integrals have no closed form or take extremely long. Set a timeout with signal handling, or try integrate(expr, x, meijerg=False) to disable the slow MeijerG algorithm. For definite integrals, fall back to scipy.integrate.quad via lambdify.
Substitution gives wrong results with floating-point arguments — expr.subs(x, 0.1) introduces floating-point into the symbolic expression. Use expr.subs(x, Rational(1, 10)) for exact substitution, or use expr.evalf(subs={x: 0.1}) for controlled numerical evaluation with specified precision.
Reviews
No reviews yet. Be the first to review this template!
Similar Templates
Full-Stack Code Reviewer
Comprehensive code review skill that checks for security vulnerabilities, performance issues, accessibility, and best practices across frontend and backend code.
Test Suite Generator
Generates comprehensive test suites with unit tests, integration tests, and edge cases. Supports Jest, Vitest, Pytest, and Go testing.
Pro Architecture Workspace
Battle-tested skill for architectural, decision, making, framework. Includes structured workflows, validation checks, and reusable patterns for development.