问题描述
我在python中有一个函数(也使用scipy和numpy)定义为
I have a function in python (using scipy and numpy also) defined as
import numpy as np
from scipy import integrate
LCDMf = lambda x: 1.0/np.sqrt(0.3*(1+x)**3+0.7)
我想将其从0集成到一个numpy数组中的每个元素,例如 z = np.arange(0,100)
I would like to integrate it from 0 to each element in a numpy array say z = np.arange(0,100)
我知道我可以为每个循环遍历的元素编写一个循环
I know I can write a loop for each element iterating through like
an=integrate.quad(LCDMf,0,z[i])
但是,我想知道是否
推荐答案
在对<$ c进行修补之后,有一种更快,更有效(更简单)的方法来执行此操作。 $ c> np.vectorize 我找到了以下解决方案。简单-优雅并且有效!
After tinkering with np.vectorize
I found the following solution. Simple - elegant and it works!
import numpy as np
from scipy import integrate
LCDMf = lambda x: 1.0/math.sqrt(0.3*(1+x)**3+0.7)
np.vectorize(LCDMf)
def LCDMfint(z):
return integrate.quad(LCDMf, 0, z)
LCDMfint=np.vectorize(LCDMfint)
z=np.arange(0,100)
an=LCDMfint(z)
print an[0]
此方法适用于未排序的浮点数组或任何我们抛出的东西
This method works with unsorted float arrays or anything we throw at it and doesn't any initial conditions as in the odeint method.
我希望这也会对某人有所帮助...谢谢大家的投入。
I hope this helps someone somewhere too... Thanks all for your inputs.
这篇关于将函数与numpy数组的每个元素集成为集成的限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!