import tensorflow as tf
import numpy as np
from PIL import Image
from IPython.display import Image as IPythonImage, display
# 查看 Tensorflow 版本
tf.__version__
'2.1.0'
复数的运算
a = 3 + 4j # 一个复数
abs(a) # 计算复数的模
5.0
a = np.array([3 + 4j, 6+8j]) # 复数数组
abs(a) # 计算复数数组的模
array([ 5., 10.])
a < 3 + 5j # 复数的分量大小比较
array([ True, False])
np.array([3+2j, 3]) < 3+4j
array([ True, True])
class Fractal:
def __init__(self, start_point=(0,0), radius=2, epoches=200, zoom_radio=1.0):
self.start_point = start_point # 迭代的起点
self.radius = radius # 迭代的半径
self.epoches = epoches # 迭代次数
self.zoom_radio = zoom_radio # 放大倍率
self.ax_size = 4.0, 3.0 # 绘制图的横轴, 纵轴大小
self.step = 0.005 # 绘制点的步长
@property
def x(self):
x0 = self.start_point[0]
a = self.ax_size[0]
p = 2.0 * self.zoom_radio
return np.arange(-a/p+x0, a/p+x0, self.step)
@property
def y(self):
y0 = self.start_point[1]
a = self.ax_size[1]
p = 2.0 * self.zoom_radio
return np.arange(a/p+y0, -a/p+y0, -self.step)
@property
def c(self):
cx, cy = np.meshgrid(self.x, self.y)
c = cx + cy*1j
return c
self = Fractal()
np.where(abs(self.c) < self.radius, 0, 3) # 与 tf.where 功能一样
array([[3, 3, 3, ..., 3, 3, 3],
[3, 3, 3, ..., 3, 3, 3],
[3, 3, 3, ..., 3, 3, 3],
...,
[3, 3, 3, ..., 3, 3, 3],
[3, 3, 3, ..., 3, 3, 3],
[3, 3, 3, ..., 3, 3, 3]])
画图
R = 4 # 迭代半径
ITER_NUM = 200 # 迭代次数
def get_color(ratio1, ratio2, ratio3):
def color(z, i):
if abs(z) < R:
return 0, 0, 0
v = np.log2(i + R - np.log2(np.log2(abs(z)))) / 4
if v < 1.0:
return v**4, v**2.5, v ** 1
else:
v = max(0, 2 - v)
return v**ratio1, v**ratio2, v**ratio3
return color
def gen_mandelbrot(Z, ratio1, ratio2, ratio3):
xs = tf.constant(Z.astype(np.complex64))
zs = tf.Variable(xs)
ns = tf.Variable(tf.zeros_like(xs, tf.float32))
for i in range(ITER_NUM):
zs_ = tf.where(tf.abs(zs) < R, zs**2 + xs, zs)
not_diverged = tf.abs(zs_) < R
zs = zs_
ns = ns + tf.cast(not_diverged, tf.float32)
r, g, b = np.frompyfunc(get_color(ratio1, ratio2, ratio3), 2, 3)(zs_, ns)
img_array = np.dstack((r, g, b))
return Image.fromarray(np.uint8(img_array * 255))
start_x = -2.5 # x range
end_x = 1
start_y = -1.2 # y rangea
end_y = 1.2
width = 1000
step = (end_x - start_x) / width
Y, X = np.mgrid[start_y:end_y:step, start_x:end_x:step]
Z = X + 1j * Y
img = gen_mandelbrot(Z, 1, 1.5, 3)
img.save('mandelbrot.png')
display(img)

output_12_0.png
# Elephant Valley
start_x = 0.275 # x range
end_x = 0.28
start_y = 0.006 # y range
end_y = 0.01
width = 1000
step = (end_x - start_x) / width
Y, X = np.mgrid[start_y:end_y:step, start_x:end_x:step]
Z = X + 1j * Y
img = gen_mandelbrot(Z, 0.9, 0.6, 0.6)
img.save('mandelbrot.png')
display(img)

output_13_0.png
start_x = -0.090 # x range
end_x = -0.086
start_y = 0.654 # y range
end_y = 0.657
width = 1000
ratio1, ratio2, ratio3 = 0.2, 0.6, 0.6
step = (end_x - start_x) / width
Y, X = np.mgrid[start_y:end_y:step, start_x:end_x:step]
Z = X + 1j * Y
img = gen_mandelbrot(Z, ratio1, ratio2, ratio3)
img.save('mandelbrot.png')
display(img)

output_14_0.png
start_x = -0.750 # x range
end_x = -0.747
start_y = 0.099 # y range
end_y = 0.102
width = 1000
step = (end_x - start_x) / width
Y, X = np.mgrid[start_y:end_y:step, start_x:end_x:step]
Z = X + 1j * Y
img = gen_mandelbrot(Z, 0.1, 0.1, 0.3)
img.save('mandelbrot.png')
display(img)

output_15_0.png
sierpinski carpet
from matplotlib import pyplot as plt
from matplotlib import colors
import matplotlib as mpl
import numpy as np
def carpet(degre):
size = 3**degre
matrix = np.ones([size, size])
for niveau in range(degre+1):
step = 3**(degre-niveau)
for x in range(size):
if x % 3 == 1:
for y in range(size):
if y % 3 == 1:
matrix[y*step:(y+1)*step, x*step:(x+1)*step] = 0
return matrix
degre = 7
matrix = carpet(degre)
zoom = 1 # default: 1
bouge = [
0, # droite - gauche
0 # haut-bas
]
mpl.rcParams['toolbar'] = 'None' # erase buttons
fig = plt.figure(dpi=72, frameon=False)
cmap = 'Purples'
# 将颜色替换此处
# cmaps['Sequential'] = [
# 'Greys', 'Purples', 'Blues', 'Greens', 'Oranges', 'Reds',
# 'YlOrBr', 'YlOrRd', 'OrRd', 'PuRd', 'RdPu', 'BuPu',
# 'GnBu', 'PuBu', 'YlGnBu', 'PuBuGn', 'BuGn', 'YlGn']
ax = plt.Axes(fig, [0., 0., 1., 1.])
ax.set_axis_off()
fig.add_axes(ax)
img = ax.imshow(matrix, cmap, interpolation="bilinear")
plt.show()

output_17_0.png
网友评论