Skip to content

Commit d16b4d0

Browse files
authored
Merge pull request #1372 from pstaabp/bugfix/elem-matrix
Fix elementary Matrix construction bug
2 parents 9c273dc + 1277fee commit d16b4d0

2 files changed

Lines changed: 29 additions & 12 deletions

File tree

lib/Value/Matrix.pm

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ Examples:
109109
110110
column(j) : MathObject Matrix or Real or Complex
111111
For a degree 1 Matrix, produces a Real or Complex
112-
For a degree n Matrix with n > 1, produces a degree n Matrix where the 2nd dimesion is length 1
112+
For a degree n Matrix with n > 1, produces a degree n Matrix where the 2nd dimension is length 1
113113
114114
element : Real/Complex/Fraction value when passed the same number of arguments as the degree of the Matrix.
115115
If passed more than n arguments, null. If the degree of the Matrix is n and C<element> is passed
@@ -366,7 +366,7 @@ sub isSquare {
366366

367367
=head3 C<isRow>
368368
369-
Return true if the matix is degree 1 (i.e., is a matrix row)
369+
Return true if the matrix is degree 1 (i.e., is a matrix row)
370370
371371
Usage:
372372
@@ -1052,7 +1052,8 @@ perform row operations.
10521052
10531053
=item * Row Swap
10541054
1055-
To perform a row swap between rows C<i> and C<j>, then C<E(n,[i, j])>.
1055+
The method C<< Value::Matrix->E(n,[i, j]) >> returns the n by n elementary matrix that
1056+
upon right multiplication performs the row swap between rows C<i> and C<j>.
10561057
10571058
Usage:
10581059
@@ -1072,7 +1073,8 @@ where the size of the resulting matrix is the number of rows of C<$A>.
10721073
10731074
=item * Multiply a row by a constant
10741075
1075-
To create the matrix that will multiply a row C<i>, by constant C<k>, then C<E(n,[i],k)>
1076+
The method C<< Value::Matrix->E(n, [i], k) >> returns the n by n elementary matrix that upon
1077+
right multiplication will multiply a row C<i>, by constant C<k>.
10761078
10771079
Usage:
10781080
@@ -1081,7 +1083,7 @@ Usage:
10811083
generates the matrix
10821084
10831085
[ [ 1, 0, 0, 0 ],
1084-
[ 0, 4, 0, 0 ],
1086+
[ 0, 3, 0, 0 ],
10851087
[ 0, 0, 1, 0 ],
10861088
[ 0, 0, 0, 1 ] ]
10871089
@@ -1093,7 +1095,8 @@ will generate the elementary matrix of size number of rows of C<$A>, which multi
10931095
10941096
=item * Multiply a row by a constant and add to another row.
10951097
1096-
To create the matrix that will multiply a row C<i>, by constant C<k> and add to row C<j> then C<E(n,[i, j],k)>
1098+
The method C<< Value::Matrix->E(n, [ i, j ], k) >> returns the n by n elementary matrix that upon
1099+
right multiplication will replace row C<j> with k times row C<i> added to row C<j>.
10971100
10981101
Usage:
10991102
@@ -1102,15 +1105,16 @@ Usage:
11021105
generates the matrix:
11031106
11041107
[ [ 1, 0, 0, 0 ],
1105-
[ 0, 1, 0, 0 ],
1106-
[ 0, -3, 1, 0 ],
1108+
[ 0, 1, -3, 0 ],
1109+
[ 0, 0, 1, 0 ],
11071110
[ 0, 0, 0, 1 ] ]
11081111
1109-
or if the matrix C<$A> exists then
1112+
or if the matrix C<$A> exists of size m by n then
11101113
11111114
$A->E([3, 4], -5);
11121115
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.
1116+
will generate the m by m elementary matrix which replaces row 4 by -5 times row 3 added to row 4.
1117+
If C<$A> does not have at least 4 rows, an error is raised.
11141118
11151119
=back
11161120
@@ -1147,7 +1151,7 @@ sub E {
11471151
if (@ij == 1) {
11481152
$row[$i] = $k if ($i == $ij[0]);
11491153
} elsif (defined $k) {
1150-
$row[ $ij[1] ] = $k if ($i == $ij[0]);
1154+
$row[ $ij[0] ] = $k if ($i == $ij[1]);
11511155
} else {
11521156
($row[ $ij[0] ], $row[ $ij[1] ]) = ($row[ $ij[1] ], $row[ $ij[0] ]) if ($i == $ij[0] || $i == $ij[1]);
11531157
}

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)