# Is preallocation possible for repeated calculation of trajectory of a discrete model (DynamicalSystems.jl)?

**URL:** <https://discourse.julialang.org/t/is-preallocation-possible-for-repeated-calculation-of-trajectory-of-a-discrete-model-dynamicalsystems-jl/67147>\
**Category:** General Usage\
**Tags:** question\
**Created:** [August 27, 2021, 7:37pm UTC](https://discourse.julialang.org/t/is-preallocation-possible-for-repeated-calculation-of-trajectory-of-a-discrete-model-dynamicalsystems-jl/67147 "2021-08-27T19:37:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![MathGuy](https://avatars.discourse-cdn.com/v4/letter/m/58f4c7/32.png) [@MathGuy](https://discourse.julialang.org/u/MathGuy)\
**Post date:** [August 27, 2021, 7:37pm UTC](https://discourse.julialang.org/t/is-preallocation-possible-for-repeated-calculation-of-trajectory-of-a-discrete-model-dynamicalsystems-jl/67147/1 "2021-08-27T19:37:22Z")

</div>

I need to analyze the following discrete model in r-k parametric space,

```julia
function test_difference_eq(x,par,n)
   r, k = par
   a = 5.0; mu = 0.5; d = 0.2;
   dx = x[1]*exp(r*(1-x[1]/k)-x[2]/(a+x[1]^2))
   dy = x[2]*exp(mu*x[1]/(a+x[1]^2)-d)
   return @SVector [dx, dy]
end

```

I used `DynamicalSystems.jl` and calculated the trajectories inside two for loops (nested):

```julia
for i = 1:xpts         
     for j = 1:ypts
          set_parameter!(ds, [par1[i], par2[j]])
          tr = trajectory(ds, NIter; Ttr=NTr)
          # do the analysis here on tr[:,1] 
          # for example, period = seqper(tr[:,1])
     end
end

```

Can `tr` be preallocated? Also `tr[:,1]` makes its own allocation. `@view tr[:,1] `doesn’t work with Dataset used in `DynamicalSystems.jl`

Any help?

Thanks in advance.

Full code can be found [here](https://discourse.julialang.org/t/solving-difference-equation-part-2/67057).

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [August 27, 2021, 7:43pm UTC](https://discourse.julialang.org/t/is-preallocation-possible-for-repeated-calculation-of-trajectory-of-a-discrete-model-dynamicalsystems-jl/67147/2 "2021-08-27T19:43:46Z")

</div>

> [@MathGuy](#):
>
> DynamicalSystems.jl

> <https://github.com/JuliaDynamics/DynamicalSystemsBase.jl/blob/e65516929ee4ed96185ee3db78a28dc8acc91ba4/src/core/continuous.jl#L162>

ask people who maintain this package

---

<div class="post-metadata">

**Author:** ![MathGuy](https://avatars.discourse-cdn.com/v4/letter/m/58f4c7/32.png) [@MathGuy](https://discourse.julialang.org/u/MathGuy)\
**Post date:** [August 28, 2021, 5:58am UTC](https://discourse.julialang.org/t/is-preallocation-possible-for-repeated-calculation-of-trajectory-of-a-discrete-model-dynamicalsystems-jl/67147/3 "2021-08-28T05:58:39Z")

</div>

> [@jling](#):
>
> ask people who maintain this package

I don’t know where should I ask. Can anyone show me?

Alternatively, can I extend the package by adding another version of trajectory that accepts an array for solution and then modifies it, Will it be helpful?

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [August 28, 2021, 6:26am UTC](https://discourse.julialang.org/t/is-preallocation-possible-for-repeated-calculation-of-trajectory-of-a-discrete-model-dynamicalsystems-jl/67147/4 "2021-08-28T06:26:58Z")

</div>

> [@MathGuy](#):
>
> I don’t know where should I ask. Can anyone show me?

For packages located on github, you can usually open an issue on their repository (found [here](https://github.com/JuliaDynamics/DynamicalSystemsBase.jl/issues)) and in it ask about adding a function `trajectory!` that takes a preallocated vector as input.

> [@MathGuy](#):
>
> Alternatively, can I extend the package by adding another version of trajectory that accepts an array for solution and then modifies it, Will it be helpful?

Yeah, that should be possible by defining `DynamicalSystems.trajectory!`. If you add that function to the issue (or directly create a PR, with it being added in the appropriate place) it will probably be appreciated by the maintainers.
