Perl 中的矩阵乘法、3D 对象的易位

发布于 2024-12-23 02:02:47 字数 341 浏览 1 评论 0原文

我想在 Perl 中将两个矩阵相乘。第一个 {n*4} 和第二个 {4*n}。我想从文本文件中获取值。我的一个文本文件看起来像
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
另一种如下所示
1 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0

请帮助我。我的主要主题是将 3D x,y,x 坐标乘以某个值以将其转换到其他位置。提前致谢。

I want to multiply two matrices in Perl. 1st {n*4} with 2nd {4*n}. I want to fetch the values from a text file. my one text file looks like
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
and othe one is like the following
1 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0

Please help me. My main motif is to multiply a 3D x,y,x coordinates with some value to translate it to other position. Thanks in advance.

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

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

发布评论

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

评论(4

风吹短裙飘 2024-12-30 02:02:47

使用Math::Matrix

将两个矩阵相乘,其中第一个矩阵中的行长度与第二个矩阵中的列长度相同。如果出现错误,则返回乘积或 undef。

Use Math::Matrix.

Multiplies two matrices where the length of the rows in the first matrix is the same as the length of the columns in the second matrix. Returns the product or undef in case of error.

风筝有风,海豚有海 2024-12-30 02:02:47

可以使用 PDL::Matrix

#!/usr/bin/env perl

use strict;
use warnings;

use PDL;
use PDL::Matrix;

if ( @ARGV != 2 ) {
    die 'Two matrix files are required as arguments';
}

my $index = 0;
my @matrices;
while (<>) {
    push @{ $matrices[$index] }, [ split /\s+/ ];
}
continue { $index++ if eof }

my $matrix_1 = PDL::Matrix->pdl( @{ $matrices[0] } );
my $matrix_2 = PDL::Matrix->pdl( @{ $matrices[1] } );

print $matrix_1 x $matrix_2;

PDL::Matrix can be used:

#!/usr/bin/env perl

use strict;
use warnings;

use PDL;
use PDL::Matrix;

if ( @ARGV != 2 ) {
    die 'Two matrix files are required as arguments';
}

my $index = 0;
my @matrices;
while (<>) {
    push @{ $matrices[$index] }, [ split /\s+/ ];
}
continue { $index++ if eof }

my $matrix_1 = PDL::Matrix->pdl( @{ $matrices[0] } );
my $matrix_2 = PDL::Matrix->pdl( @{ $matrices[1] } );

print $matrix_1 x $matrix_2;
清晰传感 2024-12-30 02:02:47

PDL 模块适合矩阵计算。

The PDL module is suited for matrix computations.

痞味浪人 2024-12-30 02:02:47

使用 PDL::Matrix 和 rcols

使用 rcols 读取以空格分隔的列的数据文件。例如,通过以下方式使用当前的 PDL-2.4.10 版本pdl2 shell 我们演示:

 pdl> #cat an4.cols;   # this is the [n,4] data file
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4

pdl> #cat a4n.cols;   # this is the [4,n] data file
1 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 

pdl> $a = rcols 'a4n.cols', [];  # read col data into 2-D piddle
Reading data into piddles of type: [ Double ]
Read in 40 elements.

pdl> $b = rcols 'an4.cols', [];  # read col data into 2-D piddle
Reading data into piddles of type: [ Double ]
Read in 36 elements.

pdl> $am = PDL::Matrix->pdl($a); # covert to usual matrix dim order

pdl> $bm = PDL::Matrix->pdl($b); # covert to usual matrix dim order

pdl> p $cm = $am x $bm;          # multiply the two matrices

[
 [10 10 10 10 10 10 10 10 10]
 [ 0  0  0  0  0  0  0  0  0]
 [ 0  0  0  0  0  0  0  0  0]
 [ 0  0  0  0  0  0  0  0  0]
 [ 0  0  0  0  0  0  0  0  0]
 [ 0  0  0  0  0  0  0  0  0]
 [ 0  0  0  0  0  0  0  0  0]
 [ 0  0  0  0  0  0  0  0  0]
 [ 0  0  0  0  0  0  0  0  0]
 [ 0  0  0  0  0  0  0  0  0]
]

pdl> help vars                   # note the dim order change between $a and $am, etc
PDL variables in package main::

Name         Type   Dimension       Flow  State          Mem
----------------------------------------------------------------
$a           Double D [4,10]               VC           0.00KB 
$am          Double D [10,4]               VC           0.00KB PDL::Matrix
$b           Double D [9,4]                VC           0.00KB 
$bm          Double D [4,9]                VC           0.00KB PDL::Matrix
$cm          Double D [10,9]               P            0.70KB PDL::Matrix
$Pi          Double D []                   P            0.01KB 

注意:$am$a 之间的唯一区别是PDL::Matrix 对象二维矩阵的维序约定与标准 PDL 约定相反。有关 PDL 所有内容的更多信息,请访问 PDL 网站,您可以在其中找到文档、邮件的链接列出档案,以及更多...

Use PDL::Matrix and rcols

Use rcols to read in data files of whitespace separated columns. For example, using the current PDL-2.4.10 release via the pdl2 shell we demonstrate:

 pdl> #cat an4.cols;   # this is the [n,4] data file
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4

pdl> #cat a4n.cols;   # this is the [4,n] data file
1 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 

pdl> $a = rcols 'a4n.cols', [];  # read col data into 2-D piddle
Reading data into piddles of type: [ Double ]
Read in 40 elements.

pdl> $b = rcols 'an4.cols', [];  # read col data into 2-D piddle
Reading data into piddles of type: [ Double ]
Read in 36 elements.

pdl> $am = PDL::Matrix->pdl($a); # covert to usual matrix dim order

pdl> $bm = PDL::Matrix->pdl($b); # covert to usual matrix dim order

pdl> p $cm = $am x $bm;          # multiply the two matrices

[
 [10 10 10 10 10 10 10 10 10]
 [ 0  0  0  0  0  0  0  0  0]
 [ 0  0  0  0  0  0  0  0  0]
 [ 0  0  0  0  0  0  0  0  0]
 [ 0  0  0  0  0  0  0  0  0]
 [ 0  0  0  0  0  0  0  0  0]
 [ 0  0  0  0  0  0  0  0  0]
 [ 0  0  0  0  0  0  0  0  0]
 [ 0  0  0  0  0  0  0  0  0]
 [ 0  0  0  0  0  0  0  0  0]
]

pdl> help vars                   # note the dim order change between $a and $am, etc
PDL variables in package main::

Name         Type   Dimension       Flow  State          Mem
----------------------------------------------------------------
$a           Double D [4,10]               VC           0.00KB 
$am          Double D [10,4]               VC           0.00KB PDL::Matrix
$b           Double D [9,4]                VC           0.00KB 
$bm          Double D [4,9]                VC           0.00KB PDL::Matrix
$cm          Double D [10,9]               P            0.70KB PDL::Matrix
$Pi          Double D []                   P            0.01KB 

Note: the only difference between $am and $a are that for the PDL::Matrix objects the dimension order convention for the 2-D matrix is reversed from the standard PDL convention. For more information on all things PDL, please go to the PDL website where you'll find links to documentation, mailing list archives, and more...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文