Skip to content

Commit 3fc8ace

Browse files
juchevalCopilot
andcommitted
refactor: add scratch-buffer to forward_simulation!
Co-authored-by: Copilot <copilot@github.com>
1 parent 891a3ce commit 3fc8ace

2 files changed

Lines changed: 13 additions & 5 deletions

File tree

src/simulate.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,10 @@ function rand(
5050

5151
output = Matrix{Bool}(undef, N, T)
5252
current_value = stationary_initial_condition(modelconnec, excitatory)
53+
past_value = similar(current_value)
5354
output[:, 1] = current_value
5455
for t in 1:(T - 1)
55-
forward_simulation!(current_value, modelconnec, excitatory)
56+
forward_simulation!(current_value, past_value, modelconnec, excitatory)
5657
output[:, t + 1] = current_value
5758
end
5859
return DiscreteTimeData(output)
@@ -151,10 +152,11 @@ end
151152

152153
function forward_simulation!(
153154
current_value::Vector{Bool},
155+
past_value::Vector{Bool},
154156
modelconnec::MarkovChainConnectivity,
155157
excitatory::Vector{Bool},
156158
)
157-
past_value = copy(current_value)
159+
copyto!(past_value, current_value)
158160

159161
model = modelconnec.model
160162
N = size(modelconnec)

test/simulate.jl

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
@testset "simulate.jl" begin
22
@test begin
33
testvalue = [true, false, true, false]
4+
buffer = similar(testvalue)
45
MF.forward_simulation!(
56
testvalue,
7+
buffer,
68
MF.MarkovChainConnectivity(MarkovChainModel(0.5, 0.5, 0.0), ones(Int, (4, 4))),
79
[true, false, true, false],
810
)
@@ -11,19 +13,23 @@
1113

1214
@test begin
1315
testvalue = [false, true, false, true]
16+
buffer = similar(testvalue)
1417
MF.forward_simulation!(
1518
testvalue,
19+
buffer,
1620
MF.MarkovChainConnectivity(MarkovChainModel(0, 0.5, 0.0), ones(Int, (4, 4))),
1721
[true, false, true, false],
1822
)
1923
testvalue == [false, false, false, false]
2024
end
2125

2226
@test begin
23-
modelconnec =
24-
MF.MarkovChainConnectivity(MarkovChainModel(0.0, 0.0, 0.0), ones(Bool, 4, 4))
27+
modelconnec = MF.MarkovChainConnectivity(
28+
MarkovChainModel(0.0, 0.0, 0.0), ones(Bool, 4, 4)
29+
)
2530
values, child, parent = MF.backward_step(collect(1:4), modelconnec)
26-
all(i -> (haskey(values, i) || (i in child)), 1:4) && length(child) == length(parent)
31+
all(i -> (haskey(values, i) || (i in child)), 1:4) &&
32+
length(child) == length(parent)
2733
end
2834

2935
@test begin

0 commit comments

Comments
 (0)