Hi there,
I am having problems to understand which of the following two scenarios is correct or if they are similar and have the same performance.
function input()
# define variables and functions inside the
# `input` function scope.
κ(t) = t
θ(t) = sin(t)
σ(t) = cos(t)
α = 1.000
β = 0.000
# define a function using variables and
# functions defined in the outer scope
fμ = (du, u, t) -> begin
r = u[1]
du[1] = κ(t) * (θ(t) - r) + σ(t) * sqrt(α(t) + β(t) * r)
end
# use the previous function to solve a problem, which
# requires evaluating the fμ function millions of times
solve(fμ)
end
Or should I explicitly pass the parameters to the function:
function input()
# define variables and functions inside the
# `input` function scope.
κ(t) = t
θ(t) = sin(t)
σ(t) = cos(t)
α = 1.000
β = 0.000
# define a function WITHOUT using variables and
# functions in the outer scope
fμ = (du, u, p, t) -> begin
r = u[1]
α = p.α
β = p.β
κ = p.κ
σ = p.σ
θ = p.θ
du[1] = κ(t) * (θ(t) - r) + σ(t) * sqrt(α(t) + β(t) * r)
end
# build the 'parameters' object:
p = (κ = κ, θ = θ, σ = σ, α = α, β = β)
# use the previous function to solve a problem, which requires
# evaluating the function fμ millions of times but, in this case,
# the parameters object `p` is provided and is used for the
# evaluation of fμ inside the solve function.
solve(fμ, p)
end
I think that, in the first case, I am defining a closure fμ
. Please, let me know if that is correct. However, I would like to know if the performance of that closure is the same as the performance of the function fμ
in the second case.
Probably the answer is around here. I am suspecting they have the same performance.
Thank you very much!