-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 2718e83
Showing
9 changed files
with
298 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"cells": [], | ||
"metadata": {}, | ||
"nbformat": 4, | ||
"nbformat_minor": 4 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"python.pythonPath": "C:\\ProgramData\\Anaconda3\\python.exe" | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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的鼠标事件的交互功能,但是实在没精力了。 | ||
随缘更新吧。 | ||
|
||
<!-- 现实太残酷了。 --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |