Permalink
Browse files

Added pi calculation scripts in the sandbox

  • Loading branch information...
dvarrazzo committed Apr 21, 2011
1 parent 0af14e6 commit 75a9400484f2026e54d5f03b4adc6cfaf0da7014
Showing with 161 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +55 −0 sandbox/pi/calc_pi.py
  3. +24 −0 sandbox/pi/calc_pi.sh
  4. +81 −0 sandbox/pi/pi.sql
View
@@ -11,4 +11,5 @@ docs/html
docs/_static/jsMath/
sandbox/hello/hello
sandbox/bench/*.png
+sandbox/pi/*.txt
dist/*
View
@@ -0,0 +1,55 @@
+#!/usr/bin/env python
+"""
+A script to calculate pi using parallel PostgreSQL processes.
+
+Copyright (C) 2011 Daniele Varrazzo
+"""
+
+import eventlet
+eventlet.patcher.monkey_patch()
+
+import sys
+import psycopg2
+
+def main():
+ dsn = 'dbname=regression'
+ nprocs = 4
+ ndigits = int(sys.argv[1])
+
+ def arccot((mult, arg)):
+ print >>sys.stderr, 'start', arg
+ cnn = psycopg2.connect(dsn)
+ cnn.set_isolation_level(0)
+ cur = cnn.cursor()
+ cur.execute("""
+ insert into pi
+ values (%s, arccot(%s, 10::mpz ^ (%s + 10)))
+ returning %s;
+ """, (mult, arg, ndigits, arg))
+ rv = cur.fetchone()[0]
+ cnn.close()
+ print >>sys.stderr, 'end', arg
+
+ cnn = psycopg2.connect(dsn)
+ cnn.set_isolation_level(0)
+ cur = cnn.cursor()
+ cur.execute("""
+ drop table if exists pi;
+ create table pi (mult int4, arccot mpz);
+ """)
+
+ pool = eventlet.GreenPool(nprocs)
+ list(pool.imap(arccot, [
+ (183, 239),
+ (32, 1023),
+ (-68, 5832),
+ (12, 110443),
+ (-12, 4841182),
+ (-100, 6826318), ]))
+
+ cur.execute("select 4 * sum(mult * arccot) / (10::mpz ^ 10) from pi;")
+ print cur.fetchone()[0]
+
+if __name__ == '__main__':
+ main()
+
View
@@ -0,0 +1,24 @@
+#!/bin/bash
+
+# A script to calculate pi using parallel backends
+# See calc_pi.py for a more advanced version.
+#
+# Copyright (C) 2011 Daniele Varrazzo
+
+export DBARGS=regression
+export NDIGITS=$1
+
+psql -q -c "drop table if exists pi;" $DBARGS
+psql -q -c "create table pi (mult int4, arccot mpz);" $DBARGS
+
+psql -Atq -c "insert into pi values (183, arccot(239, 10::mpz ^ ($NDIGITS + 10))) returning 1;" $DBARGS >&2 &
+psql -Atq -c "insert into pi values (32, arccot(1023, 10::mpz ^ ($NDIGITS + 10))) returning 2;" $DBARGS >&2 &
+psql -Atq -c "insert into pi values (-68, arccot(5832, 10::mpz ^ ($NDIGITS + 10))) returning 3;" $DBARGS >&2 &
+wait;
+psql -Atq -c "insert into pi values (12, arccot(110443, 10::mpz ^ ($NDIGITS + 10))) returning 4;" $DBARGS >&2 &
+psql -Atq -c "insert into pi values (-12, arccot(4841182, 10::mpz ^ ($NDIGITS + 10))) returning 5;" $DBARGS >&2 &
+psql -Atq -c "insert into pi values (-100, arccot(6826318, 10::mpz ^ ($NDIGITS + 10))) returning 6;" $DBARGS >&2 &
+wait;
+
+psql -At -c "select 4 * sum(mult * arccot) / (10::mpz ^ 10) from pi;" $DBARGS
+
View
@@ -0,0 +1,81 @@
+-- Some functions to calculate pi digits using mpz integers.
+--
+-- Reference:
+-- http://en.literateprograms.org/Pi_with_Machin%27s_formula_%28Python%29
+--
+-- Copyright (C) 2011 Daniele Varrazzo
+
+CREATE FUNCTION arccot(x mpz, unity mpz) RETURNS mpz
+ LANGUAGE plpgsql IMMUTABLE STRICT
+ AS $$
+DECLARE
+ xp mpz := unity / x;
+ xp2 mpz := -(x ^ 2);
+ acc mpz := xp;
+ term mpz;
+ n mpz := 3;
+BEGIN
+ LOOP
+ xp := xp / xp2;
+ term := xp / n;
+ EXIT WHEN term = 0;
+ acc := acc + term;
+ n := n + 2;
+ END LOOP;
+ RETURN acc;
+END
+$$;
+
+
+CREATE FUNCTION pi_machin(ndigits integer) RETURNS mpz
+ LANGUAGE plpgsql IMMUTABLE STRICT
+ AS $$
+DECLARE
+ unity mpz = 10::mpz ^ (ndigits + 10);
+BEGIN
+ RETURN 4 * (
+ 4 * arccot(5, unity)
+ - arccot(239, unity)
+ ) / (10::mpz ^ 10);
+END
+$$;
+
+
+CREATE FUNCTION pi_hwang_97(ndigits integer) RETURNS mpz
+ LANGUAGE plpgsql IMMUTABLE STRICT
+ AS $$
+DECLARE
+ unity mpz = 10::mpz ^ (ndigits + 10);
+BEGIN
+ RETURN 4 * (
+ 183 * arccot(239, unity)
+ + 32 * arccot(1023, unity)
+ - 68 * arccot(5832, unity)
+ + 12 * arccot(110443, unity)
+ - 12 * arccot(4841182, unity)
+ - 100 * arccot(6826318, unity)
+ ) / (10::mpz ^ 10);
+END
+$$;
+
+
+CREATE FUNCTION pi_hwang_03(ndigits integer) RETURNS mpz
+ LANGUAGE plpgsql IMMUTABLE STRICT
+ AS $$
+DECLARE
+ unity mpz = 10::mpz ^ (ndigits + 10);
+BEGIN
+ RETURN 4 * (
+ 183 * arccot(239, unity)
+ + 32 * arccot(1023, unity)
+ - 68 * arccot(5832, unity)
+ + 12 * arccot(113021, unity)
+ - 100 * arccot(6826318, unity)
+ - 12 * arccot(33366019650, unity)
+ + 12 * arccot(43599522992503626068::mpz, unity)
+ ) / (10::mpz ^ 10);
+END
+$$;
+
+
+

0 comments on commit 75a9400

Please sign in to comment.