Skip to content

Instantly share code, notes, and snippets.

@Axure
Last active June 2, 2016 05:13
Show Gist options
  • Select an option

  • Save Axure/36284e605cb60215942964add08d86ac to your computer and use it in GitHub Desktop.

Select an option

Save Axure/36284e605cb60215942964add08d86ac to your computer and use it in GitHub Desktop.
Julia operations.
function swap_row!{T}(X::Array{T,2}, i, j)
if i != j
@inbounds for k in 1:size(X, 2)
X[i, k], X[j, k] = X[j, k], X[i, k]
end
end
return X
end
function swap_column!{T}(X::Array{T,2}, i, j)
if i != j
@inbounds for k in 1:size(X, 1)
X[k, i], X[k, j] = X[k, j], X[k, i]
end
end
return X
end
function row_simple!{T}(X::Array{T,2})
column_ptr = 1
height, width = size(X)
# result=
@printf("%d, %d", width, height)
finished = false
@inbounds for i=1:height
@inbounds for j=i+1:height
# Ensure that this line starts with a non-zero element.
while X[i,column_ptr] == 0
i_,j_=first_nonzero(X,i,column_ptr)
if i_==-1
finished = true
break
else
swap_row!(X, i, i_)
swap_column!(X, column_ptr, j_)
end
end
if finished
break
end
# Do the subtrXction
@printf("Doing with row %d, %d, column %d\n", i, j, column_ptr)
X[j,:]=X[j,:] - X[j,column_ptr]/X[i,column_ptr]*X[i,:]
println(X)
end
if finished
break
end
column_ptr += 1
end
return X
end
function row_simple{T}(mat::Array{T,2})
X = copy(mat)
return row_simple!(X)
end
function row_simple{T<:Int}(X::Array{T,2})
return row_simple!(convert(Array{Float64,2},X))
end
function first_nonzero{T}(mat::Array{T,2}, i_offset=1, j_offset=1)
@printf("Offsets: %d, %d", i_offset, j_offset)
@inbounds for i=i_offset:size(mat,1)
@inbounds for j=j_offset:size(mat,2)
@printf("Checking %d, %d", i, j)
if (mat[i,j] != 0)
return i,j
end
end
end
return -1,-1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment