Skill

Perform Symbolic Math and Generate Code with SymPy

A skill for exact symbolic mathematics in Python's SymPy - algebra, calculus, linear algebra, physics, and code generation.


91
Spark score
out of 100
Updated last month
Version 1.0.0

Add to Favorites

Why it matters

Leverage SymPy for precise symbolic mathematics in Python, enabling exact calculations for algebra, calculus, and physics. This skill also facilitates converting mathematical expressions into executable code for numerical evaluation or integration into other applications.

Outcomes

What it gets done

01

Solve algebraic, differential, and systems of equations symbolically.

02

Perform symbolic calculus operations like differentiation, integration, and limits.

03

Manipulate and simplify complex mathematical expressions.

04

Generate executable Python, C, or Fortran code from symbolic expressions.

Install

Add it to your toolbox

Run in your project directory:

curl -fsSL https://spark.entire.vc/get/ag-sympy | bash

Overview

SymPy - Symbolic Mathematics in Python

A skill for exact symbolic mathematics in Python's SymPy library, covering algebra, calculus, linear algebra, physics/mechanics, and conversion to executable code or LaTeX. Use it for symbolic (exact) mathematical computation and code generation from expressions, not for general numerical computing beyond its NumPy/SciPy integration points.

What it does

This skill provides comprehensive guidance for exact symbolic computation in Python using SymPy, covering symbolic algebra, calculus, linear algebra, equation solving, physics, and code generation - producing exact results like sqrt(2) rather than floating-point approximations. Symbolic computation basics cover creating symbols with type assumptions (real, positive, integer) and simplifying/manipulating expressions via simplify, expand, factor, and cancel. Calculus operations cover derivatives (including partial and higher-order), definite/indefinite/improper integrals, limits, and Taylor series expansion. Equation solving covers algebraic equations via solveset/solve, linear and nonlinear systems via linsolve/nonlinsolve, and differential equations via dsolve. Matrix and linear algebra operations cover matrix creation, inversion, determinant, transpose, eigenvalues/eigenvectors, diagonalization, and solving linear systems Ax = b. Physics and mechanics coverage spans classical mechanics (Lagrangian methods via dynamicsymbols and LagrangesMethod), vector analysis (reference frames, dot/cross products), and quantum mechanics (kets, bras, commutators). Advanced mathematics topics include geometry (2D/3D analytic geometry, transformations), number theory (primes, factorization, modular arithmetic, Diophantine equations), combinatorics, logic/set theory, statistics (distributions, expectation, variance), special functions (Gamma, Bessel, orthogonal polynomials), and polynomial algebra including Groebner bases. Code generation and output covers converting symbolic expressions into fast NumPy functions via lambdify, generating C/Fortran source via codegen, and producing LaTeX output for documents. Best practices include always defining symbols before use, adding assumptions for cleaner simplification results (e.g. positive=True making sqrt(x**2) simplify to x instead of Abs(x)), using Rational/S for exact arithmetic instead of floating-point literals, calling .evalf() only when numerical results are actually needed, converting to lambdify'd NumPy functions for performance in loops instead of repeated subs()/evalf(), and choosing the right solver (solveset for algebraic, linsolve/nonlinsolve for systems, dsolve for differential equations, solve as a flexible legacy fallback). It demonstrates integration patterns with NumPy (vectorized evaluation via lambdify), Matplotlib (plotting a lambdified expression), and SciPy (fsolve for numerical root-finding on a symbolically defined equation), plus common use-case patterns like solve-and-verify and a full symbolic-to-numeric pipeline. A troubleshooting section covers common errors: undefined symbol NameErrors, unexpected numerical results from floating-point literals instead of Rational, slow loop performance from repeated substitution instead of lambdify, equations that won't solve algebraically (try alternate solvers or numerical methods), and simplification not working as expected (try different simplify functions or add assumptions).

When to use - and when NOT to

Use it when solving equations symbolically, performing calculus operations, manipulating algebraic expressions, working with matrices symbolically, doing physics calculations, number theory or geometric computations, converting expressions to executable code, generating LaTeX output, or needing exact rather than approximate mathematical results. It is specific to SymPy's API and symbolic-math paradigm, not general Python numerical computing (NumPy/SciPy) beyond the demonstrated integration points.

Inputs and outputs

Inputs: a mathematical expression, equation, matrix, or physics problem to solve or manipulate symbolically.
Outputs: exact symbolic results (solutions, derivatives, integrals, matrix decompositions), or converted output (a fast NumPy function via lambdify, generated C/Fortran code, or LaTeX-formatted expressions).

from sympy import symbols, diff, integrate, solve
x = symbols('x')
solve(x**2 - 5*x + 6, x)      # [2, 3]
diff(sin(x**2), x)            # 2*x*cos(x**2)
integrate(x*exp(-x**2), (x, 0, oo))  # 1/2

Who it's for

Developers, scientists, and students who need exact symbolic mathematics in Python - algebra, calculus, physics, or code generation from mathematical expressions - rather than floating-point numerical approximation.

Source README

SymPy is a Python library for symbolic mathematics that enables exact computation using mathematical symbols rather than numerical approximations. This skill provides comprehensive guidance for performing symbolic algebra, calculus, linear algebra, equation solving, physics calculations, and code generation using SymPy.

FAQ

Common questions

Discussion

Questions & comments · 0

Sign In Sign in to leave a comment.