# Unexpected InexactError() while using DifferentialEquations.jl

**URL:** <https://discourse.julialang.org/t/unexpected-inexacterror-while-using-differentialequations-jl/9482>\
**Category:** General Usage\
**Tags:** diffeq\
**Created:** [March 4, 2018, 2:26am UTC](https://discourse.julialang.org/t/unexpected-inexacterror-while-using-differentialequations-jl/9482 "2018-03-04T02:26:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marcus\_PS](https://avatars.discourse-cdn.com/v4/letter/m/46a35a/32.png) [@Marcus\_PS](https://discourse.julialang.org/u/Marcus_PS)\
**Post date:** [March 4, 2018, 2:26am UTC](https://discourse.julialang.org/t/unexpected-inexacterror-while-using-differentialequations-jl/9482/1 "2018-03-04T02:26:05Z")

</div>

Playing around with `DifferentialEquations.jl`, I ran into a simple corner case. The code corresponds to a trivial ODE, which is a modification of the following (that works just fine).

```julia
function f(u,p,t)
    return p * u
end

u0=0.5
tspan = (0.0,1.0)
prob = ODEProblem(f,u0,tspan,1.01)
sol = solve(prob)

```

If I try to implement an in-place version, however, I get an `InexactError()`

```julia
function g(du,u,p,t)
    du = p*u
end

u0=0.5
tspan = (0.0,1.0)
prob = ODEProblem(g,u0,tspan,1.01)
sol = solve(prob)

```

Here is the full stack trace

```julia
InexactError()

Stacktrace:
 [1] convert(::Type{Int64}, ::Float64) at ./float.jl:679
 [2] zeros at ./array.jl:263 [inlined]
 [3] zeros at ./array.jl:264 [inlined]
 [4] zeros at ./array.jl:266 [inlined]
 [5] alg_cache(::OrdinaryDiffEq.Tsit5, ::Float64, ::Float64, ::Type{T} where T, ::Type{T} where T, ::Type{T} where T, ::Float64, ::Float64, ::#g, ::Float64, ::Float64, ::Float64, ::Float64, ::Bool, ::Type{Val{true}}) at /home/jrun/.julia/v0.6/OrdinaryDiffEq/src/caches/low_order_rk_caches.jl:399
 [6] #init#1331(::Int64, ::Array{Float64,1}, ::Array{Float64,1}, ::Array{Float64,1}, ::Void, ::Bool, ::Void, ::Bool, ::Bool, ::Void, ::Bool, ::Bool, ::Float64, ::Bool, ::Rational{Int64}, ::Void, ::Void, ::Int64, ::Rational{Int64}, ::Int64, ::Int64, ::Rational{Int64}, ::Bool, ::Int64, ::Rational{Int64}, ::Rational{Int64}, ::Int64, ::Float64, ::Float64, ::DiffEqBase.#ODE_DEFAULT_NORM, ::DiffEqBase.#ODE_DEFAULT_ISOUTOFDOMAIN, ::DiffEqBase.#ODE_DEFAULT_UNSTABLE_CHECK, ::Bool, ::Bool, ::Bool, ::Bool, ::Bool, ::Bool, ::Bool, ::Bool, ::Int64, ::String, ::DiffEqBase.#ODE_DEFAULT_PROG_MESSAGE, ::Void, ::Bool, ::Bool, ::Array{Any,1}, ::DiffEqBase.#init, ::DiffEqBase.ODEProblem{Float64,Float64,true,Float64,#g,Void,Void,UniformScaling{Int64},DiffEqBase.StandardODEProblem}, ::OrdinaryDiffEq.Tsit5, ::Array{Any,1}, ::Array{Any,1}, ::Array{Any,1}, ::Type{Val{true}}) at /home/jrun/.julia/v0.6/OrdinaryDiffEq/src/solve.jl:240
 [7] (::DiffEqBase.#kw##init)(::Array{Any,1}, ::DiffEqBase.#init, ::DiffEqBase.ODEProblem{Float64,Float64,true,Float64,#g,Void,Void,UniformScaling{Int64},DiffEqBase.StandardODEProblem}, ::OrdinaryDiffEq.Tsit5, ::Array{Any,1}, ::Array{Any,1}, ::Array{Any,1}, ::Type{Val{true}}) at ./<missing>:0
 [8] #solve#1330(::Array{Any,1}, ::Function, ::DiffEqBase.ODEProblem{Float64,Float64,true,Float64,#g,Void,Void,UniformScaling{Int64},DiffEqBase.StandardODEProblem}, ::OrdinaryDiffEq.Tsit5, ::Array{Any,1}, ::Array{Any,1}, ::Array{Any,1}, ::Type{Val{true}}) at /home/jrun/.julia/v0.6/OrdinaryDiffEq/src/solve.jl:6
 [9] (::DiffEqBase.#kw##solve)(::Array{Any,1}, ::DiffEqBase.#solve, ::DiffEqBase.ODEProblem{Float64,Float64,true,Float64,#g,Void,Void,UniformScaling{Int64},DiffEqBase.StandardODEProblem}, ::OrdinaryDiffEq.Tsit5, ::Array{Any,1}, ::Array{Any,1}, ::Array{Any,1}, ::Type{Val{true}}) at ./<missing>:0 (repeats 2 times)
 [10] #solve#2(::Bool, ::Array{Any,1}, ::Function, ::DiffEqBase.ODEProblem{Float64,Float64,true,Float64,#g,Void,Void,UniformScaling{Int64},DiffEqBase.StandardODEProblem}, ::Void) at /home/jrun/.julia/v0.6/DifferentialEquations/src/default_solve.jl:14
 [11] (::DiffEqBase.#kw##solve)(::Array{Any,1}, ::DiffEqBase.#solve, ::DiffEqBase.ODEProblem{Float64,Float64,true,Float64,#g,Void,Void,UniformScaling{Int64},DiffEqBase.StandardODEProblem}, ::Void) at ./<missing>:0
 [12] #solve#1(::Bool, ::Array{Any,1}, ::Function, ::DiffEqBase.ODEProblem{Float64,Float64,true,Float64,#g,Void,Void,UniformScaling{Int64},DiffEqBase.StandardODEProblem}) at /home/jrun/.julia/v0.6/DifferentialEquations/src/default_solve.jl:5
 [13] solve(::DiffEqBase.ODEProblem{Float64,Float64,true,Float64,#g,Void,Void,UniformScaling{Int64},DiffEqBase.StandardODEProblem}) at /home/jrun/.julia/v0.6/DifferentialEquations/src/default_solve.jl:2
 [14] include_string(::String, ::String) at ./loading.jl:522

```

Everything goes through (with minor syntax adjustments) if the ODE is 2-dimensional or higher dimensional.

Any suggestions as to what is the issue here?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [March 4, 2018, 4:14am UTC](https://discourse.julialang.org/t/unexpected-inexacterror-while-using-differentialequations-jl/9482/2 "2018-03-04T04:14:30Z")

</div>

> [@Marcus\_PS](#):
>
> If I try to implement an in-place version, however, I get an InexactError()

> [@Marcus\_PS](#):
>
> function g(du,u,p,t)  
> du = p\*u  
> end

You cannot write in-place functions with immutable types. If you do this, notice that `du` isn’t actually updated but the original value is just shadowed. Since it’s not updated, this `g` function doesn’t actually do anything (try it). Because this isn’t actually doing anything, the cache construction isn’t setup to be compatible with immutable types.

Though we could probably use a better error message.

---

<div class="post-metadata">

**Author:** ![Marcus\_PS](https://avatars.discourse-cdn.com/v4/letter/m/46a35a/32.png) [@Marcus\_PS](https://discourse.julialang.org/u/Marcus_PS)\
**Post date:** [March 4, 2018, 4:53am UTC](https://discourse.julialang.org/t/unexpected-inexacterror-while-using-differentialequations-jl/9482/3 "2018-03-04T04:53:28Z")

</div>

Thanks, that clarifies things. I had forgotten about the importance of immutability for certain types in Julia, and was expecting a pass-by-reference behavior.

A better error message would have certainly been helpful, but I can see how it is a subtle issue.

What do you suggest is the best way to flag the issue? Make a `DifferentialEquations.jl` issue about the error message?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [March 4, 2018, 5:09am UTC](https://discourse.julialang.org/t/unexpected-inexacterror-while-using-differentialequations-jl/9482/4 "2018-03-04T05:09:57Z")

</div>

> [@Marcus\_PS](#):
>
> I had forgotten about the importance of immutability for certain types in Julia, and was expecting a pass-by-reference behavior.

Immutable `struct`s in Julia are value-types so they don’t have references, which is why they can’t pass-by-reference (this is a requirement to make then fast). You can manually wrap it in a `Ref` but that’s usually not a good idea if you don’t need to. DiffEq has the out-of-place route because immutables can be much more efficient if treated without references.

> [@Marcus\_PS](#):
>
> What do you suggest is the best way to flag the issue? Make a DifferentialEquations.jl issue about the error message?

Yup, please feel free to start that thread. There’s a few other places where we could use a more informative error as well.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [March 4, 2018, 12:23pm UTC](https://discourse.julialang.org/t/unexpected-inexacterror-while-using-differentialequations-jl/9482/5 "2018-03-04T12:23:34Z")

</div>

Every argument in Julia gets passed-by-value.
