• <i id="lxwbaq"></i>

            • <dl id="lxwbaq"><ol id="lxwbaq"><u id="lxwbaq"><noframes id="lxwbaq"><span id="lxwbaq"><tfoot id="lxwbaq"><tfoot id="lxwbaq"></tfoot></tfoot></span>
                • <td id="lxwbaq"><dl id="lxwbaq"><strong id="lxwbaq"></strong></dl></td>

                  真人麻将游戏 | 如何用Python画各种著名数学图案 | 附图+代码

                  66科技网

                  2018年08月19日 12:00


                  大数据文摘作品,转载具体要求见文末
                  编译团队:Aileen,徐凌霄
                  用Python绘制著名的数学图片或动画,展示数学中的算法魅力。
                  本项⽬目将持续更更新,数学中有着太多有趣的事物可以⽤用代码去展示。 欢迎提出建议和参与建设!
                  后台回复“数学”查看完整代码集哦
                  Mandelbrot 集
                  代码:46 lines (34 sloc)1.01 KB
                  """A fast Mandelbrot set wallpaper rendererreddit discussion: https://www.reddit.com/r/math/comments/2abwyt/smooth_colour_mandelbrot/"""import numpy as npfrom PIL import Imagefrom numba import jitMAXITERS = 200RADIUS = 100@jitdef color(z, i):v = np.log2(i + 1 - np.log2(np.log2(abs(z)))) / 5if v < 1.0:return v**4, v**2.5, velse:v = max(0, 2-v)return v, v**1.5, v**3@jitdef iterate(c):z = 0jfor i in range(MAXITERS):if z.real*z.real + z.imag*z.imag > RADIUS:return color(z, i)z = z*z + creturn 0, 0 ,0def main(xmin, xmax, ymin, ymax, width, height):x = np.linspace(xmin, xmax, width)y = np.linspace(ymax, ymin, height)z = x[None, :] + y[:, None]*1jred, green, blue = np.asarray(np.frompyfunc(iterate, 1, 3)(z)).astype(np.float)img = np.dstack((red, green, blue))Image.fromarray(np.uint8(img*255)).save("mandelbrot.png")if __name__ == "__main__":main(-2.1, 0.8, -1.16, 1.16, 1200, 960)
                  多米诺洗牌算法
                  代码链接:https://github.com/neozhaoliang/pywonderland/tree/master/src/domino
                  正二十面体万花筒
                  代码:53 lines (40 sloc)1.24 KB
                  """A kaleidoscope pattern with icosahedral symmetry."""import numpy as npfrom PIL import Imagefrom matplotlib.colors import hsv_to_rgbdef Klein(z):"""Klein"s j-function"""return 1728 * (z * (z**10 + 11 * z**5 - 1))**5 / \(-(z**20 + 1) + 228 * (z**15 - z**5) - 494 * z**10)**3def RiemannSphere(z):""" map the complex plane to Riemann"s sphere via stereographic projection """t = 1 + z.real*z.real + z.imag*z.imagreturn 2*z.real/t, 2*z.imag/t, 2/t-1def Mobius(z):""" distort the result image by a mobius transformation """return (z - 20)/(3*z + 1j)def main(imgsize):x = np.linspace(-6, 6, imgsize)y = np.linspace(6, -6, imgsize)z = x[None, :] + y[:, None]*1jz = RiemannSphere(Klein(Mobius(Klein(z))))# define colors in hsv spaceH = np.sin(z[0]*np.pi)**2S = np.cos(z[1]*np.pi)**2V = abs(np.sin(z[2]*np.pi) * np.cos(z[2]*np.pi))**0.2HSV = np.dstack((H, S, V))# transform to rgb spaceimg = hsv_to_rgb(HSV)Image.fromarray(np.uint8(img*255)).save("kaleidoscope.png")if __name__ == "__main__":import timestart = time.time()main(imgsize=800)end = time.time()print("runtime: {:3f} seconds".format(end - start))
                  Newton 迭代分形
                  代码:46 lines (35 sloc)1.05 KB
                  import numpy as npimport matplotlib.pyplot as pltfrom numba import jit# define functions manually, do not use numpy"s poly1d funciton!@jit("complex64(complex64)", nopython=True)def f(z):# z*z*z is faster than z**3return z*z*z - 1@jit("complex64(complex64)", nopython=True)def df(z):return 3*z*z@jit("float64(complex64)", nopython=True)def iterate(z):num = 0while abs(f(z)) > 1e-4:w = z - f(z)/df(z)num += np.exp(-1/abs(w-z))z = wreturn numdef render(imgsize):x = np.linspace(-1, 1, imgsize)y = np.linspace(1, -1, imgsize)z = x[None, :] + y[:, None] * 1jimg = np.frompyfunc(iterate, 1, 1)(z).astype(np.float)fig = plt.figure(figsize=(imgsize/100.0, imgsize/100.0), dpi=100)ax = fig.add_axes([0, 0, 1, 1], aspect=1)ax.axis("off")ax.imshow(img, cmap="hot")fig.savefig("newton.png")if __name__ == "__main__":import timestart = time.time()render(imgsize=400)end = time.time()print("runtime: {:03f} seconds".format(end - start))
                  李代数E8 的根系

                  代码链接:https://github.com/neozhaoliang/pywonderland/blob/master/src/misc/e8.py
                  模群的基本域

                  代码链接:
                  https://github.com/neozhaoliang/pywonderland/blob/master/src/misc/modulargroup.py
                  彭罗斯铺砌

                  代码链接:
                  https://github.com/neozhaoliang/pywonderland/blob/master/src/misc/penrose.py
                  Wilson 算法
                  代码链接:https://github.com/neozhaoliang/pywonderland/tree/master/src/wilson
                  反应扩散方程模拟
                  代码链接:https://github.com/neozhaoliang/pywonderland/tree/master/src/grayscott
                  120 胞腔
                  代码:69 lines (48 sloc)2.18 KB
                  # pylint: disable=unused-import# pylint: disable=undefined-variablefrom itertools import combinations, productimport numpy as npfrom vapory import *class Penrose(object):GRIDS = [np.exp(2j * np.pi * i / 5) for i in range(5)]def __init__(self, num_lines, shift, thin_color, fat_color, **config):self.num_lines = num_linesself.shift = shiftself.thin_color = thin_colorself.fat_color = fat_colorself.objs = self.compute_pov_objs(**config)def compute_pov_objs(self, **config):objects_pool = []for rhombi, color in self.tile():p1, p2, p3, p4 = rhombipolygon = Polygon(5, p1, p2, p3, p4, p1,Texture(Pigment("color", color), config["default"]))objects_pool.append(polygon)for p, q in zip(rhombi, [p2, p3, p4, p1]):cylinder = Cylinder(p, q, config["edge_thickness"], config["edge_texture"])objects_pool.append(cylinder)for point in rhombi:x, y = pointsphere = Sphere((x, y, 0), config["vertex_size"], config["vertex_texture"])objects_pool.append(sphere)return Object(Union(*objects_pool))def rhombus(self, r, s, kr, ks):if (s - r)**2 % 5 == 1:color = self.thin_colorelse:color = self.fat_colorpoint = (Penrose.GRIDS[r] * (ks - self.shift[s])- Penrose.GRIDS[s] * (kr - self.shift[r])) *1j / Penrose.GRIDS[s-r].imagindex = [np.ceil((point/grid).real + shift)for grid, shift in zip(Penrose.GRIDS, self.shift)]vertices = []for index[r], index[s] in [(kr, ks), (kr+1, ks), (kr+1, ks+1), (kr, ks+1)]:vertices.append(np.dot(index, Penrose.GRIDS))vertices_real = [(z.real, z.imag) for z in vertices]return vertices_real, colordef tile(self):for r, s in combinations(range(5), 2):for kr, ks in product(range(-self.num_lines, self.num_lines+1), repeat=2):yield self.rhombus(r, s, kr, ks)def put_objs(self, *args):return Object(self.objs, *args)
                  后台回复“数学”查看完整代码集哦
                  一人一笔 | 数据团队建设“全景报告”
                  清华数据科学研究院联合大数据文摘,发起一次数据团队全行业调研。本次调研将对国内外数据团队发展现状进行盘点和趋势预测,同时探索数据团队应如何建设。我们将结合一系列专访与调查问卷内容,在7月初发布《数据团队建设全景报告》。
                  如果你是数据团队的一员、和数据团队一起工作,或者希望了解其他数据团队的发展现状和未来。
                  那么恳请你花费5分钟时间,点击“阅读原文”填写问卷,帮助我们完成这次调研。
                  原文链接:https://github.com/neozhaoliang/pywonderland/blob/master/README.md

                  关于转载如需转载,请在开篇显著位置注明作者和出处(转自:大数据文摘 | bigdatadigest),并在文章结尾放置大数据文摘醒目二维码。无原创标识文章请按照转载要求编辑,可直接转载,转载后请将转载链接发送给我们;有原创标识文章,请发送【文章名称-待授权公众号名称及ID】给我们申请白名单授权。未经许可的转载以及改编者,我们将依法追究其法律责任。联系邮箱:zz@bigdatadigest.cn。志愿者介绍回复“志愿者”了解如何加入我们
                  往期精彩文章
                  点击图片阅读文章
                  想用R和Python做文本挖掘又不知如何下手?方法来了!