Skip to content

Commit c1f68bf

Browse files
authored
Merge pull request #1436 from Alex-Jordan/dont-remove-only-row
don't try to remove a Matrix's only row (or only column)
2 parents db76ca5 + e975885 commit c1f68bf

2 files changed

Lines changed: 12 additions & 0 deletions

File tree

lib/Value/Matrix.pm

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1502,6 +1502,7 @@ sub removeRow {
15021502
my @dim = $self->dimensions;
15031503
my $degree = scalar @dim;
15041504
Value::Error("removeRow cannot be used on a Matrix of degree 1") if $degree == 1;
1505+
Value::Error("cannot remove a Matrix's only row") if $dim[0] == 1;
15051506
my @indices = map { [ 1 .. $_ ] } @dim;
15061507
Value::Error("Can only remove rows 1 through $indices[0][-1]")
15071508
unless $r =~ /^\d+$/ && $r >= 1 && $r <= $indices[0][-1];
@@ -1529,6 +1530,7 @@ sub removeColumn {
15291530
my @dim = $self->dimensions;
15301531
my $degree = scalar @dim;
15311532
Value::Error("removeColumn cannot be used on a Matrix of degree 1") if $degree == 1;
1533+
Value::Error("cannot remove a Matrix's only column") if $dim[1] == 1;
15321534
my @indices = map { [ 1 .. $_ ] } @dim;
15331535
Value::Error("Can only remove columns 1 through $indices[1][-1]")
15341536
unless $r =~ /^\d+$/ && $r >= 1 && $r <= $indices[1][-1];

t/math_objects/matrix.t

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,11 @@ subtest 'Remove row' => sub {
334334
like dies {
335335
$A->removeRow('a');
336336
}, qr/Can only remove rows 1 through 4/, 'check that error is thrown for bad row specification';
337+
338+
my $D = Matrix([ [ 1, 2, 3 ] ]);
339+
like dies {
340+
$D->removeRow(1);
341+
}, qr/cannot remove a Matrix's only row/, 'check that error is thrown for trying to remove the only row';
337342
};
338343

339344
subtest 'Remove column' => sub {
@@ -362,6 +367,11 @@ subtest 'Remove column' => sub {
362367
like dies {
363368
$A->removeColumn('a');
364369
}, qr/Can only remove columns 1 through 4/, 'check that error is thrown for bad column specification';
370+
371+
my $D = Matrix([1], [2], [3]);
372+
like dies {
373+
$D->removeColumn(1);
374+
}, qr/cannot remove a Matrix's only column/, 'check that error is thrown for trying to remove the only column';
365375
};
366376

367377
subtest 'Construct an identity matrix' => sub {

0 commit comments

Comments
 (0)