Master SymPy

Symbolic Mathematics with Python

  • Técnico
  • English
Available in:
Deutsch English Español

Synopsis

Tired of struggling with manual mathematical calculations in your projects as a programmer? With this book you'll discover the secret to **solving complex equations and derivatives with Python**. **Master SymPy**, the library that makes symbolic algebra possible in simple and elegant code. Raúl Sierra Sánchez guides you through key concepts with a didactic tone designed for developers. You'll learn to define symbols, perform advanced mathematical operations, simplify expressions, solve systems of equations, and much more. All without leaving your favorite Python environment. This is not a traditional mathematics book: it's a practical tool that will allow you to **enhance your programming skills with computational mathematics**. It includes real examples applicable to machine learning, data science, and scientific computing. Don't let mathematics limit your code. Open the pages and start transforming the way you program with SymPy. The power is in your hands: what incredible projects will you create now?

Table of contents

  1. Introduction to SymPy and Symbolic Algebra
  2. Symbols, Numbers, and Basic Expressions
  3. Fundamental Algebraic Operations
  4. Simplification and Transformation of Expressions
  5. Substitution, Evaluation, and Conversion
  6. Solving Equations
  7. Systems of Equations
  8. Inequalities and Sets
  9. Limits and Continuity
  10. Derivatives and Differential Calculus
  11. Integrals and Series
  12. Matrices and Linear Algebra
  13. Discrete Mathematics and Combinatorics
  14. Applications in Data Science and Scientific Computing
  15. Code Generation, Performance, and Final Project
Ver todos los apartados
  1. Introduction to SymPy and Symbolic Algebra
    • What is symbolic algebra
    • Advantages over Numerical Calculation and Its Limitations
    • Installation and Configuration of SymPy
    • The SymPy Interactive Console
    • Your First Symbolic Expression
  2. Symbols, Numbers, and Basic Expressions
    • Creating symbols with symbols()
    • Assumptions and Properties of Variables
    • Numeric types: integers, rationals, reals and complexes
    • Built-in Mathematical Constants
    • Conversion Between Native Python and SymPy
  3. Fundamental Algebraic Operations
    • Symbolic Addition, Subtraction, Multiplication, and Division
    • Powers and Roots
    • Trigonometric and Exponential Functions
    • Logarithmic Functions
    • Inspecting expressions with args and func
  4. Simplification and Transformation of Expressions
    • General simplification with simplify()
    • Expansion of Products and Powers
    • Factorization and Polynomial Decomposition
    • Partial Fractions
    • Trigonometric Simplification
  5. Substitution, Evaluation, and Conversion
    • Substitution with subs()
    • Numerical Evaluation with evalf()
    • Precision and exact representation
    • Structural and Mathematical Comparison
    • Conversion of Expressions to Python Functions
  6. Solving Equations
    • Difference Between Expressions and Equations
    • Solving Equations with solve()
    • Linear and Polynomial Equations
    • Rational and Transcendental Equations
    • Interpretation and Validation of Solutions
  7. Systems of Equations
    • Construction of Symbolic Systems
    • Linear Systems with solve()
    • Efficient solving with linsolve()
    • Nonlinear Systems
    • Parameters and Conditions of Existence
  8. Inequalities and Sets
    • Symbolic Inequalities in SymPy
    • Solution with solveunivariateinequality()
    • Polynomial Inequalities
    • Compound Conditions with Symbolic Logic
    • Intervals and Sets
  9. Limits and Continuity
    • Calculating Limits with limit()
    • Lateral Limits
    • Limits at Infinity
    • Indeterminate Forms
    • Continuity and Discontinuities
  10. Derivatives and Differential Calculus
    • Derivatives with diff()
    • Higher-order derivatives
    • Partial Derivatives
    • Gradient, Jacobian, and Hessian
    • Critical Points and Basic Optimization
  11. Integrals and Series
    • Indefinite integrals with integrate()
    • Definite Integrals
    • Multiple Integrals
    • Taylor and Maclaurin Series
    • Convergence and Approximation by Series
  12. Matrices and Linear Algebra
    • Creation and Manipulation of Matrices
    • Matrix Operations
    • Determinants, Inverses, and Rank
    • Eigenvalues and Eigenvectors
    • Matrix Solution of Linear Systems
  13. Discrete Mathematics and Combinatorics
    • Factorials, Combinations, and Permutations
    • Binomial coefficients
    • Primes, Divisors, and Integer Factorization
    • Sequences and Recurrences
    • Applications to Algorithmic Problems
  14. Applications in Data Science and Scientific Computing
    • Symbolic Modeling of Physical Problems
    • Cost Functions and Optimization
    • Gradient Calculation for Machine Learning
    • Symbolic Data Analysis
    • Combining SymPy with NumPy and pandas
  15. Code Generation, Performance, and Final Project
    • Code Generation with lambdify()
    • Export to NumPy, C, and Other Languages
    • Performance Measurement and Limits of Symbolic Computation
    • Best Practices, Testing, and Error Handling
    • Final Project: Symbolic Analysis of a Model

Read the beginning

From the chapter «Introduction to SymPy and Symbolic Algebra»

If you are a developer and have had to implement complex mathematical formulas in your projects, you know what happens: an equation that solves elegantly on paper turns into a mass of numerical approximations, rounding errors, and code that is difficult to debug. SymPy was created precisely to avoid that struggle.

SymPy is an open-source Python library that lets you perform symbolic algebra (or symbolic computation). Instead of working with concrete numbers, SymPy manipulates mathematical expressions as objects that can be transformed, simplified, differentiated, integrated, or solved exactly, just as you would with pencil and paper, but in an automated and reproducible way.

This chapter will give you the foundations you need before moving forward in the book. We will see exactly what symbolic algebra is, how it differs from the numerical approach you already know (NumPy, SciPy, etc.), how to install SymPy correctly, and how to start using it in a real project. All from the perspective of a programmer who wants to integrate powerful mathematics into their code without becoming a professional mathematician.

SymPy is part of Python’s scientific ecosystem alongside NumPy, SciPy, Pandas, and Matplotlib. However, it occupies a distinct niche: while the other libraries focus on numerical computation (approximate floating-point values), SymPy works with exact symbolic representations.

This means it can:

Keep expressions like √2 or π without approximating them. Automatically simplify (x + 1)² - (x² + 2x + 1) to 0. Solve equations exactly, returning solutions such as x = -b/(2a) instead of a decimal value. Compute symbolic derivatives and integrals that you can later evaluate numerically only when needed.

For a developer, the advantage is enormous. In machine learning you can derive loss functions symbolically. In data science you can simplify expressions before implementing them in production. In scientific development you can validate that your numerical implementation matches the exact analytical solution.

SymPy is written entirely in Python, requires no external compilers, and integrates seamlessly with the rest of the ecosystem. You can convert symbolic expressions to fast numerical functions with lambdify, generate LaTeX code for documentation, or export expressions to other languages. […]

Keep reading the first chapter