Learn SymPy

Symbolic mathematics in pure Python: algebra, calculus and exact arithmetic

16 lessons · 3 hours · 4 levels · covers SymPy 1.14

SymPy is a computer algebra system written in pure Python. It manipulates mathematical expressions symbolically: it simplifies, expands and factors them, solves equations, differentiates and integrates, works with exact rationals and radicals, and generates numeric code from the results. Engineers, scientists and students use it to derive formulas, check algebra and turn hand calculations into fast NumPy or C code.

Before you start

SymPy builds on these; the tree links to the exact lessons you need.

How deep do you want to go?

1. EssentialsRead and write simple code, do the everyday basics5 lessons · 49 min in total
2. PracticalUse it productively at work, with the usual idioms10 lessons · 2 hours in total
3. AdvancedThe less common features, performance and trade-offs13 lessons · 2 hours in total
4. ExpertInternals and the edge cases experts know16 lessons · 3 hours in total

The SymPy tree

SymPySymbols and expressionsSymbols andexpressionsExact numbers: Rational, sqrt, piExact numbers:Rational, sqrt, piSubstitution and numeric evaluationSubstitution andnumeric evaluationsimplify, expand, factor and friendssimplify, expand,factor and friendsSolving equationsSolving equationsDerivativesDerivativesIntegrals and limitsIntegrals and limitsSymbolic matricesSymbolic matricesAssumptions on symbolsAssumptions onsymbolslambdify: fast numeric functionslambdify: fastnumeric functionsNumbers and arithmeticNumbers andarithmeticModules and importsModules and importsDefining functionsDefining functions
Essentials and Practical shown (10 of 16 lessons); the full list is below. Click a lesson to read it.

All 16 lessons, in order

Getting started

  1. Symbols and expressions · Essentials · 8 min

    Create symbols with symbols() and combine them into expressions that stay exact and unevaluated.

  2. Exact numbers: Rational, sqrt, pi · Essentials · 9 min

    Keep arithmetic exact with SymPy integers, rationals, radicals and constants, and avoid sneaking in Python floats.

  3. Substitution and numeric evaluation · Essentials · 8 min

    Replace symbols with values or other expressions using subs, and get decimal answers with evalf and N.

Algebra and solving

  1. simplify, expand, factor and friends · Essentials · 12 min

    Rewrite expressions into the form you need with expand, factor, collect, cancel, apart and simplify.

  2. Solving equations · Essentials · 12 min

    Write equations with Eq and solve them with solveset, solve and linsolve, including systems.

  3. Assumptions on symbols · Practical · 11 min

    Declare symbols positive, real or integer so SymPy can apply identities that are only valid under those conditions.

Calculus

  1. Derivatives · Practical · 9 min

    Differentiate symbolically with diff, including higher-order, partial and unevaluated derivatives.

  2. Integrals and limits · Practical · 11 min

    Compute indefinite and definite integrals with integrate and limits with limit, and recognise when SymPy gives up.

  3. Series expansions · Advanced · 10 min

    Expand expressions into Taylor, Laurent and asymptotic series with series and work with the O() order term.

  4. Differential equations with dsolve · Advanced · 12 min

    Solve ordinary differential equations symbolically with dsolve, including initial conditions and verification.

Matrices

  1. Symbolic matrices · Practical · 12 min

    Build Matrix objects with exact or symbolic entries and compute products, inverses, determinants and eigenvalues.

Numbers and code generation

  1. lambdify: fast numeric functions · Practical · 10 min

    Turn a symbolic expression into a fast Python or NumPy function that evaluates on numbers and arrays.

  2. Printing, LaTeX and code generation · Advanced · 10 min

    Render expressions as LaTeX and pretty text, and emit C, Fortran or Python source with the code printers.

Internals and extending

  1. Expression trees: func, args, srepr · Expert · 13 min

    Understand how every SymPy object is a tree of func and args, and traverse and rewrite trees reliably.

  2. Custom functions and extending SymPy · Expert · 14 min

    Define your own symbolic functions by subclassing Function with eval, fdiff and assumption handlers.

  3. Performance: cse, evaluate=False, Poly · Expert · 13 min

    Keep symbolic computations tractable with common subexpression elimination, controlled evaluation and polynomial domains.