@@ -482,13 +482,10 @@ sub I {
482
482
Value::Error(" You must provide a dimension for the Identity matrix" ) unless defined $d ;
483
483
Value::Error(" Dimension must be a positive integer" ) unless $d =~ m / ^[1-9] \d *$ / ;
484
484
my @M = ();
485
- my @Z = split (' ' , 0 x $d );
486
485
my $REAL = $context -> Package(' Real' );
487
486
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));
492
489
}
493
490
return $self -> make($context , @M );
494
491
}
@@ -510,17 +507,16 @@ sub E {
510
507
" If only one row is specified for an Elementary matrix, then a number to scale by must also be specified" )
511
508
if (@ij == 1 && !defined $k );
512
509
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 " )
514
511
unless ($_ =~ m / ^[1-9] \d *$ / && $_ >= 1 && $_ <= $d );
515
512
}
516
513
@ij = map { $_ - 1 } (@ij );
517
514
518
515
my @M = ();
519
- my @Z = split (' ' , 0 x $d );
520
516
my $REAL = $context -> Package(' Real' );
521
517
522
- foreach my $i (0 .. $d - 1) {
523
- my @row = @Z ;
518
+ for my $i (0 .. $d - 1) {
519
+ my @row = (0) x $d ;
524
520
$row [$i ] = 1;
525
521
if (@ij == 1) {
526
522
$row [$i ] = $k if ($i == $ij [0]);
@@ -536,8 +532,8 @@ sub E {
536
532
537
533
#
538
534
# 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
541
537
#
542
538
sub P {
543
539
my ($self , $d , @cycles ) = @_ ;
@@ -555,19 +551,17 @@ sub P {
555
551
Value::Error(" A permutation cycle should not repeat an index" ) unless (@$c == keys %cycle_hash );
556
552
}
557
553
my @M = ();
558
- my @Z = split (' ' , 0 x $d );
559
554
my $REAL = $context -> Package(' Real' );
560
555
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));
565
559
}
566
560
561
+ # Then apply the permutation cycles to it
567
562
for my $c (@cycles ) {
568
- my $n = @$c ;
569
563
my $swap ;
570
- for my $i (0 .. $n - 1 , 0) {
564
+ for my $i (0 .. $# $c , 0) {
571
565
($swap , $M [ $c -> [$i ] - 1 ]) = ($M [ $c -> [$i ] - 1 ], $swap );
572
566
}
573
567
}
@@ -587,12 +581,10 @@ sub Zero {
587
581
Value::Error(" You must provide dimensions for the Zero matrix" ) unless defined $m && defined $n ;
588
582
Value::Error(" Dimension must be a positive integer" ) unless $m =~ m / ^[1-9] \d *$ / && $n =~ m / ^[1-9] \d *$ / ;
589
583
my @M = ();
590
- my @Z = split (' ' , 0 x $n );
591
584
my $REAL = $context -> Package(' Real' );
592
585
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));
596
588
}
597
589
return $self -> make($context , @M );
598
590
}
0 commit comments