Skip to content

Commit f25258f

Browse files
committed
changes suggested in PR#1012
1 parent 61ad89f commit f25258f

File tree

1 file changed

+14
-22
lines changed

1 file changed

+14
-22
lines changed

lib/Value/Matrix.pm

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -482,13 +482,10 @@ sub I {
482482
Value::Error("You must provide a dimension for the Identity matrix") unless defined $d;
483483
Value::Error("Dimension must be a positive integer") unless $d =~ m/^[1-9]\d*$/;
484484
my @M = ();
485-
my @Z = split('', 0 x $d);
486485
my $REAL = $context->Package('Real');
487486

488-
foreach my $i (0 .. $d - 1) {
489-
my @row = @Z;
490-
$row[$i] = 1;
491-
push(@M, $self->make($context, map { $REAL->new($_) } @row));
487+
for my $i (0 .. $d - 1) {
488+
push(@M, $self->make($context, map { $REAL->new(($_ == $i) ? 1 : 0) } 0 .. $d - 1));
492489
}
493490
return $self->make($context, @M);
494491
}
@@ -510,17 +507,16 @@ sub E {
510507
"If only one row is specified for an Elementary matrix, then a number to scale by must also be specified")
511508
if (@ij == 1 && !defined $k);
512509
for (@ij) {
513-
Value::Error("Row numbers must be integers between 1 and $d")
510+
Value::Error("Row indices must be integers between 1 and $d")
514511
unless ($_ =~ m/^[1-9]\d*$/ && $_ >= 1 && $_ <= $d);
515512
}
516513
@ij = map { $_ - 1 } (@ij);
517514

518515
my @M = ();
519-
my @Z = split('', 0 x $d);
520516
my $REAL = $context->Package('Real');
521517

522-
foreach my $i (0 .. $d - 1) {
523-
my @row = @Z;
518+
for my $i (0 .. $d - 1) {
519+
my @row = (0) x $d;
524520
$row[$i] = 1;
525521
if (@ij == 1) {
526522
$row[$i] = $k if ($i == $ij[0]);
@@ -536,8 +532,8 @@ sub E {
536532

537533
#
538534
# Get a permutation matrix of the requested size
539-
# E.g. P(3,[1,2,3]) corresponds to cycle (123) applied to rows of I_3
540-
# And P(6,[1,4],[2,4,6]) corresponds to cycle product (14)(246) applied to rows if I_6
535+
# E.g. P(3,[1,2,3]) corresponds to cycle (123) applied to rows of I_3i,
536+
# and P(6,[1,4],[2,4,6]) corresponds to cycle product (14)(246) applied to rows of I_6
541537
#
542538
sub P {
543539
my ($self, $d, @cycles) = @_;
@@ -555,19 +551,17 @@ sub P {
555551
Value::Error("A permutation cycle should not repeat an index") unless (@$c == keys %cycle_hash);
556552
}
557553
my @M = ();
558-
my @Z = split('', 0 x $d);
559554
my $REAL = $context->Package('Real');
560555

561-
foreach my $i (0 .. $d - 1) {
562-
my @row = @Z;
563-
$row[$i] = 1;
564-
push(@M, $self->make($context, map { $REAL->new($_) } @row));
556+
# Make an identity matrix
557+
for my $i (0 .. $d - 1) {
558+
push(@M, $self->make($context, map { $REAL->new(($_ == $i) ? 1 : 0) } 0 .. $d - 1));
565559
}
566560

561+
# Then apply the permutation cycles to it
567562
for my $c (@cycles) {
568-
my $n = @$c;
569563
my $swap;
570-
for my $i (0 .. $n - 1, 0) {
564+
for my $i (0 .. $#$c, 0) {
571565
($swap, $M[ $c->[$i] - 1 ]) = ($M[ $c->[$i] - 1 ], $swap);
572566
}
573567
}
@@ -587,12 +581,10 @@ sub Zero {
587581
Value::Error("You must provide dimensions for the Zero matrix") unless defined $m && defined $n;
588582
Value::Error("Dimension must be a positive integer") unless $m =~ m/^[1-9]\d*$/ && $n =~ m/^[1-9]\d*$/;
589583
my @M = ();
590-
my @Z = split('', 0 x $n);
591584
my $REAL = $context->Package('Real');
592585

593-
foreach my $i (0 .. $m - 1) {
594-
my @row = @Z;
595-
push(@M, $self->make($context, map { $REAL->new($_) } @row));
586+
for my $i (0 .. $m - 1) {
587+
push(@M, $self->make($context, map { $REAL->new(0) } 0 .. $n - 1));
596588
}
597589
return $self->make($context, @M);
598590
}

0 commit comments

Comments
 (0)