2010-10-18 22:48:51 -05:00
|
|
|
"""
|
|
|
|
Pi - calculate pi over time via the monte carlo method. idea borrowed from #linode
|
|
|
|
Copyright (C) 2010 Brian S. Stephan
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2012-12-21 11:24:16 -06:00
|
|
|
|
2010-10-18 22:48:51 -05:00
|
|
|
"""
|
|
|
|
|
|
|
|
import math
|
|
|
|
import random
|
2011-01-07 20:37:24 -06:00
|
|
|
import re
|
2012-07-27 02:18:01 -05:00
|
|
|
|
|
|
|
import MySQLdb as mdb
|
2010-10-18 22:48:51 -05:00
|
|
|
|
|
|
|
from Module import Module
|
|
|
|
|
|
|
|
class Pi(Module):
|
|
|
|
|
2012-12-21 11:24:16 -06:00
|
|
|
"""Use the Monte Carlo method to approximate pi.
|
|
|
|
|
|
|
|
Each time this method is called, it calculates a random point inside a
|
|
|
|
square on the xy plane. If that point also falls within a circle bound
|
|
|
|
within that square, it is added to the count of points inside. Over time,
|
|
|
|
4 * count_inside / count approaches pi.
|
|
|
|
|
|
|
|
Idea from #linode on OFTC.
|
|
|
|
Code from http://www.eveandersson.com/pi/monte-carlo-circle
|
|
|
|
|
2010-10-18 22:48:51 -05:00
|
|
|
"""
|
|
|
|
|
2010-10-27 18:02:26 -05:00
|
|
|
def db_init(self):
|
2012-12-21 11:24:16 -06:00
|
|
|
"""Initialize database tables."""
|
2010-10-27 18:02:26 -05:00
|
|
|
|
|
|
|
# init the database if pi table doesn't exist
|
|
|
|
version = self.db_module_registered(self.__class__.__name__)
|
|
|
|
if version == None:
|
|
|
|
# create tables
|
|
|
|
db = self.get_db()
|
|
|
|
try:
|
2012-07-27 02:18:01 -05:00
|
|
|
version = 1
|
|
|
|
cur = db.cursor(mdb.cursors.DictCursor)
|
|
|
|
cur.execute('''
|
2010-10-27 18:02:26 -05:00
|
|
|
CREATE TABLE pi_log (
|
2012-07-27 02:18:01 -05:00
|
|
|
id SERIAL,
|
2010-10-27 18:02:26 -05:00
|
|
|
count_inside INTEGER NOT NULL,
|
|
|
|
count_total INTEGER NOT NULL,
|
2012-07-27 02:18:01 -05:00
|
|
|
time TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
2012-07-27 14:57:41 -05:00
|
|
|
) ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_bin
|
2010-10-27 18:02:26 -05:00
|
|
|
''')
|
2012-07-27 02:18:01 -05:00
|
|
|
cur.execute('''
|
2010-10-27 18:02:26 -05:00
|
|
|
CREATE VIEW pi_latest_pi AS
|
|
|
|
SELECT count_inside, count_total
|
|
|
|
FROM pi_log
|
2010-10-27 19:32:09 -05:00
|
|
|
ORDER BY id DESC
|
2010-10-27 18:02:26 -05:00
|
|
|
''')
|
|
|
|
db.commit()
|
2012-07-27 02:18:01 -05:00
|
|
|
self.db_register_module_version(self.__class__.__name__, version)
|
|
|
|
except mdb.Error as e:
|
2010-10-27 18:02:26 -05:00
|
|
|
db.rollback()
|
2012-07-27 02:18:01 -05:00
|
|
|
self.log.error("database error trying to create tables")
|
|
|
|
self.log.exception(e)
|
2010-10-27 18:02:26 -05:00
|
|
|
raise
|
2012-07-27 02:18:01 -05:00
|
|
|
finally: cur.close()
|
2010-10-27 18:02:26 -05:00
|
|
|
|
2011-01-07 23:09:07 -06:00
|
|
|
def do(self, connection, event, nick, userhost, what, admin_unlocked):
|
2011-01-07 20:37:24 -06:00
|
|
|
match = re.search('^!pi$', what)
|
|
|
|
if match:
|
2010-10-27 18:02:26 -05:00
|
|
|
db = self.get_db()
|
2010-10-18 22:48:51 -05:00
|
|
|
try:
|
2012-07-27 02:18:01 -05:00
|
|
|
cur = db.cursor(mdb.cursors.DictCursor)
|
|
|
|
cur.execute('SELECT * FROM pi_latest_pi')
|
|
|
|
datum = cur.fetchone()
|
2010-10-18 22:48:51 -05:00
|
|
|
|
2010-10-27 18:02:26 -05:00
|
|
|
if datum == None:
|
|
|
|
count_inside = 0
|
|
|
|
count = 0
|
|
|
|
else:
|
|
|
|
# load values
|
|
|
|
count_inside = datum['count_inside']
|
|
|
|
count = datum['count_total']
|
2010-10-18 22:48:51 -05:00
|
|
|
|
|
|
|
x = random.random()
|
|
|
|
y = random.random()
|
|
|
|
inside = False
|
|
|
|
d = math.hypot(x,y)
|
|
|
|
if d < 1:
|
|
|
|
inside = True
|
|
|
|
count_inside += 1
|
|
|
|
count += 1
|
|
|
|
pi = 4.0 * count_inside / count
|
|
|
|
|
2012-07-27 02:18:01 -05:00
|
|
|
cur.execute('INSERT INTO pi_log (count_inside, count_total) VALUES (%s,%s)',
|
|
|
|
(count_inside, count))
|
2010-10-27 18:02:26 -05:00
|
|
|
db.commit()
|
2012-07-27 02:18:01 -05:00
|
|
|
except mdb.Error as e:
|
2010-10-27 18:02:26 -05:00
|
|
|
db.rollback()
|
2012-07-27 02:18:01 -05:00
|
|
|
self.log.error("database error doing pi stuff")
|
|
|
|
self.log.exception(e)
|
2012-12-19 20:51:35 -06:00
|
|
|
return self.irc.reply(event,
|
2012-07-29 09:45:19 -05:00
|
|
|
"database error while estimating pi: {0:s}".format(str(e)))
|
2012-07-27 02:18:01 -05:00
|
|
|
finally: cur.close()
|
2010-10-18 22:48:51 -05:00
|
|
|
|
2012-12-19 20:51:35 -06:00
|
|
|
return self.irc.reply(event,
|
2012-12-18 20:12:26 -06:00
|
|
|
"({0:.10f}, {1:.10f}) is {2}within the unit circle. "\
|
2012-07-29 09:45:19 -05:00
|
|
|
"pi is {5:.10f}. (i:{3:d} p:{4:d})".format(x, y,
|
|
|
|
"" if inside else "not ",
|
|
|
|
count_inside, count, pi))
|
2010-10-18 22:48:51 -05:00
|
|
|
|
|
|
|
# vi:tabstop=4:expandtab:autoindent
|
|
|
|
# kate: indent-mode python;indent-width 4;replace-tabs on;
|