我試圖創建一個天真的數值積分函數來說明蒙特卡洛積分在高維上的好處。我想是這樣的:如何在numpy中創建一個n維網格來評估任意n的函數?
def quad_int(f, mins, maxs, numPoints=100):
'''
Use the naive (Riemann sum) method to numerically integrate f on a box
defined by the mins and maxs.
INPUTS:
f - A function handle. Should accept a 1-D NumPy array
as input.
mins - A 1-D NumPy array of the minimum bounds on integration.
maxs - A 1-D NumPy array of the maximum bounds on integration.
numPoints - An integer specifying the number of points to sample in
the Riemann-sum method. Defaults to 100.
'''
n = len(mins)
# Create a grid of evenly spaced points to evaluate f on
# Evaluate f at each point in the grid; sum all these values up
dV = np.prod((maxs-mins/numPoints))
# Multiply the sum by dV to get the approximate integral
我知道我的dV
將會導致與數值穩定性問題,但現在我在與正在創建的域什麼麻煩。如果維度的數量是固定的,這將是很容易只使用np.meshgrid
這樣的:
# We don't want the last value since we are using left-hand Riemann sums
x = np.linspace(mins[0],maxs[0],numPoints)[:-1]
y = np.linspace(mins[1],maxs[1],numPoints)[:-1]
z = np.linspace(mins[2],maxs[2],numPoints)[:-1]
X, Y, Z = np.meshgrid(x,y,z)
tot = 0
for index, x in np.ndenumerate(X):
tot += f(x, Y[index], Z[index])
是否有一個模擬到np.meshgrid
可以爲任意尺寸做到這一點,也許接受陣列的一個元組?或者是否有其他方式可以在更高維度上進行黎曼和?我想過遞歸地做這件事,但無法弄清楚它是如何工作的。
謝謝,這對於創建域非常適用。切片數組時,有沒有辦法解開元組?也就是說,給定'np.ndenumerate'中的'index',是否有一種簡單的方法可以訪問切片'X [:,index [0],...,index [n-1]]?'現在我使用列表理解,但似乎應該有一個更快的方法。 – 2015-03-03 20:31:13
我添加了一些索引筆記 – hpaulj 2015-03-03 21:39:53