10 Working with Symbolic Expressions
This chapter covers
- Modeling algebraic expressions as data structures in Python
- Writing code to analyze, transform, or evaluate an algebraic expression
- Finding the derivative of a function by manipulating the expression that defines it
- Writing a Python function to take derivatives automatically
- Using the SymPy library to take integrals automatically
In your programming career, you’ve probably thought of functions as small programs. They are self-contained sets of instructions that accept some input data, do some ordered computations with it, and identify some result value as an output. In this book I’ve promoted the functional perspective, where we consider functions pieces of data themselves. You’ve now seen a number of functions that take other functions as input, or that produce functions as output.
In most of our examples so far, we’ve produced new functions from old ones by composing them, partially applying them, or calling them multiple times. What we haven’t yet covered is how to compute facts about functions. Say we have a mathematical function like:
f(x) = (3x2+x)sin(x)
We can easily translate it to Python:
from math import sin
def f(x):
return (3*x**2 + x) * sin(x)