Skip to content

Commit 5e2e0d2

Browse files
committed
Fix a bug related to elementary matrices, fix the test and add
some additional tests for elementary matrices.
1 parent a328b64 commit 5e2e0d2

2 files changed

Lines changed: 20 additions & 6 deletions

File tree

lib/Value/Matrix.pm

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,15 +1102,16 @@ Usage:
11021102
generates the matrix:
11031103
11041104
[ [ 1, 0, 0, 0 ],
1105-
[ 0, 1, 0, 0 ],
1106-
[ 0, -3, 1, 0 ],
1105+
[ 0, 1, -3, 0 ],
1106+
[ 0, 0, 1, 0 ],
11071107
[ 0, 0, 0, 1 ] ]
11081108
1109-
or if the matrix C<$A> exists then
1109+
or if the matrix C<$A> exists of size m by n then
11101110
11111111
$A->E([3, 4], -5);
11121112
1113-
will generate the elementary matrix of size number of rows of C<$A>, which multiplies row 3 by -5 and adds to row 4.
1113+
will generate the m by m elementary matrix which multiplies row 3 by -5 and adds to row 4.
1114+
If $A does not have at least 4 rows, an error is raised.
11141115
11151116
=back
11161117
@@ -1147,7 +1148,7 @@ sub E {
11471148
if (@ij == 1) {
11481149
$row[$i] = $k if ($i == $ij[0]);
11491150
} elsif (defined $k) {
1150-
$row[ $ij[1] ] = $k if ($i == $ij[0]);
1151+
$row[ $ij[0] ] = $k if ($i == $ij[1]);
11511152
} else {
11521153
($row[ $ij[0] ], $row[ $ij[1] ]) = ($row[ $ij[1] ], $row[ $ij[0] ]) if ($i == $ij[0] || $i == $ij[1]);
11531154
}

t/math_objects/matrix.t

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,8 +357,21 @@ subtest 'Construct an elementary matrix' => sub {
357357
'Elementary Matrix with row multiple';
358358

359359
my $E3 = Value::Matrix->E(4, [ 3, 2 ], -3);
360-
is $E3->TeX, Matrix([ [ 1, 0, 0, 0 ], [ 0, 1, 0, 0 ], [ 0, -3, 1, 0 ], [ 0, 0, 0, 1 ] ])->TeX,
360+
is $E3->TeX, Matrix([ [ 1, 0, 0, 0 ], [ 0, 1, -3, 0 ], [ 0, 0, 1, 0 ], [ 0, 0, 0, 1 ] ])->TeX,
361361
'Elementary Matrix with row multiple and add';
362+
363+
my $A = Matrix([ [ 1, 2, 3, 4 ], [ 5, 6, 7, 8 ], [ 9, 10, 11, 12 ], [ 13, 14, 15, 16 ] ]);
364+
is $A->E([ 1, 4 ])->TeX,
365+
Matrix([ [ 0, 0, 0, 1 ], [ 0, 1, 0, 0 ], [ 0, 0, 1, 0 ], [ 1, 0, 0, 0 ] ])->TeX,
366+
'Elementary Matrix from syntax $A->E an existing matrix with a row swap';
367+
368+
is $A->E([2], 5)->TeX,
369+
Matrix([ [ 1, 0, 0, 0 ], [ 0, 5, 0, 0 ], [ 0, 0, 1, 0 ], [ 0, 0, 0, 1 ] ])->TeX,
370+
'Elementary Matrix from syntax $A->E an existing matrix with a row multiple';
371+
372+
is $A->E([ 2, 4 ], -4)->TeX,
373+
Matrix([ [ 1, 0, 0, 0 ], [ 0, 1, 0, 0 ], [ 0, 0, 1, 0 ], [ 0, -4, 0, 1 ] ])->TeX,
374+
'Elementary Matrix from syntax $A->E an existing matrix with a row multiple and add';
362375
};
363376

364377
subtest 'Extract a slice from a Matrix' => sub {

0 commit comments

Comments
 (0)