gpt4 book ai didi

python - 寻找用于在镶嵌域上进行数值积分的 Python 包

转载 作者:太空狗 更新时间:2023-10-29 21:44:33 25 4
gpt4 key购买 nike

我想知道是否有人知道基于 numpy/scipy 的 python 包可以在镶嵌域(在我的特定情况下,由 voronoi 单元界定的 2D 域)上对复杂的数值函数进行数值积分?过去我使用了几个来自 matlab 文件交换的包,但如果可能的话我想留在我当前的 python 工作流程中。 matlab例程是

http://www.mathworks.com/matlabcentral/fileexchange/9435-n-dimensional-simplex-quadrature

正交和网格生成使用:

http://www.mathworks.com/matlabcentral/fileexchange/25555-mesh2d-automatic-mesh-generation

如有任何关于网格生成以及该网格数值积分的建议,我们将不胜感激。

最佳答案

这直接对三角形积分,而不是 Voronoi 区域,但应该很接近。 (以不同数量的点运行以查看?)它也适用于 2d、3d ...

#!/usr/bin/env python
from __future__ import division
import numpy as np

__date__ = "2011-06-15 jun denis"

#...............................................................................
def sumtriangles( xy, z, triangles ):
""" integrate scattered data, given a triangulation
zsum, areasum = sumtriangles( xy, z, triangles )
In:
xy: npt, dim data points in 2d, 3d ...
z: npt data values at the points, scalars or vectors
triangles: ntri, dim+1 indices of triangles or simplexes, as from
http://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.Delaunay.html
Out:
zsum: sum over all triangles of (area * z at midpoint).
Thus z at a point where 5 triangles meet
enters the sum 5 times, each weighted by that triangle's area / 3.
areasum: the area or volume of the convex hull of the data points.
For points over the unit square, zsum outside the hull is 0,
so zsum / areasum would compensate for that.
Or, make sure that the corners of the square or cube are in xy.
"""
# z concave or convex => under or overestimates
npt, dim = xy.shape
ntri, dim1 = triangles.shape
assert npt == len(z), "shape mismatch: xy %s z %s" % (xy.shape, z.shape)
assert dim1 == dim+1, "triangles ? %s" % triangles.shape
zsum = np.zeros( z[0].shape )
areasum = 0
dimfac = np.prod( np.arange( 1, dim+1 ))
for tri in triangles:
corners = xy[tri]
t = corners[1:] - corners[0]
if dim == 2:
area = abs( t[0,0] * t[1,1] - t[0,1] * t[1,0] ) / 2
else:
area = abs( np.linalg.det( t )) / dimfac # v slow
zsum += area * z[tri].mean(axis=0)
areasum += area
return (zsum, areasum)

#...............................................................................
if __name__ == "__main__":
import sys
from time import time
from scipy.spatial import Delaunay

npt = 500
dim = 2
seed = 1

exec( "\n".join( sys.argv[1:] )) # run this.py npt= dim= ...
np.set_printoptions( 2, threshold=100, edgeitems=5, suppress=True )
np.random.seed(seed)

points = np.random.uniform( size=(npt,dim) )
z = points # vec; zsum should be ~ constant
# z = points[:,0]
t0 = time()
tessellation = Delaunay( points )
t1 = time()
triangles = tessellation.vertices # ntri, dim+1
zsum, areasum = sumtriangles( points, z, triangles )
t2 = time()

print "%s: %.0f msec Delaunay, %.0f msec sum %d triangles: zsum %s areasum %.3g" % (
points.shape, (t1 - t0) * 1000, (t2 - t1) * 1000,
len(triangles), zsum, areasum )
# mac ppc, numpy 1.5.1 15jun:
# (500, 2): 25 msec Delaunay, 279 msec sum 983 triangles: zsum [ 0.48 0.48] areasum 0.969
# (500, 3): 111 msec Delaunay, 3135 msec sum 3046 triangles: zsum [ 0.45 0.45 0.44] areasum 0.892

关于python - 寻找用于在镶嵌域上进行数值积分的 Python 包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5941113/

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