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