About

Here we calculate the frequencies distribution for the corners of all paths going to the right or upwards from the bottom-left to the top-right of a regular lattice of size n x m.

In [4]:
import sympy
import matplotlib.pyplot as plt
import numpy as np
In [5]:
fact = sympy.combinatorial.numbers.factorial
nC = sympy.combinatorial.numbers.nC
In [6]:
def totalPaths(n, m):
    return nC(n + m, n)


def pathsWithOddCorners(n, m, r):
    return 2 * nC((n - 1), (r - 1) / 2) * nC((m - 1), (r - 1) / 2)


def pathsWithEvenCorners(n, m, r):
    return nC(n - 1, r / 2) * nC(m - 1, (r - 2) / 2) + nC(n - 1, (r - 2) / 2) * nC(m - 1, r / 2)
In [7]:
%matplotlib inline
In [8]:
n, m = 500, 300
tpaths = totalPaths(n, m)

evenCorners = np.array(xrange(2, 2 * min(n, m)))
oddCorners = np.array(xrange(1, 2 * min(n, m) + 1, 2))
In [10]:
totalEvenCorners = [pathsWithEvenCorners(n, m, c) for c in evenCorners]
totalOddCorners = [pathsWithOddCorners(n, m, c) for c in oddCorners]
In [11]:
evenDistribuition = [corner / float(tpaths) for corner in totalEvenCorners]
oddDistribuition = [corner / float(tpaths) for corner in totalOddCorners]
In [31]:
plt.plot(evenCorners, evenDistribuition, 'r-', oddCorners, oddDistribuition, 'b-')
plt.title('Corners distribuition in a %s x %s lattice' % (n, m))
plt.xlabel('Number of corners')
plt.ylabel('Corners density')
plt.legend(('Even corners', 'Odd corners'), loc="upper left")
Out[31]:
<matplotlib.legend.Legend at 0x65dc650>