In order to carry out any symbolic computation we have
to define symbolic variables which means we need to invoke SYMPY library. 
>>> from sympy import Symbol
>>> x = Symbol('x')
>>> type(x)
>>> y = Symbol('y')
>>> 2*x -x
x
>>> x + y + x + 10*y
2*x + 11*y
>>> y + x - y + 10
x + 10
 
Instead of creating variable by variable we can create
multiple variables using Symbols function. 
>>> import sympy
>>> x,y,z = sympy.symbols('x,y,z')
>>> x + 2 * y + 3 * z - x
2*y + 3*z
Once we’ve computed our term manipulation, it’s time
to insert numbers. This can be done using ‘subs’ method. 
>>> from sympy import symbols
>>> x,y,z=sympy.symbols('x,y,z')
>>> z = x + 2 * y
>>> print z
x + 2*y
>>> z.subs(x,10)
2*y + 10
>>> z.subs(x,10).subs(y,3)
16
>>> z.subs({x:10, y:3})
16
We can also substitute symbolic variable for another
(we will use expression z = x + 2*y).
>>> z = x + 2*y >>> z.subs(x,y) 3*y >>> z.subs(x,y).subs(y,2) 6
Nema komentara:
Objavi komentar