我正在编写一个用Python重现Barnsley蕨类植物的脚本,但是我得到了一些不同的东西:
这是我的代码:
def iterate_vec(f, x0, a, steps=100):
"""x0: initial vector
a: parameter to f(x,a)"""
n = len(x0) # state dimension
x = np.zeros((steps+1, n))
x[0] = x0
for k in range(steps):
x[k+1] = f(x[k], a)
return(x)
def barnsley(x, A):
"""Barnley's fern:
x: initial point in 2D
a: matrices [A_0,...,A_m-1]"""
m = A.shape # should be (4, 2, 2), last column for bias
fs = [0,1,2,3]
i = f = np.random.choice(fs, p=[0.01, 0.85, 0.07, 0.07]) # choose one, in this case m[0] = 4, hence a randint btw 0 and 4
y = A[i] @ np.append(x[0],1) #in this case either first or second matrices are multiplied by vector x
return(y)
x = iterate_vec(barnsley, [0,0], A, 10000)
print(x.shape)
plt.plot(x[:,0], x[:,1], '.', markersize=0.5)
这是我的矩阵A:
A = np.array([
[[0, 0],
[0, 0.16]],
[[0.85, 0.04],
[-0.04, 0.85]],
[[0.2, -0.26],
[0.23, 0.22]],
[[-0.15, 0.28],
[0.26, 0.24]],
])
答案 0 :(得分:1)
这看起来像一个有趣的问题。将您的代码与Wikipedia上的描述进行比较,似乎您缺少转换中的常量。我添加了以下内容。
def barnsley(x, A):
"""Barnley's fern:
x: initial point in 2D
a: matrices [A_0,...,A_m-1]"""
m = A.shape # should be (4, 2, 2), last column for bias
fs = [0,1,2,3]
i = f = np.random.choice(fs, p=[0.01, 0.85, 0.07, 0.07])
y = A[i] @ np.append(x[0], x[1])
if i == 1 or i == 2:
y[1] += 1.6
elif i == 3:
y[1] += 0.44
return(y)