简体   繁体   English

如何快速计算插值二维数组上的一维积分?

[英]How to quickly calculate a 1D integral over an interpolated 2D array?

Given is a geometrical object, for simplification a semisphere with a certain radius.给定一个几何 object,用于简化具有一定半径的半球。 This is displayed as a 2D matrix with the Z data being the height.这显示为二维矩阵,Z 数据为高度。 Assuming that I cut the object along any line, I want to calculate the area of the cut.假设我沿着任意一条线切割 object,我想计算切割的面积。 My solution is to interpolate the semisphere using scipys RectBivariateSpline to accurately display it.我的解决方案是使用 scipys RectBivariateSpline 插入半球以准确显示它。

import numpy as np
import scipy.interpolate as intp

radius = 15.
gridsize = 0.5
spectrum = np.arange(-radius,radius+gridsize,gridsize)
X,Y = np.meshgrid(spectrum,spectrum)
Z = np.where(np.sqrt(X**2+Y**2)<=radius, np.sqrt(radius**2-np.sqrt(X**2+Y**2)**2), 0)

spline = intp.RectBivariateSpline(x = X[0,:], y = Y[:,0], z = Z)

#Example coordinates of the cut
x0 = -4.78
x = -6.73
y0 = -15.
y = 15.

However, the RectBivariateSpline only offers an area integral (which can be quickly checked by setting x0 = x or y0 = y).但是,RectBivariateSpline 仅提供面积积分(可以通过设置 x0 = x 或 y0 = y 快速检查)。 On the other hand the UnivariateSpline only takes in 1D array, which would only work if my cut happened to be along one specific vector of the matrix Z.另一方面,UnivariateSpline 只接受一维数组,这只有在我的剪切恰好沿着矩阵 Z 的一个特定向量时才有效。

Since I want to perform this operation a few thousand times, I would need a comparably quick way to solve the integral (numerically or analytically doesn't matter as long as the error is somewhat negligible).由于我想执行此操作几千次,因此我需要一种相对快速的方法来求解积分(只要误差可以忽略不计,数值上或分析上都无关紧要)。 Does anyone have an idea on how to do this?有谁知道如何做到这一点?

It turned out, that, for my case, it was sufficient to sample the spline along my cut (using numpy's arange to gather equally spaced points) and then by integrating via the Simpson rule, which only requires a number of points with a sufficiently low distance (which can be controlled via arange's step parameter).事实证明,就我而言,沿着我的切割对样条曲线进行采样就足够了(使用 numpy 的 arange 来收集等间距的点),然后通过辛普森规则进行积分,这只需要足够低的点数距离(可以通过 arange 的 step 参数控制)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM