gpt4 book ai didi

python - 为什么 Python 的 numpy.polyval() 这么慢?

转载 作者:太空狗 更新时间:2023-10-30 00:07:08 25 4
gpt4 key购买 nike

更新脚本中有错误。

我正在研究 Julia 和 Mandelbrot 集以及牛顿分形的可视化 - 为此,我需要计算复平面中的很多值。我可以使用我想要的任何类型的数学函数,但对于多项式来说已经足够了。

我需要计算函数/多项式的导数和值,所以我查看了 numpy 模块并找到了 numpy.polyder() numpy.polyval()。它看起来正是我需要的东西,但突然间我的脚本变得非常慢。

我试图想出一些简单的测试来显示时间差异。为此,我编写了以下脚本:

import numpy as np
import cmath
import time
from itertools import product

C = 0.37 + 0.45j
pol = [1,0,0]

start_time = time.time()
for i in xrange(100000):
C = np.polyval(pol, C)

print "Polyval: {}".format( time.time() - start_time )
print C

C = 0.37 + 0.45j # forgot to reassign the initial value of C
start_time = time.time()
for i in xrange(100000):
C = C**2

print "Standard: {}".format( time.time() - start_time )
print C

基本上这个脚本计算了多项式 g(C) = C**2 的很多值。及时的结果是(程序的实际输出):

Polyval: 2.34903216362
0j
Standard: 0.0198249816895
0j

我可能没有以最好的方式设置这个测试,第一次做这样的事情。但是即使有任何错误,运行我的其他脚本在时间上也会有很大的不同。

问题

有没有办法让它更快?我知道调用另一个函数很耗时,但仍然如此。我是否应该重新考虑只能在一个地方更改多项式系数的优势,而不是及时的劣势?关于如何处理此类问题的任何其他建议?

最佳答案

本身不是答案,但我编写了一个函数 polyval_factory 来生成一个 mypolyval 函数,给定一个系数数组,如 pol。它以字符串形式生成像 C*C + 1.0 * C *C*C + 2.0 这样的快速表达式,然后将它们封装在 lambda 函数中。基本上它使用字符串代替像 sympy 这样的完整符号代数库。此函数在下面的示例中定义和测试,并且几乎与 C*C 一样快:

import numpy as np
import cmath
import time
from itertools import product

def polyval_factory(pol):
"""
Generate a lambda function for evaluating a given polynomial

pol : a list of coefficients with highest degree first

Note: this function basically uses strings in lieu of a
fully symbolic algebra package like sympy
"""
poly_string_list = []
for i, coeff in enumerate(pol[::-1]):

if np.abs(coeff) > 1e-10:
if i > 1:
poly_string_list.append( repr(coeff) + '*' + '*'.join(['x']*i))
elif i == 1:
poly_string_list.append(repr(coeff)+'*x')
elif i ==0:
poly_string_list.append(repr(coeff))

lambda_str = 'lambda x :' + '+'.join(poly_string_list)

print "The prepared lambda function is: \""+lambda_str + "\""

return eval(lambda_str)

C = 0.37 + 0.45j
pol = [1,0,0]

numiter = 30000

start_time = time.time()
for i in xrange(numiter):
C = np.polyval(pol, C)

print "Polyval: {}".format( time.time() - start_time )
print C

C = 0.37 + 0.45j # forgot to reassign the initial value of C
print ""
print "Generating lambda function..."
mypolyval = polyval_factory(pol) # generate the lambda function
print ""

start_time = time.time()
for i in xrange(numiter):
C = mypolyval(C)
print "Polyval_factory: {}".format( time.time() - start_time )
print C

C = 0.37 + 0.45j # forgot to reassign the initial value of C
print ""
start_time = time.time()
for i in xrange(numiter):
C = C**2
print "Standard: {}".format( time.time() - start_time )
print C

输出是:

Polyval: 0.738290071487
0j

Generating lambda function...
The prepared lambda function is: "lambda x :1*x*x"

Polyval_factory: 0.013610124588
0j

Standard: 0.00678110122681
0j

编辑:polyval_factory:现在运行 mypolyval = polyval_factory([2.0,3.0,1.0]) 做适当的事情并打印:

    The prepared lambda function is: "lambda x :1.0+3.0*x+2.0*x*x"

关于python - 为什么 Python 的 numpy.polyval() 这么慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24150553/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com