分形

作者: 水之心 | 来源:发表于2020-08-20 21:03 被阅读0次
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

相关文章

  • 分形创新-自我分形

    讲到分形创新,我对自己的职业生涯做一下剖析。 我进入眼镜行业之前尝试过很多行业,都没有坚持太久。直到进入眼镜行业,...

  • 分形

    Classic Iterated Function Systems: Sierpinski Carpet danc...

  • 分形

    当红利消失殆尽的时候,唯有创新才能提供源源不断的增长。教授这句话意味深远,生活中也到处可见创新的影子,比如过去我们...

  • 分形

    分形之城 原题链接[https://www.acwing.com/activity/content/problem...

  • 分形星空

    APO7X软件作的图片…致敬梵高的星空

  • 11 分形

    “大道无形,生育天地;大道无情,运行日月;大道无名,长养万物;吾不知其名,强名曰道。” 不知其所云,然不明觉厉。天...

  • 雪花分形

    时常在看完一本精彩小说后,冒出什么时候自己也写一本的念头。 被各种文章鼓舞,每天动手写一百几十字,几十天之后,发觉...

  • 分形理论

    分形理论(Fractal Theory)是当今十分风靡和活跃的新理论、新学科。 分形的概念是美籍数学家本华·曼德博...

  • 分形创新

    分形原理 我们去观察树木会发现分支上有分支,一再重复那种分支过程,而且尺度越来越小。我们人类的肺与血管也遵...

  • 分形创新

    我想分享一个真实案例来说说身边的分形创新。 青岛的一家眼镜店。老板T先生最早是在武汉学校旁边开店,后来来到青岛。进...

网友评论

      本文标题:分形

      本文链接:https://www.haomeiwen.com/subject/fuwejktx.html