Reduced row echelon form: Difference between revisions

Content added Content deleted
(Updated D entry)
No edit summary
Line 1,430: Line 1,430:
replace n e l = a ++ e : b
replace n e l = a ++ e : b
where (a, _ : b) = splitAt n l</lang>
where (a, _ : b) = splitAt n l</lang>

=={{header|Icon}} and {{header|Unicon}}==

Works in both languages:
<lang unicon>procedure main(A)
tM := [[ 1, 2, -1, -4],
[ 2, 3, -1,-11],
[ -2, 0, -3, 22]]
showMat(rref(tM))
end

procedure rref(M)
lead := 1
rCount := *\M | stop("no Matrix?")
cCount := *(M[1]) | 0
every r := !rCount do {
i := r
while M[i,lead] = 0 do {
if (i+:=1) > rCount then {
i := r
if cCount < (lead +:= 1) then stop("can't reduce")
}
}
M[i] :=: M[r]
if 0 ~= (m0 := M[r,lead]) then every !M[r] /:= real(m0)
every r ~= (i := !rCount) do {
every !(mr := copy(M[r])) *:= M[i,lead]
every M[i,j := !cCount] -:= mr[j]
}
lead +:= 1
}
return M
end

procedure showMat(M)
every r := !M do every writes(right(!r,5)||" " | "\n")
end</lang>

Output:
<pre>
->rref
1.0 0.0 0.0 -8.0
0.0 1.0 0.0 1.0
0.0 0.0 1.0 -2.0
->
</pre>


=={{header|J}}==
=={{header|J}}==