Skip to content
certik edited this page Feb 8, 2011 · 4 revisions

We couldn't go to SciPy2008, so we'll use these examples in some of our future presentation about sympy.

Besides EuroSciPy2008_examples we can also add the following:

Table of Contents

Numerical Integration

One-liner:

In [1]: Integral(sin(1/x), (x, 0, 1)).transform(x, 1/x).evalf(quad="osc")
Out[1]: 0.504067061906928

Detailed steps:

In [1]: e = Integral(sin(1/x), (x, 0, 1))

In [2]: e
Out[2]: 
1          
âÅ           
⎮    âŽâº1⎞   
⎮ sinâŽÅââ€�€âŽŸ dx
⎮    ⎢Ž    
âÅ¡          
0          

In [3]: e.transform(x, 1/x)
Out[3]: 
âËž          
âÅ           
⎮ sin(x)   
⎮ ������ dx
⎮    2     
⎮   x      
âÅ¡          
1          

In [4]: e.transform(x, 1/x).evalf(quad="osc")
Out[4]: 0.504067061906928

In [5]: e.transform(x, 1/x).evalf(40, quad="osc")
Out[5]: 0.504067061906928371989856117741148229625

Numerical Summation

It works for quickly convergent series:

>>> Sum((2*n**3+1)/factorial(2*n+1), (n, 0, oo)).evalf(1000)
1.652941212640472981900739198325231452667553042183503755040875167115365207002854
77118747045228498906167383807929789641305010501152379438610698437723585110992132
48084094702974173459412697848275449887634172363108079619463778928999727406730383
57199917316237084560028761604522443350080698146577601430156851863096927635778314
88062076063878821591479918536110213351662499708829217876455721476648748647659612
72185645529206548668821178422050797739640819097159967650626965341984007864872054
71812636349043868903125201137904072881174848578339123166638219650148561227868156
80738028532199588253087223349198266285072706513063361416254124560602074234127566
32410682925916059738774890040375938723705381947697574581499793671926177145966891
33271029543103694271529306325574205636661264488189585018019114290293809963899283
90070084916840020684307314192359067368407129281676733087681860839859648692202393
41225132757138225024317713163659365040869159437217031345698535519950979370407285
20746689993201707235774309731234398779684
And slowly convergent (polynomial rate) series:
>>> Sum(n/(n**3+9), (n, 1, oo)).evalf(1000)
0.572085799521274038128017585783700438130384580104388084551740050974925897207818
98311108798290436060631856133690814143188244308005734075188518963064503611766727
51975068157408446403629166383226981406071893503958716023483643384018192761835469
62523276298459470487661766581612076405188965696292563597978253602870433142733727
49456336446570299555622044023184339325169717382623431811996989431779585758743983
22657597287758887471781904704253408614010644740045975234864559308102917760390712
09858646969081826648914656188008932364779703396061488751933093758374187906616981
59935678929938625204474297765447285426340636797285832219467575552277926359443579
66448919469783095915588358346137013995560248274612167594346431054534148807909065
87026974372235853955946903025185089032108053973102877186484901797732760077569507
62103250578219908729410121672429672442237773445952371487389948096056503557145790
85480428757289997024542130099656261002247342979582278399887560907241960471987518
890694794314366435375093779451882224094794

Numerical Simplification

In [4]: float(1/7)
Out[4]: 0.142857142857

In [5]: nsimplify(_)
Out[5]: 1/7

In [6]: float(1/81)
Out[6]: 0.0123456790123

In [7]: nsimplify(_)
Out[7]: 1/81

>>> nsimplify(pi, tolerance=0.01)
22/7
>>> nsimplify(pi, tolerance=0.001)
355/113
>>> nsimplify(0.33333, tolerance=1e-4)
1/3
>>> nsimplify(4.71, [pi], tolerance=0.01)
3*pi/2
>>> nsimplify(2.0**(1/3.), tolerance=0.001)
635/504
>>> nsimplify(2.0**(1/3.), tolerance=0.001, full=True)
2**(1/3)

>>> pprint(nsimplify(cos(atan('1/3'))))
    ____
3*\/ 10

   10

>>> pprint(nsimplify(4/(1+sqrt(5)), [GoldenRatio]))
-2 + 2*GoldenRatio

>>> pprint(nsimplify(2 + exp(2*atan('1/4')*I)))
49   8*I
-- + ---
17    17

>>> pprint(nsimplify((1/(exp(3*pi*I/5)+1))))
             
            /         ___
           /        \/ 5
1/2 - I*  /   1/4 + -----
        \/            10

>>> pprint(nsimplify(I**I, [pi]))
 -pi
 ---
  2
e

>>> pprint(nsimplify(Sum(1/n**2, (n, 1, oo)), [pi]))
  2
pi
---
 6

>>> pprint(nsimplify(gamma('1/4')*gamma('3/4'), [pi]))
     ___
pi*\/ 2

Curvilinear Coordinates

$ python examples/advanced/curvilinear_coordinates.py 

Transformation: polar
Ï= Ïâ‹…cos(φ)
Ï= Ïâ‹…sin(φ)
Jacobian:
⎡cos(φ)  -Ïâ‹…sin(φ)⎤
⎢                 ⎥
⎣sin(φ)  Ïâ‹…cos(φ) ⎦
metric tensor g_{ij}:
⎡1  0 ⎤
⎢     ⎥
⎢    2⎥
⎣0  Ï⎦
inverse metric tensor g^{ij}:
⎡1  0 ⎤
⎢     ⎥
⎢   1 ⎥
⎢0  ──⎥
⎢    2⎥
⎣   Ï⎦
det g_{ij}:
 2
ÏLaplace:
                 2                           
d               d                            
──(f(�, φ))   ─────(f(�, φ))      2          
d           
─────────── + ────────────── + ─────(f(�, φ))
     Ï2         ÏTransformation: cylindrical
Ï= Ïâ‹…cos(φ)
Ï= Ïâ‹…sin(φ)
z = z
Jacobian:
⎡cos(φ)  -Ïâ‹…sin(φ)  0⎤
⎢                    ⎥
⎢sin(φ)  Ïâ‹…cos(φ)   0⎥
⎢                    ⎥
⎣  0         0      1⎦
metric tensor g_{ij}:
⎡1  0   0⎤
⎢        ⎥
⎢    2   ⎥
⎢0  Ï0⎥
⎢        ⎥
⎣0  0   1⎦
inverse metric tensor g^{ij}:
⎡1  0   0⎤
⎢        ⎥
⎢   1    ⎥
⎢0  ──  0⎥
⎢    2   ⎥
⎢   Ï⎥
⎢        ⎥
⎣0  0   1⎦
det g_{ij}:
 2
ÏLaplace:
                    2                                                     
d                  d                                                      
──(f(�, φ, z))   ─────(f(�, φ, z))      2                   2             
d                   d              
────────────── + ───────────────── + ─────(f(�, φ, z)) + ─────(f(�, φ, z))
      Ï2          dz dz            
                         ÏTransformation: spherical
Ï= Ïâ‹…cos(φ)â‹…sin(θ)
θ = Ïâ‹…sin(φ)â‹…sin(θ)
Ï= Ïâ‹…cos(θ)
Jacobian:
⎡cos(φ)â‹…sin(θ)  Ïâ‹…cos(φ)â‹…cos(θ)  -Ïâ‹…sin(φ)â‹…sin(θ)⎤
⎢                                                ⎥
⎢sin(φ)â‹…sin(θ)  Ïâ‹…cos(θ)â‹…sin(φ)  Ïâ‹…cos(φ)â‹…sin(θ) ⎥
⎢                                                ⎥
⎣   cos(θ)         -Ïâ‹…sin(θ)            0        ⎦
metric tensor g_{ij}:
⎡   2         2       2       2         2       2       2                                                        ⎤
⎢cos (θ) + cos (φ)⋅cos (θ)⋅tan (θ) + cos (θ)⋅sin (φ)⋅tan (θ)  0                          0                       ⎥
⎢                                                                                                                ⎥
⎢                                                              2                                                 ⎥
⎢                             0                               Ï0                       ⎥
⎢                                                                                                                ⎥
⎢                                                                  2    2       2       2    2       2       2   ⎥
⎣                             0                               0   Ïâ‹…cos (φ)â‹…sin (θ) + Ïâ‹…cos (θ)â‹…sin (φ)â‹…tan (θ)⎦
metric tensor g_{ij} specified by hand:
⎡1  0       0     ⎤
⎢                 ⎥
⎢    2            ⎥
⎢0  Ï0     ⎥
⎢                 ⎥
⎢        2    2   ⎥
⎣0  0   Ïâ‹…sin (θ)⎦
inverse metric tensor g^{ij}:
⎡1  0       0     ⎤
⎢                 ⎥
⎢   1             ⎥
⎢0  ──      0     ⎥
⎢    2            ⎥
⎢   Ï⎥
⎢                 ⎥
⎢           1     ⎥
⎢0  0   ──────────⎥
⎢        2    2   ⎥
⎣       Ïâ‹…sin (θ)⎦
det g_{ij}:
 4    2   
Ïâ‹…sin (θ)
Laplace:
   2                                      2                                                         
  d                   d                  d                 d                                        
─────(f(�, θ, φ))   2⋅──(f(�, θ, φ))   ─────(f(�, θ, φ))   ──(f(�, θ, φ))⋅cos(θ)      2             
¸ ¸                 ¸                        d              
───────────────── + ──────────────── + ───────────────── + ───────────────────── + ─────(f(�, θ, φ))
         2                 Ï2    2                2                ÏÏâ‹…sin (θ)            Ïâ‹…sin(θ)                          

Transformation: rotating disk
t = t
x = ‹…cos(‹…w) - ‹…sin(‹…w)
y = ‹…sin(‹…w) + ‹…cos(‹…w)
z = z
Jacobian:
⎡             1                   0          0      0⎤
⎢                                                    ⎥
⎢-‹…‹…sin(‹…w) - ‹…‹…cos(‹…w)  cos(‹…w)  -sin(‹…w)  0⎥
⎢                                                    ⎥
⎢‹…‹…cos(‹…w) - ‹…‹…sin(‹…w)   sin(‹…w)  cos(‹…w)   0⎥
⎢                                                    ⎥
⎣             0                   0          0      1⎦
metric tensor g_{ij}:
⎡     2  2    2  2              ⎤
⎢1 + w â‹…x  + w â‹…y   -‹…y  ‹…x  0⎥
⎢                               ⎥
⎢      -‹…y          1     0   0⎥
⎢                               ⎥
⎢       ‹…x          0     1   0⎥
⎢                               ⎥
⎣        0           0     0   1⎦
inverse metric tensor g^{ij}:
⎡ 1       ‹…y       -‹…x     0⎤
⎢                             ⎥
⎢           2  2         2    ⎥
⎢‹…y   1 + w â‹…y    -‹…‹…w    0⎥
⎢                             ⎥
⎢             2        2  2   ⎥
⎢-‹…x   -‹…‹…w    1 + w â‹…x   0⎥
⎢                             ⎥
⎣ 0        0          0      1⎦
det g_{ij}:
1
Laplace:
               2                                  2                          2                          2                          2                       
âŽ2  2⎞   d                    âŽ2  2⎞   d                          d                          d                          d                        
âŽ1 + w â‹…x ⎠⋅─────(f(t, x, y, z)) + âŽ1 + w â‹…y ⎠⋅─────(f(t, x, y, z)) + ‹…‹…─────(f(t, x, y, z)) + ‹…‹…─────(f(t, x, y, z)) - ‹…‹…─────(f(t, x, y, z)) - ‹…‹…
            dy dy                              dx dx                      dx dt                      dt dx                      dy dt                      

   2                             2                             2                      2                      2                
  d                         2   d                         2   d                      d                      d                 
─────(f(t, x, y, z)) - ‹…‹…w ⋅─────(f(t, x, y, z)) - ‹…‹…w ⋅─────(f(t, x, y, z)) + ─────(f(t, x, y, z)) + ─────(f(t, x, y, z))
dt dy                         dy dx                         dx dy                  dt dt                  dz dz               

Transformation: parabolic
σ = σ⋅Ï2    2
    Ïσ 
Ï= ── - ──
    2    2 
Jacobian:
⎡Ïσ⎤
⎢     ⎥
⎣-σ  Ï⎦
metric tensor g_{ij}:
⎡ 2    2         ⎤
⎢σ  + Ï0   ⎥
⎢                ⎥
⎢          2    2⎥
⎣   0     σ  + Ï⎦
inverse metric tensor g^{ij}:
⎡   1            ⎤
⎢───────     0   ⎥
⎢ 2    2         ⎥
⎢σ  + Ï⎥
⎢                ⎥
⎢            1   ⎥
⎢   0     ───────⎥
⎢          2    2⎥
⎣         σ  + Ï⎦
det g_{ij}:
   2  2    4    4
2⋅σ â‹…Ï+ σ  + ÏLaplace:
   2                2                                                                                  
  d                d                 âŽ2      3⎞ d                   âŽ2      3⎞ d             
─────(f(σ, Ï„))   ─────(f(σ, Ï„))      âŽ4⋅σ⋅Ï+ 4⋅σ ⎠⋅──(f(σ, Ï„))         âŽ4â‹…Ï⋅σ  + 4â‹…Ï⎠⋅──(f(σ, Ï„))   
dσ dσ            dσ                                  ────────────── + ────────────── + ───────────────────────────────── + ─────────────────────────────────
    2    2           2    2       âŽ2    2⎞ âŽ2  2      4      4⎞   âŽ2    2⎞ âŽ2  2      4      4⎞
   σ  + Ïσ  + ÏâŽÏƒ  + Ï⎠⋅âŽ4⋅σ â‹…Ï+ 2⋅σ  + 2â‹…Ï⎠   âŽÏƒ  + Ï⎠⋅âŽ4⋅σ â‹…Ï+ 2⋅σ  + 2â‹…Ï⎠

Transformation: elliptic
μ = ‹…cos(ν)â‹…cosh(μ)
ν = ‹…sin(ν)â‹…sinh(μ)
Jacobian:
⎡‹…cos(ν)â‹…sinh(μ)  -‹…cosh(μ)â‹…sin(ν)⎤
⎢                                   ⎥
⎣‹…cosh(μ)â‹…sin(ν)  ‹…cos(ν)â‹…sinh(μ) ⎦
metric tensor g_{ij}:
⎡ 2    2        2       2     2       2                                              ⎤
⎢a ⋅cos (ν)⋅sinh (μ) + a ⋅cosh (μ)⋅sin (ν)                      0                    ⎥
⎢                                                                                    ⎥
⎢                                            2    2        2       2     2       2   ⎥
⎣                    0                      a ⋅cos (ν)⋅sinh (μ) + a ⋅cosh (μ)⋅sin (ν)⎦
inverse metric tensor g^{ij}:
⎡                     2    2        2       2     2       2                                                                                                
⎢                    a ⋅cos (ν)⋅sinh (μ) + a ⋅cosh (μ)⋅sin (ν)                                                                                             
⎢──────────────────────────────────────────────────────────────────────────────────                                          0                             
⎢   4    2        2       2        2       4    4        4       4     4       4                                                                           
⎢2⋅a ⋅cos (ν)⋅cosh (μ)⋅sin (ν)⋅sinh (μ) + a ⋅cos (ν)⋅sinh (μ) + a ⋅cosh (μ)⋅sin (ν)                                                                        
⎢                                                                                                                                                          
⎢                                                                                                         2    2        2       2     2       2            
⎢                                                                                                        a ⋅cos (ν)⋅sinh (μ) + a ⋅cosh (μ)⋅sin (ν)         
⎢                                        0                                           ──────────────────────────────────────────────────────────────────────
⎢                                                                                       4    2        2       2        2       4    4        4       4     
⎣                                                                                    2⋅a ⋅cos (ν)⋅cosh (μ)⋅sin (ν)⋅sinh (μ) + a ⋅cos (ν)⋅sinh (μ) + a ⋅cosh

            ⎤
            ⎥
            ⎥
            ⎥
            ⎥
            ⎥
            ⎥
            ⎥
────────────⎥
4       4   ⎥
 (μ)⋅sin (ν)⎦
det g_{ij}:
   4    2        2       2        2       4    4        4       4     4       4   
2⋅a ⋅cos (ν)⋅cosh (μ)⋅sin (ν)⋅sinh (μ) + a ⋅cos (ν)⋅sinh (μ) + a ⋅cosh (μ)⋅sin (ν)
Laplace:
                                                           2                                                                                    2          
            âŽ2    2        2       2     2       2   ⎞   d                                      âŽ2    2        2       2     2       2   ⎞   d           
            âŽa â‹…cos (ν)â‹…sinh (μ) + a â‹…cosh (μ)â‹…sin (ν)⎠⋅─────(f(μ, ν))                           âŽa â‹…cos (ν)â‹…sinh (μ) + a â‹…cosh (μ)â‹…sin (ν)⎠⋅─────(f(μ, ν))
                                                        ¼ ¼                                                                                ½ ½         
────────────────────────────────────────────────────────────────────────────────── + ──────────────────────────────────────────────────────────────────────
   4    2        2       2        2       4    4        4       4     4       4         4    2        2       2        2       4    4        4       4     
2⋅a ⋅cos (ν)⋅cosh (μ)⋅sin (ν)⋅sinh (μ) + a ⋅cos (ν)⋅sinh (μ) + a ⋅cosh (μ)⋅sin (ν)   2⋅a ⋅cos (ν)⋅cosh (μ)⋅sin (ν)⋅sinh (μ) + a ⋅cos (ν)⋅sinh (μ) + a ⋅cosh

                                                                                                                                                           
               âŽ2    2        2       2     2       2   ⎞ âŽ4    4        3                 4     3       4                 4    2        3       2     
               âŽa â‹…cos (ν)â‹…sinh (μ) + a â‹…cosh (μ)â‹…sin (ν)⎠⋅âŽ4â‹…a â‹…cos (ν)â‹…sinh (μ)â‹…cosh(μ) + 4â‹…a â‹…cosh (μ)â‹…sin (ν)â‹…sinh(μ) + 4â‹…a â‹…cos (ν)â‹…cosh (μ)â‹…sin (ν)â‹…s
                                                                                                                                                           
──────────── + ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
4       4                   âŽ4    2        2       2        2       4    4        4       4     4       4   ⎞ âŽ4    2        2       2        2      
 (μ)â‹…sin (ν)                âŽ2â‹…a â‹…cos (ν)â‹…cosh (μ)â‹…sin (ν)â‹…sinh (μ) + a â‹…cos (ν)â‹…sinh (μ) + a â‹…cosh (μ)â‹…sin (ν)⎠⋅âŽ4â‹…a â‹…cos (ν)â‹…cosh (μ)â‹…sin (ν)â‹…sinh (μ) + 

                                                                                                                                                           
            4    2       2        3           ⎞ d             âŽ2    2        2       2     2       2   ⎞ âŽ4    3        4                4     4     
inh(μ) + 4â‹…a â‹…cos (ν)â‹…sin (ν)â‹…sinh (μ)â‹…cosh(μ)⎠⋅──(f(μ, ν))   âŽa â‹…cos (ν)â‹…sinh (μ) + a â‹…cosh (μ)â‹…sin (ν)⎠⋅âŽ- 4â‹…a â‹…cos (ν)â‹…sinh (μ)â‹…sin(ν) + 4â‹…a â‹…cosh (μ)â‹…s
                                                ¼                                                                                                         
─────────────────────────────────────────────────────────── + ─────────────────────────────────────────────────────────────────────────────────────────────
   4    4        4         4     4       4   ⎞                             âŽ4    2        2       2        2       4    4        4       4     4       4
2â‹…a â‹…cos (ν)â‹…sinh (μ) + 2â‹…a â‹…cosh (μ)â‹…sin (ν)⎠                             âŽ2â‹…a â‹…cos (ν)â‹…cosh (μ)â‹…sin (ν)â‹…sinh (μ) + a â‹…cos (ν)â‹…sinh (μ) + a â‹…cosh (μ)â‹…sin 

                                                                                                          
  3                4     2       3        2                4    3        2        2          ⎞ d          
in (ν)⋅cos(ν) - 4⋅a ⋅cosh (μ)⋅sin (ν)⋅sinh (μ)⋅cos(ν) + 4⋅a ⋅cos (ν)⋅cosh (μ)⋅sinh (μ)⋅sin(ν)⎠⋅──(f(μ, ν))
                                                                                               ½         
──────────────────────────────────────────────────────────────────────────────────────────────────────────
   ⎞ âŽ4    2        2       2        2         4    4        4         4     4       4   ⎞             
(ν)⎠⋅âŽ4â‹…a â‹…cos (ν)â‹…cosh (μ)â‹…sin (ν)â‹…sinh (μ) + 2â‹…a â‹…cos (ν)â‹…sinh (μ) + 2â‹…a â‹…cosh (μ)â‹…sin (ν)⎠             

Category:Documentation

Clone this wiki locally