PHP 中的插值
我正在寻找 PHP 中的一个函数,用于将一组不规则放置的数据 (x,y,z) 插值到网格数据集,以便在 JPGraph 的 ContourPlot 函数中使用它。我开发了一个基于简单反距离加权的函数,但它太慢了。我需要使用另一种方法,例如“修改的谢泼德方法”或任何其他可能的更准确的方法,以使其更快、更平滑。
这是我当前的代码:
for($i = 0, $ij = 0; $i < $gridX; $i ++) {
for($j = 0; $j < $gridY; $j ++, $ij ++) {
$x = $startP->x + ($deltaX * $i);
$y = $startP->y + ($deltaY * $j);
$g [$ij]->i = $i;
$g [$ij]->j = $j;
$g [$ij]->x = ( int ) $x;
$g [$ij]->y = ( int ) $y;
$g [$ij]->z = IDW_U ( $x, $y, $sampleData, $sampleSize, $p );
}
}
function IDW_U($x, $y, $data, $size, $p) {
$idw_sum = IDWeightSum ( $x, $y, $data, $size, $p );
$idw_u = 0.0;
for($k = 0; $k < $size; $k ++) {
if ($x == $data [$k]->x && $y == $data [$k]->y)
return $data [$k]->z;
$idw_u += IDWeight ( $x, $y, $data [$k], $p ) * $data [$k]->z / $idw_sum;
}
return $idw_u;
}
function IDWeightSum($x, $y, $data, $size, $p) {
$sum = 0.0;
for($k = 0; $k < $size; $k ++)
$sum += IDWeight ( $x, $y, $data [$k], $p );
return $sum;
}
function IDWeight($x, $y, $d, $p) {
if ($x == $d->x && $y == $d->y)
return 1.0;
$dx = $x - $d->x;
$dy = $y - $d->y;
$ret = 1.0 / pow ( sqrt ( pow ( $dx, 2 ) + pow ( $dy, 2 ) ), $p );
return $ret;
}
有人知道可用于此目的的函数或库吗?
I'm looking for a function in PHP for interpolating a set of irregular placed data (x,y,z) to a gridded data set for using it in ContourPlot function in JPGraph. I've developed a function based on simple Inverse distance weighting, but it is too slow. I need to use another method like "Modified Shepard's Method" or any other possible methods with more accuracy to make it faster and smoother.
Here is my current code:
for($i = 0, $ij = 0; $i < $gridX; $i ++) {
for($j = 0; $j < $gridY; $j ++, $ij ++) {
$x = $startP->x + ($deltaX * $i);
$y = $startP->y + ($deltaY * $j);
$g [$ij]->i = $i;
$g [$ij]->j = $j;
$g [$ij]->x = ( int ) $x;
$g [$ij]->y = ( int ) $y;
$g [$ij]->z = IDW_U ( $x, $y, $sampleData, $sampleSize, $p );
}
}
function IDW_U($x, $y, $data, $size, $p) {
$idw_sum = IDWeightSum ( $x, $y, $data, $size, $p );
$idw_u = 0.0;
for($k = 0; $k < $size; $k ++) {
if ($x == $data [$k]->x && $y == $data [$k]->y)
return $data [$k]->z;
$idw_u += IDWeight ( $x, $y, $data [$k], $p ) * $data [$k]->z / $idw_sum;
}
return $idw_u;
}
function IDWeightSum($x, $y, $data, $size, $p) {
$sum = 0.0;
for($k = 0; $k < $size; $k ++)
$sum += IDWeight ( $x, $y, $data [$k], $p );
return $sum;
}
function IDWeight($x, $y, $d, $p) {
if ($x == $d->x && $y == $d->y)
return 1.0;
$dx = $x - $d->x;
$dy = $y - $d->y;
$ret = 1.0 / pow ( sqrt ( pow ( $dx, 2 ) + pow ( $dy, 2 ) ), $p );
return $ret;
}
Does anybody know a function or library available for this purpose?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
据我所知,IDWeight 被经常调用。通过计算 IDW_U ,您可以将调用次数减少一半:
我认为主要问题是,对于一个像素的计算,使用了所有数据集,而其中大多数数据集的影响很小。通过将数据分割成小区域并单独计算所有区域,您确实可以提高性能。
As far as I can see IDWeight is called quite often. You could half the number of calls to that by calculating IDW_U like that:
I think the main problem is, that for the calculation of one pixel, all datasets are used, while most of them will have small impact. You could really increase the performance by splitting the data into small areas and calculate all areas seperatly.
我不确定 PHP 是否是此类数学密集型功能的不错选择。然而,有很多可用的图形库,其中的代码已经过优化,放入 DLL 等中。
我们使用高级软件工程 ChartDirector PHP 图表来绘制一些相当复杂的图表,而且速度很快。我不确定它是否包含您感兴趣的算法,但它确实包含一些像 LOWESS 的算法。我看到的主要问题是您正在处理 X、Y 和 Z。处理第三维并不是最常见的功能。我不确定这个库是否真的支持......
I am not that sure PHP would be a good choice for math intensive functionality like that. However, there are a lot of graphing libraries available where the code has been optimized, put into DLL's and things like that.
We have used Advanced Software Engineering ChartDirector PHP Charting for some pretty complex graphs and it is fast. I don't know for sure if it includes the algorithm you are interested in, but it does include some like LOWESS. The main problem I see is that you are dealing with X, Y, and Z. The handling the third dimension is not the most common feature. I am not sure this library will actually support that...
如果您不依赖 PHP,那么您应该考虑尽可能多地放弃 PHP 进行密集处理。 PHP 是一种速度较慢的语言(我上次查看时大约一年前,Python 和 Ruby 以及 C、C++ 和 Java 都优于它。
因此,通过切换到离线绘图工具(例如前面提到的 gnuplot)在评论中),重新运行性能测试,并在必要时将算法切换到 Python 或 Ruby 脚本或编译的 C 或 C++ 应用程序,您将获得性能提升。
但是,我找不到任何更新的性能数据。自 2008 年末/2009 年初以来,我对 PHP 与其他语言的比较 - 我的数据可能不再真实。
If you aren't tied to PHP, you should look at switching away from it for as much of this intensive processing as you can. PHP is a slower language (last time I looked - about a year ago, both Python and Ruby outperformed it, as well as C, C++, and Java.
So by switching to an off-line graphing tool (such as gnuplot, as mentioned in the comments), rerunning your performance tests, and if necessary, switching the algorithm to a Python or Ruby script or a compiled C or C++ application, you would get a performance boost.
However, I couldn't find any more recent performance data on PHP compared to other languages since late 2008/early 2009 - my data might not be true anymore.
它可能会帮助您通过引用传递数据:
It might help you to pass the data by reference: