forked from sympy/sympy
-
Notifications
You must be signed in to change notification settings - Fork 0
Scipy2008 examples
Aaron Meurer edited this page Mar 12, 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 20746689993201707235774309731234398779684And 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
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 (ν)âŽ