In [1]: #This is a basic tutorial introducing you to sympy.

advertisement
8/22/2015
avery_tutorial_sympy_1_basics
In [1]: #This is a basic tutorial introducing you to sympy.
#It introduces initialization, basic syntax, etc.
#You have to execute the early commands sequentially
#so that everything is initialized correctly
In [2]: #First import all the variables and functions in sympy
from sympy import *
In [3]: #The symbols x, y, z, t are predefined as variables
#The symbols k, m, n are predefined to be integer variables
#The symbols f, g, h are redefined to be functions
#This also turns on pretty printing (Latex?)
init_session()
IPython console for SymPy 0.7.6 (Python 2.7.5-64-bit) (ground types:
python)
These commands were executed:
>>> from __future__ import division
>>> from sympy import *
>>> x, y, z, t = symbols('x y z t')
>>> k, m, n = symbols('k m n', integer=True)
>>> f, g, h = symbols('f g h', cls=Function)
>>> init_printing()
Documentation can be found at http://www.sympy.org (http://www.symp
y.org)
In [4]: #It's easy to define comments directly in the input cell
#using a # before the comment.
4 + 5
#Inline comment ignored by Python during evaluation
Out[4]:
In [5]: #If you evaluate multiple ipython lines, only the output of the
#last line is printed
4*2
sin(2.)
sqrt(2)
sqrt(2.)
Out[5]:
http://localhost:8888/notebooks/avery_tutorial_sympy_1_basics.ipynb#
1/6
8/22/2015
avery_tutorial_sympy_1_basics
In [6]: #You can suppress output by adding a semicolon at the end of the line
sin(2);
In [7]: #If you want to list the output of multiple commands, put them
#on a line each separated by a comma ",". This creates a tuple
#of values
4*2, sin(2.), sqrt(2), sqrt(2.), 2**5
Out[7]:
In [8]: #You can put them on different lines using the continuation
#character "\" at the end of the line
#This creates a single multiline command that creates a tuple
4*2, \
sin(2.), \
sqrt(2), \
sqrt(2.)
Out[8]:
In [9]: #You can always show intermediate output by using the print statement.
#However, the output is not typeset as beautifully as when typesetting
#is set.
print sqrt(2)/2
sqrt(2)/2
sqrt(2)/2
Out[9]:
In [10]: #Sympy treats expressions as exact, unless a decimal point is used,
#in which case the accuracy is that of standard computer floating
#point representation, about 15-16 digits.
1, 1., sqrt(2), sqrt(2.)
Out[10]:
http://localhost:8888/notebooks/avery_tutorial_sympy_1_basics.ipynb#
2/6
8/22/2015
avery_tutorial_sympy_1_basics
In [11]: #Sympy treats division of numbers as floating point division
#(1/2 = 0.50, 3/2 = 1.50).
#To input an exact rational such as 3/2, you must use the S
#function for either the numerator or denominator
S(1)/2 + S(1)/3 + 1/S(4), \
1/2 + 1/3 + 1/4
Out[11]:
In [12]: #Or
#
#
#
look at the following expressions.
Note that 2 - sqrt(2)^2 is 0 exactly
However, 2 - sqrt(2.)^2 is not exactly zero because
the floating point representation is not exact
sqrt(2), 2-sqrt(2)**2, sqrt(2.), 2-sqrt(2.)**2, sin(1), sin(1.)
Out[12]:
In [13]: #You can get the numerical value of any expression
#using n(ndigits), which outputs the value to the number
#of requested digits. If n() is used, the standard
#floating point precision is used.
#
#In the following, n() is used as suffix, where it is
#called an "object method" (i.e., function belonging
#to the object)
pi.n(30), E.n(), (sqrt(2)).n(40)
Out[13]:
In [14]: #N(var, ndigits) is an ordinary function that provides the
#same capability
N(pi,30), N(E), N(sqrt(2),40)
Out[14]:
In [15]: #Sympy has standard math constants, pi, E = 2.718... and I = sqrt(-1).
#Arbitrary precision values of other constants are available
#through the mpmath module. Note the use of the expand() function
#which expands expresssions. We will see this later.
pi, E, I, 5 + 3*I, (5+3*I) * (5-3*I), expand( (5+3*I) * (5-3*I))
Out[15]:
http://localhost:8888/notebooks/avery_tutorial_sympy_1_basics.ipynb#
3/6
8/22/2015
avery_tutorial_sympy_1_basics
In [16]: #+infinity is represented by oo (two lower case "o" letters)
oo, -oo, tan(pi/2)
Out[16]:
In [17]: #Sympy has all the standard math and trig functions.
#Note the use of S(1)/2 to represent the fraction 1/2
exp(3), sqrt(5), sin(2), log(2), tan(2), \
asin(S(1)/2), acos(S(1)/2), atan(oo)
Out[17]:
In [18]: #The hyperbolic trig functions are also supported
sinh(2), cosh(2), tanh(2), asinh(S(1)/2), acosh(S(1)/2), atanh(1)
Out[18]:
In [19]: #Combinatoric related quantities such as factorial and
#binomial are supported.
#
#binomial(N,n) = N! / [n! * (N-n)! ]
factorial(6), binomial(6, 3)
Out[19]:
In [20]: #The factorint() function factors integers and returns a dictionary.
factorint(1035), factorint(factorial(6)), factorint(2048)
Out[20]:
In [21]: #factor() factors algebraic expressions.
#It can be used as a global function or object method,
#as in f.factor()
(x**2 - 5*x + 6).factor(), factor(x**4 + x**3 - 38*x**2 - 8*x + 240)
Out[21]:
http://localhost:8888/notebooks/avery_tutorial_sympy_1_basics.ipynb#
4/6
8/22/2015
avery_tutorial_sympy_1_basics
In [22]: #factor() can also be used to simplify algebraic fractions
f = 3 + 1/(x+1) + 1/(x**2-1);
f,
f.factor()
Out[22]:
In [23]: #expand() is the opposite of factor. It can be used as a
#function or object method
expand( (x**2-8)*(x+6)*(x-5) ),\
expand( (x+1)**4 ),\
((x+1)**3).expand()
Out[23]:
In [24]: #"collect(expr, x)" collects terms of the specified variable
expr = expand( (x+y+z)**3 )
expr,\
collect( expr, x )
Out[24]:
In [25]: #apart writes the ratio of two polynomials as a sum of
#partial fractions together does the opposite.
#The last two expressions below are actually identical
f = 3 + 1/(x+1) + 1/(x**2-1);
f.apart(),\
f.together(),\
f.apart().together()
Out[25]:
In [26]: #New symbolic variables are defined using the symbols() function
#Remember: x, y, z, t are already defined, as are k, m, n (as integers)
#N is already defined as a function, so don't overwrite it
N_T = symbols("N_T", integer=True)
factorial(N_T), binomial(N_T,n)
#Defines the variable N_T as an intege
Out[26]:
http://localhost:8888/notebooks/avery_tutorial_sympy_1_basics.ipynb#
5/6
8/22/2015
avery_tutorial_sympy_1_basics
In [27]: #Use the subs() method to evaluate an expression with particular
#values for the variables
factorial(N_T).subs(N_T,10), exp(-x*y).subs(x,0.60).subs(y,2.5)
Out[27]:
In [28]: #But subs() can also be used to substitute more complex expressions
expr = sin(2*x) + cos(2*x)
expr.subs(sin(2*x), 2*sin(x)*cos(x))
Out[28]:
In [29]: #You can also use trigsimp() to simplify trig expressions.
#We can also use simplify(), which is a more powerful
#(and sometimes slow) general mechanism
expf = sin(x)**2 + cos(x)**2
expg = sin(x)*cos(x)
expf,
expf.trigsimp(),
expf.simplify(),
trigsimp(expg)
Out[29]:
In [30]: #The opposite of trig_simp is (for no obvious reason)
#expand_trig. It exists only as a function.
#expand_trig expands trig expression of multiple angles into
#powers of trig functions of angles
expand_trig(sin(x+y)),
expand_trig(sin(2*x)**2)
Out[30]:
In [31]: #Here's a more complex use of expand_trig
expand_trig(sin(2*(x+y)))
Out[31]:
In [ ]:
http://localhost:8888/notebooks/avery_tutorial_sympy_1_basics.ipynb#
6/6
Download