问题
I have created a vector which concatenates strings of differential equations that are in the correct format to be used be the differeq ode sovler in Julia (i.e, f(du,u,p,t):
Combine <- c("du[1] = - 1*0.4545*(u[1]^1) - 1*27000000*(u[4]^1)*(u[1]^1)",
"du[2] = - 1*3100000000*(u[2]^1)*(u[4]^1)", "du[3] = - 1*33000*(u[3]^1)*(u[4]^1)",
"du[4] =2*0.4545*(u[1]^1) - 1*3100000000*(u[2]^1)*(u[4]^1) - 1*33000*(u[3]^1)*(u[4]^1) - 1*27000000*(u[4]^1)*(u[1]^1) - 1*8500000*(u[4]^1)*(u[5]^1) - 1*390000000*(u[4]^1)*(u[6]^1)",
"du[5] = - 1*8500000*(u[4]^1)*(u[5]^1)", "du[6] = - 1*390000000*(u[4]^1)*(u[6]^1)"
My question is how can I evaluate these expressions using JuliaCall:Julia_eval? I know that if I explicitly include the expressions the ODE solver works correctly, but if I just use Combine[i] I run into an error because I am calling a global variable.
f <- JuliaCall::julia_eval("
function f(du,u,p,t)
du[1] = - 1*0.4545*(u[1]^1) - 1*27000000*(u[4]^1)*(u[1]^1)
du[2] = - 1*3100000000*(u[2]^1)*(u[4]^1)
du[3] = - 1*33000*(u[3]^1)*(u[4]^1)
du[4] = 2*0.4545*(u[1]^1) - 1*3100000000*(u[2]^1)*(u[4]^1) - 1*33000*(u[3]^1)*(u[4]^1) - 1*27000000*(u[4]^1)*(u[1]^1) - 1*8500000*(u[4]^1)*(u[5]^1) - 1*390000000*(u[4]^1)*(u[6]^1)
du[5] = - 1*8500000*(u[4]^1)*(u[5]^1)
du[6] = - 1*390000000*(u[4]^1)*(u[6]^1)
end")
Is there any way to work around this? I have tried unlisting "Combine" and creating a single string with \n included at the end of each line but this was unsuccessful. It would be ideal if I could call in the vector from the global environment and have that analyzed by the ODE solver, something like below ( i know this cant work like this):
f <- JuliaCall::julia_eval("
function f(du,u,p,t)
Combine[i]
end")
回答1:
As @jverzani mentioned,
sprintf("function f(du,u,p,t)\n%s\nend", paste(Combine, collapse="\n"))
builds the string in R that you want. If you JuliaCall::julia_eval
that, then you're done. Anything else is completely over-complicated, and will also be less optimized anyways, and so I would no recommend the other paths you are trying to take here.
来源:https://stackoverflow.com/questions/58704190/generate-differentialequations-ifrom-a-vector-in-julia