Skip to content

Commit

Permalink
new file: src/pivot.jl
Browse files Browse the repository at this point in the history
  • Loading branch information
jlapeyre committed Dec 18, 2014
1 parent fe7d61c commit 736975f
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ permkron(p,q) # kronecker product of list permutations, induced by m
cyc_pow_perm(c,n) # nth power of c returned in array representation
m=permtomat(p,flag) # permutation matrix, sparse if flag is true
mattoperm(m) # array representation of permutation (abstract) matrix m
ipiv2perm(v,[n]) # convert pivot vector to permutation in list form
perm2piv(p) # convert p to pivot vector
isperm(m) # true if matrix m is a permutation
isperm(c) # true if c is a permutation
Base.isperm(p) # already present in Base module
Expand Down
1 change: 1 addition & 0 deletions src/PermPlain.jl
Original file line number Diff line number Diff line change
Expand Up @@ -731,5 +731,6 @@ cyclestring(xs...) = pprint_to_string(cycleprint, x...)


include("matrixops.jl")
include("pivot.jl")

end # module Permplain
34 changes: 34 additions & 0 deletions src/pivot.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Copied from linalg/lu.jl
function ipiv2perm{T}(v::AbstractVector{T}, maxi::Integer)
p = T[1:maxi]
@inbounds for i in 1:length(v)
p[i], p[v[i]] = p[v[i]], p[i]
end
return p
end

function ipiv2perm{T}(v::AbstractVector{T})
ipiv2perm(v,length(v))
end


# A naive algorithm, maybe there exists a faster one.
# Build the pivot while permuting 1:n. Make
# inverse perm at the same time, so we know where to
# find p[i]
function perm2ipiv{T}(p::AbstractVector{T})
n = length(p)
m = [1:n]
v = similar(m)
x = copy(m)
@inbounds for i in 1:n
j = x[p[i]]
mi = m[i]
mj = m[j]
m[i],m[j] = mj,mi
v[i] = j
x[mj] = i
x[mi] = j
end
return v
end
2 changes: 2 additions & 0 deletions src/util.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# turns out that this is not needed,
# Julia has multiple parallel assignment
macro swap!(p,q)
return quote
begin
Expand Down

0 comments on commit 736975f

Please sign in to comment.