MatrixReduce.pl - reduced row echelon form, row operations, and elementary matrices.
Provides subroutines for elementary matrix computations using MathObjects matrices.
$Areduced = rref($A);
Should be used in the fraction context with all entries of $A made into fractions.
After selecting the Fraction context using Context('Fraction')->parens->set("[" => {formMatrix => 1}), $A = apply_fraction_to_matrix_entries($A);
applies Fraction() to all of the entries of $A, which makes subsequent matrix algebra computations with $A use fraction arithmetic.
$Areduced = rcef($A);
change_matrix_entry($A,[2,3],50);
changes the [2,3] entry to the value 50.
$E = identity_matrix(5);
(This is an alias for Value::Matrix->I(5);)
$E = elem_matrix_row_switch(5,2,4);
creates a 5 x 5 identity matrix and swaps rows 2 and 4.
$E = elem_matrix_row_mult(5,2,4);
creates a 5 x 5 identity matrix and swaps puts 4 in the second spot on the diagonal.
$E = elem_matrix_row_mult(5,2,4);
creates a 5 x 5 identity matrix and puts 4 in the second spot on the diagonal.$E3 = elem_matrix_row_add(5,3,1,35);
creates a 5 x 5 identity matrix and puts 35 in the (3,1) position.$Areduced = row_switch($A,2,4);
swaps rows 2 and 4 in matrix $A.
$Areduced = row_mult(A,2,10);
multiplies every entry in row 2 of $A by 10.
$Areduced = row_add($A,2,1,10);
adds 10 times row 1 to row 2 and places the result in row 2. (Same as constructing $E to be the identity with 10 placed in entry (2,1), then multiplying $E * $A.)
Usage:
DOCUMENT();
loadMacros(
"PGstandard.pl",
"MathObjects.pl",
"MatrixReduce.pl", # automatically loads contextFraction.pl and MathObjects.pl
"PGcourse.pl",
);
$showPartialCorrectAnswers = 0;
TEXT(beginproblem());
# Context('Matrix'); # for decimal arithmetic
Context('Fraction'); # for fraction arithmetic
$A = Matrix([
[random(-5,5,1),random(-5,5,1),random(-5,5,1),3],
[random(-5,5,1),random(-5,5,1),random(-5,5,1),0.75],
[random(-5,5,1),random(-5,5,1),random(-5,5,1),9/4],
]);
$A = apply_fraction_to_matrix_entries($A); # try commenting this line out for different results
$Arref = rref($A);
$Aswitch = row_switch($A, 2, 3);
$Amult = row_mult($A, 2, 4);
$Aadd = row_add($A, 2, 1, 10);
$E = elem_matrix_row_add(3,2,1,10);
$EA = $E * $A;
$E1 = elem_matrix_row_switch(5,2,4);
$E2 = elem_matrix_row_mult(5,4,Fraction(1/10));
$E3 = elem_matrix_row_add(5,3,1,35);
$E4 = identity_matrix(4);
change_matrix_entry($E4,[3,2],10);
Context()->texStrings;
BEGIN_TEXT
The original matrix and its row reduced echelon form:
\[ $A \sim $Arref. \]
$BR
The original matrix with rows switched, multiplied, or added together:
\[ $Aswitch, $Amult, $Aadd. \]
$BR
Some elementary matrices.
\[$E1, $E2, $E3, $E4\]
END_TEXT
Context()->normalStrings;
COMMENT('MathObject version.');
ENDDOCUMENT();
Paul Pearson, Hope College, Department of Mathematics
with help from
Davide Cervone, Union College, Department of Mathematics
Michael Doob, University of Manitoba, Department of Mathematics