New-style classes + __getattr__ sucks, here is why:
[sympy.git] / examples / pidigits.py
blob37a0b23add6eb0099698a6450d6e943dcfb55c8d
1 #!/usr/bin/env python
2 import iam_sympy_example
4 from sympy.numerics import *
5 from sympy.numerics.utils_ import *
6 from sympy.numerics.constants import pi_float
7 import math
8 from time import clock
10 def display_fraction(digits, skip=0, colwidth=10, columns=5):
11 perline = colwidth * columns
12 printed = 0
13 for linecount in range((len(digits)-skip) // (colwidth * columns)):
14 line = digits[skip+linecount*perline:skip+(linecount+1)*perline]
15 for i in range(columns):
16 print line[i*colwidth : (i+1)*colwidth],
17 print ":", (linecount+1)*perline
18 if (linecount+1) % 10 == 0:
19 print
20 printed += colwidth*columns
21 rem = (len(digits)-skip) % (colwidth * columns)
22 if rem:
23 buf = digits[-rem:]
24 s = ""
25 for i in range(columns):
26 s += buf[:colwidth].ljust(colwidth+1, " ")
27 buf = buf[colwidth:]
28 print s + ":", printed + colwidth*columns
30 def calculateit(func, base, n, tofile):
31 Float.setprec(100)
32 intpart = small_numeral(int(float(func())), base)
33 if intpart == 0:
34 skip = 0
35 else:
36 skip = len(intpart)
37 Float.setprec(int(n*math.log(base,2))+10)
38 print "Step 1 of 2: calculating binary value..."
39 t = clock()
40 a = func()
41 step1_time = clock() - t
42 print "Step 2 of 2: converting to specified base..."
43 t = clock()
44 d = bin_to_radix(a.man, -a.exp, base, n)
45 d = fixed_to_str(d, base, n)
46 step2_time = clock() - t
47 print "\nWriting output...\n"
48 if tofile:
49 out_ = sys.stdout
50 sys.stdout = tofile
51 print "%i base-%i digits of pi:\n" % (n, base)
52 print intpart, ".\n"
53 display_fraction(d, skip, colwidth=10, columns=5)
54 if tofile:
55 sys.stdout = out_
56 print "\nFinished in %f seconds (%f calc, %f convert)" % \
57 ((step1_time + step2_time), step1_time, step2_time)
59 def interactive():
60 print "Compute digits of pi with SymPy\n"
61 base = input("Which base? (2-36, 10 for decimal) \n> ")
62 digits = input("How many digits? (enter a big number, say, 10000)\n> ")
63 tofile = raw_input("Output to file? (enter a filename, or just press enter\nto print directly to the screen) \n> ")
64 if tofile:
65 tofile = open(tofile, "w")
66 global_options["verbose"] = True
67 global_options["verbose_base"] = base
68 calculateit(pi_float, base, digits, tofile)
69 raw_input("\nPress enter to close this script.")
71 interactive()