SciPy2008 examples

From SymPy

Jump to: navigation, search

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:

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

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
Personal tools