avatarMathcube

Summary

The web content provides an overview of working with piecewise functions in Python using the sympy library, detailing how to define, plot, differentiate, integrate, and manipulate these functions.

Abstract

The article "Piecewise Functions in Python’s sympy" serves as a guide for handling piecewise functions within the scientific Python ecosystem, specifically utilizing the sympy library. It introduces the Piecewise class as a tool for defining functions over distinct domains, demonstrating how to represent such functions in sympy. The article further illustrates the capabilities of sympy for plotting piecewise functions, highlighting the visualization of transition points and discontinuities. It also covers operations such as differentiation and integration of piecewise functions, maintaining their piecewise nature, and provides insights into manipulating these functions, including power series expansion and multiplication by expressions. The author emphasizes the intuitive and straightforward approach sympy offers for working with piecewise-defined functions, making complex mathematical operations more accessible to Python users.

Opinions

  • The author suggests that sympy's Piecewise class simplifies the process of working with piecewise functions, making it both easy and intuitive.
  • It is noted that sympy's plotting backend, spb, is particularly adept at plotting piecewise functions, including clear markers for transitions and discontinuities.
  • The article conveys that piecewise functions can be manipulated algebraically in sympy much like normal functions, which includes differentiation, integration, and series expansion while preserving the piecewise structure.
  • The author points out a common pitfall when multiplying a piecewise function by an expression and offers a solution using the piecewise_fold function to maintain the correct piecewise structure.
  • The author encourages readers to explore and experiment with piecewise functions in sympy, indicating a belief in the value of hands-on experience for learning and understanding.
  • A subtle endorsement is made for becoming a Medium member, suggesting that access to such content is valuable and that supporting writers can contribute to the creation of more useful articles.

Piecewise Functions in Python’s sympy

Your Daily Dose of Scientific Python

Photo by Jasmin Sessler on Unsplash

In many applications, we are faced with functions that are defined piecewise. For instance, consider the function

sympy offers an easy and intuitive way to work with functions like that: the Piecewise class. For each case in the piecewise function, we define a pair (2-tuple) with the function on the subdomain (e.g. 3−𝑥²) and a condition specifying the subdomain (e.g. |𝑥|<1). For the example above, we would write

We can easily plot piecewise functions using plot_piecewise from the sympy plotting backends spb:

Note that plot_piecewise not only marks the transition points between two cases but also discontinuities (if present) are displayed nicely.

We can work on piecewise-defined functions like normal functions. For example, we differentiate them:

We can expand them in a power series, which keeps the piecewise structure:

Or we can integrate them. For a definite integral, we just say

For an indefinite integral, we can use

and each case is handled separately.

Often, we don’t define piecewise functions ourselves, but they are output of some operation. Then we often want to get access to the different parts of the function. This can be obtained by the method as_expr_set_pairs:

Evaluating piecewise function work just like normal expressions, using subs. It behaves as one would intuitively expect. Substituting 𝑥=0 just returns the single function value for the subdomain in which 𝑥=0 lies:

However, if you substitute 𝑥 for another symbol, say 𝑦, we retain the piecewise function structure:

Finally, what if you want to multiply a piecewise function by some expression? The intuitive approach doesn’t work:

Use the function piecewise_fold instead! Here, we get

That covers the typical use cases of piecewise functions. Have fun playing!

If you found this article useful, you may want to consider becoming a Medium member to get unlimited access to all Medium articles. By registering using this link you can even support me as a writer.

Mathematics
Python
Data Science
Science
Jupyter Notebook
Recommended from ReadMedium