SciPy2008 examples
From SymPy
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:
Contents |
Numerical Integration
One-liner: <source lang="python"> In [1]: Integral(sin(1/x), (x, 0, 1)).transform(x, 1/x).evalf(quad="osc") Out[1]: 0.504067061906928 </source>
Detailed steps: <source lang="python"> In [1]: e = Integral(sin(1/x), (x, 0, 1))
In [2]: e Out[2]: 1 ⌠⎮ ⎛1⎞ ⎮ sin⎜ââ€â‚¬Ã¢Å½Å¸ dx ⎮ âŽÂx⎠⌡ 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 </source>
Numerical Summation
It works for quickly convergent series: <source lang="python"> >>> 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 </source> And slowly convergent (polynomial rate) series: <source lang="python"> >>> Sum(n/(n**3+9), (n, 1, oo)).evalf(1000) 0.572085799521274038128017585783700438130384580104388084551740050974925897207818 98311108798290436060631856133690814143188244308005734075188518963064503611766727 51975068157408446403629166383226981406071893503958716023483643384018192761835469 62523276298459470487661766581612076405188965696292563597978253602870433142733727 49456336446570299555622044023184339325169717382623431811996989431779585758743983 22657597287758887471781904704253408614010644740045975234864559308102917760390712 09858646969081826648914656188008932364779703396061488751933093758374187906616981 59935678929938625204474297765447285426340636797285832219467575552277926359443579 66448919469783095915588358346137013995560248274612167594346431054534148807909065 87026974372235853955946903025185089032108053973102877186484901797732760077569507 62103250578219908729410121672429672442237773445952371487389948096056503557145790 85480428757289997024542130099656261002247342979582278399887560907241960471987518 890694794314366435375093779451882224094794 </source>
Numerical Simplification
<source lang="python"> 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 </source>
Curvilinear Coordinates
<source lang="python"> $ 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Ï dφ dφ d ─────────── + ────────────── + ─────(f(Ï, φ))
Ï 2 dÏ dÏ
Ï
________________________________________________________________________________ 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φ dφ d d ────────────── + ───────────────── + ─────(f(Ï, φ, z)) + ─────(f(Ï, φ, z))
Ï 2 dÏ dÏ 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θ dθ dÏ dφ dφ dθ d ───────────────── + ──────────────── + ───────────────── + ───────────────────── + ─────(f(Ï, θ, φ))
2 Ï 2 2 2 dÏ dÏ
Ï Ï â‹…sin (θ) Ï â‹…sin(θ)
________________________________________________________________________________ Transformation: rotating disk t = t x = x⋅cos(t⋅w) - y⋅sin(t⋅w) y = x⋅sin(t⋅w) + y⋅cos(t⋅w) z = z Jacobian: ⎡ 1 0 0 0⎤ ⎢ ⎥ ⎢-w⋅x⋅sin(t⋅w) - w⋅y⋅cos(t⋅w) cos(t⋅w) -sin(t⋅w) 0⎥ ⎢ ⎥ ⎢w⋅x⋅cos(t⋅w) - w⋅y⋅sin(t⋅w) sin(t⋅w) cos(t⋅w) 0⎥ ⎢ ⎥ ⎣ 0 0 0 1⎦ metric tensor g_{ij}: ⎡ 2 2 2 2 ⎤ ⎢1 + w ⋅x + w ⋅y -w⋅y w⋅x 0⎥ ⎢ ⎥ ⎢ -w⋅y 1 0 0⎥ ⎢ ⎥ ⎢ w⋅x 0 1 0⎥ ⎢ ⎥ ⎣ 0 0 0 1⎦ inverse metric tensor g^{ij}: ⎡ 1 w⋅y -w⋅x 0⎤ ⎢ ⎥ ⎢ 2 2 2 ⎥ ⎢w⋅y 1 + w ⋅y -x⋅y⋅w 0⎥ ⎢ ⎥ ⎢ 2 2 2 ⎥ ⎢-w⋅x -x⋅y⋅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)) + wâ‹…y⋅─────(f(t, x, y, z)) + wâ‹…y⋅─────(f(t, x, y, z)) - wâ‹…x⋅─────(f(t, x, y, z)) - wâ‹…xâ‹…
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)) - x⋅y⋅w ⋅─────(f(t, x, y, z)) - x⋅y⋅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Ï„ 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 μ = a⋅cos(ν)⋅cosh(μ) ν = a⋅sin(ν)⋅sinh(μ) Jacobian: ⎡a⋅cos(ν)⋅sinh(μ) -a⋅cosh(μ)⋅sin(ν)⎤ ⎢ ⎥ ⎣a⋅cosh(μ)⋅sin(ν) a⋅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(μ, ν))
dμ dμ dν dν
────────────────────────────────────────────────────────────────────────────────── + ──────────────────────────────────────────────────────────────────────
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
dμ
─────────────────────────────────────────────────────────── + ─────────────────────────────────────────────────────────────────────────────────────────────
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(μ, ν))
dν
──────────────────────────────────────────────────────────────────────────────────────────────────────────
⎞ ⎛ 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 (ν)⎠</source>
