简体   繁体   English

未定义子程序 &PDL::divide

[英]Undefined subroutine &PDL::divide

I'm trying Perl's PDL in the following code:我正在以下代码中尝试 Perl 的 PDL:

#!/usr/bin/perl  -w
use strict;
use PDL::Core qw(pdl);
use PDL::Math qw(isfinite);
use PDL::Primitive qw(statsover);


my $div = 4;
my @array1 = (0..10);
my $pdl_array = log(pdl(@array1)/$div);
$pdl_array->where(!isfinite($pdl_array)) .= 0;
my($mean,$stdev) = statsover($pdl_array);
die $pdl_array,"\n",$mean," ",$stdev,"\n";

and I'm getting this error:我收到此错误:

Undefined subroutine &PDL::divide called at ./compare_const.pl line 10.未定义的子程序 &PDL::divide 在 ./compare_const.pl 第 10 行调用。

Any hint, please?Thanks a lot.有什么提示吗?非常感谢。

PDL is unusual in its design, and therefore has an unusual and somewhat fragile import mechanism. PDL 在设计上很不寻常,因此具有不寻常且有些脆弱的导入机制。 Each PDL module adds functionality to PDL by inserting a new method directly into PDL's package .每个 PDL 模块通过将新方法直接插入到 PDL 的包中来向 PDL 添加功能。 This decision was made very early in the design of PDL v2, and has not been changed in the intervening decade.这个决定是在 PDL v2 的设计早期就做出的,并且在此后的十年中没有改变。 (There's no reason it couldn't be changed, even in a backward compatible way, but none of the PDL developers have set aside the time to do so.) (没有理由不能改变它,即使以向后兼容的方式,但没有一个 PDL 开发人员留出时间这样做。)

As a result, you must load a handful of modules to ensure that PDL has its requisite, basic functionality.因此,您必须加载少量模块以确保 PDL 具有其必需的基本功能。 If you take a look at PDL's import function, you'll notice thatit explicitly loads a number of packages into the caller's namespace .如果您查看 PDL 的导入函数,您会注意到它显式地将许多包加载到调用者的命名空间中 The reason for this is good---splitting functionality across multiple modules to keep the distribution sane---but the implementation is not aligned with common Perl practices.这样做的原因是好的——将功能拆分到多个模块以保持分布合理——但实现与常见的 Perl 实践不一致。 This is why your attempt to import specific functions into your namespace failed.这就是您尝试将特定函数导入命名空间失败的原因。

The solution has already been explained.解决方法已经解释过了。 Either replace all of your use PDL::... statements with a single use PDL :要么用单个use PDL替换所有use PDL::...语句:

use strict;
use warnings;
use PDL;

my $div = 4;
...

or say use PDL::Lite (to ensure that PDL's package is complete) and then import the specific functions into your (main) package或者说use PDL::Lite (以确保 PDL 的包是完整的),然后将特定功能导入到您的(主)包中

use strict;
use warnings;

use PDL::Lite;
use PDL::Core qw(pdl);
use PDL::Math qw(isfinite);
use PDL::Primitive qw(statsover);

my $div = 4;
...

PDL has a minimum set of things that must be loaded. PDL 具有必须加载的最少内容集。 To get these all properly loaded, you must either use PDL (which also exports a bunch of stuff) or use PDL::Lite .要正确加载所有这些,您必须use PDL (它也导出一堆东西)或use PDL::Lite

(I for some reason thought you were explicitly calling PDL::divide directly and getting that error, hence my original answer below.) (出于某种原因,我认为您是直接调用 PDL::divide 并得到该错误,因此我在下面给出了原始答案。)

Original answer:原答案:

I'm wondering why you think that should work?我想知道为什么你认为这应该有效?

Yes, PDL exports a bunch of stuff (if you use it, which you do not), but that doesn't give you any guarantee about where it is exporting it from .是的,PDL 会导出一堆东西(如果您使用它,则不会),但这并不能保证您. (In point of fact, it appears to export from a number of different places directly into the use`ing package.) (事实上​​,它似乎从许多不同的地方直接导出到使用包中。)

If you are trying to avoid namespace pollution, I would recommend either importing into a designated package and using stuff from there:如果你想避免命名空间污染,我建议要么导入到指定的包中,然后使用那里的东西:

{
    package My::PDL;
    use PDL;
}
...
My::PDL::divide...

or using the OO interface (see PDL::Lite, I think?)或使用 OO 接口(请参阅 PDL::Lite,我想?)

Just add use PDL;只需添加use PDL; and your code will work:并且您的代码将起作用:

#!/usr/bin/perl  -w
use strict;

use PDL;
use PDL::Core qw(pdl);
use PDL::Math qw(isfinite);
use PDL::Primitive qw(statsover);

my $div = 4;
my @array1 = (0..10);
my $pdl_array = log( pdl(@array1) / $div );
$pdl_array->where(!isfinite($pdl_array)) .= 0;
my ($mean, $stdev) = statsover($pdl_array);
die $pdl_array, "\n", $mean, " ", $stdev, "\n";

Outputs:输出:

[0 -1.3862944 -0.69314718 -0.28768207 0 0.22314355 0.40546511 0.55961579 0.69314718 0.81093022 0.91629073]
0.112860814716055 0.696414187766251

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

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