Reduced row echelon form: Difference between revisions

Added AutoHotkey
(Added 11l)
(Added AutoHotkey)
Line 641:
0.0 1.0 0.0 1.0
0.0 0.0 1.0 -2.0
</pre>
 
=={{header|AutoHotkey}}==
<lang AutoHotkey>ToReducedRowEchelonForm(M){
rowCount := M.Count() ; the number of rows in M
columnCount := M.1.Count() ; the number of columns in M
r := lead := 1
while (r <= rowCount) {
if (columnCount < lead)
return M
i := r
while (M[i, lead] = 0) {
i++
if (rowCount+1 = i) {
i := r, lead++
if (columnCount+1 = lead)
return M
}
}
if (i<>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
num := M[r, lead]
if (M[r, lead] <> 0)
for col, val in M[r]
M[r, col] /= num ; If M[r, lead] is not 0 divide row r by M[r, lead]
i := 1
while (i <= rowCount) {
num := M[i, lead]
if (i <> r)
for col, val in M[i] ; Subtract M[i, lead] multiplied by row r from row i
M[i, col] -= num * M[r, col]
i++
}
lead++, r++
}
return M
}</lang>
Examples:<lang AutoHotkey>M := [[1 , 2, -1, -4 ]
, [2 , 3, -1, -11]
, [-2, 0, -3, 22]]
M := ToReducedRowEchelonForm(M)
for row, obj in M
{
for col, v in obj
output .= RegExReplace(v, "\.0+$|0+$") "`t"
output .= "`n"
}
MsgBox % output
return</lang>
{{out}}
<pre>1 0 0 -8
-0 1 0 1
-0 -0 1 -2
</pre>
 
299

edits