Dear colleagues,

a big thanks to all those who came to the first compu-salon in the Tapir interaction room last Friday.

I'm sending this e-mail to the "big" distribution lists again because I don't think the compu-salon mailing list (below) is complete yet. Please e-mail me if you don't see your e-mail so I can switch and stop spamming everybody.

Per popular request, the next salon will be this Friday, Jan 27, at 4:30pm. The topic of the day will be building a Python tool to pull bibtex citations from the ADS archive, for which we'll need to learn a bit about each of the following:

  • installing Python packages
  • regular expressions
  • file I/O
  • http forms and web-service APIs
  • html structure

I'll send out a reminder on Friday morning. And remember, Beautiful is Better than Ugly!

Michele

Compu-salon ep. 1 (2012/01/20) in summary

Also at www.vallis.org/salon.

In this first meeting, we talked about

  • the nature of scripting languages: interpreted, expressive, suitable for rapid development (but also performance with the right tricks)
  • why they're good for scientists: they allow for communicative, understandable computer code; they are fun to program; they shorten coding and debugging
  • the zen of Python

We then ran through a couple of code examples to compute pi (attached below in one of their iterations). In these examples we learned about:

  • basic expressions in Python
  • importing modules, such as math and random
  • Boolean expressions
  • defining functions
  • tuples (and unpacking them), lists (and using their elements), and list comprehensions

Want homework?

I left the third, most modern spigot algorithm for pi as homework for the more ambitious. I only flashed my solution, which involved Python generator functions. If you wish, read about the algorithm and then send me your solution.

Loose ends

  • Python 2.x vs. 3: after looking into major package support, I would suggest sticking to 2.6/2.7 for the moment. In a few months the situation will be different (e.g., matplotlib announced support in its next version; scipy has beta support). Furthermore, most operating systems still have 2.x as default.

  • Arbitrary-precision arithmetics: I was wrong in suggesting the package "Decimal", which was meant mostly to track money. The right package is mpmath. If you know how to install a package, try replacing import math with import mpmath as math in the examples below, then set mpmath.mp.dps = 100 and try archipi(200). [Bonus homework question: why does the replacement make the program work?] If you don't know how to install packages, we'll do it first thing next time.

Python code follows!

import random
import math

def buffontrial():
    """Returns 1 if a randomly sampled needle configuration intersects the
Buffon rules. (By the way, this is a Python docstring. Look it up.)"""

    return 0 if (0 < random.random() + math.sin(2 * math.pi * random.random()) < 1) else 1

def buffonpi(n):
    """Collects n Buffon-needle trials and computes the resulting 
approximation of pi."""
    trials = [buffontrial() for i in xrange(n)]     # a list comprehension

    return (2.0 * n) / sum(trials)


def archistep(ab):
    """Performs one iteration of the Archimedian iteration for pi."""

    a,b = ab    # unpack the tuple

    anext = 2 * a * b  / (a + b)
    bnext = math.sqrt(anext * b)

    return (anext,bnext)

def archipi(n):
    """Performs n Archimedian bisections of inscribed and circumscribed
polygons, returning the resulting lower and higher approximations of pi."""

    a,b = 2 * math.sqrt(3), 3   # seed the iteration with the hexagon
                                # a_n = 6 * 2**n * tan(theta_n)
                                # b_n = 6 * 2**n * sin(theta_n)
                                # theta_n = pi / (6 * 2**n)

    for i in xrange(n):
        a,b = archistep((a,b))

    return a,b