将 PDL 标量转换为 Perl 标量

发布于 2024-12-29 00:00:06 字数 277 浏览 4 评论 0原文

我有一个使用 PDL 的函数。最后一步是点积,因此它返回一个标量。然而,当我尝试打印这个标量时,它显然仍然是一个 piddle 并在屏幕上打印如下:

[
  [ 3 ]
]

我想知道如何将其转换回常规 Perl 标量,以便它打印如下:

3 

更重要的是,什么是如果我不转换并将该 piddle 在纯 Perl 上下文(不涉及 PDL)中进行进一步的算术操作,则会产生后果。谢谢!

I have a function that uses PDL. The final step is a dot product so it returns a scalar. However, when I try to print this scalar, it is clearly still a piddle and prints like this on screen:

[
  [ 3 ]
]

I'm wondering how I can convert it back to the regular Perl scalar so that it prints like:

3 

More importantly, what is the consequence if I don't convert and take that piddle on to further arithmetic manipulations in a pure Perl context (that does not involve PDL). Thx!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花海 2025-01-05 00:00:06

使用 sclr 方法,将任意维数的单元素 PDL 矩阵转换为简单的 Perl 标量

my $dotp = sclr($mata x $matb);

Use the sclr method, which converts a single-element PDL matrix with any number of dimensions into a simple Perl scalar

my $dotp = sclr($mata x $matb);
剧终人散尽 2025-01-05 00:00:06

回答第二个问题(“如果我不转换并在纯 Perl 上下文(不涉及 PDL)中使用该 piddle 进行进一步的算术操作,会产生什么后果”);有两个主要考虑因素:

  • PDL 实体(“ndarrays”)具有重载算术,因此与它们一起使用的任何 Perl 标量都将得到提升,并且结果也将是 ndarrays。对于标量(单元素)ndarray,这可能是不可取的,因为 PDL 操作对性能的影响对于单个元素来说是不值得的,
  • 示例中显示的 ndarray 有一个元素,但它是二维的 (1x1);要成为正确的标量 ndarray,您需要使用 $dotp->squeeze 删除所有长度为 1 的暗淡

To answer the second question ("what is the consequence if I don't convert and take that piddle on to further arithmetic manipulations in a pure Perl context (that does not involve PDL)"); there are two major considerations:

  • PDL entities ("ndarrays") have overloaded arithmetic so that any Perl scalars used with them will get promoted, and the results will also be ndarrays. For scalar (single-element) ndarrays this is probably undesirable since PDL operations have a performance hit that isn't worth it for a single element
  • the ndarray shown in the example has one element, but it is 2-dimension (1x1); to be a proper scalar ndarray, you'd do $dotp->squeeze to drop all length-1 dims
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文