- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试绘制方波的傅立叶序列,但是对于许多项,该程序需要太多时间来计算所有点。我尝试使用多处理模块但没有成功。请帮助我如何使用多处理。我正在 fedora linux 上运行,并拥有 AMD FX Octa 核心。谢谢
#!/usr/bin/env python
import pylab,math
#Square Wave approximation by Fourier Series:
#y=4/pi*(sin(x) + sin(3x)/3 + sin(5x)/5 + ... n) terms
n=input("how many terms do you want?")
y=[]
# generate x's to calculate the points.
x=pylab.arange(0,360,0.5)
#generate de odd numbers to add the sines terms
impar=range(1,n+2,2)
no=4/math.pi #just for normalize the sequence to converge to 1 (not pi/4)
#calculate the points
for ps in x:
a=0
for i in impar:
a=a+(((math.sin(math.radians(i*ps)))/i))
a=no*a
y.append(a)
#plot
pylab.figure()
pylab.plot(x,y,"o")
pylab.title(str(n)+str(" terms"))
pylab.grid()
#save a image(just in case)
pylab.savefig(str(n)+str("sqwv.png"))
#show the graph
pylab.show()
最佳答案
这个问题是并行的,所以很容易让脚本使用多个CPU。建立在 @HYRY's answer 之上:
from multiprocessing.dummy import Pool # use threads
def compute_y(i):
np.add.reduce(no * np.sin(np.radians(impar * x[i])) / impar, out=y[i])
step = 10**8 // n
Pool().map(compute_y, (slice(j, j + step) for j in range(0, len(x), step)))
矢量化 numpy 操作释放了 GIL,因此该代码应该能够利用多个 CPU。
完整脚本:
#!/usr/bin/env python
"""Square Wave approximation by Fourier Series."""
import math
from multiprocessing.dummy import Pool # use threads
from timeit import default_timer as timer
import matplotlib.pyplot as plt
import numpy as np
n = 100000 # compute first n terms in the series
# generate x's to calculate the points.
x = np.arange(0, 360, .1)
# generate odd numbers to add the sines terms
impar = np.c_[1:n+2:2]
no = 4 / math.pi # just for normalize the sequence to converge to 1 (not pi/4)
# calculate the points
start = timer()
# y = 4/pi*(sin(x) + sin(3x)/3 + sin(5x)/5 + ... n) terms
y = np.empty_like(x)
def compute_y(i):
t = impar * x[i]
np.radians(t, out=t)
np.sin(t, out=t)
t *= no
t /= impar
np.add.reduce(t, out=y[i])
step = 10**8 // n
Pool().map(compute_y, (slice(j, j + step) for j in range(0, len(x), step)))
print("y calc. takes us %.2f seconds" % (timer() - start,))
# plot
plt.plot(x, y, "-")
plt.title("%d terms" % n)
plt.grid()
# save a image(just in case)
plt.savefig("%dsqwv.png" % n)
# show the graph
plt.show()
关于Python 多处理 pylab 傅里叶,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17139222/
我注意到pylab中有一些名称重复。假设我导入 pylab。以下是一些示例: 例如1: pylab.ion() pylab.plt.ion() 例如2 pylab.figure(1) pylab.pl
我希望情节线覆盖文本。这是当前代码: pylab.plot( data[:,0], data[:,1], color='red', lw=3, label='Temp' ) pylab.figtext
这个问题在这里已经有了答案: pylab.ion() in python 2, matplotlib 1.1.1 and updating of the plot while the program
我正在使用 pylab 绘制一些数据,一切都按我的预期完美运行。我有 6 个不同的图表要绘制,我可以将它们单独绘制在单独的图中。但是当我尝试 subplot() 这些图表时,最后一个 (subplot
我有两个数组 x、y 以及函数的坐标。 示例: x=[0,1,2,3,4,5] y=[0,1,5,20,30,32] pylab.plot(x,y) 向我展示了一个平滑的函数。 有没有办法获取y=3的
我正在使用以下代码制作一个非常简单的 pandas/pylab 条形图: df2 = pd.DataFrame(np.random.rand(10, 4), columns=['a', 'b', 'c
我正在使用 PyBrain,在文档中它使用 PyLab...我尝试导入 PyLab 并且它有效,但是当我尝试使用其中的任何模块时,python 返回错误。 >>> import pylab
我需要修改我的图例。 noise_mu 具有诸如 1.6e-29 之类的值,我希望它能够以科学的方式准确绘制。不幸的是,我的图例总是以 0.000000000000000000000016 的形式打印
我有一个 mac OS X Yosimite,我使用 python 2.7.10 和 Pycharm 作为我的 IDLE。我已经正确安装了 pylab,但无法使用它的任何模块。 尝试时: from p
我正在尝试关注 this PCA tutorial并且在尝试使用 启动 pylab 时遇到了障碍 $pylab inline 我在哪里遇到语法错误。我正在使用 Anaconda/Spyder 和 Py
我正在使用 PyLab 在 Python 中制作一些图表。我想制作一个带有黑色文本的洋红色文本框,但无法将文本变为黑色。 text(x, y, 'Summary', backgroundcolor =
我访问了scipy site for PyLab.我在那里找不到它的文档。 matplotlib site也没有提供任何关于它的信息。 我在哪里可以找到 PyLab 的教程/文档? 最佳答案 Pyla
我确定这是一个非常基本的问题,但我似乎找不到正确的代码。我正在创建的箱线图有我的代码。我想标记轴并有一个标题。 from pylab import * import numpy raw_data =
我不断看到人们在各种代码片段中使用 %pylab,尤其是使用 iPython。但是,我看不到“学习 Python”(以及我拥有的其他几本 Python 书籍)中的任何地方提到了 %pylab,也不确定
我已经在 Mac (OSX Lion) 上安装了 iPython + SciPy Superpack。 如果我使用 matplotlib.pyplot 绘图,它会弹出一个带有图形的窗口,我关闭它以便
我有用于绘制以下捕食者猎物模型的代码: dx/dt = x − xy, dy/dt = −y + xy from pylab import * xvalues, yvalues = meshgrid(
我正在绘制图表并使用 pylab.poly1d 函数绘制最佳拟合线。但最佳拟合线不够长。我怎样才能延长这条线。这是我的代码: y=np.genfromtxt('_02total.txt').T[0]
我正在尝试绘制方波的傅立叶序列,但是对于许多项,该程序需要太多时间来计算所有点。我尝试使用多处理模块但没有成功。请帮助我如何使用多处理。我正在 fedora linux 上运行,并拥有 AMD FX
我有一个小代码,用此代码生成以下图片: 代码1: hist, rhist = np.histogram(r, bins=40, range=(0, 0.25)) hist = -hist/np.tra
是否可以使 pylab.show() 不会忘记? 我的意思是这样的: x = np.linspace(0, 10, 1000) y = np.sin(x) pylab.plot(x, y) pylab
我是一名优秀的程序员,十分优秀!