Reduced row echelon form: Difference between revisions

Content added Content deleted
Line 645: Line 645:
=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
<lang AutoHotkey>ToReducedRowEchelonForm(M){
<lang AutoHotkey>ToReducedRowEchelonForm(M){
rowCount := M.Count() ; the number of rows in M
rowCount := M.Count() ; the number of rows in M
columnCount := M.1.Count() ; the number of columns in M
columnCount := M.1.Count() ; the number of columns in M
r := lead := 1
r := lead := 1
while (r <= rowCount) {
while (r <= rowCount) {
if (columnCount < lead)
if (columnCount < lead)
return M
return M
i := r
i := r
while (M[i, lead] = 0) {
while (M[i, lead] = 0) {
i++
i++
if (rowCount+1 = i) {
if (rowCount+1 = i) {
i := r, lead++
i := r, lead++
if (columnCount+1 = lead)
if (columnCount+1 = lead)
return M
return M
}
}
}
}
if (i<>r)
if (i<>r)
for col, v in M[i] ; Swap rows i and r
for col, v in M[i] ; Swap rows i and r
tempVal := M[i, col], M[i, col] := M[r, col], M[r, col] := tempVal
tempVal := M[i, col], M[i, col] := M[r, col], M[r, col] := tempVal
num := M[r, lead]
num := M[r, lead]
if (M[r, lead] <> 0)
if (M[r, lead] <> 0)
for col, val in M[r]
for col, val in M[r]
M[r, col] /= num ; If M[r, lead] is not 0 divide row r by M[r, lead]
M[r, col] /= num ; If M[r, lead] is not 0 divide row r by M[r, lead]
i := 2
i := 2
while (i <= rowCount) {
while (i <= rowCount) {
num := M[i, lead]
num := M[i, lead]
if (i <> r)
if (i <> r)
for col, val in M[i] ; Subtract M[i, lead] multiplied by row r from row i
for col, val in M[i] ; Subtract M[i, lead] multiplied by row r from row i
M[i, col] -= num * M[r, col]
M[i, col] -= num * M[r, col]
i++
i++
}
}
lead++, r++
lead++, r++
}
}
return M
return M
}</lang>
}</lang>
Examples:<lang AutoHotkey>M := [[1 , 2, -1, -4 ]
Examples:<lang AutoHotkey>M := [[1 , 2, -1, -4 ]
, [2 , 3, -1, -11]
, [2 , 3, -1, -11]
, [-2, 0, -3, 22]]
, [-2, 0, -3, 22]]
M := ToReducedRowEchelonForm(M)
M := ToReducedRowEchelonForm(M)
for row, obj in M
for row, obj in M
{
{
for col, v in obj
for col, v in obj
output .= RegExReplace(v, "\.0+$|0+$") "`t"
output .= RegExReplace(v, "\.0+$|0+$") "`t"
output .= "`n"
output .= "`n"
}
}
MsgBox % output
MsgBox % output