Skip to content

Commit

Permalink
Add to GitHub.
Browse files Browse the repository at this point in the history
  • Loading branch information
LummiGhost committed Oct 22, 2020
0 parents commit 2718e83
Show file tree
Hide file tree
Showing 9 changed files with 298 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .ipynb_checkpoints/Untitled-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"cells": [],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 4
}
43 changes: 43 additions & 0 deletions .ipynb_checkpoints/try2-checkpoint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import numpy as np
import matplotlib.pyplot as plt
import os

#定义迭代函数
iter_func = lambda z,c:(z**2 + c)
def calc_steps(c,max_iter_num = 128):
z = complex(0,0)
num = 0
while abs(z)<2 and num <max_iter_num:
z = iter_func(z , c)
num = num+1
if num == max_iter_num:
return z.real
else:
return num
def display_mandelbrot(xArea=[-2,2],yArea=[-2,2],num = 1000):
X,Y = np.meshgrid(np.linspace(xArea[0],xArea[1],num+1),np.linspace(yArea[1],yArea[0],num+1))
C = X + Y*1j
result = np.zeros((num+1,num+1))
plt.ion()
plt.hot()
#计算出结果
for i in range(num+1):
for j in range(num+1):
result[i,j] = calc_steps(C[i,j])
if i*100%num == 0 :
# print( i / num * 100 )
plt.imshow(result,vmax=abs(result).max(),vmin=abs(result).min(),extent = xArea + yArea)
plt.show(block=False)
plt.pause(0.1)
# threading._start_new_thread(plt.pause,(0.5,))
# plt.pause(0.5)
# plt.savefig("fig1.png")



if __name__ =="__main__":
# display_mandelbrot([-0.5226,-0.5225],[0.6243,0.6244])
# display_mandelbrot([0.2537269133080432,0.2537269133080432+0.0000000000000009],[0.000365995381749671,0.000365995381749671+0.0000000000000009],100)
# display_mandelbrot(num = 200)
display_mandelbrot([-0.56,-0.53],[-0.55,-0.52],4096)
input()
15 changes: 15 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: 当前文件",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
}
]
}
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"python.pythonPath": "C:\\ProgramData\\Anaconda3\\python.exe"
}
105 changes: 105 additions & 0 deletions Untitled.ipynb

Large diffs are not rendered by default.

Binary file added __pycache__/try2.cpython-38.pyc
Binary file not shown.
34 changes: 34 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# 生成曼德勃罗集图像

**GPU版本暂时无法使用!!!**
**GPU版本暂时无法使用!!!**
**GPU版本暂时无法使用!!!**

随便写的,玩玩而已,功能还不完善。
借鉴了其他大佬的思路。

## 依赖库

- numpy
- matplotlab
- numba (cuda GPU support)(但是GPU版目前用不了)

```shell
pip install numpy,matplotlib,numba
```

## 实例

```python
from try2 import mandelbrot
import matplotlab as mp
mp.hot()
mandelbrot([-0.56,-0.53],[-0.55,-0.52],100)
#前两个参数为需要生成的图形的区域(建议传入方形区域,不然可能会变形。),最后一个参数是方阵的阶数。
mp.show()
```

目前还有很多要改的地方。本来还想加入捕获matplotlab的鼠标事件的交互功能,但是实在没精力了。
随缘更新吧。

<!-- 现实太残酷了。 -->
39 changes: 39 additions & 0 deletions try2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import numpy as np
import matplotlib.pyplot as plt
import os

#定义迭代函数
iterFunction = lambda z,c:(z**2 + c)
def calcPoints(c,maxIterNum = 128):
z = complex(0,0)
num = 0
while abs(z)<2 and num <maxIterNum:
z = iterFunction(z , c)
num = num+1
if num == maxIterNum:
return z.real
else:
return num

def mandelbrot(xArea=[-2,2],yArea=[-2,2],num = 1000):
X,Y = np.meshgrid(np.linspace(xArea[0],xArea[1],num+1),np.linspace(yArea[1],yArea[0],num+1))
C = X + Y*1j
result = np.zeros((num+1,num+1))
for i in range(num+1):
for j in range(num+1):
result[i,j] = calcPoints(C[i,j])
##打印百分比
if i*10%num == 0 :
print( i / num * 100 )
plt.imshow(result,vmax=abs(result).max(),vmin=abs(result).min(),extent = xArea + yArea)
return result



if __name__ =="__main__":
# display_mandelbrot([-0.5226,-0.5225],[0.6243,0.6244])
# display_mandelbrot(num = 200)
plt.hot()
mandelbrot([-0.56,-0.53],[-0.55,-0.52],100)
# plt.imshow()
plt.show()
53 changes: 53 additions & 0 deletions try2_gpu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import numpy as np
import matplotlib.pyplot as plt
import os
import math
from numba import cuda

#定义迭代函数
iterFunc = lambda z,c:(z**2 + c)
maxIterNum = 128
class index2D:
def __init__(self,x,y):
self.x = x
self.y = y

@cuda.jit
def calcMandelbrot(img,C,accuracy):
idx = index2D(cuda.blockIdx.x * cuda.blockDim.x + cuda.threadIdx.x , cuda.blockIdx.y * cuda.blockDim.y + cuda.threadIdx.y)
z = complex(0,0)
num = 0
if idx.x<accuracy and idx.y<accuracy:
while abs(z)<2 and num <maxIterNum:
z = iterFunc(z , C[idx.x,idx.y])
num = num+1
if num == maxIterNum:
img[idx.x,idx.y] = z.real
else:
img[idx.x,idx.y] = num



def mandelbrot(xArea = [-2,2],yArea = [-2,2],accuracy = 1000):
X,Y = np.meshgrid(np.linspace(xArea[0],xArea[1],accuracy+1),np.linspace(yArea[1],yArea[0],accuracy+1))
C = X + Y*1j
img = np.zeros((accuracy+1,accuracy+1))
# resurt = np.zeros((accuracy+1,accuracy+1))

#GPU计算
threadsPerBlock = 1024
blocksPerGrid = math.ceil(accuracy / threadsPerBlock)
calcMandelbrot[blocksPerGrid,threadsPerBlock](img,C,accuracy)
cuda.synchronize()
return img



if __name__ =="__main__":
# display_mandelbrot(x_num=200,y_num=200)
# display_mandelbrot([-1,0],[0,1],x_num=200,y_num=200)
# display_mandelbrot([-0.5226,-0.5225],[0.6243,0.6244])
img = mandelbrot()
# os.system('pause')
# display_mandelbrot()
# input()

0 comments on commit 2718e83

Please sign in to comment.