Description:
Recently, I have a thought that writing a function to show the transformation process
when a matrix is transformed to upper-triangular.
My trail
Formatting the result(with the help of Mr.Wizard and Öskå)
augmentedMatrixForm[mat_?MatrixQ] :=
TraditionalForm@
MatrixForm@List@
Grid[
mat, Dividers -> {Length[mat] + 1 -> {Red, Dashed}, False}, Alignment -> Center]
Function LadderMatrixSteps
LadderMatrixSteps[mat_?MatrixQ] /; Equal @@ Dimensions@mat && Det@mat != 0 :=
Block[{augmentedMat, finalLadder, row, ladderStep, ladderTransform},
row = Length@mat;
augmentedMat = Join[mat, IdentityMatrix@Length@mat, 2];
ladderStep[0] = augmentedMat;
(*=========================================*)
ladderTransform[i_] :=
{ladderStep[i] =
MapAt[
#/ladderStep[i - 1][[(i + 1)/2, (i + 1)/2]] &, ladderStep[i - 1], (i + 1)/2],
ladderStep[i + 1] =
MapAt[
# - ladderStep[i][[(i + 1)/2]] #[[(i + 1)/2]] &,ladderStep[i],
List /@ Range[(i + 3)/2, row]]};
(*==============Construct Results=================*)
ladderTransform /@ Range[1, 2 row - 3, 2];
ladderStep[2 row - 1] =
MapAt[
#/ladderStep[2 row - 2][[row, row]] &, ladderStep[2 row -2], row];
(*============Show Results=================*)
Row[
augmentedMatrixForm /@
Table[ladderStep[i], {i, 0, 2 row - 1}], Style["-\[Rule]", 20, Red]]
// TraditionalForm
]
Test process
testMat= RandomInteger[{1, 10}, {5, 5}];
LadderMatrixSteps[testMat]
However, in my function LadderMatrixSteps
, I use the construct ladderStep[i]
to store the intermidiate results(like array in C). Although it works good, I think it is not suitable
in Mathematica .
Question
Is there other elegant method to relize my function
LadderMatrixSteps
(for exampleRule-Based
)?Is it possible to add
tags
between two steps (I just add a decollator"-\[Rule]"
inRow
)? Namely, shown as below: