Jupyter Snippet CB2nd 01_sympy_intro

Jupyter Snippet CB2nd 01_sympy_intro

15.1. Diving into symbolic computing with SymPy

from sympy import *
init_printing()
var('x y')

png

x, y = symbols('x y')
expr1 = (x + 1) ** 2
expr2 = x**2 + 2 * x + 1
expr1 == expr2
False
simplify(expr1 - expr2)

png

expr1.subs(x, expr1)

png

expr1.subs(x, pi)

png

expr1.subs(x, S(1) / 2)

png

_.evalf()

png

f = lambdify(x, expr1)
import numpy as np
f(np.linspace(-2., 2., 5))
array([ 1.,  0.,  1.,  4.,  9.])