forked from sympy/sympy
-
Notifications
You must be signed in to change notification settings - Fork 0
Scipy2008 examples
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:
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
⎮ âŽÂ�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
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
>>> Sum(n/(n**3+9), (n, 1, oo)).evalf(1000)
0.572085799521274038128017585783700438130384580104388084551740050974925897207818
98311108798290436060631856133690814143188244308005734075188518963064503611766727
51975068157408446403629166383226981406071893503958716023483643384018192761835469
62523276298459470487661766581612076405188965696292563597978253602870433142733727
49456336446570299555622044023184339325169717382623431811996989431779585758743983
22657597287758887471781904704253408614010644740045975234864559308102917760390712
09858646969081826648914656188008932364779703396061488751933093758374187906616981
59935678929938625204474297765447285426340636797285832219467575552277926359443579
66448919469783095915588358346137013995560248274612167594346431054534148807909065
87026974372235853955946903025185089032108053973102877186484901797732760077569507
62103250578219908729410121672429672442237773445952371487389948096056503557145790
85480428757289997024542130099656261002247342979582278399887560907241960471987518
890694794314366435375093779451882224094794
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
$ 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 (ν)âŽ