diff --git a/lib/Value/Matrix.pm b/lib/Value/Matrix.pm index 12a36bf49..f696b18d0 100644 --- a/lib/Value/Matrix.pm +++ b/lib/Value/Matrix.pm @@ -109,7 +109,7 @@ Examples: column(j) : MathObject Matrix or Real or Complex For a degree 1 Matrix, produces a Real or Complex - For a degree n Matrix with n > 1, produces a degree n Matrix where the 2nd dimesion is length 1 + For a degree n Matrix with n > 1, produces a degree n Matrix where the 2nd dimension is length 1 element : Real/Complex/Fraction value when passed the same number of arguments as the degree of the Matrix. If passed more than n arguments, null. If the degree of the Matrix is n and C is passed @@ -366,7 +366,7 @@ sub isSquare { =head3 C -Return true if the matix is degree 1 (i.e., is a matrix row) +Return true if the matrix is degree 1 (i.e., is a matrix row) Usage: @@ -1052,7 +1052,8 @@ perform row operations. =item * Row Swap -To perform a row swap between rows C and C, then C. +The method C<< Value::Matrix->E(n,[i, j]) >> returns the n by n elementary matrix that +upon right multiplication performs the row swap between rows C and C. Usage: @@ -1072,7 +1073,8 @@ where the size of the resulting matrix is the number of rows of C<$A>. =item * Multiply a row by a constant -To create the matrix that will multiply a row C, by constant C, then C +The method C<< Value::Matrix->E(n, [i], k) >> returns the n by n elementary matrix that upon +right multiplication will multiply a row C, by constant C. Usage: @@ -1081,7 +1083,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 ] ] @@ -1093,7 +1095,8 @@ will generate the elementary matrix of size number of rows of C<$A>, which multi =item * Multiply a row by a constant and add to another row. -To create the matrix that will multiply a row C, by constant C and add to row C then C +The method C<< Value::Matrix->E(n, [i], k) >> returns the n by n elementary matrix that upon +right multiplication will multiply a row C, by constant C and add to row C. Usage: @@ -1102,15 +1105,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 C<$A> does not have at least 4 rows, an error is raised. =back @@ -1147,7 +1151,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 {