Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit 2d76c3705d459343d489d36cac96cd90464e3bf5
Author: John Lapeyre <[email protected]>
Date:   Mon May 22 19:21:31 2017 +0200

    more fixes for v0.6

    fix outdated syntax, silence warnings

commit de54dc2c0093c9ad11577799c32ef413e0e1af14
Author: John Lapeyre <[email protected]>
Date:   Wed Dec 7 02:34:59 2016 +0100

    fix outdated syntax

commit dd77542870dd4da64f09a28047468a8f81ca2824
Author: John Lapeyre <[email protected]>
Date:   Wed Dec 7 02:34:41 2016 +0100

    fix outdated syntax

commit 244b33c33f799bd465f2f5b910b47603906e3007
Author: John Lapeyre <[email protected]>
Date:   Wed Dec 7 02:04:48 2016 +0100

    fix outdated syntax
  • Loading branch information
jlapeyre committed May 22, 2017
1 parent 9ba8f9f commit 53128a4
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 49 deletions.
51 changes: 25 additions & 26 deletions src/PermPlain.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ randperm{T<:Real}(::Type{T}, n::Integer) = collect(T,randperm(n))
function permcycles{T<:Real}(p::AbstractVector{T})
n = length(p)
visited = falses(n)
cycles = Array(Array{T,1},0)
cycles = Array{Array{T,1}}(0)
for k in convert(T,1):convert(T,n)
if ! visited[k]
knext = k
cycle = Array(T,0)
cycle = Array{T}(0)
while ! visited[knext]
push!(cycle,knext)
visited[knext] = true
Expand All @@ -38,7 +38,7 @@ permcycles{T<:Real}(m::AbstractArray{T,2}) = permcycles(permlist(m))

permcycles{T}(sp::Dict{T,T}) = sparsetocycles(sp)
function sparsetocycles{T}(sp::Dict{T,T})
cycs = Array(Array{T,1},0)
cycs = Array{Array{T,1}}(0)
length(sp) == 0 && return cycs
ks = collect(keys(sp))
n = length(ks)
Expand All @@ -57,7 +57,7 @@ function sparsetocycles{T}(sp::Dict{T,T})
end
foundunseen == false && break
kcyclestart = k
cyc = Array(T,0)
cyc = Array{T}(0)
while true
push!(cyc,k)
if seen[k] == true
Expand All @@ -78,7 +78,7 @@ end
## permlist. permutation in single-list form ##

permlist{T<:Real}(m::AbstractArray{T,2}) = mattoperm(m)
mattoperm{T<:Real}(m::AbstractArray{T,2}) = mattoperm!(m,Array(T,size(m)[1]))
mattoperm{T<:Real}(m::AbstractArray{T,2}) = mattoperm!(m,Array{T}(size(m)[1]))

function mattoperm!{T<:Real}(m::AbstractArray{T,2}, p)
n = size(m)[1]
Expand All @@ -96,23 +96,24 @@ end

permlist{T<:Real}(cycs::AbstractArray{Array{T,1},1}, pmax::Real = 0) = cycstoperm(cycs,pmax)
function cycstoperm{T<:Real}(cycs::AbstractArray{Array{T,1},1}, pmax::Integer = 0)
length(cycs) == 0 && return [one(T):convert(T,pmax)]
isempty(cycs) && return [one(T):convert(T,pmax)]
cmaxes = [maximum(c) for c in cycs]
cmax = maximum(cmaxes) # must be a faster way
perm = [one(T): (pmax > cmax ? convert(T,pmax) : convert(T,cmax))]
# perm = [one(T): (pmax > cmax ? convert(T,pmax) : convert(T,cmax))]
perm = collect(one(T): (pmax > cmax ? convert(T,pmax) : convert(T,cmax)))
for c in cycs
for i in convert(T,2):convert(T,length(c))
for i in 2:length(c)
perm[c[i-1]] = c[i]
end
perm[c[end]] = c[1]
end
perm[cmax+1:pmax] = [cmax+1:pmax]
perm[cmax+1:pmax] = collect(cmax+1:pmax)
return perm
end

permlist{T}(sp::Dict{T,T}) = sparsetolist(sp::Dict{T,T})
function sparsetolist{T}(sp::Dict{T,T})
p = [one(T):convert(T,maximum(sp)[1])]
p = collect(one(T):convert(T,maximum(sp)[1]))
for (i,v) in sp
p[i] = v
end
Expand Down Expand Up @@ -185,7 +186,7 @@ end
# used by gap, Mma, and Arndt thesis.
# Note that Arndt uses a different order internally to store the cycles as a single list.
function canoncycles{T}(cycs::AbstractArray{Array{T,1},1})
ocycs = Array(Array{T,1},0)
ocycs = Array{Array{T,1}}(0)
for cyc in cycs
push!(ocycs,circshift(cyc,-indmin(cyc)+1))
end
Expand All @@ -198,7 +199,7 @@ end
function cyclelengths{T<:Real}(p::AbstractVector{T})
n = length(p)
visited = falses(n)
lengths = Array(Int,0)
lengths = Array{Int}(0)
for k in convert(T,1):convert(T,n)
if ! visited[k]
knext = k
Expand All @@ -218,7 +219,7 @@ cyclelengths{T<:Real}(c::AbstractArray{Array{T,1},1}) = [length(x) for x in c]

# Gives cyclelengths in canonical order.
function cyclelengths{T}(sp::Dict{T,T})
cyclens = Array(Int,0)
cyclens = Array{Int}(0)
isempty(sp) && return cyclens
ks = sort(collect(keys(sp)))
n = length(ks)
Expand Down Expand Up @@ -311,7 +312,7 @@ function permcompose{T<:Real, V<:Real}(q::AbstractVector{T}, p::AbstractVector{V
lq = length(q)
lp == lq && return q[p] # prbly not much time saved
lr = lp < lq ? lq : lp
r = Array(T,lr)
r = Array{T}(lr)
if lp <= lq
r[1:lp] = q[p]
r[lp+1:lq] = q[lp+1:lq]
Expand Down Expand Up @@ -382,17 +383,15 @@ function permapply{T<:Real, V}(q::AbstractVector{T}, a::AbstractArray{V})
aout
end

function permapply{T<:Real, V<:String}(q::Union(Dict{T,T},AbstractVector{T}), a::V)
ASCIIString(permapply(q,a.data))
end
permapply{T<:Real}(q::Union{Dict{T,T},AbstractVector{T}}, a::String) = String(permapply(q,a.data))

# power of PLIST. output is PLIST
# The method permpower below is usually preferred because it does less allocation
function permpower2{T<:Real}(p::AbstractVector{T}, n::Integer)
n == 0 && return [one(T):convert(T,length(p))]
n == 1 && return copy(p) # for consistency, don't return ref
n < 0 && return permpower2(invperm(p),-n)
q = permpower2(p, int(floor(n/2)))
q = permpower2(p, Int(floor(n/2)))
q = q[q]
return iseven(n) ? q : p[q]
end
Expand All @@ -406,7 +405,7 @@ function permpower!{T<:Real}(p::AbstractVector{T},
n == 0 && (for i in onep:lenp pret[i] = i end; return )
n < 0 && (permpower!(invperm(p), pret, ptmp, -n); return )
n == 1 && (copy!(pret,p); return)
permpower!(p, ptmp, pret,int(floor(n/2)))
permpower!(p, ptmp, pret,Int(floor(n/2)))
if iseven(n)
for i in onep:lenp pret[i] = ptmp[ptmp[i]] end
else
Expand All @@ -432,7 +431,7 @@ function permpower{T<:Real}(cyc::AbstractArray{Array{T,1},1}, exp::Integer)
for j in 1:length(cyc)
r += gcd(length(cyc[j])-1,exp)
end
c = Array(Array{T,1},0)
c = Array{Array{T,1}}(0)
for j in 1:length(cyc)
v = cyc[j]
n = length(v)
Expand All @@ -441,7 +440,7 @@ function permpower{T<:Real}(cyc::AbstractArray{Array{T,1},1}, exp::Integer)
m = div(n,g)
if m == 1 continue end
for i in 1:g
p = Array(Int,0)
p = Array{Int}(0)
l = i
for k in 1:m
push!(p,v[l])
Expand All @@ -466,7 +465,7 @@ function cyc_pow_perm{T<:Real}(cyc::AbstractArray{Array{T,1},1}, exp::Integer)
# for j = 1:length(cyc)
# n += length(cyc[j])+1
# end
p = T[1:n] # wasteful
p = collect(T,1:n) # wasteful
for j = 1:length(cyc)
v = cyc[j]
n = length(v)
Expand Down Expand Up @@ -620,7 +619,7 @@ end
function same{T<:Real, V<:Real}(p::AbstractVector{T}, q::AbstractVector{V})
lp = length(p)
lq = length(q)
d = Array(eltype(p),0)
d = Array{eltype(p)}(0)
if lp < lq
for i in 1:lp
p[i] == q[i] ? push!(d,p[i]) : nothing
Expand Down Expand Up @@ -677,7 +676,7 @@ end

function support{T<:Real}(p::AbstractVector{T})
lp = length(p)
mov = Array(eltype(p),0)
mov = Array{eltype(p)}(0)
for i in 1:lp
k = p[i]
k != i ? push!(mov,i) : nothing
Expand All @@ -687,7 +686,7 @@ end

function fixed{T<:Real}(p::AbstractVector{T})
lp = length(p)
fixedel = Array(eltype(p),0)
fixedel = Array{eltype(p)}(0)
for i in 1:lp
k = p[i]
k == i ? push!(fixedel,i) : nothing
Expand Down Expand Up @@ -726,7 +725,7 @@ permarrprint(v::Array) = permarrprint(STDOUT,v)

# Copied from Base string.jl
function pprint_to_string(printfunc::Function, xs...)
s = IOBuffer(Array(Uint8,isa(xs[1],String) ? endof(xs[1]) : 0), true, true)
s = IOBuffer(Array{Uint8}(isa(xs[1],String) ? endof(xs[1]) : 0), true, true)
truncate(s,0)
for x in xs
printfunc(s, x)
Expand Down
19 changes: 7 additions & 12 deletions src/collect.jl
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
# surely, there is a more straightforward way
# These are meant to collect and convert arguments for use with PermutationsA

function collect1{T<:String}(a::Array{T,1})
map(BigInt,a)
end

function collect1{T<:Real}(a::Array{T,1})
return a
end
collect1(a::Array{String,1}) = map(BigInt,a)
collect1(a::Array{Real,1}) = a

function collect1{T<:Real, V<:Real}(::Type{T}, a::Array{V,1})
return T == V ? a : collect(T,a) # note that it may return a copy!
end

function tupcollect{T}(::Type{T}, a::Tuple)
aout = Array(Array{T,1},0)
aout = Array{Array{T,1}}(0)
for x in a
a1 = Array(T,0)
a1 = Array{T}(0)
for x1 in x
push!(a1,convert(T,x1))
end
Expand All @@ -25,12 +20,12 @@ function tupcollect{T}(::Type{T}, a::Tuple)
return aout
end

for (f,t) in ((:int32, Int32), (:int64, Int64), (:int, Int) , (:BigInt, BigInt))
for (f,t) in ((:int32, Int32), (:int64, Int64), (:BigInt, BigInt))
@eval begin
function tupcollect(::Type{$t}, a::Tuple)
aout = Array(Array{$t,1},0)
aout = Array{Array{$t,1}}(0)
for x in a
a1 = Array($t,0)
a1 = Array{$t}(0)
for x1 in x
push!(a1, ($f)(x1))
end
Expand Down
2 changes: 1 addition & 1 deletion src/matrixops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Kronecker product for matrices induces a Kronecker product on permutations.
function permkron{T<:Real,S<:Real}(p::AbstractVector{T}, q::AbstractVector{S})
np = length(p); nq = length(q)
dc = Array(promote_type(T,S), np*nq)
dc = Array{promote_type(T,S)}(np*nq)
for i in 1:np
for k in 1:nq
dc[nq*(i-1) + k] = nq*(p[i]-1)+q[k]
Expand Down
7 changes: 2 additions & 5 deletions src/pivot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,13 @@ function ipiv2perm{T}(v::AbstractVector{T}, maxi::Integer)
return p
end

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

ipiv2perm(v::AbstractVector) = ipiv2perm(v,length(v))

# 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})
function perm2ipiv(p::AbstractVector)
n = length(p)
m = [1:n]
v = similar(m)
Expand Down
10 changes: 5 additions & 5 deletions test/test1.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
c = Array(Array{Int,1},0)
push!(c, Int[1,6,14,7,3,11])
push!(c, Int[2,8,12,9,10,5,15,4])
import PermPlain: permcycles, cycstoperm

@test PermPlain.permcycles(PermPlain.cycstoperm(c)) == c
c = [ [1,6,14,7,3,11], [2,8,12,9,10,5,15,4]]

@test permcycles(cycstoperm(c)) == c
np = 3
p = PermPlain.cycstoperm(c)
pp = PermPlain.permpower(p,np);
Expand All @@ -14,7 +14,7 @@ p2 = PermPlain.cycstoperm(c1);
@test PermPlain.permcycles(pp) == PermPlain.canoncycles(c1)
@test PermPlain.cyc_pow_perm(c,np) == pp

p = Int[3,7,2,4,8,10,1,6,9,5];
p = [3,7,2,4,8,10,1,6,9,5];
s,max = permsparse(permmatrix(p))
@test permlist(permcycles(s)) == p
s,max = permsparse(permcycles(p))
Expand Down

0 comments on commit 53128a4

Please sign in to comment.