Fractal Dimension Computation in Python Code
Huaiyu Zhu
hzhu at users.sourceforge.net
Fri Sep 29 22:37:35 EDT 2000
On Fri, 29 Sep 2000 20:01:10 -0000, Jake Speed <speed@?.com> wrote:
>
>And having said that, here's an updated version. Note
>that in the examples the points aren't limited to the
>0.0 -> 1.0 range anymore.
Hi,
I've changed your code to use Numeric module so that it is applicable to any
dimension of the containing space. This can also be used as a module.
"""
fractaldim2.py - calculate fractal dimensions
Adopted by Huaiyu Zhu from
From: speed@?.com (Jake Speed)
Newsgroups: comp.lang.python
Subject: Re: Fractal Dimension Computation in Python Code
Date: Fri, 29 Sep 2000 20:01:10 -0000
"""
from math import log
from Numeric import *
def N(points, scale):
"""Return (num of coverage, scale) of points by boxes of size 1/scale"""
unique = {}
for point in points:
box = tuple((point * scale).astype(Int))
unique[box] = 1
return float(len(unique)), scale
def dim(points):
""" Calculate dimensions of points at various scale = 2**level"""
f0 = 1, 1
for level in xrange(1, 12):
f1 = N(points, 2.0**level)
dim = log(f1[0]/f0[0]) / log(f1[1]/f0[1])
print "%2d: %.4g" % (level, dim)
f0 = f1
M = array([[0,-1], [1,0]]) * sqrt(3)/2
def makekoch(x1, x2, level, flist=[]):
""" Return a list of points on Koch curve, subdivided levels """
if level == 0: return flist
xd = (x2 - x1) / 3.
xa = x1 + xd
xb = x2 - xd
xm = (x1 + x2) / 2.0 - matrixmultiply(M, xd)
flist.append(xa)
makekoch(x1, xa, level-1, flist)
makekoch(xa, xm, level-1, flist)
flist.append(xm)
makekoch(xm, xb, level-1, flist)
makekoch(xb, x2, level-1, flist)
flist.append(xb)
return flist
#------------------------------------------------------------------
if __name__ == "__main__":
print "line dimension"
x = (arange(100)/100.)[:,NewAxis]
dim(x)
print "plane dimension"
list2d = []
for x in xrange(100):
for y in xrange(100):
list2d.append(array((x, y))/100.0)
dim(list2d)
print "space dimension"
list3d = []
for x in xrange(30):
for y in xrange(30):
for z in xrange(30):
list3d.append(array((x, y, z))/30.0)
dim(list3d)
print "koch dimension"
listkoch = makekoch(array((0,0)), array((1,0)), 7)
dim(listkoch)
--
Huaiyu Zhu hzhu at users.sourceforge.net
Matrix for Python Project http://MatPy.sourceforge.net
More information about the Python-list
mailing list