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.
import sympy
import matplotlib.pyplot as plt
import numpy as np
fact = sympy.combinatorial.numbers.factorial
nC = sympy.combinatorial.numbers.nC
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)
%matplotlib inline
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))
totalEvenCorners = [pathsWithEvenCorners(n, m, c) for c in evenCorners]
totalOddCorners = [pathsWithOddCorners(n, m, c) for c in oddCorners]
evenDistribuition = [corner / float(tpaths) for corner in totalEvenCorners]
oddDistribuition = [corner / float(tpaths) for corner in totalOddCorners]
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")
<matplotlib.legend.Legend at 0x65dc650>