Skip to content

Commit 0abcab6

Browse files
committed
use matrix generator methods on a matrix
1 parent 5388832 commit 0abcab6

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

lib/Value/Matrix.pm

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -473,12 +473,14 @@ sub transpose {
473473

474474
#
475475
# Get an identity matrix of the requested size
476+
# Value::Matrix->I(n)
477+
# $A->I # n is the number of rows of $A
476478
#
477479
sub I {
478480
my $self = shift;
479481
my $d = shift;
480482
my $context = shift || $self->context;
481-
$d = ($self->dimensions)[0] if !defined $d && ref($self) && $self->isSquare;
483+
$d = ($self->dimensions)[0] if !defined $d && ref($self);
482484
Value::Error("You must provide a dimension for the Identity matrix") unless defined $d;
483485
Value::Error("Dimension must be a positive integer") unless $d =~ m/^[1-9]\d*$/;
484486
my @M = ();
@@ -492,13 +494,20 @@ sub I {
492494

493495
#
494496
# Get an elementary matrix of the requested size and type
495-
# E(n,[i,j]) nxn, swap rows i and j
496-
# E(n,[i,j],k) nxn, replace row i with row i added to k times row j
497-
# E(n,[i],k) nxn, scale row i by k
497+
# Value::Matrix->E(n,[i,j]) nxn, swap rows i and j
498+
# Value::Matrix->E(n,[i,j],k) nxn, replace row i with row i added to k times row j
499+
# Value::Matrix->E(n,[i],k) nxn, scale row i by k
500+
# $A->E([i,j]) # n is the number of rows of $A
501+
# $A->E([i,j],k) # n is the number of rows of $A
502+
# $A->E([i],k) # n is the number of rows of $A
498503
#
499504
sub E {
500505
my ($self, $d, $rows, $k, $context) = @_;
501-
$context = $self->context unless $context;
506+
if (ref $d eq 'ARRAY') {
507+
($rows, $k, $context) = ($d, $rows, $k);
508+
$d = ($self->dimensions)[0] if ref($self);
509+
}
510+
$context = $self->context unless $context;
502511
Value::Error("You must provide a dimension for an Elementary matrix") unless defined $d;
503512
Value::Error("Dimension must be a positive integer") unless $d =~ m/^[1-9]\d*$/;
504513
my @ij = @{$rows};
@@ -534,9 +543,15 @@ sub E {
534543
# Get a permutation matrix of the requested size
535544
# E.g. P(3,[1,2,3]) corresponds to cycle (123) applied to rows of I_3i,
536545
# and P(6,[1,4],[2,4,6]) corresponds to cycle product (14)(246) applied to rows of I_6
546+
# Value::Matrix->P(n,(cycles))
547+
# $A->P((cycles)) # n is the number of rows of $A
537548
#
538549
sub P {
539550
my ($self, $d, @cycles) = @_;
551+
if (ref $d eq 'ARRAY') {
552+
unshift(@cycles, $d);
553+
$d = ($self->dimensions)[0] if ref($self);
554+
}
540555
my $context = $self->context;
541556
$d = ($self->dimensions)[0] if !defined $d && ref($self) && $self->isSquare;
542557
Value::Error("You must provide a dimension for a Permutation matrix") unless defined $d;
@@ -571,6 +586,9 @@ sub P {
571586

572587
#
573588
# Get an all zero matrix of the requested size
589+
# Value::Matrix->Zero(m,n)
590+
# Value::Matrix->Zero(n)
591+
# $A->Zero # n is the number of rows of $A
574592
#
575593
sub Zero {
576594
my ($self, $m, $n, $context) = @_;

0 commit comments

Comments
 (0)