# Variable rate jumps with DifferentialEquations.jl and matrices

**URL:** <https://discourse.julialang.org/t/variable-rate-jumps-with-differentialequations-jl-and-matrices/40996>\
**Category:** General Usage\
**Tags:** question\
**Created:** [June 8, 2020, 2:28pm UTC](https://discourse.julialang.org/t/variable-rate-jumps-with-differentialequations-jl-and-matrices/40996 "2020-06-08T14:28:49Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![TTolppanen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ttolppanen/32/12568_2.png) [@TTolppanen](https://discourse.julialang.org/u/TTolppanen)\
**Post date:** [June 8, 2020, 2:28pm UTC](https://discourse.julialang.org/t/variable-rate-jumps-with-differentialequations-jl-and-matrices/40996/1 "2020-06-08T14:28:49Z")

</div>

I’ve been going through some examples on [differential equations with variable jumps](https://docs.sciml.ai/stable/tutorials/jump_diffusion/#Variable-Rate-Jumps-1) and I did this example for myself and got it working.

```julia
function f(du, u, p, t)
    du .= u
end

prob = ODEProblem(f, [1.0 2.0; 3.0 4.0], (0.0, 1.0))
rate(u,p,t) = u[1] + u[2]
affect!(integrator) = (integrator.u[1] = 0.25; integrator.u[2] = 0.5;
                       integrator.u[3] = 0.75; integrator.u[4] = 1)
jump = ConstantRateJump(rate, affect!)
jump_prob = JumpProblem(prob, Direct(), jump)
sol = solve(jump_prob)
plot(sol)
```

Notice here that the rate is not constant and therefore I should use `VariableRateJump()` function instead of `ConstantRateJump()`. But if I replace it in the code it doesn’t work (Dimension mismatch). So what is the correct way to do this for a variable jump rate, where `u` is not a float but a matrix instead? I understand I could do `du[1] = u[1]; du[2] = u[2]` for the components of the matrix, but I would prefer to have just one equation with matrices.

---

<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:** [June 9, 2020, 7:21am UTC](https://discourse.julialang.org/t/variable-rate-jumps-with-differentialequations-jl-and-matrices/40996/2 "2020-06-09T07:21:02Z")

</div>

It’s a bug which will be fixed by [fix variable rate jump broadcast sizing by ChrisRackauckas · Pull Request #117 · SciML/JumpProcesses.jl · GitHub](https://github.com/SciML/DiffEqJump.jl/pull/117) . I’ll release the fix tomorrow.

---

<div class="post-metadata">

**Author:** ![TTolppanen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ttolppanen/32/12568_2.png) [@TTolppanen](https://discourse.julialang.org/u/TTolppanen)\
**Post date:** [June 9, 2020, 7:27am UTC](https://discourse.julialang.org/t/variable-rate-jumps-with-differentialequations-jl-and-matrices/40996/3 "2020-06-09T07:27:23Z")

</div>

Thank you!

---

<div class="post-metadata">

**Author:** ![TTolppanen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ttolppanen/32/12568_2.png) [@TTolppanen](https://discourse.julialang.org/u/TTolppanen)\
**Post date:** [June 10, 2020, 10:24am UTC](https://discourse.julialang.org/t/variable-rate-jumps-with-differentialequations-jl-and-matrices/40996/4 "2020-06-10T10:24:31Z")

</div>

I updated the package and now that example works! But I ran into more problems. This example seems to run just fine:

```julia
function f(du, u, p, t)
    du .= u
end

prob = ODEProblem(f, Complex{Float64}[1.0 2.0; 3.0 4.0], (0.0, 1.0))
rate(u,p,t) = u[1] + u[2]
affect!(integrator) = (integrator.u = Complex{Float64}[1.0 2.0; 3.0 4.0])
jump = ConstantRateJump(rate, affect!)
jump_prob = JumpProblem(prob, Direct(), jump)
sol = solve(jump_prob)
```

but changing from ` ConstantRateJump() ` to ` VariableRateJump() ` breaks it. There are couple things going on here. I did some testing and seems like the ` VariableRateJump() ` problem breaks if the affect is defined directly as a matrix:

```julia
affect!(integrator) = (integrator.u = [1.0 2.0; 3.0 4.0])
```

And also it breaks if the affect is complex:

```julia
affect!(integrator) = (integrator.u[1] = complex(1.0); integrator.u[2] = complex(2.0);
                       integrator.u[3] = complex(3.0); integrator.u[4] = complex(4.0))
```

I hope you could look into these once you have time, thanks for the help! Also, if I run into new bugs what is the best method for reporting them?

---

<div class="post-metadata">

**Author:** ![isaacsas](https://avatars.discourse-cdn.com/v4/letter/i/f6c823/32.png) [@isaacsas](https://discourse.julialang.org/u/isaacsas)\
**Post date:** [June 10, 2020, 10:27pm UTC](https://discourse.julialang.org/t/variable-rate-jumps-with-differentialequations-jl-and-matrices/40996/5 "2020-06-10T22:27:01Z")

</div>

What does it mean to have a complex valued rate in a jump process? I’m not sure any of the jump methods we have will work with complex rates…

---

<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:** [June 11, 2020, 12:57am UTC](https://discourse.julialang.org/t/variable-rate-jumps-with-differentialequations-jl-and-matrices/40996/6 "2020-06-11T00:57:44Z")

</div>

Yeah, I think that’s the issue, Maybe norm(u[1] + u[2]) or something would work, but I am not sure what it means to have a complex value as the rate for a jump process.

---

<div class="post-metadata">

**Author:** ![TTolppanen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ttolppanen/32/12568_2.png) [@TTolppanen](https://discourse.julialang.org/u/TTolppanen)\
**Post date:** [June 11, 2020, 7:10am UTC](https://discourse.julialang.org/t/variable-rate-jumps-with-differentialequations-jl-and-matrices/40996/7 "2020-06-11T07:10:24Z")

</div>

My bad! The rate should be real. But changing to ` norm(u[1] + u[2]) ` did not seem to fix the issues. So to be clear both of these do not work

```julia
function f(du, u, p, t)
    du .= u
end

prob = ODEProblem(f, Complex{Float64}[1.0 2.0; 3.0 4.0], (0.0, 1.0))
rate(u,p,t) = norm(u[1] + u[2])
affect!(integrator) = (integrator.u[1] = complex(1.0); integrator.u[2] = complex(2.0);
                       integrator.u[3] = complex(3.0); integrator.u[4] = complex(4.0))
jump = VariableRateJump(rate, affect!)
jump_prob = JumpProblem(prob, Direct(), jump)
sol = solve(jump_prob)
```

and

```julia
function f(du, u, p, t)
    du .= u
end

prob = ODEProblem(f, [1.0 2.0; 3.0 4.0], (0.0, 1.0))
rate(u,p,t) = u[1] + u[2]
affect!(integrator) = (integrator.u = [1.0 2.0; 3.0 4.0])
jump = VariableRateJump(rate, affect!)
jump_prob = JumpProblem(prob, Direct(), jump)
sol = solve(jump_prob)
```

but they do work for `ConstantRateJump()`.
