In [1]: #This tutorial introduces you to applying sympy #calculations using calculus. #

advertisement
8/22/2015
avery_tutorial_sympy_2_functions_calculus
In [1]: #This tutorial introduces you to applying sympy
#calculations using calculus.
#
#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 as integer variables
#The symbols f, g, h are predefined as 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]: #Create some more variables, including integer ones
a,b,c,d = symbols("a b c d");
i,j
= symbols("i j", integer=True)
In [5]: #Test to see that integer variables work as expected
sin(i*pi), cos(j*pi)
Out[5]:
http://localhost:8888/notebooks/avery_tutorial_sympy_2_functions_calculus.ipynb#
1/7
8/22/2015
avery_tutorial_sympy_2_functions_calculus
In [6]: #Create a function using basic python mechanisms, which are
#different from other languages.
#
#The lambda notation is simpler to set up for short functions
#Notice the difference between print output and typeset output
f = lambda x,y: sin(x+y)
def g(x): return (1+x)**2
print f(x,y)
print g(x)
f(x,y), g(x)
sin(x + y)
(x + 1)**2
Out[6]:
In [7]: #Sympy treats functions the same as expressions.
#But you can substitute different values for the dummy arguments
f(x,2*y)**2, f(x,x), 1/g(a+b)
Out[7]:
In [8]: #Sympy has powerful features for calculus.
#Let's consider differentiation first. There are several forms
#we can use.
ff = x**2 * y**3
ff.diff(x), ff.diff(y), ff.diff(x,2), ff.diff(x,y), diff(ff,y,x)
Out[8]:
In [9]: #We can do indefinite or definite integrals, depending on whether
#limits are provided. Multiple integrals are also supported
ff = x**2 * y
integrate(ff, x),\
integrate(ff, (x,1,2)),\
integrate(ff, (x,1,2), (y,0,2) )
Out[9]:
http://localhost:8888/notebooks/avery_tutorial_sympy_2_functions_calculus.ipynb#
2/7
8/22/2015
avery_tutorial_sympy_2_functions_calculus
In [10]: #You can differentiate and integrate functions just like
#ordinary expressions
g(x).diff(x), integrate(g(x), (x, 0, pi))
Out[10]:
In [11]: #Note that diff can handle multiple derivatives in sequence,
#with varying syntax
D1 = diff(f(x,y), x, 2)
E1A = (x**3 * y**4).diff(x,
E1B = (x**3 * y**4).diff(x,
E2A = (x**3 * y**4).diff(y,
E2B = (x**3 * y**4).diff(y,
D1, E1A, E1B, E2A, E2B
1,
y,
2,
y,
y, 2)
y)
x, 2)
x, x)
#Second x derivative
#Single x, then double
#Same thing, different
#Double y, then double
#Same thing, different
y
syntax
x
syntax
Out[11]:
In [12]: #Power series using the series() function. Do a simple one first.
#Expand exp(x) around 0 to 5th order. Note that the O() term
#includes the power specified
series(exp(x), x, 0, 5)
Out[12]:
In [13]: #Here's a more complicated one expanded to 10th order!
#This seems to take a lot of time
series(exp(tan(x)), x, 0, 8)
Out[13]:
In [14]: #Expand an even more complicated expression
ff = log(x + sqrt(1+x**2) )
ffseries = series(ff, x, 0, 11).factor()
ffseries
Out[14]:
http://localhost:8888/notebooks/avery_tutorial_sympy_2_functions_calculus.ipynb#
3/7
8/22/2015
avery_tutorial_sympy_2_functions_calculus
In [15]: #We can also grab each coefficient with the coeff(x,n) command.
#For this to work, the expression must be completely expanded
#(not factored)
ffseries_exp = ffseries.expand()
c5 = ffseries_exp.coeff(x,5)
c7 = ffseries_exp.coeff(x,7)
c5, c7
Out[15]:
In [16]: #We can use python list comprehension to get all the coefficients
coeffs = [ffseries_exp.coeff(x,i) for i in range(12)]
coeffs
Out[16]:
In [17]: #Let's expand the potential function
#
phi(x,t) = 1 / sqrt(1 - 2*t*x + x^2)
#The coefficients of x^n are n^th order Legendre polynomials in t.
#hi(x,t) is called a "generating function" for Legendre polynomials.
#x and t were defined at the beginning of the file so we don't have
#to define them as symbols here
phi = 1 / sqrt(1 - 2*t*x + x**2)
phi_series = series(phi, x, 0, 5)
phi_series
Out[17]:
In [18]: #You can remove the O(x^n) term with the removeO() method
phi_series_trunc = phi_series.removeO()
phi_series_trunc
Out[18]:
http://localhost:8888/notebooks/avery_tutorial_sympy_2_functions_calculus.ipynb#
4/7
8/22/2015
avery_tutorial_sympy_2_functions_calculus
In [19]: #We can use the coeff() command to get the individual Legendre
#polynomials. Note the use of the factor method to make the
#polynomials look better
coeffs_leg = [phi_series_trunc.coeff(x,i).factor() for i in range(6)]
coeffs_leg
Out[19]:
In [20]: #Integrate exp(-x^2) from -infinity to +infinity. This is the
#(unnormalized) error function
integrate(exp(-x**2), (x, -oo, oo) )
Out[20]:
In [21]: #Sympy can also handle sums
#First, do some sums over powers of integers
print "Sum of integer powers n^k from 1 to 100: k = 1, 2, 3"
summation(n,
(n, 1, 100)), \
summation(n**2, (n, 1, 100)), \
summation(n**3, (n, 1, 100))
Sum of integer powers n^k from 1 to 100: k = 1, 2, 3
Out[21]:
In [22]: #Sympy can also handle integer sums with symbolic endpoints
#N is a defined function, don't overwrite
N_T = symbols("N_T", integer=True)
print "Sum of integer powers n^k from 1 to N_T: k = 1, 2, 3"
summation(n,
(n, 1, N_T)).factor(), \
summation(n**2, (n, 1, N_T)).factor(), \
summation(n**3, (n, 1, N_T)).factor()
Sum of integer powers n^k from 1 to N_T: k = 1, 2, 3
Out[22]:
http://localhost:8888/notebooks/avery_tutorial_sympy_2_functions_calculus.ipynb#
5/7
8/22/2015
avery_tutorial_sympy_2_functions_calculus
In [23]: #How about a sum over a large power of integers: n^8
print "
Sum of integer powers n^8 from 1 to N_T"
summation(n**8, (n, 1, N_T)).factor()
Sum of integer powers n^8 from 1 to N_T
Out[23]:
In [24]: #Sums of negative powers of integers are also possible.
#These are useful in physics applications, e.g., when integrating
#the photon energy distribution or photon number distribution over
#a blackbody spectrum
summation(1/n**2, (n, 1, oo)), \
summation(1/n**4, (n, 1, oo))
Out[24]:
In [25]: #The sum of negative powers of integers defines the Riemann
#zeta function:
#
#
zeta(k) = sum(1/n^k, (n, 1, oo))
#
#This function is available in Sympy. Note that zeta(n) is always a
#rational * pi^n when n is even
summation(1/n**2,
summation(1/n**3,
summation(1/n**4,
summation(1/n**6,
(n,
(n,
(n,
(n,
1,
1,
1,
1,
oo)),
oo)),
oo)),
oo)),
zeta(2), \
zeta(3), \
zeta(4), \
zeta(6)
Out[25]:
In [26]: #Summing a power series over finite or infinite ranges
#assume(abs(x) < 1)
summation(x**n, (n, 0, N_T))
#summation(x**(2*n+1), (n, 0, N_T)), \
#summation( x**n/factorial(n), (n, 0, oo))
Out[26]:
http://localhost:8888/notebooks/avery_tutorial_sympy_2_functions_calculus.ipynb#
6/7
8/22/2015
avery_tutorial_sympy_2_functions_calculus
In [27]: #Somehow, assumptions do not work
assumptions.assume(x, integer=True)
-------------------------------------------------------------------------TypeError
Traceback (most recent cal
l last)
<ipython-input-27-e277671a20d1> in <module>()
1 #Somehow, assumptions do not work
2
----> 3 assumptions.assume(x, integer=True)
TypeError: 'module' object is not callable
In [ ]: #m was initially defined to be an integer by init_session()
integrate(sin(x), (x, 0, 2*m*pi))
In [ ]:
http://localhost:8888/notebooks/avery_tutorial_sympy_2_functions_calculus.ipynb#
7/7
Download