Code Search for Developers
 
 
  

wallis_pi.py from matplotlib at Krugle


Show wallis_pi.py syntax highlighted

#!/usr/bin/env python
"""Simple demonstration of Python's arbitrary-precision integers."""

# We need exact division between integers as the default, without manual
# conversion to float b/c we'll be dividing numbers too big to be represented
# in floating point.
from __future__ import division

def pi(n):
    """Compute pi using n terms of Wallis' product.

    Wallis' formula approximates pi as

    pi(n) = 2 \prod_{i=1}^{n}\frac{4i^2}{4i^2-1}."""
    
    num = 1
    den = 1
    for i in xrange(1,n+1):
	tmp = 4*i*i
	num *= tmp
	den *= tmp-1
    return 2.0*(num/den)

def part_range(n1,n2,nchunks):
    """Partition a range specification in nchunks"""
    size,rem = divmod(n2-n1,nchunks)
    sizes = [size]*nchunks
    while rem > 0:
        for i in range(nchunks):
            sizes[i] += 1
            rem -= 1
            if rem == 0:
                break

    # The sizes list has the offsets, now we need the actual start,stop pairs
    ranges = []
    start=n1
    for size in sizes:
        ranges.append((start,start+size))
        start += size
    return ranges
    
def wpi_nd(range_spec):
    """Compute pi using n terms of Wallis' product.

    Wallis' formula approximates pi as

    pi(n) = 2 \prod_{i=1}^{n}\frac{4i^2}{4i^2-1}."""

    n1,n2 = range_spec
    
    num = 1
    den = 1
    for i in xrange(n1,n2):
	tmp = 4*i*i
	num *= tmp
	den *= tmp-1

    return num,den

def par_pi(n,num_engines=1):
    """Compute pi using n terms of Wallis' product.

    Wallis' formula approximates pi as

    pi(n) = 2 \prod_{i=1}^{n}\frac{4i^2}{4i^2-1}.

    Parallel version."""

    num,den = reduce(lambda x,y:(x[0]*y[0],x[1]*y[1]),
                     map(wpi_nd,part_range(1,n+1,num_engines)))
    return 2.0*(num/den)
    

# This part only executes when the code is run as a script, not when it is
# imported as a library
if __name__ == '__main__':
    # Simple convergence demo.

    # A few modules we need
    import pylab as P
    import numpy as N

    # Create a list of points 'nrange' where we'll compute Wallis' formula
    nrange = N.linspace(10,2000,20).astype(int)
    # Make an array of such values
    wpi = N.array(map(pi,nrange))
    # Compute the difference against the value of pi in numpy (standard
    # 16-digit value)
    diff = abs(wpi-N.pi)

    # Make a new figure and build a semilog plot of the difference so we can
    # see the quality of the convergence
    
    P.figure()
    # Line plot with red circles at the data points
    P.semilogy(nrange,diff,'-o',mfc='red')

    # A bit of labeling and a grid
    P.title(r"Convergence of Wallis' product formula for $\pi$")
    P.xlabel('Number of terms')
    P.ylabel(r'|Error|')
    P.grid()

    # Display the actual plot
    P.show()




See more files for this project here

matplotlib

Matplotlib is a pure python plotting library with the goal of making\r\npublication quality plots using a syntax familiar to matlab users. \r\nThe library uses Numeric for handling large\r\ndata sets and supports a variety of output backends

Project homepage: http://sourceforge.net/projects/matplotlib
Programming language(s): C,C++,Python
License: other

  data/
    key_stats/
      CROX_key_stats.html
      GE_key_stats.html
      GOOG_key_stats.html
      INTC_key_stats.html
      MSFT_key_stats.html
      WMT_key_stats.html
      YHOO_key_stats.html
    HISTORY.gz
    ge.csv
    hsales.dat
    monthly_sunspots.dat
    moonlanding.jpg
    nm560.dat
    synapse_data.dat
    synapse_times.dat
  extras/
    fft_demo.py
    spec_interp.py
    steinman_interp.py
    weave_examples.py
  faces/
    data_test/
      face10tn.pcx
      face11tn.pcx
      face12tn.pcx
      face13tn.pcx
      face14tn.pcx
      face15tn.pcx
      face16tn.pcx
      face17tn.pcx
      face18tn.pcx
      face19tn.pcx
      face1tn.pcx
      face20tn.pcx
      face21tn.pcx
      face22tn.pcx
      face23tn.pcx
      face24tn.pcx
      face25tn.pcx
      face26tn.pcx
      face27tn.pcx
      face28tn.pcx
      face29tn.pcx
      face2tn.pcx
      face30tn.pcx
      face3tn.pcx
      face4tn.pcx
      face5tn.pcx
      face6tn.pcx
      face7tn.pcx
      face8tn.pcx
      face9tn.pcx
    data_train/
      face10n.pcx
      face11n.pcx
      face12n.pcx
      face13n.pcx
      face14n.pcx
      face15n.pcx
      face16n.pcx
      face17n.pcx
      face18n.pcx
      face19n.pcx
      face1n.pcx
      face20n.pcx
      face21n.pcx
      face22n.pcx
      face23n.pcx
      face24n.pcx
      face25n.pcx
      face26n.pcx
      face27n.pcx
      face28n.pcx
      face29n.pcx
      face2n.pcx
      face30n.pcx
      face31n.pcx
      face32n.pcx
      face33n.pcx
      face34n.pcx
      face35n.pcx
      face36n.pcx
      face37n.pcx
      face38n.pcx
      face39n.pcx
      face3n.pcx
      face40n.pcx
      face41n.pcx
      face42n.pcx
      face43n.pcx
      face44n.pcx
      face45n.pcx
      face46n.pcx
      face47n.pcx
      face48n.pcx
      face49n.pcx
      face4n.pcx
      face50n.pcx
      face51n.pcx
      face52n.pcx
      face53n.pcx
      face54n.pcx
      face55n.pcx
      face56n.pcx
      face57n.pcx
      face58n.pcx
      face59n.pcx
      face5n.pcx
      face60n.pcx
      face61n.pcx
      face62n.pcx
      face63n.pcx
      face64n.pcx
      face65n.pcx
      face6n.pcx
      face7n.pcx
      face8n.pcx
      face9n.pcx
    faces.py
    fmatch.py
    imatch.py
    imatch2.py
    imtools.py
    test_imatch.py
  logistic/
    sethna_ori/
    __init__.py
    exercise01.py
    exercise02.py
    maplib.py
    maplib.pyc
  numpy_wrap/
    f2py/
    pyrex/
    swig/
  schrodinger/
    Schrodinger_FDTD.pdf
    schrod_fdtd.py
  skel/
    faces/
    fortran_wrap/
    distributions_skel.py
    erathostenes_skel.py
    fft_imdenoise_skel.py
    fit_synapse_skel.py
    fitting_skel.py
    montecarlo_pi_skel.py
    polyroots1d_skel.py
    qsort_skel.py
    quad_newton_skel.py
    recarray_demo_skel.py
    regress_demo_skel.py
    scrape_key_stats_skel.py
    shoot_skel.py
    spline_demo_skel.py
    stats_descriptives_skel.py
    stats_distributions_skel.py
    trapezoid_skel.py
    wallis_pi_skel.py
    wordfreqs_skel.py
  visual/
    bounce.py
    shoot.py
    shoot_t.py
    toroid_drag.py
  BeautifulSoup.py
  __init__.py
  bessel.py
  distributions.py
  erathostenes.py
  erathostenes_fperez.py
  erathostenes_list.py
  erathostenes_set.py
  fft_imdenoise.py
  fit_synapse.py
  fitting.py
  getbibtex.py
  lsys.py
  montecarlo_pi.py
  numpy-blitz_1000.png
  numpy-blitz_300.png
  numpy-blitz_500.png
  numpy_slicing.py
  polyroots1d.py
  qsort.py
  quad_newton.py
  recarray_demo.py
  regress.py
  regress_demo.py
  scrape_key_stats.py
  spline_demo.py
  stats_descriptives.py
  stats_distributions.py
  test.ipy
  trapezoid.py
  wallis_pi.py
  weave_blitz.py
  weave_blitz0.py
  weave_blitz_comp.png
  weave_examples_simple.py
  weave_exercises.py
  wordfreqs.py