Reduced row echelon form: Difference between revisions

Content added Content deleted
(Reduced row echelon form using Julia language)
(→‎{{header|Julia}}: use built-in rref function in Julia)
Line 1,797: Line 1,797:


=={{header|Julia}}==
=={{header|Julia}}==
Julia comes with a built-in function <code>rref</code> to compute the reduced-row echelon form:
<lang julia>
function ToReducedRowEchelonForm(matrix)
lead = 1
rowCount,columnCount = size(matrix)

for row = 1:rowCount
if lead > columnCount
return
end

i = row
while matrix[i,lead] == 0
i += 1
if i == rowCount
i = row
lead += 1
if columnCount == lead
return
end
end
end

matrix[i,:],matrix[row,:] =matrix[row,:],matrix[i,:]
leadValue = matrix[row,lead]
matrix[row,:] = [ val / leadValue for val in matrix[row,:]]

for i=1:rowCount
if i != row
leadValue = matrix[i,lead]
matrix[i,:] = [ (ivalue - leadValue*rowvalue) for (rowvalue,ivalue) in zip(matrix[row,:],matrix[i,:])]
end
end
lead += 1
end
end

</lang>
Output:
<pre>
<pre>
julia> matrix = [1 2 -1 -4 ; 2 3 -1 -11 ; -2 0 -3 22]
julia> matrix = [1 2 -1 -4 ; 2 3 -1 -11 ; -2 0 -3 22]
Line 1,842: Line 1,805:
-2 0 -3 22
-2 0 -3 22


julia> ToReducedRowEchelonForm(matrix)
julia> rref(matrix)
3x4 Array{Float64,2}:

1.0 0.0 0.0 -8.0

0.0 1.0 0.0 1.0
julia> matrix
0.0 0.0 1.0 -2.0
3x4 Int32 Array:
1 0 0 -8
0 1 0 1
0 0 1 -2


</pre>
</pre>