Skip to content

Commit

Permalink
SEQ operator + stirling aprox. for Catalan numbers + partitions and c…
Browse files Browse the repository at this point in the history
…ompositions + partitions with restricted summand + test unit using problem from page 43
  • Loading branch information
fargolo committed Sep 27, 2023
1 parent f655150 commit cddc7b8
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 28 deletions.
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ authors = ["fargolo <[email protected]> and contributors"]
version = "1.0.0-DEV"

[deps]
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
SymPy = "24249f21-da20-56a4-8eb1-6a02cf4ae2e6"

[compat]
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ pkg>add https://github.com/fargolo/AnalyticComb.jl.git

# Quick start

SymPy.jl functionalities are reexported.

Probability for consecutive runs of lenght k in binary words of length n, use `p_binary_words_runl(k,n)`

```
Expand All @@ -47,3 +49,14 @@ julia> stirling_factorial(7)
4980.395831612462
```

Using SEQ operator to solve Polya denumerant problem (see Flajolet & Sedgewick,p. 43) about the number of ways of giving change of 99 cents using pennies (1 cent), nickels (5 cents), dimes (10 cents) and quarters (25 cents).
```
julia> restricted_sum_part_gf([1,5,10,25]) # generating function SEQ(z)*SEQ(z^5)*SEQ(z^10)*SEQ(z^25)
1
────────────────────────────────────
⎛ 5⎞ ⎛ 10⎞ ⎛ 25⎞
(1 - z)⋅⎝1 - z ⎠⋅⎝1 - z ⎠⋅⎝1 - z ⎠
julia>restricted_sum_part(99,[1,5,10,25])
213
```
3 changes: 0 additions & 3 deletions dev/chap1.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,3 @@ permutation_count(n) = factorial(n)
# p 20 and 35
catalan_gen(z) = (1sqrt(14z))/2z
series(catalan_gen(z),z,0,10)

#p38
stirling_catalan(n) = 4^n/(sqrt(pi*n^3))
16 changes: 9 additions & 7 deletions dev/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@ primes_composition(n) = 0.30365 * 1.47622^n

#change
using SymPy
change_counts(z) = 1/((1-z)(1 - z^5)(1 - z^10)(1 - z^25))
series(change_counts(z),z,0,99)
coefs = collect(series(change_counts(z),z,0,99),z)
coefs.coeff(z,99)
coefs.coeff(z^99)
coefs.coeffs()
@vars z
p.coeffs()
r = [1,5,10,25]
restricted_sum_part(r)=prod([1/(1-z^i) for i in r])
restricted_sum_part(r)
change_counts(z) = 1/((1-z)*(1 - z^5)*(1 - z^10)*(1 - z^25))
SEQ(z) = 1/(1-z)
change_counts(z) = SEQ(z)*SEQ(z^5)*SEQ(z^10)*SEQ(z^25)
coefs = collect(series(change_counts(z),z,0,100),z)
coefs = collect(series(restricted_sum_part(r),z,0,100),z)
coefs.coeff(z,99)
16 changes: 11 additions & 5 deletions src/AnalyticComb.jl
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
module AnalyticComb

using SymPy
#using TaylorSeries
#using DynamicalSystems
#using SymPy
using Reexport

@reexport using SymPy

export
stirling_factorial, partitions, primes_composition,
SEQ,
stirling_factorial, stirling_catalan,
partitions, primes_composition, restricted_sum_part_gf, restricted_sum_part,
p_binary_words_runl

include("binary_words.jl")
include("operators.jl")
include("asymptotics.jl")
include("parts_comps.jl")
include("binary_words.jl")


end
14 changes: 3 additions & 11 deletions src/asymptotics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,10 @@ Stirling approximation for n! as (n/exp(1))^n*sqrt(2*pi*n).
"""
stirling_factorial(n) = (n/exp(1))^n*sqrt(2*pi*n)


"""
partitions(n)
stirling_catalan(n)
Asymptotics for partition of integers by Hardy and Ramanujan, later improved by Rademache
Stirling approximation for n_th Catalan number .
"""
partitions(n) = (1/(4*n*sqrt(3)))*exp(pi*sqrt(2n/3))

stirling_catalan(n) = 4^n/(sqrt(pi*n^3))

"""
primes_composition(n)
Asymptotics for composition of primes.
"""
primes_composition(n) = 0.30365 * 1.47622^n
2 changes: 0 additions & 2 deletions src/binary_words.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,3 @@ function p_binary_words_runl(k,n)
return(Float64(result_sym))
end



6 changes: 6 additions & 0 deletions src/operators.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"""
SEQ(z)
Sequence operator
"""
SEQ(z) = 1/(1-z)
37 changes: 37 additions & 0 deletions src/parts_comps.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""
partitions(n)
Asymptotics for partition of integers by Hardy and Ramanujan, later improved by Rademache
"""
partitions(n) = (1/(4*n*sqrt(3)))*exp(pi*sqrt(2n/3))


"""
primes_composition(n)
Asymptotics for composition of primes.
"""
primes_composition(n) = 0.30365 * 1.47622^n

"""
restricted_sum_part_gf(r)
Generating function for partition with restricted summand.
"""
function restricted_sum_part_gf(r)
z = SymPy.symbols("z")
return(prod([SEQ(z^i) for i in r]))
end

"""
restricted_sum_part(n,r)
Number of partitions with components in r with restricted summand n.
n must be an integer and r must be a set of integers, like in r = [1,5,10,25] , n = 99.
"""
function restricted_sum_part(n,r)
z = SymPy.symbols("z")
coefs = collect(series(restricted_sum_part_gf(r),z,0,n+1),z)
return(coefs.coeff(z,n))
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ using Test
0.0440234813776148,
0.022607888203734293]) .< 0.01) != 0

@test restricted_sum_part(99,[1,5,10,25]) == 213 # Polya problem for partition with restricted sum

end

0 comments on commit cddc7b8

Please sign in to comment.