Skip to content

Commit 18af124

Browse files
committed
Formatting code
1 parent c6832b3 commit 18af124

14 files changed

+302
-284
lines changed

src/combinations.jl

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,28 @@
11
export combinations,
2-
CoolLexCombinations,
3-
multiset_combinations,
4-
with_replacement_combinations,
5-
powerset
2+
CoolLexCombinations, multiset_combinations, with_replacement_combinations, powerset
63

74
#The Combinations iterator
85
struct Combinations
96
n::Int
107
t::Int
118
end
129

13-
@inline function Base.iterate(c::Combinations, s = [min(c.t - 1, i) for i in 1:c.t])
10+
@inline function Base.iterate(c::Combinations, s=[min(c.t - 1, i) for i in 1:c.t])
1411
if c.t == 0 # special case to generate 1 result for t==0
1512
isempty(s) && return (s, [1])
16-
return
13+
return nothing
1714
end
1815
for i in c.t:-1:1
1916
s[i] += 1
2017
if s[i] > (c.n - (c.t - i))
2118
continue
2219
end
23-
for j in i+1:c.t
20+
for j in (i+1):c.t
2421
s[j] = s[j-1] + 1
2522
end
2623
break
2724
end
28-
s[1] > c.n - c.t + 1 && return
25+
s[1] > c.n - c.t + 1 && return nothing
2926
(s, s)
3027
end
3128

@@ -49,14 +46,13 @@ function combinations(a, t::Integer)
4946
(reorder(c) for c in Combinations(length(a), t))
5047
end
5148

52-
5349
"""
5450
combinations(a)
5551
5652
Generate combinations of the elements of `a` of all orders. Chaining of order iterators
5753
is eager, but the sequence at each order is lazy.
5854
"""
59-
combinations(a) = Iterators.flatten([combinations(a, k) for k = 0:length(a)])
55+
combinations(a) = Iterators.flatten([combinations(a, k) for k in 0:length(a)])
6056

6157
# cool-lex combinations iterator
6258

@@ -102,7 +98,7 @@ function Base.iterate(C::CoolLexCombinations)
10298
end
10399

104100
function Base.iterate(C::CoolLexCombinations, S::CoolLexIterState)
105-
(S.R3 & S.R2 != 0) && return
101+
(S.R3 & S.R2 != 0) && return nothing
106102

107103
R0 = S.R0
108104
R1 = S.R1
@@ -134,7 +130,6 @@ end
134130

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

137-
138133
struct MultiSetCombinations{T}
139134
m::T
140135
f::Vector{Int}
@@ -158,7 +153,7 @@ function Base.length(c::MultiSetCombinations)
158153
end
159154
else
160155
for j in t:-1:1
161-
p[j+1] = sum(p[max(1,j+1-f):(j+1)])
156+
p[j+1] = sum(p[max(1, j+1-f):(j+1)])
162157
end
163158
end
164159
end
@@ -167,7 +162,7 @@ end
167162

168163
function multiset_combinations(m, f::Vector{<:Integer}, t::Integer)
169164
length(m) == length(f) || error("Lengths of m and f are not the same.")
170-
ref = length(f) > 0 ? vcat([[i for j in 1:f[i] ] for i in 1:length(f)]...) : Int[]
165+
ref = length(f) > 0 ? vcat([[i for j in 1:f[i]] for i in 1:length(f)]...) : Int[]
171166
if t < 0
172167
t = length(ref) + 1
173168
end
@@ -185,8 +180,9 @@ function multiset_combinations(a, t::Integer)
185180
multiset_combinations(m, f, t)
186181
end
187182

188-
function Base.iterate(c::MultiSetCombinations, s = c.ref)
189-
((!isempty(s) && max(s[1], c.t) > length(c.ref)) || (isempty(s) && c.t > 0)) && return
183+
function Base.iterate(c::MultiSetCombinations, s=c.ref)
184+
((!isempty(s) && max(s[1], c.t) > length(c.ref)) || (isempty(s) && c.t > 0)) &&
185+
return nothing
190186

191187
ref = c.ref
192188
n = length(ref)
@@ -196,7 +192,7 @@ function Base.iterate(c::MultiSetCombinations, s = c.ref)
196192
if t > 0
197193
s = copy(s)
198194
for i in t:-1:1
199-
if s[i] < ref[i + (n - t)]
195+
if s[i] < ref[i+(n-t)]
200196
j = 1
201197
while ref[j] <= s[i]
202198
j += 1
@@ -232,8 +228,8 @@ Generate all combinations with replacement of size `t` from an array `a`.
232228
"""
233229
with_replacement_combinations(a, t::Integer) = WithReplacementCombinations(a, t)
234230

235-
function Base.iterate(c::WithReplacementCombinations, s = [1 for i in 1:c.t])
236-
(!isempty(s) && s[1] > length(c.a) || c.t < 0) && return
231+
function Base.iterate(c::WithReplacementCombinations, s=[1 for i in 1:c.t])
232+
(!isempty(s) && s[1] > length(c.a) || c.t < 0) && return nothing
237233

238234
n = length(c.a)
239235
t = c.t
@@ -269,7 +265,7 @@ returns an iterator object. Use `collect(powerset(a, min, max))` to get an array
269265
subsets.
270266
"""
271267
function powerset(a, min::Integer=0, max::Integer=length(a))
272-
itrs = [combinations(a, k) for k = min:max]
268+
itrs = [combinations(a, k) for k in min:max]
273269
min < 1 && append!(itrs, eltype(a)[])
274270
Iterators.flatten(itrs)
275271
end

src/factorials.jl

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#Factorials and elementary coefficients
22

3-
export
4-
derangement,
3+
export derangement,
54
partialderangement,
65
factorial,
76
subfactorial,
@@ -17,7 +16,7 @@ export
1716
1817
Compute ``n!/k!``.
1918
"""
20-
function Base.factorial(n::T, k::T) where T<:Integer
19+
function Base.factorial(n::T, k::T) where {T<:Integer}
2120
if k < 0 || n < 0 || k > n
2221
throw(DomainError((n, k), "n and k must be nonnegative with k ≤ n"))
2322
end
@@ -42,7 +41,6 @@ function Base.factorial(n::BigInt, k::BigInt)
4241
end
4342
Base.factorial(n::Integer, k::Integer) = factorial(promote(n, k)...)
4443

45-
4644
"""
4745
derangement(n)
4846
@@ -98,13 +96,14 @@ end
9896
# Hyperfactorial
9997
hyperfactorial(n::Integer) = n==0 ? BigInt(1) : prod(i->i^i, BigInt(1):n)
10098

101-
10299
function multifactorial(n::Integer, m::Integer)
103100
if n < 0
104101
throw(DomainError(n, "n must be nonnegative"))
105102
end
106103
z = Ref{BigInt}(0)
107-
ccall((:__gmpz_mfac_uiui, :libgmp), Cvoid, (Ref{BigInt}, UInt, UInt), z, UInt(n), UInt(m))
104+
ccall(
105+
(:__gmpz_mfac_uiui, :libgmp), Cvoid, (Ref{BigInt}, UInt, UInt), z, UInt(n), UInt(m)
106+
)
108107
return z[]
109108
end
110109

src/multinomials.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ end
1010

1111
# Standard stars and bars:
1212
# https://en.wikipedia.org/wiki/Stars_and_bars_(combinatorics)
13-
function Base.iterate(m::MultiExponents, s = nothing)
13+
function Base.iterate(m::MultiExponents, s=nothing)
1414
next = s === nothing ? iterate(m.c) : iterate(m.c, s)
15-
next === nothing && return
15+
next === nothing && return nothing
1616
stars, ss = next
1717

1818
# stars minus their consecutive
@@ -49,7 +49,7 @@ julia> collect(multiexponents(3, 2))
4949
"""
5050
function multiexponents(m, n)
5151
# number of stars and bars = m+n-1
52-
c = combinations(1:m+n-1, n)
52+
c = combinations(1:(m+n-1), n)
5353

5454
MultiExponents(c, m)
5555
end

src/numbers.jl

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,15 @@ function bellnum(n::Integer)
2525
end
2626
list = Vector{BigInt}(undef, n)
2727
list[1] = 1
28-
for i = 2:n
29-
for j = 1:i - 2
30-
list[i - j - 1] += list[i - j]
28+
for i in 2:n
29+
for j in 1:(i-2)
30+
list[i-j-1] += list[i-j]
3131
end
32-
list[i] = list[1] + list[i - 1]
32+
list[i] = list[1] + list[i-1]
3333
end
3434
return list[n]
3535
end
3636

37-
3837
"""
3938
catalannum(n)
4039
@@ -55,7 +54,7 @@ end
5554
Compute the Lobb number `L(m,n)`, or the generalised Catalan number given by ``\\frac{2m+1}{m+n+1} \\binom{2n}{m+n}``.
5655
Wikipedia : https://en.wikipedia.org/wiki/Lobb_number
5756
"""
58-
function lobbnum(bm::Integer,bn::Integer)
57+
function lobbnum(bm::Integer, bn::Integer)
5958
if !(0 <= bm <= bn)
6059
throw(DomainError("m and n must be non-negative"))
6160
else
@@ -71,14 +70,14 @@ end
7170
Compute the Narayana number `N(n,k)` given by ``\\frac{1}{n}\\binom{n}{k}\\binom{n}{k-1}``
7271
Wikipedia : https://en.wikipedia.org/wiki/Narayana_number
7372
"""
74-
function narayana(bn::Integer,bk::Integer)
73+
function narayana(bn::Integer, bk::Integer)
7574
if !(1 <= bk <= bn)
7675
throw(DomainError("Domain is 1 <= k <= n"))
7776
else
7877
n = BigInt(bn)
7978
k = BigInt(bk)
8079
end
81-
div(binomial(n, k)*binomial(n, k - 1) , n)
80+
div(binomial(n, k)*binomial(n, k - 1), n)
8281
end
8382

8483
function fibonaccinum(n::Integer)
@@ -90,7 +89,6 @@ function fibonaccinum(n::Integer)
9089
return z[]
9190
end
9291

93-
9492
function jacobisymbol(a::Integer, b::Integer)
9593
ba = Ref{BigInt}(a)
9694
bb = Ref{BigInt}(b)
@@ -104,8 +102,12 @@ Compute the ``n``th entry in Lassalle's sequence, OEIS entry A180874.
104102
"""
105103
function lassallenum(m::Integer)
106104
A = ones(BigInt, m)
107-
for n = 2:m
108-
A[n] = (-1)^(n-1) * (catalannum(n) + sum(j->(-1)^j*binomial(2n-1, 2j-1)*A[j]*catalannum(n-j), 1:n-1))
105+
for n in 2:m
106+
A[n] =
107+
(-1)^(n-1) * (
108+
catalannum(n) +
109+
sum(j->(-1)^j*binomial(2n-1, 2j-1)*A[j]*catalannum(n-j), 1:(n-1))
110+
)
109111
end
110112
A[m]
111113
end

0 commit comments

Comments
 (0)