๐Ÿ“– ๐Ÿ› ๏ธ 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:

  1. Basic Swapping:

from sympy import *

x, y = symbols("x y")

expr = cos(x) + 1
expr.subs(x, y)
\[\displaystyle \cos{\left(y \right)} + 1\]

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. ๐Ÿ”ข

  1. Evaluating Expressions at Points:

expr.subs(x, 0)
\[\displaystyle 2\]

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 ๐ŸŒŒ.

  1. Building Complex Expressions:

expr = x**y
expr = expr.subs(y, x**y)

expr
\[\displaystyle x^{x^{y}}\]

Historical Connection: Mathematicians like Euler (18th century) used iterative substitution to study exponential growth, laying the groundwork for modern calculus. ๐Ÿ“ˆ

  1. Targeted Simplifications:

expr = sin(2 * x) + cos(2 * x)
expr.subs(sin(2 * x), 2 * sin(x) * cos(x))
\[\displaystyle 2 \sin{\left(x \right)} \cos{\left(x \right)} + \cos{\left(2 x \right)}\]

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)
\[\displaystyle \frac{19}{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:

  1. Approximate Roots:

expr = sqrt(8)
expr.evalf()
\[\displaystyle 2.82842712474619\]

Historical Note: The ancient Greeks, like Euclid, approximated square roots geometrically. Today, evalf does it in milliseconds. โฑ๏ธ

  1. High-Precision ฯ€:

pi.evalf(100)
\[\displaystyle 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068\]

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. ๐ŸŒŒ

  1. Substitution + Precision:

expr = cos(2 * x)
expr.evalf(subs={x: 2.4})
\[\displaystyle 0.0874989834394464\]

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! ๐Ÿ›ก๏ธ