def ex632():
	n = 2**8
	rule = 110
	ratio = 2
	ca = CA(rule, n, ratio = ratio)
	#ca.start_random()
	m = ratio * n / 2
	ca.array[0, m-1] = 1
	ca.array[0, m] = 1
	for i in range(20):
		ca.array[0, m+i] = 1
	ca.start_single()
	ca.loop(n-1)
	return ca
def fractal_dim(ca_rule, t = 100, plot = True):
	""" plot box counting dimension and return estimated dimension"""
	ca = CA(ca_rule, t)
	ca.start_single()
	Ns = [1]
	drawer = PyplotDrawer()
	for i in range(t-1):
		ca.step()
		Ns.append(sum(ca.array[ca.next-1]))
	Ns = np.cumsum(Ns)
	one_over_eps = range(1, ca.n + 1)
	dim, _ = np.polyfit(np.log(one_over_eps), np.log(Ns), 1)
	if (plot):
		pyplot.plot(one_over_eps, Ns)
		scale = 'log'
		pyplot.xscale(scale)
		pyplot.yscale(scale)
		pyplot.title('')
		pyplot.xlabel('1/eps')
		pyplot.ylabel('N(eps)')
		pyplot.show()
		drawer.draw(ca)
		drawer.show()
	return dim