๐ ๐ ๏ธ Basic Operations with SymPy#
Manipulating mathematical expressions is at the core of both ancient problem-solving and modern computational tools. SymPy bridges millennia of mathematical tradition with cutting-edge digital computation. Letโs dive into how these basic operations connect to the past and empower the present! ๐งฎโจ
๐ Substitution: From Babylon to Code#
Substitution, the process of replacing variables or expressions, has been foundational since the Babylonian era (~1800 BCE), when scribes solved quadratic equations by substituting and rearranging terms on clay tablets! ๐บ Today, we code it as:
Basic Swapping:
from sympy import *
x, y = symbols("x y")
expr = cos(x) + 1
expr.subs(x, y)
Historical Twist: Babylonian mathematicians might substitute specific lengths into a geometric formula for areas or volumes. Now, you can do it in one line of code. ๐ข
Evaluating Expressions at Points:
expr.subs(x, 0)
This mirrors techniques used in the Islamic Golden Age (~10th century), when scholars like Al-Khwarizmi - the founder of algebra - substituted numeric approximations into formulas for astronomical calculations ๐.
Building Complex Expressions:
expr = x**y
expr = expr.subs(y, x**y)
expr
Historical Connection: Mathematicians like Euler (18th century) used iterative substitution to study exponential growth, laying the groundwork for modern calculus. ๐
Targeted Simplifications:
expr = sin(2 * x) + cos(2 * x)
expr.subs(sin(2 * x), 2 * sin(x) * cos(x))
Symbolic manipulation like this helped 17th-century astronomers, including Newton, refine orbital mechanics by rewriting trigonometric equations. ๐
Tip
SymPy objects are immutable! Substitutions create a new expression, ensuring you donโt overwrite the original. This mirrors how historical mathematicians kept records intact for future verification. ๐
๐งโโ๏ธ String to Math Magic with sympify
#
Converting strings into symbolic expressions today echoes the shift from natural language to formal mathematical notation during the Renaissance (15th-17th centuries). Back then, scholars like Vieta introduced symbols (+
, -
, x
) to streamline problem-solving.
Example:
str_expr = "x**2 + 3*x - 1/2"
expr = sympify(str_expr)
expr.subs(x, 2)
Warning
โ ๏ธ Like an alchemistโs recipe, sympify
relies on eval
- this means it runs code from strings. Ensure your input is pureโdonโt use untrusted sources! ๐ก๏ธ
๐ Numerical Evaluation with evalf
: Precision Through Time#
From Archimedes (~250 BCE) calculating ฯ to 15 digits to todayโs computers calculating millions, numerical evaluation has always been about pushing precision. SymPyโs evalf
brings the precision game to your fingertips:
Approximate Roots:
expr = sqrt(8)
expr.evalf()
Historical Note: The ancient Greeks, like Euclid, approximated square roots geometrically. Today, evalf
does it in milliseconds. โฑ๏ธ
High-Precision ฯ:
pi.evalf(100)
Fun Fact: The Indian mathematician Madhava (14th century) calculated ฯ to 11 digits using series expansions. With SymPy, you can match his precision and go far beyond. ๐
Substitution + Precision:
expr = cos(2 * x)
expr.evalf(subs={x: 2.4})
Numerical evaluation played a critical role in designing 20th-century engineering marvels like suspension bridges ๐, where exact trigonometric values helped calculate load-bearing capacities.
โก Fast Evaluation with lambdify
: Scaling Efficiency#
In the Industrial Revolution (18th-19th centuries), engineers faced a growing need for repetitive calculations. Tables of logarithms and trigonometric values were precomputed for quick reference. Now, lambdify
brings that efficiency to modern computation:
Example:
import numpy as np
a = np.arange(10)
f = lambdify(x, sin(x), "numpy")
f(a)
array([ 0. , 0.84147098, 0.90929743, 0.14112001, -0.7568025 ,
-0.95892427, -0.2794155 , 0.6569866 , 0.98935825, 0.41211849])
Note
lambdafy is a function that takes a SymPy expression and converts it into a function that can be used with NumPy arrays. It is based on the concept of lambda functions in Python. This is a powerful but advance syntax, so donโt worry if you donโt understand it fully yet. You are not expected to know this for the course. But if you are curious, you can learn more about it here.
Warning
โ ๏ธ Like substitution, lambdify
uses eval
. Avoid unsanitized inputs, just as early engineers double-checked precomputed tables to avoid costly errors! ๐ก๏ธ