Reduced row echelon form: Difference between revisions
Content added Content deleted
(Reduced row echelon form using Julia language) |
|||
Line 1,794: | Line 1,794: | ||
0,1,0,1.666666666666667 |
0,1,0,1.666666666666667 |
||
0,0,1,1</pre> |
0,0,1,1</pre> |
||
=={{header|Julia}}== |
|||
<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> |
|||
julia> matrix = [1 2 -1 -4 ; 2 3 -1 -11 ; -2 0 -3 22] |
|||
3x4 Int32 Array: |
|||
1 2 -1 -4 |
|||
2 3 -1 -11 |
|||
-2 0 -3 22 |
|||
julia> ToReducedRowEchelonForm(matrix) |
|||
julia> matrix |
|||
3x4 Int32 Array: |
|||
1 0 0 -8 |
|||
0 1 0 1 |
|||
0 0 1 -2 |
|||
</pre> |
|||
=={{header|Lua}}== |
=={{header|Lua}}== |