Skip to content

Commit

Permalink
Fix all errors
Browse files Browse the repository at this point in the history
	modified:   src/PermPlain.jl
	modified:   test/runtests.jl
	modified:   test/test1.jl
  • Loading branch information
jlapeyre committed Dec 6, 2018
1 parent 5bde487 commit 3d4e597
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 37 deletions.
7 changes: 5 additions & 2 deletions src/PermPlain.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ export permlist, permcycles, permsparse, permmatrix # whether to export, and wha

randperm(::Type{T}, n::Integer) where T = collect(T,randperm(n))

eye(::Type{T}, n::Integer) where T = Matrix{T}(LinearAlgebra.I, n, n)
speye(::Type{T}, n) where T = sparse(LinearAlgebra.I * one(T), n, n)

AbstractVectorVector{T} = AbstractVector{V} where {V <: (AbstractVector{T} where {T})}

## permcycles. Find cyclic decomposition ##
Expand Down Expand Up @@ -176,15 +179,15 @@ permmatrix(p::AbstractVector{<:Real}, sparse::Bool = false) = permtomat(p, spars
# Convert PLIST to PMAT
function permtomat(p::AbstractVector{T}, sparse::Bool = false) where T <: Real
n::T = length(p)
A = sparse ? sparse(LinearAlgebra.I*one(T), n, n) : Matrix{T}(LinearAlgebra.I, n, n)
A = sparse ? speye(T, n) : eye(T, n)
return A[p,:]
end

function permmatrix(sp::Dict{T, T}) where T
n = convert(T,maximum(sp)[1])
ot = one(T)
z = zero(T)
m = Matrix(LinearAlgebra.I, n, n)
m = eye(Int, n)
@inbounds for (i, v) in sp
m[i,i] = z
m[i,v] = ot
Expand Down
42 changes: 41 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,44 @@
using PermPlain
using Test
using PermPlain: permcycles, cycstoperm

include("test1.jl")
makec() = [[1, 6, 14, 7, 3, 11], [2, 8, 12, 9, 10, 5, 15, 4]]
makep() = [3,7,2,4,8,10,1,6,9,5]

@testset "permcycles cyclestoperm" begin
c = makec()
@test permcycles(cycstoperm(c)) == c
end

@testset "permpower canoncycles" begin
np = 3
c = makec()
p = PermPlain.cycstoperm(c)
pp = PermPlain.permpower(p,np);
pp1 = PermPlain.permpower2(p,np);
c1 = PermPlain.permpower(c,np);
p2 = PermPlain.cycstoperm(c1);
@test pp == p2
@test pp1 == p2
@test PermPlain.permcycles(pp) == PermPlain.canoncycles(c1)
@test PermPlain.cyc_pow_perm(c,np) == pp
end

@testset "permsparse" begin
p = makep()
s,max = permsparse(permmatrix(p))
@test permlist(permcycles(s)) == p

s,max = permsparse(permcycles(p))
@test permlist(permmatrix(s)) == p
end

@testset "isperm" begin
p = makep()
s,max = permsparse(p)
@test permlist(permcycles(permmatrix(s))) == p
@test isperm(s)
@test isperm(permcycles(s))
@test isperm(permmatrix(s))
@test isperm(permlist(s)) # defined in Base
end
34 changes: 0 additions & 34 deletions test/test1.jl
Original file line number Diff line number Diff line change
@@ -1,34 +0,0 @@
using PermPlain: permcycles, cycstoperm

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);
pp1 = PermPlain.permpower2(p,np);
c1 = PermPlain.permpower(c,np);
p2 = PermPlain.cycstoperm(c1);
@test pp == p2
@test pp1 == p2
@test PermPlain.permcycles(pp) == PermPlain.canoncycles(c1)
@test PermPlain.cyc_pow_perm(c,np) == pp

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

# FIXME: all sparse stuff broke in Julia v1.0 upgrade.
# It should not be hard to fix

# s,max = permsparse(permcycles(p))
# @test permlist(permmatrix(s)) == p

#s,max = permsparse(p)
# @test permlist(permcycles(permmatrix(s))) == p
# @test isperm(s)
# @test isperm(permcycles(s))
# @test isperm(permmatrix(s))
# @test isperm(permlist(s)) # defined in Base

true

0 comments on commit 3d4e597

Please sign in to comment.