Skip to content

Instantly share code, notes, and snippets.

View drvinceknight's full-sized avatar
😀
👍

Vince Knight drvinceknight

😀
👍
View GitHub Profile
from __future__ import division
import random
from csv import reader
class Player:
"""
A class for a player
"""
def __init__(self, name):
self.name = name
import random
def utility(s1, s2):
if s1 == s2 == 'B':
return 4,4
if s1 == s2 == 'C':
return 2,2
if s1 == 'C' and s2 == 'B':
return 5,0
return 0,5
cost_of_items = {'apple': 4,
'cake': 6,
'doughnut': 12}
class ShoppingBasket:
"""
A class for a shopping basket with attributes for the cost and the contents of a shopping basket
"""
def __init__(self):
self.cost = 0 # Initialising a cost of 0 (basically the value of the basket when someone picks it up
import random
deck = ['1S','2S','3S','4S', '1H', '2H', '3H', '4H']
p1 = []
p2 = []
random.shuffle(deck)
while len(deck) > 0:
p1.append(deck.pop())
p2.append(deck.pop())
#!/bin/sh
tmux new-session -d # Create a new session
tmux split-window -v -p 25 # Create a horizontal split (25 % of the screen)
tmux split-window -h # Create a vertical split
tmux selectp -t 1 # Go to top pane
tmux -2 attach-session -d # Attach
"""
Going to simulate 100 '1st two days of xmas'
"""
from __future__ import division
import random
partidges = []
turtle_doves= []
for xmas in range(100):
partidges.append(random.choice([0,1,1,1,2]))
# Shortest possible simulation of an MM1 Q (with warmup). Contributors: Geraint, Jason, Vince, Harald and Chris.
from random import expovariate
lmbda, mu, T, warmup, waits, sample, arrival_time, start_time, end_time = 1, 2, 5000, 1000, [], lambda x: expovariate(x), 0, 0, 0
while end_time < T:
arrival_time += sample(lmbda)
start_time = max(end_time, arrival_time)
end_time = start_time + sample(mu)
if arrival_time > warmup: waits.append(start_time - arrival_time)
print 'Mean wait: ' + str(sum(waits) / len(waits))
for i in range(10):
print i
@drvinceknight
drvinceknight / Checking the exponential approximation
Last active August 29, 2015 14:07
There is a nice paper by Cleve Moler and Charles Van Loan called: "Nineteen Dubious Ways to Compute the Exponential of a Matrix" which is a return to a paper (by the same authors) called: "Nineteen Dubious Ways to Compute the Exponential of a Matrix". This interact simply looks at the Series method. The inputs allow control of the number of term…
@interact
def _(A=matrix(RDF,[[1,3,1],[5,-2,2],[3,3,4]]),n=("$n$",slider(0,100,1)),Precision=slider(0,10,1,4)):
exp_A=exp(A)
approx_exp=sum([A^i/factorial(i) for i in range(n+1)]).n(digits=Precision)
p=list_plot([(sum([A^i/factorial(i) for i in range(n+1)])-exp(A)).norm(2) for n in range(0,21)])
p.axes_labels(['$n$','$|| e^{A}-\sum_{i=0}^{n}A^{i}/{i!}||_2$'])
print "\n"
print "Value of approximation for n=%s: \n%s" % (n, approx_exp)
print "\n"
print "Exact value: \n%s" % exp(A)
@interact
def _(A=matrix(RDF,3,3,[8,2,0,1,7,2,1,3,6])/10,x_init=("$\pi^{(0)}$",vector([.2,.5,.3]))):
x_init = vector(x_init)
data = [x_init * (A^i) for i in range(20)]
pi = zip(*data)
p = line(enumerate(pi[0]),color='red',legend_label='$\pi_1$',thickness=3)
p += line(enumerate(pi[1]),color='blue',legend_label='$\pi_2$',thickness=3)
p += line(enumerate(pi[2]),color='green',legend_label='$\pi_3$',thickness=3)
p.ymax(1)
show(p)