Skip to content
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

typing #22

Merged
merged 5 commits into from
Mar 13, 2024
Merged
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
12 changes: 6 additions & 6 deletions src/cca.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
abstract type AbstractCCARule <: AbstractODRule end

struct CCA{T} <: AbstractCCARule
struct CCA{T<:Real} <: AbstractCCARule
rule::T
radius::Int
end
Expand Down Expand Up @@ -37,19 +37,19 @@ next_generation = cca(starting_array) # Evolve to next generation
The evolution is determined by the rule applied to the sum of the neighborhood states,
normalized by their count, for each cell in the array.
"""
function CCA(rule; radius=1)
function CCA(rule::T; radius=1) where {T<:Real}
return CCA(rule, radius)
end

function (cca::CCA)(starting_array)
function (cca::CCA)(starting_array::AbstractArray)
return nextgen = evolution(starting_array, cca.rule, cca.radius)
end

function c_state_reader(neighborhood, radius)
function c_state_reader(neighborhood::AbstractArray, radius)
return sum(neighborhood) / length(neighborhood)
end

function evolution(cell, rule, radius::Number)
function evolution(cell::AbstractArray, rule::T, radius::Number) where {T<:Real}
neighborhood_size = radius * 2 + 1
output = zeros(length(cell))
cell = vcat(
Expand All @@ -65,7 +65,7 @@ function evolution(cell, rule, radius::Number)
return output
end

function evolution(cell, rule, radius::Tuple)
function evolution(cell::AbstractArray, rule::T, radius::Tuple) where {T<:Real}
neighborhood_size = sum(radius) + 1
output = zeros(length(cell))
cell = vcat(
Expand Down
22 changes: 11 additions & 11 deletions src/dca.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
abstract type AbstractDCARule <: AbstractODRule end

struct DCA{B,R,T} <: AbstractDCARule
rule::B
struct DCA{T<:Integer,R,P} <: AbstractDCARule
rule::T
ruleset::R
states::Int
radius::T
radius::P
end

"""
Expand Down Expand Up @@ -35,34 +35,34 @@ starting_array = [0, 1, 0, 1, 1, 0] # Initial state
next_generation = dca(starting_array) # Evolve to the next generation
```
"""
function DCA(rule; states=2, radius=1)
function DCA(rule::T; states::Int=2, radius=1) where {T<:Integer}
ruleset = conversion(rule, states, radius)
return DCA(rule, ruleset, states, radius)
end

function (dca::DCA)(starting_array)
return nextgen = evolution(starting_array, dca.ruleset, dca.states, dca.radius)
function (dca::DCA)(starting_array::AbstractArray)
return evolution(starting_array, dca.ruleset, dca.states, dca.radius)
end

function conversion(rule, states, radius::Int)
function conversion(rule::T, states::Int, radius::Int) where {T<:Integer}
rule_len = states^(2 * radius + 1)
rule_bin = parse.(Int, split(string(rule; base=states), ""))
rule_bin = vcat(zeros(typeof(rule_bin[1]), rule_len - length(rule_bin)), rule_bin)
return reverse!(rule_bin)
end

function conversion(rule, states, radius::Tuple)
function conversion(rule::T, states::Int, radius::Tuple) where {T<:Integer}
rule_len = states^(sum(radius) + 1)
rule_bin = parse.(Int, split(string(rule; base=states), ""))
rule_bin = vcat(zeros(typeof(rule_bin[1]), rule_len - length(rule_bin)), rule_bin)
return reverse!(rule_bin)
end

function state_reader(neighborhood, states)
function state_reader(neighborhood::AbstractArray, states::Int)
return parse(Int, join(convert(Array{Int}, neighborhood)); base=states) + 1 #ugly
end

function evolution(cell, ruleset, states, radius::Int)
function evolution(cell::AbstractArray, ruleset, states::Int, radius::Int)
neighborhood_size = radius * 2 + 1
output = zeros(length(cell))
cell = vcat(
Expand All @@ -76,7 +76,7 @@ function evolution(cell, ruleset, states, radius::Int)
return output
end

function evolution(cell, ruleset, states, radius::Tuple)
function evolution(cell::AbstractArray, ruleset, states::Int, radius::Tuple)
neighborhood_size = sum(radius) + 1
output = zeros(length(cell))#da qui in poi da modificare
cell = vcat(cell[(end - radius[1] + 1):end], cell, cell[1:radius[2]])
Expand Down
14 changes: 7 additions & 7 deletions src/life.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@

abstract type AbstractLifeRule <: AbstractTDRule end

struct Life{T,A,C} <: AbstractLifeRule
struct Life{T,A} <: AbstractLifeRule
born::T
survive::A
radius::C
radius::Int
end

"""
Expand Down Expand Up @@ -50,16 +50,16 @@ starting_array[4, 2:4] = 1
next_generation = life(starting_array)
```
"""
function Life(life_description; radius=1)
function Life(life_description::Tuple; radius=1)
born, survive = life_description[1], life_description[2]
return Life(born, survive, radius)
end

function (life::Life)(starting_array)
return nextgen = life_evolution(starting_array, life.born, life.survive, life.radius)
function (life::Life)(starting_array::AbstractMatrix)
return life_evolution(starting_array, life.born, life.survive, life.radius)
end

function virtual_expansion(starting_array, radius)
function virtual_expansion(starting_array::AbstractMatrix, radius::Int)
height, width = size(starting_array)
nh, nw = height - radius + 1, width - radius + 1
left = vcat(
Expand Down Expand Up @@ -91,7 +91,7 @@ function life_application(state, born, survive)
end
end

function life_evolution(starting_array, born, survive, radius)
function life_evolution(starting_array::AbstractMatrix, born, survive, radius)
height, width = size(starting_array)
output = zeros(typeof(starting_array[2]), height, width)
virtual_output = virtual_expansion(starting_array, radius)
Expand Down
8 changes: 4 additions & 4 deletions src/tca.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function TCA(code; states=2, radius=1)
return TCA(code, codeset, states, radius)
end

function (tca::TCA)(starting_array)
function (tca::TCA)(starting_array::AbstractArray)
return nextgen = tca_evolution(starting_array, tca.codeset, tca.states, tca.radius)
end

Expand All @@ -64,11 +64,11 @@ function tca_conversion(code, states, radius::Tuple)
return reverse!(code_bin)
end

function tca_state_reader(neighborhood, codeset_len)
function tca_state_reader(neighborhood::AbstractArray, codeset_len)
return mod1(sum(neighborhood) + 1, codeset_len)
end

function tca_evolution(cell, codeset, states, radius::Number)
function tca_evolution(cell::AbstractArray, codeset, states, radius::Number)
neighborhood_size = radius * 2 + 1
output = zeros(length(cell))
cell = vcat(
Expand All @@ -84,7 +84,7 @@ function tca_evolution(cell, codeset, states, radius::Number)
return output
end

function tca_evolution(cell, codeset, states, radius::Tuple)
function tca_evolution(cell::AbstractArray, codeset, states, radius::Tuple)
neighborhood_size = sum(radius) + 1
output = zeros(length(cell))
cell = vcat(cell[(end - radius[1] + 1):end], cell, cell[1:radius[2]])
Expand Down
Loading