Perl 中的矩阵矩阵

发布于 2024-07-14 07:20:22 字数 991 浏览 3 评论 0原文

在我正在处理的 Perl 脚本中,我需要用其他几个矩阵构建一个矩阵。 我查看了 CPAN 中的几个模块(数学: :矩阵PDL::矩阵, Math::Cephes::Matrix< /a>),但这些似乎都不支持这一点。

在 Octave 中,这非常容易。 这是一个与我想做的事情类似的例子:

octave:1> A = [ 1, 2; 3, 4 ]
A =    
   1   2
   3   4

octave:2> B = [ 5, 6; 7, 8 ]
B =    
   5   6
   7   8

octave:3> C = [ 9, 10; 11, 12 ]
C =    
    9   10
   11   12

octave:4> D = [ 13, 14; 15, 16 ]
D =    
   13   14
   15   16

octave:5> E = [ A, B; C, D ]
E =    
    1    2    5    6
    3    4    7    8
    9   10   13   14
   11   12   15   16

似乎自己尝试这样做会很快变得混乱,这可能就是为什么这些模块不支持它的原因......还有其他人有过这样的经历吗?需要这个吗? 你解决了吗?

In a Perl script I'm working on, I need to build a matrix out of several other matrices. I've looked at a couple of modules in CPAN (Math::Matrix, PDL::Matrix, Math::Cephes::Matrix), but none of these seem to support this.

In Octave, this is very easy. Here's an example of something similar to what I'm trying to do:

octave:1> A = [ 1, 2; 3, 4 ]
A =    
   1   2
   3   4

octave:2> B = [ 5, 6; 7, 8 ]
B =    
   5   6
   7   8

octave:3> C = [ 9, 10; 11, 12 ]
C =    
    9   10
   11   12

octave:4> D = [ 13, 14; 15, 16 ]
D =    
   13   14
   15   16

octave:5> E = [ A, B; C, D ]
E =    
    1    2    5    6
    3    4    7    8
    9   10   13   14
   11   12   15   16

It seems trying to do this myself would get messy kinda quickly, which is probably why these modules don't support it... Has anyone else out there ever had a need for this? Have you solved it?

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

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

发布评论

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

评论(3

︶ ̄淡然 2024-07-21 07:20:22

自己滚动并不太痛苦。

use List::Util qw(max);

@A = ([1, 2], [3, 4]);
@B = ([5, 6], [7, 8]);
@C = ([9, 10], [11, 12]);
@D = ([13, 14], [15, 16]);

sub hmerge(\@\@;\@\@\@\@\@\@) {
    my @ret;
    for my $i (0 .. max map $#$_, @_) {
        push @ret, [map @{$_[$i]}, @_];
    }
    @ret;
}

@E = (hmerge(@A, @B), hmerge(@C, @D));

Rolling your own isn't too painful.

use List::Util qw(max);

@A = ([1, 2], [3, 4]);
@B = ([5, 6], [7, 8]);
@C = ([9, 10], [11, 12]);
@D = ([13, 14], [15, 16]);

sub hmerge(\@\@;\@\@\@\@\@\@) {
    my @ret;
    for my $i (0 .. max map $#$_, @_) {
        push @ret, [map @{$_[$i]}, @_];
    }
    @ret;
}

@E = (hmerge(@A, @B), hmerge(@C, @D));
烟酒忠诚 2024-07-21 07:20:22

Perl 数据语言 (PDL) 版本 2.4.10 支持 pdl 构造函数,以及 appendglue 例程可用于将子数组粘贴在一起,如 pdl2 会话所示

pdl> $A = pdl q[ 1, 2 ; 3, 4 ];       # pdl constructor with string argument

pdl> $B = pdl q[ 5, 6 ; 7, 8 ];       # pdl constructor with string argument

pdl> $C = pdl q[ 9, 10 ; 11, 12 ];    # pdl constructor with string argument

pdl> $D = pdl q[ 13, 14 ; 15, 16];    # pdl constructor with string argument

pdl> ?vars
PDL variables in package main::

Name         Type   Dimension       Flow  State          Mem
----------------------------------------------------------------
$A           Double D [2,2]                P            0.03KB 
$B           Double D [2,2]                P            0.03KB 
$C           Double D [2,2]                P            0.03KB 
$D           Double D [2,2]                P            0.03KB 


pdl> p $A, $B, $C, $D;

[
 [1 2]
 [3 4]
]

[
 [5 6]
 [7 8]
]

[
 [ 9 10]
 [11 12]
]

[
 [13 14]
 [15 16]
]

pdl> p $AB = $A->append($B);          # concatenate horizontally (actually on dim(0))

[
 [1 2 5 6]
 [3 4 7 8]
]

pdl> p $CD = $C->append($D);          # concatenate horizontally (actually on dim(0))

[
 [ 9 10 13 14]
 [11 12 15 16]
]

pdl> p $E = $AB->glue(1,$CD);         # glue vertically (actually on dim(1))
[
 [ 1  2  5  6]
 [ 3  4  7  8]
 [ 9 10 13 14]
 [11 12 15 16]
]

: a href="http://pdl.perl.org/content/pdl-book-toc.html" rel="nofollow">PDL 书 和 PDL 邮件列表是有关 PDL 的更多信息的重要来源。

The Perl Data Language (PDL) version 2.4.10 supports MATLAB-style convenience input for the pdl constructor when using a string argument and the append and glue routines can be used to paste subarrays together as this pdl2 session shows:

pdl> $A = pdl q[ 1, 2 ; 3, 4 ];       # pdl constructor with string argument

pdl> $B = pdl q[ 5, 6 ; 7, 8 ];       # pdl constructor with string argument

pdl> $C = pdl q[ 9, 10 ; 11, 12 ];    # pdl constructor with string argument

pdl> $D = pdl q[ 13, 14 ; 15, 16];    # pdl constructor with string argument

pdl> ?vars
PDL variables in package main::

Name         Type   Dimension       Flow  State          Mem
----------------------------------------------------------------
$A           Double D [2,2]                P            0.03KB 
$B           Double D [2,2]                P            0.03KB 
$C           Double D [2,2]                P            0.03KB 
$D           Double D [2,2]                P            0.03KB 


pdl> p $A, $B, $C, $D;

[
 [1 2]
 [3 4]
]

[
 [5 6]
 [7 8]
]

[
 [ 9 10]
 [11 12]
]

[
 [13 14]
 [15 16]
]

pdl> p $AB = $A->append($B);          # concatenate horizontally (actually on dim(0))

[
 [1 2 5 6]
 [3 4 7 8]
]

pdl> p $CD = $C->append($D);          # concatenate horizontally (actually on dim(0))

[
 [ 9 10 13 14]
 [11 12 15 16]
]

pdl> p $E = $AB->glue(1,$CD);         # glue vertically (actually on dim(1))
[
 [ 1  2  5  6]
 [ 3  4  7  8]
 [ 9 10 13 14]
 [11 12 15 16]
]

The PDL book and the PDL mailing lists are essential sources for more information on PDL.

野の 2024-07-21 07:20:22

编辑

我误解了OP,认为他们想要迭代几个矩阵的所有可能的排列(这就是 Iterator::Array::Jagged 所做的)。


看看 Iterator::Array::Jagged

这是一个概要示例:

use Iterator::Array::Jagged;

# Build up a set of data:
my @data = (
  [qw/ a b /],
  [qw/ c d /],
  [qw/ e f g /]
);

# Iterator is a subref:
my $itersub = Iterator::Array::Jagged->get_iterator( @data );
while( my @set = $itersub->() )
{
  print "Next set: '" . join("&", @set) . "'\n";
}# end while()

上面代码中的示例打印以下内容:

Next set: 'a&c&e'
Next set: 'b&c&e'
Next set: 'a&d&e'
Next set: 'b&d&e'
Next set: 'a&c&f'
Next set: 'b&c&f'
Next set: 'a&d&f'
Next set: 'b&d&f'
Next set: 'a&c&g'
Next set: 'b&c&g'
Next set: 'a&d&g'
Next set: 'b&d&g'

EDIT

I misunderstood the OP, thinking that they wanted to iterate over all possible permutations of several matrices (which is what Iterator::Array::Jagged does).


Take a look at Iterator::Array::Jagged

Here is an example from the synopsis:

use Iterator::Array::Jagged;

# Build up a set of data:
my @data = (
  [qw/ a b /],
  [qw/ c d /],
  [qw/ e f g /]
);

# Iterator is a subref:
my $itersub = Iterator::Array::Jagged->get_iterator( @data );
while( my @set = $itersub->() )
{
  print "Next set: '" . join("&", @set) . "'\n";
}# end while()

The example in the code above code prints the following:

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