In [1]:
import sympy as sp
sp.init_printing()
In [2]:
x, y = sp.symbols("x y")
In [3]:
expression = (x+y)**4
In [4]:
expression
Out[4]:
In [5]:
sp.expand(expression)
Out[5]:
In [6]:
expression = 8*x**2 + 26*x*y + 15*y**2
In [7]:
expression
Out[7]:
In [8]:
sp.factor(expression)
Out[8]:
In [9]:
expression - 20 *x*y - 14*y**2
Out[9]:
In [10]:
sp.factor(expression - 20*x*y - 14*y**2)
Out[10]:
In [16]:
expression
Out[16]:
In [12]:
f = sp.lambdify((x,y), expression, 'numpy')
In [17]:
f(3,4)
Out[17]:
In [18]:
8 * 3**2 + 26 * 3 * 4 + 15 * 4**2
Out[18]:
In [26]:
expression = 5*x**2 * sp.sin(3*x**3)
In [27]:
expression
Out[27]:
In [28]:
expression.diff(x)
Out[28]:
In [29]:
expression = sp.cos(x)
In [30]:
expression.integrate(x)
Out[30]:
In [34]:
expression.integrate((x, 0, sp.pi / 2))
Out[34]:
You can also create unevalated integrals or derivatives. These can later be evaluated with their doit
methods.
In [35]:
deriv = sp.Derivative(expression)
In [36]:
deriv
Out[36]:
In [37]:
deriv.doit()
Out[37]:
In [42]:
inte = sp.Integral(expression, (x, 0, sp.pi / 2))
In [43]:
inte
Out[43]:
In [44]:
inte.doit()
Out[44]:
In [ ]: