Skip to content

Commit

Permalink
Add combinatorics.jl
Browse files Browse the repository at this point in the history
  • Loading branch information
Samayel committed Jul 1, 2015
1 parent 14c58b5 commit 3dc8bca
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 2 deletions.
2 changes: 0 additions & 2 deletions src/Math/NumberTheory/numbertheory.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ using TaylorSeries: Taylor1, taylor1_variable, get_coeff
using Pipe.@pipe
using Reexport.@reexport

VERSION < v"0.4-" && @reexport using Combinatorics
@reexport using ContinuedFractions
@reexport using Digits
@reexport using Multicombinations

include("primes.jl")
include("factor.jl")
Expand Down
26 changes: 26 additions & 0 deletions src/Math/combinatorics.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

VERSION < v"0.4-" && @reexport using Combinatorics
@reexport using Multicombinations

export
permutations_with_repetitions,
# variations, variations_with_repetitions,
combinations_with_repetitions


# Base.permutations()

permutations_with_repetitions{T}(a::AbstractArray{T,1}) =
variations_with_repetitions(a, length(a))


# http://www.aconnect.de/friends/editions/computer/combinatoricode_e.html
#variations{T}(a::AbstractArray{T,1}, k::Integer) = nothing

# product(repeated(a, k)...)
#variations_with_repetitions{T}(a::AbstractArray{T,1}, k::Integer) = nothing

# Base.combinations()

combinations_with_repetitions{T}(a::AbstractArray{T,1}, k::Integer) =
multicombinations(a, k)
1 change: 1 addition & 0 deletions src/Math/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ macro activate_mathematica(); Expr(:using, :Mathematica); end
macro activate_matlab(); Expr(:using, :MATLAB); end

include("NumberTheory/numbertheory.jl")
include("combinatorics.jl")

end
139 changes: 139 additions & 0 deletions test/Math/combinatorics.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
function test_combinatorics_permutations()
@test collect(permutations(["a", "b", "c"])) == Array[
["a", "b", "c"],
["a", "c", "b"],
["b", "a", "c"],
["b", "c", "a"],
["c", "a", "b"],
["c", "b", "a"],
]
@test length(permutations(["a", "b", "c"])) == 6
@test eltype(permutations(["a", "b", "c"])) == Array{ASCIIString,1}
end

function test_combinatorics_permutations_with_repetitions()
#=
@test collect(permutations_with_repetitions(["a", "b", "c"])) == Array[
["a", "a", "a"],
["a", "a", "b"],
["a", "a", "c"],
["a", "b", "a"],
["a", "b", "b"],
["a", "b", "c"],
["a", "c", "a"],
["a", "c", "b"],
["a", "c", "c"],
["b", "a", "a"],
["b", "a", "b"],
["b", "a", "c"],
["b", "b", "a"],
["b", "b", "b"],
["b", "b", "c"],
["b", "c", "a"],
["b", "c", "b"],
["b", "c", "c"],
["c", "a", "a"],
["c", "a", "b"],
["c", "a", "c"],
["c", "b", "a"],
["c", "b", "b"],
["c", "b", "c"],
["c", "c", "a"],
["c", "c", "b"],
["c", "c", "c"],
]
@test length(permutations_with_repetitions(["a", "b", "c"])) == 27
@test eltype(permutations_with_repetitions(["a", "b", "c"])) == Array{ASCIIString,1}
=#
end

function test_combinatorics_variations()
#=
@test collect(variations(["a", "b", "c", "d"], 2)) == Array[
["a", "b"],
["a", "c"],
["a", "d"],
["b", "a"],
["b", "c"],
["b", "d"],
["c", "a"],
["c", "b"],
["c", "d"],
["d", "a"],
["d", "b"],
["d", "c"],
]
@test length(variations(["a", "b", "c", "d"], 2)) == 12
@test eltype(variations(["a", "b", "c", "d"], 2)) == Array{ASCIIString,1}
=#
end

function test_combinatorics_variations_with_repetitions()
#=
@test collect(variations_with_repetitions(["a", "b", "c", "d"], 2)) == Array[
["a", "a"],
["a", "b"],
["a", "c"],
["a", "d"],
["b", "a"],
["b", "b"],
["b", "c"],
["b", "d"],
["c", "a"],
["c", "b"],
["c", "c"],
["c", "d"],
["d", "a"],
["d", "b"],
["d", "c"],
["d", "d"],
]
@test length(variations_with_repetitions(["a", "b", "c", "d"], 2)) == 16
@test eltype(variations_with_repetitions(["a", "b", "c", "d"], 2)) == Array{ASCIIString,1}
=#
end

function test_combinatorics_combinations()
@test collect(combinations(["a", "b", "c", "d"], 2)) == Array[
["a", "b"],
["a", "c"],
["a", "d"],
["b", "c"],
["b", "d"],
["c", "d"],
]
@test length(combinations(["a", "b", "c", "d"], 2)) == 6
@test eltype(combinations(["a", "b", "c", "d"], 2)) == Array{ASCIIString,1}
end

function test_combinatorics_combinations_with_repetitions()
@test collect(combinations_with_repetitions(["a", "b", "c", "d"], 2)) == Array[
["a", "a"],
["a", "b"],
["a", "c"],
["a", "d"],
["b", "b"],
["b", "c"],
["b", "d"],
["c", "c"],
["c", "d"],
["d", "d"],
]
@test length(combinations_with_repetitions(["a", "b", "c", "d"], 2)) == 10
@test eltype(combinations_with_repetitions(["a", "b", "c", "d"], 2)) == Array{ASCIIString,1}
end

function test_combinatorics_all()
print(rpad("Math.Combinatorics...", 50, ' '))

test_combinatorics_permutations()
test_combinatorics_permutations_with_repetitions()

test_combinatorics_variations()
test_combinatorics_variations_with_repetitions()

test_combinatorics_combinations()
test_combinatorics_combinations_with_repetitions()

println("PASS")
end
2 changes: 2 additions & 0 deletions test/Math/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ using Brainstorm.Math
using Base.Test

include("NumberTheory/numbertheory.jl")
include("combinatorics.jl")

function test_all()
println("")
NumberTheory.test_all()
test_combinatorics_all()
end

end

0 comments on commit 3dc8bca

Please sign in to comment.