I tried the example from Symbolics.jl but ran into a problem:
using Symbolics
function lotka_volterra!(du, u, p, t)
x, y = u
α, β, δ, γ = p
du[1] = dx = α*x - β*x*y
du[2] = dy = -δ*y + γ*x*y
end
@variables t du[1:2] u[1:2] p[1:4]
lotka_volterra!(du, u, p, t)
D = Differential(t)
f = build_function((@. D(u) ~ du), u, p, t, target=Symbolics.JuliaTarget())
It seems that build_function
can not handle the Differential(t)
terms:
julia> Base.remove_linenums!(f[1])
:(function (var"##arg#312", var"##arg#313", t)
let u₁ = var"##arg#312"[1], u₂ = var"##arg#312"[2], p₁ = var"##arg#313"[1], p₂ = var"##arg#313"[2], p₃ = var"##arg#313"[3], p₄ = var"##arg#313"[4]
(SymbolicUtils.Code.create_array)(typeof(var"##arg#312"), nothing, Val{(2,)}(), Differential(t)(u₁) ~ p₁*u₁ - (p₂*u₁*u₂), Differential(t)(u₂) ~ p₄*u₁*u₂ - (p₃*u₂))
end
end)
The generated function can not be evaluated:
julia> eval(f[1])(rand(2),rand(4),rand())
2-element Array{Equation,1}:
Differential(t)(u₁) ~ p₁*u₁ - (p₂*u₁*u₂)
Differential(t)(u₂) ~ p₄*u₁*u₂ - (p₃*u₂)
If I just use the RHS
f = build_function(du, u, p, t, target=Symbolics.JuliaTarget())
it can be
julia> eval(f[1])(rand(2),rand(4),rand())
2-element Array{Float64,1}:
0.1351112517260627
-0.00769245404130421
The MATLABTarget
seems to cope with this:
julia> f = build_function((@. D(u) ~ du), u, p, t, target=Symbolics.MATLABTarget())
┌ Warning: build_function(::Array{<:Equation}...) is deprecated. Use build_function(::AbstractArray...) instead.
└ @ Symbolics .julia\packages\Symbolics\cO1c0\src\build_function.jl:677
"diffeqf = @(t,internal_var___u) [internal_var___p(1) * internal_var___u(1) + -1 * internal_var___p(2) * internal_var___u(1) * internal_var___u(2); -1 * internal_var___p(3) * internal_var___u(2) + internal_var___p(4) * internal_var___u(1) * internal_var___u(2)];"
And also the CTaret
:
julia> f = build_function((@. D(u) ~ du), u, p, t, target=Symbolics.CTarget())
┌ Warning: build_function(::Array{<:Equation}...) is deprecated. Use build_function(::AbstractArray...) instead.
└ @ Symbolics .julia\packages\Symbolics\cO1c0\src\build_function.jl:463
"void diffeqf(double* du, double* RHS1, double* RHS2, double RHS3) {\n du[0] = RHS2[0] * RHS1[0] + -1 * RHS2[1] * RHS1[0] * RHS1[1];\n du[1] = -1 * RHS2[2] * RHS1[1] + RHS2[3] * RHS1[0] * RHS1[1];\n}\n"
```