# Resizing ODE integrator of type matrix

**URL:** <https://discourse.julialang.org/t/resizing-ode-integrator-of-type-matrix/126000>\
**Category:** General Usage\
**Tags:** differentialequation\
**Created:** [February 17, 2025, 4:02pm UTC](https://discourse.julialang.org/t/resizing-ode-integrator-of-type-matrix/126000 "2025-02-17T16:02:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![B4rtel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/b4rtel/32/214171_2.png) [@B4rtel](https://discourse.julialang.org/u/B4rtel)\
**Post date:** [February 17, 2025, 4:02pm UTC](https://discourse.julialang.org/t/resizing-ode-integrator-of-type-matrix/126000/1 "2025-02-17T16:02:41Z")

</div>

I’m trying to resize the integrator in and ODE solved with `solve` where `u` is a matrix. I also found a post similar to this ([Resizing ODE integrator with matrix or VectorOfArray initial condition](https://discourse.julialang.org/t/resizing-ode-integrator-with-matrix-or-vectorofarray-initial-condition/89583)) but I want to do it with a Matrix instead of an `ElasticArray`. I’d like to fix the code

```julia
using DifferentialEquations
function odefun!(du, u, p, t)
    du[:, 1] .= rand(2)
    du[:, 2] .= rand(2)
    du[:, 3] .= rand(2)
    du[:, 4] .= rand(2)
    nothing 
end
u0 = [0.0 0.0 0.0 0.0; 0.0 0.0 0.0 0.0]
tspan=(0.0,1.0)
prob=ODEProblem(odefun!, u0, tspan)
integrator = init(prob,Tsit5())
resize!(integrator, (2,3))

```

Unfortunately, when using `resize!(integrator, (2,3))` I get the error

```julia
ERROR: MethodError: no method matching resize!(::Matrix{Float64}, ::Tuple{Int64, Int64})
Closest candidates are:
  resize!(::OrdinaryDiffEq.AbstractNLSolver, ::Any, ::Int64)
   @ OrdinaryDiffEq ~/.julia/packages/OrdinaryDiffEq/Y6bIX/src/nlsolve/utils.jl:453
  resize!(::OrdinaryDiffEq.AbstractNLSolverCache, ::Any, ::Any, ::Int64)
   @ OrdinaryDiffEq ~/.julia/packages/OrdinaryDiffEq/Y6bIX/src/nlsolve/utils.jl:462
  resize!(::StochasticDiffEq.SDEIntegrator, ::Any, ::Any)
   @ StochasticDiffEq ~/.julia/packages/StochasticDiffEq/aR0ZE/src/integrators/integrator_interface.jl:91

```

and when changing it to `resize!(integrator, 3)` I get the error

```julia
ERROR: MethodError: no method matching resize!(::Matrix{Float64}, ::Int64)
Closest candidates are:
  resize!(::BitVector, ::Integer)
   @ Base bitarray.jl:814
  resize!(::LoopVectorization.LoopOrder, ::Int64)
   @ LoopVectorization ~/.julia/packages/LoopVectorization/tIJUA/src/modeling/graphs.jl:439
  resize!(::Vector, ::Integer)
   @ Base array.jl:1312

```

Do you have any idea how to fix it?

---

<div class="post-metadata">

**Author:** ![DanielVandH](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielvandh/32/31134_2.png) [@DanielVandH](https://discourse.julialang.org/u/DanielVandH)\
**Post date:** [February 17, 2025, 4:35pm UTC](https://discourse.julialang.org/t/resizing-ode-integrator-of-type-matrix/126000/2 "2025-02-17T16:35:04Z")

</div>

I used `ElasticMatrix` in that thread exactly so that `resize!` works - `Matrix` isn’t mutable. Do you have a particular reason to want to only use `Matrix`?

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [February 17, 2025, 4:35pm UTC](https://discourse.julialang.org/t/resizing-ode-integrator-of-type-matrix/126000/3 "2025-02-17T16:35:31Z")

</div>

`Array{T,N}` where `N` is not 1 cannot currently be resized (in-place); `resize!` is even documented in Base for `Vector` in particular. ElasticArrays.jl exists exactly to implement resizing along the last dimension, which is the only efficient dimension for resizing column-major arrays.

---

<div class="post-metadata">

**Author:** ![B4rtel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/b4rtel/32/214171_2.png) [@B4rtel](https://discourse.julialang.org/u/B4rtel)\
**Post date:** [February 17, 2025, 4:54pm UTC](https://discourse.julialang.org/t/resizing-ode-integrator-of-type-matrix/126000/4 "2025-02-17T16:54:58Z")

</div>

I wanted to keep the Type the same for the following computations. But since apparently it’s not possible with `Matrix` I changed it now to `ElasticMatrix` which works 🙂

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [February 17, 2025, 5:02pm UTC](https://discourse.julialang.org/t/resizing-ode-integrator-of-type-matrix/126000/5 "2025-02-17T17:02:42Z")

</div>

It’s not _currently_ implemented. It’s hypothetically possible to expand and move the underlying linear buffer (currently `Memory`) for higher dimension `Array`s like `Vector`s do now. Before the `Memory` implementation change, this was brought up but rejected for inhibiting compiler optimizations that rely on the linear buffer to stay in the same place and thus the same size. It was not explicitly explained if `Vector`s lose those optimizations.

[resize! for multidimensional arrays · Issue #37900 · JuliaLang/julia](https://github.com/JuliaLang/julia/issues/37900)
