@@ -69,6 +69,26 @@ Generate all permutations of an indexable object `a` in lexicographic order. Bec
6969can be very large, this function returns an iterator object.
7070Use `collect(permutations(a))` to get an array of all permutations.
7171Only works for `a` with defined length.
72+
73+ # Examples
74+ ```jldoctest
75+ julia> permutations(1:2)
76+ Combinatorics.Permutations{UnitRange{Int64}}(1:2, 2)
77+
78+ julia> collect(permutations(1:2))
79+ 2-element Vector{Vector{Int64}}:
80+ [1, 2]
81+ [2, 1]
82+
83+ julia> collect(permutations(1:3))
84+ 6-element Vector{Vector{Int64}}:
85+ [1, 2, 3]
86+ [1, 3, 2]
87+ [2, 1, 3]
88+ [2, 3, 1]
89+ [3, 1, 2]
90+ [3, 2, 1]
91+ ```
7292"""
7393permutations (a) = permutations (a, length (a))
7494
@@ -78,6 +98,27 @@ permutations(a) = permutations(a, length(a))
7898Generate all size `t` permutations of an indexable object `a`.
7999Only works for `a` with defined length.
80100If `(t <= 0) || (t > length(a))`, then returns an empty vector of eltype of `a`
101+
102+ # Examples
103+ ```jldoctest
104+ julia> [ (len, permutations(1:3, len)) for len in -1:4 ]
105+ 6-element Vector{Tuple{Int64, Any}}:
106+ (-1, Vector{Int64}[])
107+ (0, [Int64[]])
108+ (1, [[1], [2], [3]])
109+ (2, Combinatorics.Permutations{UnitRange{Int64}}(1:3, 2))
110+ (3, Combinatorics.Permutations{UnitRange{Int64}}(1:3, 3))
111+ (4, Vector{Int64}[])
112+
113+ julia> [ (len, collect(permutations(1:3, len))) for len in -1:4 ]
114+ 6-element Vector{Tuple{Int64, Vector{Vector{Int64}}}}:
115+ (-1, [])
116+ (0, [[]])
117+ (1, [[1], [2], [3]])
118+ (2, [[1, 2], [1, 3], [2, 1], [2, 3], [3, 1], [3, 2]])
119+ (3, [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]])
120+ (4, [])
121+ ```
81122"""
82123function permutations (a, t:: Integer )
83124 if t == 0
174215 multiset_permutations(a, t)
175216
176217Generate all permutations of size `t` from an array `a` with possibly duplicated elements.
218+
219+ # Examples
220+ ```jldoctest
221+ julia> collect(permutations([1,1,1], 2))
222+ 6-element Vector{Vector{Int64}}:
223+ [1, 1]
224+ [1, 1]
225+ [1, 1]
226+ [1, 1]
227+ [1, 1]
228+ [1, 1]
229+
230+ julia> collect(multiset_permutations([1,1,1], 2))
231+ 1-element Vector{Vector{Int64}}:
232+ [1, 1]
233+
234+ julia> collect(multiset_permutations([1,1,2], 3))
235+ 3-element Vector{Vector{Int64}}:
236+ [1, 1, 2]
237+ [1, 2, 1]
238+ [2, 1, 1]
239+ ```
177240"""
178241function multiset_permutations (a, t:: Integer )
179242 m = unique (collect (a))
200263 nthperm!(a, k)
201264
202265In-place version of [`nthperm`](@ref); the array `a` is overwritten.
266+
267+ # Examples
268+ ```jldoctest
269+ julia> a = [1, 2, 3];
270+
271+ julia> collect(permutations(a))
272+ 6-element Vector{Vector{Int64}}:
273+ [1, 2, 3]
274+ [1, 3, 2]
275+ [2, 1, 3]
276+ [2, 3, 1]
277+ [3, 1, 2]
278+ [3, 2, 1]
279+
280+ julia> nthperm!(a, 3); a
281+ 3-element Vector{Int64}:
282+ 2
283+ 1
284+ 3
285+
286+ julia> nthperm!(a, 4); a
287+ 3-element Vector{Int64}:
288+ 1
289+ 3
290+ 2
291+
292+ julia> nthperm!(a, 0)
293+ ERROR: ArgumentError: permutation k must satisfy 0 < k ≤ 6, got 0
294+ [...]
295+ ```
203296"""
204297function nthperm! (a:: AbstractVector , k:: Integer )
205298 n = length (a)
225318 nthperm(a, k)
226319
227320Compute the `k`th lexicographic permutation of the vector `a`.
321+
322+ # Examples
323+ ```jldoctest
324+ julia> collect(permutations([1,2]))
325+ 2-element Vector{Vector{Int64}}:
326+ [1, 2]
327+ [2, 1]
328+
329+ julia> nthperm([1,2], 1)
330+ 2-element Vector{Int64}:
331+ 1
332+ 2
333+
334+ julia> nthperm([1,2], 2)
335+ 2-element Vector{Int64}:
336+ 2
337+ 1
338+
339+ julia> nthperm([1,2], 3)
340+ ERROR: ArgumentError: permutation k must satisfy 0 < k ≤ 2, got 3
341+ [...]
342+ ```
228343"""
229344nthperm (a:: AbstractVector , k:: Integer ) = nthperm! (collect (a), k)
230345
@@ -233,6 +348,37 @@ nthperm(a::AbstractVector, k::Integer) = nthperm!(collect(a), k)
233348
234349Return the integer `k` that generated permutation `p`. Note that
235350`nthperm(nthperm([1:n], k)) == k` for `1 <= k <= factorial(n)`.
351+
352+ # Examples
353+ ```jldoctest
354+ julia> nthperm(nthperm([1:3...], 4))
355+ 4
356+
357+ julia> collect(permutations([1, 2, 3]))
358+ 6-element Vector{Vector{Int64}}:
359+ [1, 2, 3]
360+ [1, 3, 2]
361+ [2, 1, 3]
362+ [2, 3, 1]
363+ [3, 1, 2]
364+ [3, 2, 1]
365+
366+ julia> nthperm([1, 2, 3])
367+ 1
368+
369+ julia> nthperm([3, 2, 1])
370+ 6
371+
372+ julia> nthperm([1, 1, 1])
373+ ERROR: ArgumentError: argument is not a permutation
374+ [...]
375+
376+ julia> nthperm(collect(1:10))
377+ 1
378+
379+ julia> nthperm(collect(10:-1:1))
380+ 3628800
381+ ```
236382"""
237383function nthperm (p:: AbstractVector{<:Integer} )
238384 isperm (p) || throw (ArgumentError (" argument is not a permutation" ))
@@ -262,6 +408,24 @@ is even, -1 if it is odd, and 0 otherwise.
262408
263409The parity is computed by using the fact that a permutation is odd if and
264410only if the number of even-length cycles is odd.
411+
412+ # Examples
413+ ```jldoctest
414+ julia> levicivita([1, 2, 3])
415+ 1
416+
417+ julia> levicivita([3, 2, 1])
418+ -1
419+
420+ julia> levicivita([1, 1, 1])
421+ 0
422+
423+ julia> levicivita(collect(1:100))
424+ 1
425+
426+ julia> levicivita(ones(Int, 100))
427+ 0
428+ ````
265429"""
266430function levicivita (p:: AbstractVector{<:Integer} )
267431 n = length (p)
298462Compute the parity of a permutation `p` using the [`levicivita`](@ref) function,
299463permitting calls such as `iseven(parity(p))`. If `p` is not a permutation then an
300464error is thrown.
465+
466+ # Examples
467+ ```jldoctest
468+ julia> parity([1, 2, 3])
469+ 0
470+
471+ julia> parity([3, 2, 1])
472+ 1
473+
474+ julia> parity([1, 1, 1])
475+ ERROR: ArgumentError: Not a permutation
476+ [...]
477+
478+ julia> parity((collect(1:100)))
479+ 0
480+ ```
301481"""
302482function parity (p:: AbstractVector{<:Integer} )
303483 epsilon = levicivita (p)
0 commit comments