# Inconsistent typing making adding arrays and adding a vector to the end of an array difficult

**URL:** <https://discourse.julialang.org/t/inconsistent-typing-making-adding-arrays-and-adding-a-vector-to-the-end-of-an-array-difficult/47662>\
**Category:** General Usage\
**Tags:** question, ode\
**Created:** [October 2, 2020, 8:15pm UTC](https://discourse.julialang.org/t/inconsistent-typing-making-adding-arrays-and-adding-a-vector-to-the-end-of-an-array-difficult/47662 "2020-10-02T20:15:46Z")\
**Posts on this page:** 8\
**Page:** 2

<div class="post-metadata">

**Author:** ![fusion809](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fusion809/32/6080_2.png) [@fusion809](https://discourse.julialang.org/u/fusion809)\
**Post date:** [October 4, 2020, 11:43am UTC](https://discourse.julialang.org/t/inconsistent-typing-making-adding-arrays-and-adding-a-vector-to-the-end-of-an-array-difficult/47662/21 "2020-10-04T11:43:42Z")

</div>

I am in awe of how immensely helpful you’ve been, Rafael, thank you! While your code cuts down execution time on my system, it doesn’t quite go down to 0.03s, but ~0.18s is still pretty good, significantly faster than my JS code (which takes about 0.77s).

---

<div class="post-metadata">

**Author:** ![fusion809](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fusion809/32/6080_2.png) [@fusion809](https://discourse.julialang.org/u/fusion809)\
**Post date:** [October 4, 2020, 12:11pm UTC](https://discourse.julialang.org/t/inconsistent-typing-making-adding-arrays-and-adding-a-vector-to-the-end-of-an-array-difficult/47662/22 "2020-10-04T12:11:28Z")

</div>

As RKF45 is meant to solve ODE systems of any size, conds should probably be generalized (i.e. not have its size fixed at 3 elements long). I’ve tried changing to `conds::SVector{Float64}`, but that gives an error (namely:

```julia
MethodError: no method matching rkf45(::typeof(Lorenz), ::NamedTuple{(:sigma, :beta, :rho),Tuple{Float64,Float64,Float64}}, ::Float64, ::Float64, ::SArray{Tuple{3},Float64,1,3}, ::Float64, ::Float64)

```

). Any ideas on how to resolve this problem?

---

<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:** [October 4, 2020, 12:40pm UTC](https://discourse.julialang.org/t/inconsistent-typing-making-adding-arrays-and-adding-a-vector-to-the-end-of-an-array-difficult/47662/23 "2020-10-04T12:40:44Z")

</div>

StaticArrays is a package that makes the size of the array available to the compiler for optimizations. `SVector{3, UInt}` means “This vector holds 3 UInt”. There’s no need to restrict the argument to that though (it doesn’t allow for more optimizations, the performance will depend on the types of the objects passed in, not on the types of the assertions) - if you remove the type assertions from the function arguments and simply pass in objects of the type you want (SVector in this case), the function will be compiled optimized for the types that are passed in.

So if you remove the type assertion and simply make `conds = @Svector [x0, y0, z0, w0]`, it should work fine for the 4 dimensional case.

---

<div class="post-metadata">

**Author:** ![fusion809](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fusion809/32/6080_2.png) [@fusion809](https://discourse.julialang.org/u/fusion809)\
**Post date:** [October 4, 2020, 12:42pm UTC](https://discourse.julialang.org/t/inconsistent-typing-making-adding-arrays-and-adding-a-vector-to-the-end-of-an-array-difficult/47662/24 "2020-10-04T12:42:47Z")

</div>

Ah, so for SVectors specifying the type in function definitions does not further optimize the performance of the function?

---

<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:** [October 4, 2020, 12:44pm UTC](https://discourse.julialang.org/t/inconsistent-typing-making-adding-arrays-and-adding-a-vector-to-the-end-of-an-array-difficult/47662/25 "2020-10-04T12:44:10Z")

</div>

Asserting function argument types doesn’t optimize anything, ever. It only restricts what types are allowed to be used to call the function at all and is used to disambiguate between different methods of the same function with the same arity (=number of arguments).

Note that there is an upper limit to the usefulness of SVectors - at some point, the large known size can increase compile times dramatically.

---

<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:** [October 4, 2020, 12:48pm UTC](https://discourse.julialang.org/t/inconsistent-typing-making-adding-arrays-and-adding-a-vector-to-the-end-of-an-array-difficult/47662/26 "2020-10-04T12:48:58Z")

</div>

> [@rafael.guerra](#):
>
> The edited code below will run in ~0.02 s (for some reason not on the first run but only in all subsequent ones).

`@time` includes compilation time on the first run. Try using `@btime` from BenchmarkTools.jl for somewhat similar functionality instead.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [October 4, 2020, 12:52pm UTC](https://discourse.julialang.org/t/inconsistent-typing-making-adding-arrays-and-adding-a-vector-to-the-end-of-an-array-difficult/47662/27 "2020-10-04T12:52:39Z")

</div>

Thank you Sukera.  
With @btime: 13.9 ms (39 allocations: 9.75 MiB)

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [October 4, 2020, 1:48pm UTC](https://discourse.julialang.org/t/inconsistent-typing-making-adding-arrays-and-adding-a-vector-to-the-end-of-an-array-difficult/47662/28 "2020-10-04T13:48:49Z")

</div>

Interestingly, Wikipedia ([Lorenz system - Wikipedia](https://en.wikipedia.org/wiki/Lorenz_system)) provides Julia code for this same Lorenz problem that uses [DifferentialEquations.jl](https://diffeq.sciml.ai/v3.0/).  
Fyi, this problem then seems to run 12x faster than the faster implementation above, with a @btime of 1.2 ms. If we are comparing apples to apples then there is a lot of room for improvement.  
See the code here for your reference:

```julia
using DifferentialEquations, ParameterizedFunctions, Plots
using BenchmarkTools

lorenz = @ode_def begin # define the system
 dx = σ * (y - x)
 dy = x * (ρ - z) - y
 dz = x * y - β*z
end σ ρ β

u0 = [1.0,1.0,1.0] # initial conditions
tspan = (0.0,60.0) # timespan
p = [10.0,28.0,8/3] # parameters

prob = ODEProblem(lorenz, u0, tspan, p) # define the problem
sol0 = solve(prob, reltol=1e-9) # solve it
# @btime solve(prob, reltol=1e-9)
plot(sol0, vars = (1, 2, 3))

```

[Previous page](https://discourse.julialang.org/t/inconsistent-typing-making-adding-arrays-and-adding-a-vector-to-the-end-of-an-array-difficult/47662.md?page=1)
