Trieste Lectures 2015

From Werner KRAUTH

Jump to: navigation, search

Lecture 1: Programs

Lecture 2: Programs

This is markov-disks-box.py

import random

L = [[0.25, 0.25], [0.75, 0.25], [0.25, 0.75], [0.75, 0.75]]
sigma = 0.15
sigma_sq = sigma ** 2
delta = 0.1
n_steps = 1000
for steps in range(n_steps):
    a = random.choice(L)
    b = [a[0] + random.uniform(-delta, delta), a[1] + random.uniform(-delta, delta)]
    min_dist = min((b[0] - c[0]) ** 2 + (b[1] - c[1]) ** 2 for c in L if c != a)
    box_cond = min(b[0], b[1]) < sigma or max(b[0], b[1]) > 1.0 - sigma
    if not (box_cond or min_dist < 4.0 * sigma ** 2):
        a[:] = b
print L

This is lift-two-disks.py

import math, random

def dist(x, y):
    d_x = abs(x[0] - y[0]) % 1.0
    d_x = min(d_x, 1.0 - d_x)
    d_y = abs(x[1] - y[1]) % 1.0
    d_y = min(d_y, 1.0 - d_y)
    return d_x**2 + d_y**2

L = [[0.25, 0.25], [0.75, 0.75]]
sigma = 0.18
for steps in range(10000):
    delta = random.uniform(0.0, 0.04)
    lift = random.choice([0, 1])
    a = L[lift]
    dirc = random.choice([0, 1])
    for inner_steps in range(100):
          b = a[:]
          b[dirc] += delta
          distance = dist(b, L[int( not lift)])
          if  distance > 4.0 * sigma ** 2:
              a[:] = [b[0] % 1.0, b[1] % 1.0]
          else:
              lift = int(not lift)
              a = L[lift]

This is event-chain.py

import random, math

def event(a, b, dirc, sigma):
    d_perp = abs(b[not dirc] - a[not dirc]) % 1.0
    d_perp = min(d_perp, 1.0 - d_perp)
    if d_perp > 2.0 * sigma:
        return float("inf")
    else:
        d_para = math.sqrt(4.0 * sigma ** 2 - d_perp ** 2)
        return (b[dirc] - a[dirc] - d_para + 1.0) % 1.0

L = [[0.25, 0.25], [0.25, 0.75], [0.75, 0.25], [0.75, 0.75]]
ltilde = 0.819284; sigma = 0.15
for iter in xrange(20000):
    dirc = random.randint(0, 1)
    print iter, dirc, L
    distance_to_go = ltilde
    next_a = random.choice(L)
    while distance_to_go > 0.0:
        a = next_a
        event_min = distance_to_go
        for b in [x for x  in L if x != a]:
            event_b = event(a, b, dirc, sigma)
            if event_b < event_min:
                next_a = b
                event_min = event_b
        a[dirc] = (a[dirc] + event_min) % 1.0
        distance_to_go -= event_min
Personal tools