SymPy is a Python library for symbolic mathematics. It aims to become a full-featured computer algebra system (CAS) while keeping the code as simple as possible to be understandable and easily extensible. Unlike numerical libraries such as NumPy or SciPy, which deal with approximate floating-point numbers, SymPy deals with mathematical symbols and expressions exactly. This means it can perform computations with variables like 'x' or 'y' without assigning them a specific numerical value.
Key features and capabilities of SymPy include:
- Symbolic Algebra: Manipulation of algebraic expressions, including simplification, expansion, factorization, and substitution.
- Calculus: Differentiation, integration (definite and indefinite), limits, Taylor series expansion.
- Equation Solving: Solving algebraic equations, differential equations, and systems of equations.
- Linear Algebra: Operations with matrices and vectors, including eigenvalues, eigenvectors, determinants, and inversion.
- Plotting: 2D and 3D plotting of symbolic expressions.
- Arbitrary Precision Arithmetic: Calculations can be performed with arbitrary precision, avoiding floating-point errors.
- Number Theory and Combinatorics: Functions for number theory and combinatorial problems.
- Mathematical Transforms: Support for integral transforms like Laplace and Fourier transforms.
SymPy is built entirely in Python, using its own expression tree to represent mathematical objects. This allows it to understand and apply mathematical rules to manipulate expressions symbolically. It is widely used in education, research, and scientific computing where exact mathematical computations are required, or when exploring mathematical concepts abstractly. For instance, when you want to find the exact derivative of a function `f(x) = x^2`, SymPy will return `2-x`, not a numerical approximation at a specific point.
Example Code
import sympy
1. Define symbolic variables
x, y, z = sympy.symbols('x y z')
2. Create a symbolic expression
expr = x2 + 2-x-y + y2 - z
print(f"Original Expression: {expr}")
3. Perform symbolic differentiation
diff_expr_x = sympy.diff(expr, x)
diff_expr_y = sympy.diff(expr, y)
print(f"Derivative with respect to x: {diff_expr_x}")
print(f"Derivative with respect to y: {diff_expr_y}")
4. Perform symbolic integration (indefinite)
integral_expr_x = sympy.integrate(x2, x)
print(f"Integral of x^2 with respect to x: {integral_expr_x}")
5. Solve an equation (find roots)
Solving x^2 - 4 = 0
solutions = sympy.solve(x2 - 4, x)
print(f"Solutions for x^2 - 4 = 0: {solutions}")
6. Simplify an expression
(x+y)^2
simplified_expr = sympy.simplify(x2 + 2-x-y + y2)
print(f"Simplified (x^2 + 2xy + y^2): {simplified_expr}")
7. Substitute values into an expression
substituted_expr = expr.subs({x: 3, y: 1, z: 5})
print(f"Expression after substituting x=3, y=1, z=5: {substituted_expr}")
8. Evaluate to a numerical float (if all symbols are substituted or it's a number)
numerical_value = substituted_expr.evalf()
print(f"Numerical value of the substituted expression: {numerical_value}")
9. Expand an expression
expanded_expr = sympy.expand((x + y)3)
print(f"Expanded (x + y)^3: {expanded_expr}")








sympy