I’m writing some code to solve a couple of o.d.e systems I’m working with.
At the moment I have some code that looks like this:
function timestep_a(init;T=1.,dt=0.0005)
for t in 0.:dt:T
init=init+dt*ode_a(init)
end
return init
end
function timestep_b(init;T=1.,dt=0.0005)
for t in 0.:dt:T
init=init+dt*ode_b(init)
end
return init
end
#Foo's equation
function ode_a(input)
return input*input-0.5*input
end
#Bar's equation
function ode_b(arr)
return -input*input+2*input-4.0
end
There is a lot of redundancy in the timestep functions, so I would like to make these one function. However if I do this, and pass the specific ode to solve as an argument, the code can’t optimise as well, and is much slower.
Is there a way to have my cake and eat it? Can I get julia to compile a separate timestep function with the same name for both o.d.es without actually writing it twice?