Skip to content

Add .JuliaFormatter.toml #191

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .JuliaFormatter.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
style = "blue"
whitespace_ops_in_indices=false
36 changes: 16 additions & 20 deletions src/combinations.jl
Original file line number Diff line number Diff line change
@@ -1,31 +1,28 @@
export combinations,
CoolLexCombinations,
multiset_combinations,
with_replacement_combinations,
powerset
CoolLexCombinations, multiset_combinations, with_replacement_combinations, powerset

#The Combinations iterator
struct Combinations
n::Int
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

Expand All @@ -49,14 +46,13 @@
(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

Expand Down Expand Up @@ -102,7 +98,7 @@
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
Expand Down Expand Up @@ -134,7 +130,6 @@

Base.length(C::CoolLexCombinations) = max(0, binomial(C.n, C.t))


struct MultiSetCombinations{T}
m::T
f::Vector{Int}
Expand All @@ -158,7 +153,7 @@
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)])

Check warning on line 156 in src/combinations.jl

View check run for this annotation

Codecov / codecov/patch

src/combinations.jl#L156

Added line #L156 was not covered by tests
end
end
end
Expand All @@ -167,7 +162,7 @@

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
Expand All @@ -185,8 +180,9 @@
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)
Expand All @@ -196,7 +192,7 @@
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
Expand Down Expand Up @@ -232,8 +228,8 @@
"""
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
Expand Down Expand Up @@ -269,7 +265,7 @@
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
11 changes: 5 additions & 6 deletions src/factorials.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#Factorials and elementary coefficients

export
derangement,
export derangement,
partialderangement,
factorial,
subfactorial,
Expand All @@ -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
Expand All @@ -42,7 +41,6 @@ function Base.factorial(n::BigInt, k::BigInt)
end
Base.factorial(n::Integer, k::Integer) = factorial(promote(n, k)...)


"""
derangement(n)

Expand Down Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions src/multinomials.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
24 changes: 13 additions & 11 deletions src/numbers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand Down
Loading
Loading