diff --git a/.JuliaFormatter.toml b/.JuliaFormatter.toml new file mode 100644 index 0000000..78d61d4 --- /dev/null +++ b/.JuliaFormatter.toml @@ -0,0 +1,2 @@ +style = "blue" +whitespace_ops_in_indices=false diff --git a/src/combinations.jl b/src/combinations.jl index ec466a9..63231f2 100644 --- a/src/combinations.jl +++ b/src/combinations.jl @@ -1,8 +1,5 @@ export combinations, - CoolLexCombinations, - multiset_combinations, - with_replacement_combinations, - powerset + CoolLexCombinations, multiset_combinations, with_replacement_combinations, powerset #The Combinations iterator struct Combinations @@ -10,22 +7,22 @@ struct Combinations t::Int end -@inline function Base.iterate(c::Combinations, s = [min(c.t - 1, i) for i in 1:c.t]) +@inline function Base.iterate(c::Combinations, s=[min(c.t - 1, i) for i in 1:c.t]) if c.t == 0 # special case to generate 1 result for t==0 isempty(s) && return (s, [1]) - return + return nothing end for i in c.t:-1:1 s[i] += 1 if s[i] > (c.n - (c.t - i)) continue end - for j in i+1:c.t + for j in (i+1):c.t s[j] = s[j-1] + 1 end break end - s[1] > c.n - c.t + 1 && return + s[1] > c.n - c.t + 1 && return nothing (s, s) end @@ -49,14 +46,13 @@ function combinations(a, t::Integer) (reorder(c) for c in Combinations(length(a), t)) end - """ combinations(a) Generate combinations of the elements of `a` of all orders. Chaining of order iterators is eager, but the sequence at each order is lazy. """ -combinations(a) = Iterators.flatten([combinations(a, k) for k = 0:length(a)]) +combinations(a) = Iterators.flatten([combinations(a, k) for k in 0:length(a)]) # cool-lex combinations iterator @@ -102,7 +98,7 @@ function Base.iterate(C::CoolLexCombinations) end function Base.iterate(C::CoolLexCombinations, S::CoolLexIterState) - (S.R3 & S.R2 != 0) && return + (S.R3 & S.R2 != 0) && return nothing R0 = S.R0 R1 = S.R1 @@ -134,7 +130,6 @@ end Base.length(C::CoolLexCombinations) = max(0, binomial(C.n, C.t)) - struct MultiSetCombinations{T} m::T f::Vector{Int} @@ -158,7 +153,7 @@ function Base.length(c::MultiSetCombinations) end else for j in t:-1:1 - p[j+1] = sum(p[max(1,j+1-f):(j+1)]) + p[j+1] = sum(p[max(1, j+1-f):(j+1)]) end end end @@ -167,7 +162,7 @@ end function multiset_combinations(m, f::Vector{<:Integer}, t::Integer) length(m) == length(f) || error("Lengths of m and f are not the same.") - ref = length(f) > 0 ? vcat([[i for j in 1:f[i] ] for i in 1:length(f)]...) : Int[] + ref = length(f) > 0 ? vcat([[i for j in 1:f[i]] for i in 1:length(f)]...) : Int[] if t < 0 t = length(ref) + 1 end @@ -185,8 +180,9 @@ function multiset_combinations(a, t::Integer) multiset_combinations(m, f, t) end -function Base.iterate(c::MultiSetCombinations, s = c.ref) - ((!isempty(s) && max(s[1], c.t) > length(c.ref)) || (isempty(s) && c.t > 0)) && return +function Base.iterate(c::MultiSetCombinations, s=c.ref) + ((!isempty(s) && max(s[1], c.t) > length(c.ref)) || (isempty(s) && c.t > 0)) && + return nothing ref = c.ref n = length(ref) @@ -196,7 +192,7 @@ function Base.iterate(c::MultiSetCombinations, s = c.ref) if t > 0 s = copy(s) for i in t:-1:1 - if s[i] < ref[i + (n - t)] + if s[i] < ref[i+(n-t)] j = 1 while ref[j] <= s[i] j += 1 @@ -232,8 +228,8 @@ Generate all combinations with replacement of size `t` from an array `a`. """ with_replacement_combinations(a, t::Integer) = WithReplacementCombinations(a, t) -function Base.iterate(c::WithReplacementCombinations, s = [1 for i in 1:c.t]) - (!isempty(s) && s[1] > length(c.a) || c.t < 0) && return +function Base.iterate(c::WithReplacementCombinations, s=[1 for i in 1:c.t]) + (!isempty(s) && s[1] > length(c.a) || c.t < 0) && return nothing n = length(c.a) t = c.t @@ -269,7 +265,7 @@ returns an iterator object. Use `collect(powerset(a, min, max))` to get an array subsets. """ function powerset(a, min::Integer=0, max::Integer=length(a)) - itrs = [combinations(a, k) for k = min:max] + itrs = [combinations(a, k) for k in min:max] min < 1 && append!(itrs, eltype(a)[]) Iterators.flatten(itrs) end diff --git a/src/factorials.jl b/src/factorials.jl index 50fa026..48042a9 100644 --- a/src/factorials.jl +++ b/src/factorials.jl @@ -1,7 +1,6 @@ #Factorials and elementary coefficients -export - derangement, +export derangement, partialderangement, factorial, subfactorial, @@ -17,7 +16,7 @@ export Compute ``n!/k!``. """ -function Base.factorial(n::T, k::T) where T<:Integer +function Base.factorial(n::T, k::T) where {T<:Integer} if k < 0 || n < 0 || k > n throw(DomainError((n, k), "n and k must be nonnegative with k ≤ n")) end @@ -42,7 +41,6 @@ function Base.factorial(n::BigInt, k::BigInt) end Base.factorial(n::Integer, k::Integer) = factorial(promote(n, k)...) - """ derangement(n) @@ -98,13 +96,14 @@ end # Hyperfactorial hyperfactorial(n::Integer) = n==0 ? BigInt(1) : prod(i->i^i, BigInt(1):n) - function multifactorial(n::Integer, m::Integer) if n < 0 throw(DomainError(n, "n must be nonnegative")) end z = Ref{BigInt}(0) - ccall((:__gmpz_mfac_uiui, :libgmp), Cvoid, (Ref{BigInt}, UInt, UInt), z, UInt(n), UInt(m)) + ccall( + (:__gmpz_mfac_uiui, :libgmp), Cvoid, (Ref{BigInt}, UInt, UInt), z, UInt(n), UInt(m) + ) return z[] end diff --git a/src/multinomials.jl b/src/multinomials.jl index 6dc52f3..5c4d0cc 100644 --- a/src/multinomials.jl +++ b/src/multinomials.jl @@ -10,9 +10,9 @@ end # Standard stars and bars: # https://en.wikipedia.org/wiki/Stars_and_bars_(combinatorics) -function Base.iterate(m::MultiExponents, s = nothing) +function Base.iterate(m::MultiExponents, s=nothing) next = s === nothing ? iterate(m.c) : iterate(m.c, s) - next === nothing && return + next === nothing && return nothing stars, ss = next # stars minus their consecutive @@ -49,7 +49,7 @@ julia> collect(multiexponents(3, 2)) """ function multiexponents(m, n) # number of stars and bars = m+n-1 - c = combinations(1:m+n-1, n) + c = combinations(1:(m+n-1), n) MultiExponents(c, m) end diff --git a/src/numbers.jl b/src/numbers.jl index f1c2bb3..545bccf 100644 --- a/src/numbers.jl +++ b/src/numbers.jl @@ -25,16 +25,15 @@ function bellnum(n::Integer) end list = Vector{BigInt}(undef, n) list[1] = 1 - for i = 2:n - for j = 1:i - 2 - list[i - j - 1] += list[i - j] + for i in 2:n + for j in 1:(i-2) + list[i-j-1] += list[i-j] end - list[i] = list[1] + list[i - 1] + list[i] = list[1] + list[i-1] end return list[n] end - """ catalannum(n) @@ -55,7 +54,7 @@ end Compute the Lobb number `L(m,n)`, or the generalised Catalan number given by ``\\frac{2m+1}{m+n+1} \\binom{2n}{m+n}``. Wikipedia : https://en.wikipedia.org/wiki/Lobb_number """ -function lobbnum(bm::Integer,bn::Integer) +function lobbnum(bm::Integer, bn::Integer) if !(0 <= bm <= bn) throw(DomainError("m and n must be non-negative")) else @@ -71,14 +70,14 @@ end Compute the Narayana number `N(n,k)` given by ``\\frac{1}{n}\\binom{n}{k}\\binom{n}{k-1}`` Wikipedia : https://en.wikipedia.org/wiki/Narayana_number """ -function narayana(bn::Integer,bk::Integer) +function narayana(bn::Integer, bk::Integer) if !(1 <= bk <= bn) throw(DomainError("Domain is 1 <= k <= n")) else n = BigInt(bn) k = BigInt(bk) end - div(binomial(n, k)*binomial(n, k - 1) , n) + div(binomial(n, k)*binomial(n, k - 1), n) end function fibonaccinum(n::Integer) @@ -90,7 +89,6 @@ function fibonaccinum(n::Integer) return z[] end - function jacobisymbol(a::Integer, b::Integer) ba = Ref{BigInt}(a) bb = Ref{BigInt}(b) @@ -104,8 +102,12 @@ Compute the ``n``th entry in Lassalle's sequence, OEIS entry A180874. """ function lassallenum(m::Integer) A = ones(BigInt, m) - for n = 2:m - A[n] = (-1)^(n-1) * (catalannum(n) + sum(j->(-1)^j*binomial(2n-1, 2j-1)*A[j]*catalannum(n-j), 1:n-1)) + for n in 2:m + A[n] = + (-1)^(n-1) * ( + catalannum(n) + + sum(j->(-1)^j*binomial(2n-1, 2j-1)*A[j]*catalannum(n-j), 1:(n-1)) + ) end A[m] end diff --git a/src/partitions.jl b/src/partitions.jl index 9c9b6d5..17d35eb 100644 --- a/src/partitions.jl +++ b/src/partitions.jl @@ -1,12 +1,7 @@ #Partitions -export - integer_partitions, - ncpartitions, - partitions, - prevprod - #nextprod, - +export integer_partitions, ncpartitions, partitions, prevprod +#nextprod, # integer partitions @@ -17,9 +12,9 @@ end Base.length(p::IntegerPartitions) = npartitions(p.n) Base.eltype(p::IntegerPartitions) = Vector{Int} -function Base.iterate(p::IntegerPartitions, xs = Int[]) - length(xs) == p.n && return - xs = nextpartition(p.n,xs) +function Base.iterate(p::IntegerPartitions, xs=Int[]) + length(xs) == p.n && return nothing + xs = nextpartition(p.n, xs) (xs, xs) end @@ -73,14 +68,13 @@ julia> length(partitions(10)) """ partitions(n::Integer) = IntegerPartitions(n) - function nextpartition(n, as) isempty(as) && return Int[n] xs = similar(as, 0) sizehint!(xs, length(as) + 1) - for i = 1:length(as)-1 + for i in 1:(length(as)-1) if as[i+1] == 1 x = as[i]-1 push!(xs, x) @@ -114,8 +108,10 @@ let _npartitions = Dict{Int,Int}() else np = 0 sgn = 1 - for k = 1:n - np += sgn * (npartitions(n - (k*(3k-1)) >> 1) + npartitions(n - (k*(3k+1)) >> 1)) + for k in 1:n + np += + sgn * + (npartitions(n - (k*(3k-1)) >> 1) + npartitions(n - (k*(3k+1)) >> 1)) sgn = -sgn end _npartitions[n] = np @@ -132,7 +128,7 @@ struct FixedPartitions m::Int end -Base.length(f::FixedPartitions) = npartitions(f.n,f.m) +Base.length(f::FixedPartitions) = npartitions(f.n, f.m) Base.eltype(f::FixedPartitions) = Vector{Int} """ @@ -178,18 +174,21 @@ Stacktrace: [...] ``` """ -partitions(n::Integer, m::Integer) = - n >= 1 && m >= 1 ? - FixedPartitions(n, m) : +function partitions(n::Integer, m::Integer) + if n >= 1 && m >= 1 + FixedPartitions(n, m) + else throw(DomainError((n, m), "n and m must be positive")) + end +end -function Base.iterate(f::FixedPartitions, s::Vector{Int} = Int[]) - f.m <= f.n || return +function Base.iterate(f::FixedPartitions, s::Vector{Int}=Int[]) + f.m <= f.n || return nothing if !isempty(s) - (f.m == 1 || s[1]-1 <= s[end]) && return + (f.m == 1 || s[1]-1 <= s[end]) && return nothing end - xs = nextfixedpartition(f.n,f.m,s) + xs = nextfixedpartition(f.n, f.m, s) (xs, xs) end @@ -197,7 +196,8 @@ function nextfixedpartition(n, m, bs) as = copy(bs) if isempty(as) # First iteration - as = ones(Int, m); as[1] = n - m + 1 + as = ones(Int, m); + as[1] = n - m + 1 elseif as[2] < as[1]-1 # Most common iteration as[1] -= 1 @@ -206,13 +206,13 @@ function nextfixedpartition(n, m, bs) # Iterate j = 0 s = as[1]+as[2]-1 - for jj = 3:m # TODO: use `for outer j = ...` on 0.7 + for jj in 3:m # TODO: use `for outer j = ...` on 0.7 j = jj as[jj] < as[1]-1 && break s += as[jj] end x = as[j] += 1 - for k = j-1:-1:2 + for k in (j-1):-1:2 as[k] = x s -= x end @@ -229,7 +229,7 @@ let _nipartitions = Dict{Tuple{Int,Int},Int}() 0 elseif n == m 1 - elseif (np = get(_nipartitions, (n,m), 0)) > 0 + elseif (np = get(_nipartitions, (n, m), 0)) > 0 np else _nipartitions[(n, m)] = npartitions(n-1, m-1) + npartitions(n-m, m) @@ -297,41 +297,44 @@ function Base.iterate(p::SetPartitions) iterate(p, (zeros(Int32, n), ones(Int32, n-1), n, 1)) end function Base.iterate(p::SetPartitions, s) - s[1][1] > 0 && return + s[1][1] > 0 && return nothing nextsetpartition(p.s, s...) end function nextsetpartition(s::AbstractVector, a, b, n, m) function makeparts(s, a, m) - temp = [similar(s, 0) for k = 0:m] - for i = 1:n + temp = [similar(s, 0) for k in 0:m] + for i in 1:n push!(temp[a[i]+1], s[i]) end filter!(!isempty, temp) end - if isempty(s); return ([s], (eltype(a)[1], eltype(b)[], n, 1)); end + if isempty(s) + ; + return ([s], (eltype(a)[1], eltype(b)[], n, 1)); + end - part = makeparts(s,a,m) + part = makeparts(s, a, m) if a[end] != m a[end] += 1 else j = 0 - for jj = n-1:-1:1 + for jj in (n-1):-1:1 j = jj a[jj] == b[jj] || break end a[j] += 1 m = Int(b[j]) + (a[j] == b[j]) - for k = j+1:n-1 + for k in (j+1):(n-1) a[k] = 0 b[k] = m end a[end] = 0 end - return (part, (a,b,n,m)) + return (part, (a, b, n, m)) end let _nsetpartitions = Dict{Int,Int}() @@ -345,8 +348,8 @@ let _nsetpartitions = Dict{Int,Int}() wn else wn = 0 - for k = 0:n-1 - wn += binomial(n-1,k)*nsetpartitions(n-1-k) + for k in 0:(n-1) + wn += binomial(n-1, k)*nsetpartitions(n-1-k) end _nsetpartitions[n] = wn end @@ -358,7 +361,7 @@ struct FixedSetPartitions{T<:AbstractVector} m::Int end -Base.length(p::FixedSetPartitions) = nfixedsetpartitions(length(p.s),p.m) +Base.length(p::FixedSetPartitions) = nfixedsetpartitions(length(p.s), p.m) Base.eltype(p::FixedSetPartitions) = Vector{Vector{eltype(p.s)}} """ @@ -405,15 +408,22 @@ true # References - [Partition of a set - Wikipedia](https://en.wikipedia.org/wiki/Partition_of_a_set) """ -partitions(s::AbstractVector, m::Int) = - length(s) >= 1 && m >= 1 ? - FixedSetPartitions(s, m) : +function partitions(s::AbstractVector, m::Int) + if length(s) >= 1 && m >= 1 + FixedSetPartitions(s, m) + else throw(DomainError((length(s), m), "length(s) and m must be positive")) + end +end function Base.iterate(p::FixedSetPartitions) n = length(p.s) m = p.m - state = m <= n ? (vcat(ones(Int, n-m),1:m), vcat(1:1,n-m+2:n), n) : (Int[], Int[], n) + state = if m <= n + (vcat(ones(Int, n-m), 1:m), vcat(1:1, (n-m+2):n), n) + else + (Int[], Int[], n) + end # state consists of: # vector a of length n describing to which partition every element of s belongs # vector b of length n describing the first index b[i] that belongs to partition i @@ -423,20 +433,20 @@ function Base.iterate(p::FixedSetPartitions) end function Base.iterate(p::FixedSetPartitions, s) - (isempty(s[1]) || s[1][1] > 1) && return - nextfixedsetpartition(p.s,p.m, s...) + (isempty(s[1]) || s[1][1] > 1) && return nothing + nextfixedsetpartition(p.s, p.m, s...) end function nextfixedsetpartition(s::AbstractVector, m, a, b, n) function makeparts(s, a) - local part = [ similar(s,0) for k = 1:m ] - for i = 1:n + local part = [similar(s, 0) for k in 1:m] + for i in 1:n push!(part[a[i]], s[i]) end return part end - part = makeparts(s,a) + part = makeparts(s, a) if m == 1 a[1] = 2 @@ -447,7 +457,7 @@ function nextfixedsetpartition(s::AbstractVector, m, a, b, n) a[end] += 1 else j = k = 0 - for jj = n-1:-1:1 + for jj in (n-1):-1:1 j = jj if a[j] < m && b[a[j]+1] < j break @@ -455,31 +465,31 @@ function nextfixedsetpartition(s::AbstractVector, m, a, b, n) end if j > 1 a[j] += 1 - for p = j+1:n + for p in (j+1):n if b[a[p]] != p a[p] = 1 end end else - for kk = m:-1:2 + for kk in m:-1:2 k = kk if b[k-1] < b[k] - 1 break end end b[k] -= 1 - b[k+1:m] = n-m+k+1:n + b[(k+1):m] = (n-m+k+1):n a[1:n] .= 1 a[b] = 1:m end end - return (part, (a,b,n)) + return (part, (a, b, n)) end function nfixedsetpartitions(n::Int, m::Int) numpart = 0 - for k = 0:m + for k in 0:m numpart += (-1)^(m-k) * binomial(m, k) * (k^n) end numpart = div(numpart, factorial(m)) @@ -568,7 +578,7 @@ function prevprod(a::Vector{Int}, x) end k = length(a) v = ones(Int, k) # current value of each counter - mx = [nextpow(ai,x) for ai in a] # allow each counter to exceed p (sentinel) + mx = [nextpow(ai, x) for ai in a] # allow each counter to exceed p (sentinel) first = Int(prevpow(a[1], x)) # start at best case in first factor v[1] = first p::widen(Int) = first @@ -600,7 +610,6 @@ function prevprod(a::Vector{Int}, x) return Int(best) end - """ integer_partitions(n) @@ -656,14 +665,13 @@ function integer_partitions(n::Integer) for p in integer_partitions(n-1) push!(list, [p; 1]) if length(p) == 1 || p[end] < p[end-1] - push!(list, [p[1:end-1]; p[end]+1]) + push!(list, [p[1:(end-1)]; p[end]+1]) end end list end - # Noncrossing partitions const _cmp = cmp @@ -706,28 +714,28 @@ true """ function ncpartitions(n::Int) partitions = Vector{Vector{Int}}[] - _ncpart!(1,n,n,Vector{Int}[], partitions) + _ncpart!(1, n, n, Vector{Int}[], partitions) partitions end function _ncpart!(a::Int, b::Int, nn::Int, x::Vector, partitions::Vector) n = b - a + 1 - for k = 1:n, root in CoolLexCombinations(n, k) + for k in 1:n, root in CoolLexCombinations(n, k) root .+= a - 1 #Abort if construction is out of lex order - !isempty(x) && _cmp(x[end], root) == 1 && return + !isempty(x) && _cmp(x[end], root) == 1 && return nothing #Save if we've filled all the holes sofar = Vector{Int}[x..., root] ssofaru = sort!(union(sofar...)) if length(ssofaru) == nn && ssofaru == collect(1:nn) push!(partitions, sofar) - return + return nothing end #otherwise patch all remaining holes blob = [ssofaru; nn + 1] - for l = 1:length(blob)-1 + for l in 1:(length(blob)-1) ap, bp = blob[l] + 1, blob[l+1] - 1 ap <= bp && _ncpart!(ap, bp, nn, sofar, partitions) end diff --git a/src/permutations.jl b/src/permutations.jl index 93e1541..e66c04a 100644 --- a/src/permutations.jl +++ b/src/permutations.jl @@ -1,14 +1,7 @@ #Permutations -export - derangements, - levicivita, - multiset_permutations, - nthperm!, - nthperm, - parity, - permutations - +export derangements, + levicivita, multiset_permutations, nthperm!, nthperm, parity, permutations struct Permutations{T} data::T @@ -47,7 +40,9 @@ function next_permutation!(state::Vector{Int}, min::Int, max::Int) end end -function Base.iterate(p::Permutations, state::Vector{Int}=fill(firstindex(p.data), p.length)) +function Base.iterate( + p::Permutations, state::Vector{Int}=fill(firstindex(p.data), p.length) +) next_permutation!(state, firstindex(p.data), lastindex(p.data)) if first(state) > lastindex(p.data) return nothing @@ -57,14 +52,13 @@ end function Base.length(p::Permutations) length(p.data) < p.length && return 0 - return Int(prod(length(p.data) - p.length + 1:length(p.data))) + return Int(prod((length(p.data)-p.length+1):length(p.data))) end Base.eltype(p::Permutations) = Vector{eltype(p.data)} Base.IteratorSize(p::Permutations) = Base.HasLength() - """ permutations(a) @@ -171,8 +165,9 @@ julia> derangements("julia") |> collect ['a', 'i', 'u', 'l', 'j'] ``` """ -derangements(a) = (d for d in multiset_permutations(a, length(a)) if all(t -> t[1] != t[2], zip(a, d))) - +function derangements(a) + (d for d in multiset_permutations(a, length(a)) if all(t -> t[1] != t[2], zip(a, d))) +end function nextpermutation(m, t, state) perm = [m[state[i]] for i in 1:t] @@ -240,7 +235,7 @@ function Base.length(c::MultiSetPermutations) else for j in t:-1:1 q = 0 - for k in (j+1):-1:max(1, j + 1 - f) + for k in (j+1):-1:max(1, j+1-f) q += p[k] / g[j+2-k] end p[j+1] = q @@ -250,7 +245,6 @@ function Base.length(c::MultiSetPermutations) return round(Int, p[t+1]) end - """ multiset_permutations(a) @@ -301,11 +295,11 @@ function multiset_permutations(m, f::Vector{<:Integer}, t::Integer) end function Base.iterate(p::MultiSetPermutations, s=p.ref) - (!isempty(s) && max(s[1], p.t) > length(p.ref) || (isempty(s) && p.t > 0)) && return + (!isempty(s) && max(s[1], p.t) > length(p.ref) || (isempty(s) && p.t > 0)) && + return nothing nextpermutation(p.m, p.t, s) end - """ nthperm!(a, k) @@ -347,13 +341,13 @@ function nthperm!(a::AbstractVector, k::Integer) f = factorial(oftype(k, n)) 0 < k <= f || throw(ArgumentError("permutation k must satisfy 0 < k ≤ $f, got $k")) k -= 1 # make k 1-indexed - for i = 1:n-1 + for i in 1:(n-1) f ÷= n - i + 1 j = k ÷ f k -= j * f j += i elt = a[j] - for d = j:-1:i+1 + for d in j:-1:(i+1) a[d] = a[d-1] end a[i] = elt @@ -430,22 +424,20 @@ julia> nthperm(collect(10:-1:1)) function nthperm(p::AbstractVector{<:Integer}) isperm(p) || throw(ArgumentError("argument is not a permutation")) k, n = 1, length(p) - for i = 1:n-1 + for i in 1:(n-1) f = factorial(n - i) - for j = i+1:n + for j in (i+1):n k += ifelse(p[j] < p[i], f, 0) end end return k end - # Parity of permutations -const levicivita_lut = cat([0 0 0; 0 0 1; 0 -1 0], - [0 0 -1; 0 0 0; 1 0 0], - [0 1 0; -1 0 0; 0 0 0]; - dims=3) +const levicivita_lut = cat( + [0 0 0; 0 0 1; 0 -1 0], [0 0 -1; 0 0 0; 1 0 0], [0 1 0; -1 0 0; 0 0 0]; dims=3 +) """ levicivita(p) diff --git a/src/youngdiagrams.jl b/src/youngdiagrams.jl index 232f6a3..a8fc36f 100644 --- a/src/youngdiagrams.jl +++ b/src/youngdiagrams.jl @@ -14,7 +14,9 @@ struct SkewDiagram function SkewDiagram(λ::Partition, μ::Partition) m, n = length(λ), length(μ) - n > m && throw(ArgumentError("Cannot construct skew diagram with partition lengths $m, $n")) + n > m && throw( + ArgumentError("Cannot construct skew diagram with partition lengths $m, $n") + ) new(λ, μ) end end @@ -22,11 +24,11 @@ SkewDiagram(λ::Vector{Int}, μ::Vector{Int}) = SkewDiagram(Partition(λ), Parti SkewDiagram(t::Union{NTuple{2,Partition},NTuple{2,Vector{Int}}}) = SkewDiagram(t...) export Partition, - SkewDiagram, #skew diagrams - partitionsequence, - isrimhook, #Check if skew diagram is rim hook - leglength, - character #Computes character of irrep of Sn + SkewDiagram, #skew diagrams + partitionsequence, + isrimhook, #Check if skew diagram is rim hook + leglength, + character #Computes character of irrep of Sn ################# # Skew diagrams # @@ -51,25 +53,25 @@ function isrimhook(ξ::SkewDiagram) #XXX This is a horribly inefficient way of checking condition 1! l = maximum(λ.x) youngdiagram = zeros(Int, m, l) - for i = 1:n - youngdiagram[i, μ[i]+1:λ[i]] .= 1 + for i in 1:n + youngdiagram[i, (μ[i]+1):λ[i]] .= 1 end - for i = n+1:m + for i in (n+1):m youngdiagram[i, 1:λ[i]] .= 1 end #Condition 1. Must be edgewise connected youngdiagramList = Any[] - for i = 1:m - for j = 1:l + for i in 1:m + for j in 1:l if youngdiagram[i, j] == 1 push!(youngdiagramList, (i, j)) end end end - for k = 1:length(youngdiagramList) + for k in 1:length(youngdiagramList) i, j = youngdiagramList[k] numNeighbors = 0 - for kp = 1:length(youngdiagramList) + for kp in 1:length(youngdiagramList) ip, jp = youngdiagramList[kp] if abs(i-ip) + abs(j-jp) == 1 numNeighbors += 1 @@ -80,8 +82,8 @@ function isrimhook(ξ::SkewDiagram) end end #Condition 2. Must not contain 2x2 square of cells - for i = 1:m-1 - for j = 1:l-1 + for i in 1:(m-1) + for j in 1:(l-1) if youngdiagram[i, j] == 0 continue end @@ -93,7 +95,6 @@ function isrimhook(ξ::SkewDiagram) return true end - """ leglength(ξ::SkewDiagram) leglength(λ::Partition, μ::Partition) @@ -113,19 +114,18 @@ function leglength(ξ::SkewDiagram) m == 0 && return -1 l = maximum(λ.x) youngdiagram = zeros(Int, m, l) - for i = 1:n - youngdiagram[i, μ[i]+1:λ[i]] .= 1 + for i in 1:n + youngdiagram[i, (μ[i]+1):λ[i]] .= 1 end - for i = n+1:m + for i in (n+1):m youngdiagram[i, 1:λ[i]] .= 1 end - for i = m:-1:1 - any(==(1), youngdiagram[i,:]) && return i-1 + for i in m:-1:1 + any(==(1), youngdiagram[i, :]) && return i-1 end return -1 #If entire matrix is empty end - ####################### # partition sequences # ####################### @@ -139,8 +139,8 @@ function partitionsequence(lambda::Partition) Λ▔ = Int[] λ = [lambda.x; 0] m = length(lambda) - for i = m:-1:1 - for k = 1:(λ[i]-λ[i+1]) + for i in m:-1:1 + for k in 1:(λ[i]-λ[i+1]) push!(Λ▔, 1) end push!(Λ▔, 0) @@ -155,7 +155,6 @@ Take two elements of a partition sequence, with `a` to the left of `b`. """ isrimhook(a::Int, b::Int) = (a == 1) && (b == 0) - ############################# # Character of irreps of Sn # ############################# @@ -168,18 +167,18 @@ function MN1inner(R::Vector{Int}, T::Dict, μ::Partition, t::Integer) χ::Integer = 1 if t <= length(μ) χ, σ::Integer = 0, 1 - for j = 1:μ[t]-1 + for j in 1:(μ[t]-1) if R[j] == 0 σ = -σ end end - for i = 1:s-μ[t] + for i in 1:(s-μ[t]) if R[i] != R[i+μ[t]-1] σ = -σ end if isrimhook(R[i], R[i+μ[t]]) R[i], R[i+μ[t]] = R[i+μ[t]], R[i] - rhohat = R[i:i+μ[t]] + rhohat = R[i:(i+μ[t])] if !haskey(T, rhohat) #Cache result in lookup table T[rhohat] = MN1inner(R, T, μ, t+1) end @@ -204,8 +203,9 @@ Implements the Murnaghan-Nakayama algorithm as described in: Journal of Symbolic Computation, vol. 37 iss. 6 (2004), pp 727-748. doi:10.1016/j.jsc.2003.11.001 """ -character(λ::Partition, μ::Partition) = +function character(λ::Partition, μ::Partition) MN1inner(partitionsequence(λ), Dict{Any,Int}(()=>0), μ, 1) +end ## Deprecations # Previously these were defined in terms of Base types. It's not possible to deprecate @@ -216,11 +216,11 @@ character(λ::Partition, μ::Partition) = using Base: @deprecate, @deprecate_binding @deprecate isrimhook(λ::Vector{Int}, μ::Vector{Int}) isrimhook(Partition(λ), Partition(μ)) -@deprecate isrimhook(ξ::NTuple{2,Vector{Int}}) isrimhook(SkewDiagram(ξ)) +@deprecate isrimhook(ξ::NTuple{2,Vector{Int}}) isrimhook(SkewDiagram(ξ)) @deprecate leglength(λ::Vector{Int}, μ::Vector{Int}) leglength(Partition(λ), Partition(μ)) -@deprecate leglength(ξ::NTuple{2,Vector{Int}}) leglength(SkewDiagram(ξ)) +@deprecate leglength(ξ::NTuple{2,Vector{Int}}) leglength(SkewDiagram(ξ)) @deprecate character(λ::Vector{Int}, μ::Vector{Int}) character(Partition(λ), Partition(μ)) -@deprecate partitionsequence(λ::Vector{Int}) partitionsequence(Partition(λ)) +@deprecate partitionsequence(λ::Vector{Int}) partitionsequence(Partition(λ)) # This isn't actually used anywhere and is not a particularly useful alias # TODO: Perhaps revisit YoungDiagram as its own type should the need arise diff --git a/test/combinations.jl b/test/combinations.jl index f70a566..64b9fa4 100644 --- a/test/combinations.jl +++ b/test/combinations.jl @@ -1,6 +1,7 @@ @testset "combinations" begin @test [combinations([])...] == [[]] - @test [combinations(['a', 'b', 'c'])...] == [[],['a'],['b'],['c'],['a','b'],['a','c'],['b','c'],['a','b','c']] + @test [combinations(['a', 'b', 'c'])...] == + [[], ['a'], ['b'], ['c'], ['a', 'b'], ['a', 'c'], ['b', 'c'], ['a', 'b', 'c']] @test length(collect(combinations(1:5))) == 32 @test [combinations("abc", 3)...] == [['a', 'b', 'c']] @@ -13,7 +14,8 @@ # multiset_combinations @test [multiset_combinations("aabc", 5)...] == Any[] - @test [multiset_combinations("aabc", 2)...] == Any[['a', 'a'], ['a', 'b'], ['a', 'c'], ['b', 'c']] + @test [multiset_combinations("aabc", 2)...] == + Any[['a', 'a'], ['a', 'b'], ['a', 'c'], ['b', 'c']] @test [multiset_combinations("aabc", 1)...] == Any[['a'], ['b'], ['c']] @test [multiset_combinations("aabc", 0)...] == Any[Char[]] @test [multiset_combinations("aabc", -1)...] == Any[] @@ -22,8 +24,8 @@ @test [multiset_combinations("", -1)...] == Any[] # with_replacement_combinations - @test [with_replacement_combinations("abc", 2)...] == Any[['a', 'a'], ['a', 'b'], ['a', 'c'], - ['b', 'b'], ['b', 'c'], ['c', 'c']] + @test [with_replacement_combinations("abc", 2)...] == + Any[['a', 'a'], ['a', 'b'], ['a', 'c'], ['b', 'b'], ['b', 'c'], ['c', 'c']] @test [with_replacement_combinations("abc", 1)...] == Any[['a'], ['b'], ['c']] @test [with_replacement_combinations("abc", 0)...] == Any[Char[]] @test [with_replacement_combinations("abc", -1)...] == Any[] @@ -31,17 +33,21 @@ @test [with_replacement_combinations("", 0)...] == Any[Char[]] @test [with_replacement_combinations("", -1)...] == Any[] - #cool-lex iterator @test_throws DomainError [CoolLexCombinations(-1, 1)...] @test_throws DomainError [CoolLexCombinations(5, 0)...] - @test [CoolLexCombinations(4, 2)...] == Vector[[1, 2], [2, 3], [1, 3], [2, 4], [3, 4], [1, 4]] - @test isa(iterate(CoolLexCombinations(1000, 20))[2], Combinatorics.CoolLexIterState{BigInt}) + @test [CoolLexCombinations(4, 2)...] == + Vector[[1, 2], [2, 3], [1, 3], [2, 4], [3, 4], [1, 4]] + @test isa( + iterate(CoolLexCombinations(1000, 20))[2], Combinatorics.CoolLexIterState{BigInt} + ) # Power set @test collect(powerset([])) == Any[[]] - @test collect(powerset(['a', 'b', 'c'])) == Any[[], ['a'], ['b'], ['c'], ['a', 'b'], ['a', 'c'], ['b', 'c'], ['a', 'b', 'c']] - @test collect(powerset(['a', 'b', 'c'], 1)) == Any[['a'], ['b'], ['c'], ['a', 'b'], ['a', 'c'], ['b', 'c'], ['a', 'b', 'c']] - @test collect(powerset(['a', 'b', 'c'], 1, 2)) == Any[['a'], ['b'], ['c'], ['a', 'b'], ['a', 'c'], ['b', 'c']] - + @test collect(powerset(['a', 'b', 'c'])) == + Any[[], ['a'], ['b'], ['c'], ['a', 'b'], ['a', 'c'], ['b', 'c'], ['a', 'b', 'c']] + @test collect(powerset(['a', 'b', 'c'], 1)) == + Any[['a'], ['b'], ['c'], ['a', 'b'], ['a', 'c'], ['b', 'c'], ['a', 'b', 'c']] + @test collect(powerset(['a', 'b', 'c'], 1, 2)) == + Any[['a'], ['b'], ['c'], ['a', 'b'], ['a', 'c'], ['b', 'c']] end diff --git a/test/factorials.jl b/test/factorials.jl index 3f06f7a..18b6fb1 100644 --- a/test/factorials.jl +++ b/test/factorials.jl @@ -24,7 +24,8 @@ @test_throws DomainError partialderangement(-8, 0) # doublefactorial - @test doublefactorial(70) == parse(BigInt, "355044260642859198243475901411974413130137600000000") + @test doublefactorial(70) == + parse(BigInt, "355044260642859198243475901411974413130137600000000") @test_throws DomainError doublefactorial(-1) # hyperfactorial @@ -51,11 +52,10 @@ @test_throws OverflowError multinomial(10, 10, 10, 6) @test_throws OverflowError multinomial(10, 10, 10, 10) # binomial(200, 100) overflows - @test_throws OverflowError multinomial(100, 100) + @test_throws OverflowError multinomial(100, 100) end # primorial @test primorial(17) == 510510 @test_throws DomainError primorial(-1) - end diff --git a/test/multinomials.jl b/test/multinomials.jl index 635dd31..a538c5f 100644 --- a/test/multinomials.jl +++ b/test/multinomials.jl @@ -20,10 +20,22 @@ using LinearAlgebra @test [multiexponents(1, 10)...] == [[10]] # general cases - @test [multiexponents(3, 2)...] == [[2, 0, 0], [1, 1, 0], [1, 0, 1], [0, 2, 0], [0, 1, 1], [0, 0, 2]] + @test [multiexponents(3, 2)...] == + [[2, 0, 0], [1, 1, 0], [1, 0, 1], [0, 2, 0], [0, 1, 1], [0, 0, 2]] @test [multiexponents(2, 3)...] == [[3, 0], [2, 1], [1, 2], [0, 3]] @test [multiexponents(2, 2)...] == [[2, 0], [1, 1], [0, 2]] - @test [multiexponents(3, 3)...] == [[3, 0, 0], [2, 1, 0], [2, 0, 1], [1, 2, 0], [1, 1, 1], [1, 0, 2], [0, 3, 0], [0, 2, 1], [0, 1, 2], [0, 0, 3]] + @test [multiexponents(3, 3)...] == [ + [3, 0, 0], + [2, 1, 0], + [2, 0, 1], + [1, 2, 0], + [1, 1, 1], + [1, 0, 2], + [0, 3, 0], + [0, 2, 1], + [0, 1, 2], + [0, 0, 3], + ] @test length(multiexponents(1, 1)) == 1 @test length(multiexponents(2, 2)) == 3 diff --git a/test/numbers.jl b/test/numbers.jl index affeb4b..8fbdc3e 100644 --- a/test/numbers.jl +++ b/test/numbers.jl @@ -1,87 +1,88 @@ @testset "numbers" begin - # catalan - @test catalannum(5) == 42 - @test catalannum(30) == parse(BigInt, "3814986502092304") - @test_throws DomainError catalannum(-1) + # catalan + @test catalannum(5) == 42 + @test catalannum(30) == parse(BigInt, "3814986502092304") + @test_throws DomainError catalannum(-1) - # fibonacci - @test fibonaccinum(5) == 5 - @test fibonaccinum(101) == parse(BigInt, "573147844013817084101") - @test_throws DomainError fibonaccinum(-1) + # fibonacci + @test fibonaccinum(5) == 5 + @test fibonaccinum(101) == parse(BigInt, "573147844013817084101") + @test_throws DomainError fibonaccinum(-1) - # lobb - @test lobbnum(2, 3) == 5 - @test lobbnum(50, 100) == parse(BigInt, "303574146822833458064977353764024400258025594128") - @test_throws DomainError lobbnum(-1, 2) + # lobb + @test lobbnum(2, 3) == 5 + @test lobbnum(50, 100) == + parse(BigInt, "303574146822833458064977353764024400258025594128") + @test_throws DomainError lobbnum(-1, 2) - # narayana - @test narayana(8, 5) == 490 - @test narayana(100, 50) == parse(BigInt, "99794739256977899071474889425225225330079579752931446368") - @test_throws DomainError narayana(-1, -1) + # narayana + @test narayana(8, 5) == 490 + @test narayana(100, 50) == + parse(BigInt, "99794739256977899071474889425225225330079579752931446368") + @test_throws DomainError narayana(-1, -1) - # lassalle - @test lassallenum(14) == parse(BigInt, "270316008395632253340") + # lassalle + @test lassallenum(14) == parse(BigInt, "270316008395632253340") - # legendresymbol - @test legendresymbol(1001, 9907) == jacobisymbol(1001, 9907) == -1 + # legendresymbol + @test legendresymbol(1001, 9907) == jacobisymbol(1001, 9907) == -1 - # lucas - @test lucasnum(10) == 123 - @test lucasnum(100) == parse(BigInt, "792070839848372253127") - @test_throws DomainError lucasnum(-1) + # lucas + @test lucasnum(10) == 123 + @test lucasnum(100) == parse(BigInt, "792070839848372253127") + @test_throws DomainError lucasnum(-1) - # stirlings1 - @test_throws DomainError stirlings1(-1, 1) - @test typeof(stirlings1(6, 2)) == Int - @test stirlings1(0, 0) == 1 - @test stirlings1(1, 1) == 1 - @test stirlings1(2, 6) == 0 - @test stirlings1(6, 0) == 0 - @test stirlings1(6, 0, true) == 0 - @test stirlings1(6, 1) == 120 - @test stirlings1(6, 1, true) == -120 - @test stirlings1(6, 2) == 274 - @test stirlings1(6, 2, true) == 274 - @test stirlings1(6, 3) == 225 - @test stirlings1(6, 3, true) == -225 - @test stirlings1(6, 4) == 85 - @test stirlings1(6, 4, true) == 85 - @test stirlings1(6, 5) == 15 - @test stirlings1(6, 5, true) == -15 - @test stirlings1(6, 6) == 1 - @test stirlings1(6, 6, true) == 1 - @test sum([abs(stirlings1(8, i, true)) for i = 0:8]) == factorial(8) + # stirlings1 + @test_throws DomainError stirlings1(-1, 1) + @test typeof(stirlings1(6, 2)) == Int + @test stirlings1(0, 0) == 1 + @test stirlings1(1, 1) == 1 + @test stirlings1(2, 6) == 0 + @test stirlings1(6, 0) == 0 + @test stirlings1(6, 0, true) == 0 + @test stirlings1(6, 1) == 120 + @test stirlings1(6, 1, true) == -120 + @test stirlings1(6, 2) == 274 + @test stirlings1(6, 2, true) == 274 + @test stirlings1(6, 3) == 225 + @test stirlings1(6, 3, true) == -225 + @test stirlings1(6, 4) == 85 + @test stirlings1(6, 4, true) == 85 + @test stirlings1(6, 5) == 15 + @test stirlings1(6, 5, true) == -15 + @test stirlings1(6, 6) == 1 + @test stirlings1(6, 6, true) == 1 + @test sum([abs(stirlings1(8, i, true)) for i in 0:8]) == factorial(8) - # stirlings2 - @test_throws DomainError stirlings2(-1, 1) - @test typeof(stirlings2(6, 2)) == Int - @test stirlings2(0, 0) == 1 - @test stirlings2(1, 1) == 1 - @test stirlings2(2, 6) == 0 - @test stirlings2(6, 0) == 0 - @test stirlings2(6, 1) == 1 - @test stirlings2(6, 2) == 31 - @test stirlings2(6, 3) == 90 - @test stirlings2(6, 4) == 65 - @test stirlings2(6, 5) == 15 - @test stirlings2(6, 6) == 1 + # stirlings2 + @test_throws DomainError stirlings2(-1, 1) + @test typeof(stirlings2(6, 2)) == Int + @test stirlings2(0, 0) == 1 + @test stirlings2(1, 1) == 1 + @test stirlings2(2, 6) == 0 + @test stirlings2(6, 0) == 0 + @test stirlings2(6, 1) == 1 + @test stirlings2(6, 2) == 31 + @test stirlings2(6, 3) == 90 + @test stirlings2(6, 4) == 65 + @test stirlings2(6, 5) == 15 + @test stirlings2(6, 6) == 1 - # bell - @test bellnum.(0:10) == [ - 1 - 1 - 2 - 5 - 15 - 52 - 203 - 877 + # bell + @test bellnum.(0:10) == [ + 1 + 1 + 2 + 5 + 15 + 52 + 203 + 877 4140 - 21147 - 115975 - ] - - @test bellnum(42) == parse(BigInt, "35742549198872617291353508656626642567") - @test_throws DomainError(-1) bellnum(-1) + 21147 + 115975 + ] + @test bellnum(42) == parse(BigInt, "35742549198872617291353508656626642567") + @test_throws DomainError(-1) bellnum(-1) end diff --git a/test/partitions.jl b/test/partitions.jl index 311d633..b304534 100644 --- a/test/partitions.jl +++ b/test/partitions.jl @@ -1,5 +1,4 @@ @testset "partitions" begin - @testset "partitions(n::Integer)" begin @test_broken collect(partitions(0)) == [[]] @test collect(partitions(1)) == [[1]] @@ -22,7 +21,8 @@ @testset "partitions(n::Integer, m::Integer)" begin @test collect(partitions(8, 1)) == [[8]] - @test collect(partitions(8, 3)) == [[6, 1, 1], [5, 2, 1], [4, 3, 1], [4, 2, 2], [3, 3, 2]] + @test collect(partitions(8, 3)) == + [[6, 1, 1], [5, 2, 1], [4, 3, 1], [4, 2, 2], [3, 3, 2]] @test collect(partitions(8, 8)) == [ones(Int, 8)] @test collect(partitions(8, 9)) == [] @test collect(partitions(90, 90)) == [ones(Int, 90)] @@ -52,7 +52,8 @@ @testset "partitions(s::AbstractVector)" begin @test collect(partitions([1])) == [[[1]]] @test collect(partitions([1, 2])) == [[[1, 2]], [[1], [2]]] - @test collect(partitions([1, 2, 3])) == [[[1, 2, 3]], [[1, 2], [3]], [[1, 3], [2]], [[1], [2, 3]], [[1], [2], [3]]] + @test collect(partitions([1, 2, 3])) == + [[[1, 2, 3]], [[1, 2], [3]], [[1, 3], [2]], [[1], [2, 3]], [[1], [2], [3]]] @test collect(partitions(1:3)) == collect(partitions([1, 2, 3])) @test collect(partitions('a':'b')) == [[['a', 'b']], [['a'], ['b']]] @@ -67,15 +68,15 @@ end @testset "partitions(s::AbstractVector, m::Int)" begin - @test collect(partitions(1:3, 2)) == [ - [[1, 2], [3]], - [[1, 3], [2]], - [[1], [2, 3]], - ] + @test collect(partitions(1:3, 2)) == [[[1, 2], [3]], [[1, 3], [2]], [[1], [2, 3]]] @test collect(partitions([1, 2, 3, 4], 1)) == [[[1, 2, 3, 4]]] @test collect(partitions([1, 2, 3, 4], 3)) == [ - [[1, 2], [3], [4]], [[1, 3], [2], [4]], [[1], [2, 3], [4]], - [[1, 4], [2], [3]], [[1], [2, 4], [3]], [[1], [2], [3, 4]] + [[1, 2], [3], [4]], + [[1, 3], [2], [4]], + [[1], [2, 3], [4]], + [[1, 4], [2], [3]], + [[1], [2, 4], [3]], + [[1], [2], [3, 4]], ] @test collect(partitions([1, 2, 3, 4], 4)) == [[[1], [2], [3], [4]]] @test collect(partitions([1, 2, 3, 4], 5)) == [] @@ -101,15 +102,8 @@ @test integer_partitions(2) == [[1, 1], [2]] @test integer_partitions(3) == [[1, 1, 1], [2, 1], [3]] # gap> Partitions( 5 ); - @test integer_partitions(5) == [ - [1, 1, 1, 1, 1], - [2, 1, 1, 1], - [2, 2, 1], - [3, 1, 1], - [3, 2], - [4, 1], - [5] - ] + @test integer_partitions(5) == + [[1, 1, 1, 1, 1], [2, 1, 1, 1], [2, 2, 1], [3, 1, 1], [3, 2], [4, 1], [5]] # integer_partitions <--> partitions(::Integer) @test Set(integer_partitions(5)) == Set(partitions(5)) @@ -170,5 +164,4 @@ @test length(ncpartitions(n)) == catalannum(n) end end - end diff --git a/test/permutations.jl b/test/permutations.jl index b50d793..16ca30c 100644 --- a/test/permutations.jl +++ b/test/permutations.jl @@ -6,17 +6,25 @@ struct Permutations{T} end @testset "permutations" begin - @test collect(permutations("abc")) == Any[['a', 'b', 'c'], ['a', 'c', 'b'], ['b', 'a', 'c'], - ['b', 'c', 'a'], ['c', 'a', 'b'], ['c', 'b', 'a']] - - @test collect(Iterators.filter(x -> (iseven(x[1])), permutations([1, 2, 3]))) == Any[[2, 1, 3], [2, 3, 1]] - @test collect(Iterators.filter(x -> (iseven(x[3])), permutations([1, 2, 3]))) == Any[[1, 3, 2], [3, 1, 2]] + @test collect(permutations("abc")) == Any[ + ['a', 'b', 'c'], + ['a', 'c', 'b'], + ['b', 'a', 'c'], + ['b', 'c', 'a'], + ['c', 'a', 'b'], + ['c', 'b', 'a'], + ] + + @test collect(Iterators.filter(x -> (iseven(x[1])), permutations([1, 2, 3]))) == + Any[[2, 1, 3], [2, 3, 1]] + @test collect(Iterators.filter(x -> (iseven(x[3])), permutations([1, 2, 3]))) == + Any[[1, 3, 2], [3, 1, 2]] @test length(permutations(0)) == 1 @test collect(permutations("abc", 4)) == Any[] - @test collect(permutations("abc", 2)) == Any[['a', 'b'], ['a', 'c'], ['b', 'a'], - ['b', 'c'], ['c', 'a'], ['c', 'b']] + @test collect(permutations("abc", 2)) == + Any[['a', 'b'], ['a', 'c'], ['b', 'a'], ['b', 'c'], ['c', 'a'], ['c', 'b']] @test collect(permutations(1:5, 1)) == [[x] for x in 1:5] @test collect(permutations("abc", 0)) == [Char[]] @test collect(permutations("abc", -1)) == Any[] @@ -57,9 +65,11 @@ end # multiset_permutations @test collect(multiset_permutations("aabc", 5)) == Any[] - @test collect(multiset_permutations("aabc", 2)) == Any[['a', 'a'], ['a', 'b'], ['a', 'c'], ['b', 'a'], - ['b', 'c'], ['c', 'a'], ['c', 'b']] - @test collect(multiset_permutations("aabcc", 5)) == collect(multiset_permutations("aabcc")) + @test collect(multiset_permutations("aabc", 2)) == Any[ + ['a', 'a'], ['a', 'b'], ['a', 'c'], ['b', 'a'], ['b', 'c'], ['c', 'a'], ['c', 'b'] + ] + @test collect(multiset_permutations("aabcc", 5)) == + collect(multiset_permutations("aabcc")) @test collect(multiset_permutations("")) == Any[Char[]] @test collect(multiset_permutations("aabc", 0)) == Any[Char[]] @test collect(multiset_permutations("aabc", -1)) == Any[] @@ -78,10 +88,11 @@ end @test collect(derangements([1, 1, 2])) == Vector{Int}[] @test collect(derangements([1, 1, 2, 2])) == [[2, 2, 1, 1]] @test map(join, derangements("aabbc")) == ["bbaca", "bbcaa", "bcaab", "cbaab"] - @test map(join, derangements("aaabbbc")) == ["bbbaaca", "bbbacaa", "bbbcaaa", "bbcaaab", "bcbaaab", "cbbaaab"] + @test map(join, derangements("aaabbbc")) == + ["bbbaaca", "bbbacaa", "bbbcaaa", "bbcaaab", "bcbaaab", "cbbaaab"] #nthperm! - for n = 0:7, k = 1:factorial(n) + for n in 0:7, k in 1:factorial(n) p = nthperm!([1:n;], k) @test isperm(p) @test nthperm(p) == k @@ -118,5 +129,4 @@ end @test Combinatorics.nsetpartitions(-1) == 0 @test collect(permutations([])) == [[]] - end diff --git a/test/youngdiagrams.jl b/test/youngdiagrams.jl index c4a4334..c91e143 100644 --- a/test/youngdiagrams.jl +++ b/test/youngdiagrams.jl @@ -18,5 +18,4 @@ @test leglength(λ, μ) == -1 @test character(λ, μ) == 1 end - end