From 87c3de21c5d27bbe5cff603de943150103929230 Mon Sep 17 00:00:00 2001 From: Peter Staab Date: Wed, 28 Jan 2026 19:59:09 -0500 Subject: [PATCH] Fix a bug related to elementary matrices, fix the POD, fix the test and add some additional tests for elementary matrices. --- lib/Value/Matrix.pm | 13 +++++++------ t/math_objects/matrix.t | 15 ++++++++++++++- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/lib/Value/Matrix.pm b/lib/Value/Matrix.pm index 12a36bf49..aff00b460 100644 --- a/lib/Value/Matrix.pm +++ b/lib/Value/Matrix.pm @@ -1081,7 +1081,7 @@ Usage: generates the matrix [ [ 1, 0, 0, 0 ], - [ 0, 4, 0, 0 ], + [ 0, 3, 0, 0 ], [ 0, 0, 1, 0 ], [ 0, 0, 0, 1 ] ] @@ -1102,15 +1102,16 @@ Usage: generates the matrix: [ [ 1, 0, 0, 0 ], - [ 0, 1, 0, 0 ], - [ 0, -3, 1, 0 ], + [ 0, 1, -3, 0 ], + [ 0, 0, 1, 0 ], [ 0, 0, 0, 1 ] ] -or if the matrix C<$A> exists then +or if the matrix C<$A> exists of size m by n then $A->E([3, 4], -5); -will generate the elementary matrix of size number of rows of C<$A>, which multiplies row 3 by -5 and adds to row 4. +will generate the m by m elementary matrix which multiplies row 3 by -5 and adds to row 4. +If $A does not have at least 4 rows, an error is raised. =back @@ -1147,7 +1148,7 @@ sub E { if (@ij == 1) { $row[$i] = $k if ($i == $ij[0]); } elsif (defined $k) { - $row[ $ij[1] ] = $k if ($i == $ij[0]); + $row[ $ij[0] ] = $k if ($i == $ij[1]); } else { ($row[ $ij[0] ], $row[ $ij[1] ]) = ($row[ $ij[1] ], $row[ $ij[0] ]) if ($i == $ij[0] || $i == $ij[1]); } diff --git a/t/math_objects/matrix.t b/t/math_objects/matrix.t index dbf03b02e..32dfe3d45 100644 --- a/t/math_objects/matrix.t +++ b/t/math_objects/matrix.t @@ -357,8 +357,21 @@ subtest 'Construct an elementary matrix' => sub { 'Elementary Matrix with row multiple'; my $E3 = Value::Matrix->E(4, [ 3, 2 ], -3); - is $E3->TeX, Matrix([ [ 1, 0, 0, 0 ], [ 0, 1, 0, 0 ], [ 0, -3, 1, 0 ], [ 0, 0, 0, 1 ] ])->TeX, + is $E3->TeX, Matrix([ [ 1, 0, 0, 0 ], [ 0, 1, -3, 0 ], [ 0, 0, 1, 0 ], [ 0, 0, 0, 1 ] ])->TeX, 'Elementary Matrix with row multiple and add'; + + my $A = Matrix([ [ 1, 2, 3, 4 ], [ 5, 6, 7, 8 ], [ 9, 10, 11, 12 ], [ 13, 14, 15, 16 ] ]); + is $A->E([ 1, 4 ])->TeX, + Matrix([ [ 0, 0, 0, 1 ], [ 0, 1, 0, 0 ], [ 0, 0, 1, 0 ], [ 1, 0, 0, 0 ] ])->TeX, + 'Elementary Matrix from syntax $A->E an existing matrix with a row swap'; + + is $A->E([2], 5)->TeX, + Matrix([ [ 1, 0, 0, 0 ], [ 0, 5, 0, 0 ], [ 0, 0, 1, 0 ], [ 0, 0, 0, 1 ] ])->TeX, + 'Elementary Matrix from syntax $A->E an existing matrix with a row multiple'; + + is $A->E([ 2, 4 ], -4)->TeX, + Matrix([ [ 1, 0, 0, 0 ], [ 0, 1, 0, 0 ], [ 0, 0, 1, 0 ], [ 0, -4, 0, 1 ] ])->TeX, + 'Elementary Matrix from syntax $A->E an existing matrix with a row multiple and add'; }; subtest 'Extract a slice from a Matrix' => sub {