如何在 Perl 中计算引用数组的行数
How to calculate number of rows of a referenced array in Perl
我的部分代码计算维度超过 300 X 300 的矩阵(之前在代码中生成)的逆。我想在代码中进一步使用逆矩阵的元素。为此使用了以下代码,尝试仅使用 5X5 矩阵进行测试:
use strict;
use warnings;
use Math::MatrixReal;
my @a=(); #a is the matrix obtained
$a[0][0]=0.18761134;
$a[0][1]=0.010779401; #Have hard-coded the values here till $a[4][4]
my $ref_a = \@a;
my $b = Math::MatrixReal->new_from_rows($ref_a);
my $b_inv = $b->inverse();
print "\n Inverse is\n",$b_inv; #prints correct inverse
print "\n\nTest printing elements\n";
print $$b_inv[0][1][1],"\n"; #prints the correct element
my $row_b=scalar(@{$b});
print "Number of rows in b: ",$row_b,"\n"; #prints 6
my $col_b=@{$$b[0]};
print "Columns in b: ",$col_b,"\n"; #prints 5
my $row_binv=scalar(@$b_inv);
print "Number of rows in b_inv: ",$row_binv,"\n"; #prints 3
my $col_binv=@{$$b_inv[0]};
print "Number of columns in b_inv ",$col_binv,"\n"; #prints 5
我看不懂
为什么 b 和 b_inv 的行数输出都是错误的?如何获取正确的行数值?
尽管打印引用数组元素的语法是$$b_inv[1][1]
,但我在使用$$b_inv[0][1][1]
[=24=时得到了正确的输出]
您正在创建一个
Math::MatrixReal
矩阵对象,然后将其作为简单的 Perl 数组访问。随意在 Perl 对象内部翻找是错误的,您必须使用文档中定义的方法
特别是你的声明
print $$b_inv[0][1][1],"\n"; # prints the correct element
访问一个三维数组,如果不阅读模块[=15]的代码,就无法知道"correct element"应该是什么=]
此修改设置了一个 5 x 5 单位矩阵(将来,请提供我们可用于重现您的结果的数据)并取其逆。值输出是使用我描述的对象的方法派生的,并且都是正确的。请注意,行和列是从一开始索引的,而不是您期望的 Perl 数组从零开始索引
use strict;
use warnings 'all';
use Math::MatrixReal;
my @arr = (
[1, 0, 0, 0, 0],
[0, 1, 0, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 0, 1, 0],
[0, 0, 0, 0, 1],
);
my $ref_a = \@arr;
my $b = Math::MatrixReal->new_from_rows(\@arr);
my $b_inv = $b->inverse;
print "\nInverse is\n", $b_inv;
print "\n\nTest printing elements\n";
print $b_inv->element($_, $_), "\n" for 1 .. 5;
my ($row_b, $col_b) = $b->dim;;
print "Number of rows in b: $row_b\n"; # prints 5
print "Columns in b: $col_b\n"; # prints 5
my ($row_binv, $col_binv) = $b_inv->dim;;
print "Number of rows in b_inv: $row_binv\n"; # prints 5
print "Number of columns in b_inv $col_binv\n"; # prints 5
输出
Inverse is
[ 1.000000000000E+000 0.000000000000E+000 0.000000000000E+000 0.000000000000E+000 0.000000000000E+000 ]
[ 0.000000000000E+000 1.000000000000E+000 0.000000000000E+000 0.000000000000E+000 0.000000000000E+000 ]
[ 0.000000000000E+000 0.000000000000E+000 1.000000000000E+000 0.000000000000E+000 0.000000000000E+000 ]
[ 0.000000000000E+000 0.000000000000E+000 0.000000000000E+000 1.000000000000E+000 0.000000000000E+000 ]
[ 0.000000000000E+000 0.000000000000E+000 0.000000000000E+000 0.000000000000E+000 1.000000000000E+000 ]
Test printing elements
1
1
1
1
1
Number of rows in b: 5
Columns in b: 5
Number of rows in b_inv: 5
Number of columns in b_inv 5
我的部分代码计算维度超过 300 X 300 的矩阵(之前在代码中生成)的逆。我想在代码中进一步使用逆矩阵的元素。为此使用了以下代码,尝试仅使用 5X5 矩阵进行测试:
use strict;
use warnings;
use Math::MatrixReal;
my @a=(); #a is the matrix obtained
$a[0][0]=0.18761134;
$a[0][1]=0.010779401; #Have hard-coded the values here till $a[4][4]
my $ref_a = \@a;
my $b = Math::MatrixReal->new_from_rows($ref_a);
my $b_inv = $b->inverse();
print "\n Inverse is\n",$b_inv; #prints correct inverse
print "\n\nTest printing elements\n";
print $$b_inv[0][1][1],"\n"; #prints the correct element
my $row_b=scalar(@{$b});
print "Number of rows in b: ",$row_b,"\n"; #prints 6
my $col_b=@{$$b[0]};
print "Columns in b: ",$col_b,"\n"; #prints 5
my $row_binv=scalar(@$b_inv);
print "Number of rows in b_inv: ",$row_binv,"\n"; #prints 3
my $col_binv=@{$$b_inv[0]};
print "Number of columns in b_inv ",$col_binv,"\n"; #prints 5
我看不懂
为什么 b 和 b_inv 的行数输出都是错误的?如何获取正确的行数值?
尽管打印引用数组元素的语法是
[=24=时得到了正确的输出]$$b_inv[1][1]
,但我在使用$$b_inv[0][1][1]
您正在创建一个
Math::MatrixReal
矩阵对象,然后将其作为简单的 Perl 数组访问。随意在 Perl 对象内部翻找是错误的,您必须使用文档中定义的方法
特别是你的声明
print $$b_inv[0][1][1],"\n"; # prints the correct element
访问一个三维数组,如果不阅读模块[=15]的代码,就无法知道"correct element"应该是什么=]
此修改设置了一个 5 x 5 单位矩阵(将来,请提供我们可用于重现您的结果的数据)并取其逆。值输出是使用我描述的对象的方法派生的,并且都是正确的。请注意,行和列是从一开始索引的,而不是您期望的 Perl 数组从零开始索引
use strict;
use warnings 'all';
use Math::MatrixReal;
my @arr = (
[1, 0, 0, 0, 0],
[0, 1, 0, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 0, 1, 0],
[0, 0, 0, 0, 1],
);
my $ref_a = \@arr;
my $b = Math::MatrixReal->new_from_rows(\@arr);
my $b_inv = $b->inverse;
print "\nInverse is\n", $b_inv;
print "\n\nTest printing elements\n";
print $b_inv->element($_, $_), "\n" for 1 .. 5;
my ($row_b, $col_b) = $b->dim;;
print "Number of rows in b: $row_b\n"; # prints 5
print "Columns in b: $col_b\n"; # prints 5
my ($row_binv, $col_binv) = $b_inv->dim;;
print "Number of rows in b_inv: $row_binv\n"; # prints 5
print "Number of columns in b_inv $col_binv\n"; # prints 5
输出
Inverse is
[ 1.000000000000E+000 0.000000000000E+000 0.000000000000E+000 0.000000000000E+000 0.000000000000E+000 ]
[ 0.000000000000E+000 1.000000000000E+000 0.000000000000E+000 0.000000000000E+000 0.000000000000E+000 ]
[ 0.000000000000E+000 0.000000000000E+000 1.000000000000E+000 0.000000000000E+000 0.000000000000E+000 ]
[ 0.000000000000E+000 0.000000000000E+000 0.000000000000E+000 1.000000000000E+000 0.000000000000E+000 ]
[ 0.000000000000E+000 0.000000000000E+000 0.000000000000E+000 0.000000000000E+000 1.000000000000E+000 ]
Test printing elements
1
1
1
1
1
Number of rows in b: 5
Columns in b: 5
Number of rows in b_inv: 5
Number of columns in b_inv 5