# Operations on output from solve()

**URL:** <https://discourse.julialang.org/t/operations-on-output-from-solve/40312>\
**Category:** New to Julia\
**Created:** [May 28, 2020, 2:58am UTC](https://discourse.julialang.org/t/operations-on-output-from-solve/40312 "2020-05-28T02:58:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![erlebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erlebach/32/12973_2.png) [@erlebach](https://discourse.julialang.org/u/erlebach)\
**Post date:** [May 28, 2020, 2:58am UTC](https://discourse.julialang.org/t/operations-on-output-from-solve/40312/1 "2020-05-28T02:58:46Z")

</div>

I am running a simple jump\_process. Here is some code (which is not runnable, but works). The details are not important.

```julia
        prob = DiscreteProblem(u0,tspan,params)
	prob_jump = JumpProblem(prob, Direct(), infection_jump, recovery_jump)
	sol_jump = solve(prob_jump, FunctionMap());
	out_jump = sol_jump(t);

```

out\_jump has type of

```julia
DiffEqArray{Int64,2,Array{Array{Int64,1},1},StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}}

```

What is interesting is that I compute 100 different solutions and wish to average them. So I added up out\_jump and divided by the number of runs. And it worked! Examining further, I found that the following operation:

```julia
s = out_jump + out_jump

```

result in s having type `Array{Int64,2}` and the time component is not affected. This is what I wanted, but it seems strange to me. This change of type was unexpected. Is this a feature?

Finally, what is the best way to compute the average of many runs performed within the DifferentialEquations.jl package?

Thanks. Gordon.

---

<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:** [May 28, 2020, 1:58pm UTC](https://discourse.julialang.org/t/operations-on-output-from-solve/40312/2 "2020-05-28T13:58:38Z")

</div>

> [@erlebach](#):
>
> This change of type was unexpected. Is this a feature?

Yes, the `DESolution <: DiffEqArray <: AbstractVectorOfArray <: AbstractArray`, so it inherits the capabilities of acting like an array. That’s very tersely described here: [https://diffeq.sciml.ai/latest/basics/solution/](https://diffeq.sciml.ai/latest/basics/solution/) .

So you can compute a mean like `mean(sol,dims=2)` to average over time, since it just behave like an array so “things that want arrays” will work on the solution type. It also behaves as a table, so `DataFrame(sol)` and things like statistical operations will work.

If you want to do a bunch of analyses over many trajectories though, you might want to look at the ensemble interface:

[https://diffeq.sciml.ai/latest/features/ensemble/](https://diffeq.sciml.ai/latest/features/ensemble/)

---

<div class="post-metadata">

**Author:** ![erlebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erlebach/32/12973_2.png) [@erlebach](https://discourse.julialang.org/u/erlebach)\
**Post date:** [May 28, 2020, 2:09pm UTC](https://discourse.julialang.org/t/operations-on-output-from-solve/40312/3 "2020-05-28T14:09:19Z")

</div>

Great! I will look into these possibilities.

What is the best way to find out the type hierarchy of a particular type?

---

<div class="post-metadata">

**Author:** ![erlebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erlebach/32/12973_2.png) [@erlebach](https://discourse.julialang.org/u/erlebach)\
**Post date:** [May 28, 2020, 3:09pm UTC](https://discourse.julialang.org/t/operations-on-output-from-solve/40312/4 "2020-05-28T15:09:08Z")

</div>

Chris,

Quick question. Looking through the source code of various package, I notice that Julia style is radically different than most other languages, mostly because of the dispatch algorithms. Also, various functions are defined where the function name is a structure type listed in parentheses.

Where can I find a tutorial (or anything) that explains how to construct package in the “correct” style? Of course, I can write my own module, but that does note mean it will be done the Julia way. Surely the thought process is explained somewhere?

Thanks, as usual.

---

<div class="post-metadata">

**Author:** ![erlebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erlebach/32/12973_2.png) [@erlebach](https://discourse.julialang.org/u/erlebach)\
**Post date:** [May 28, 2020, 3:31pm UTC](https://discourse.julialang.org/t/operations-on-output-from-solve/40312/5 "2020-05-28T15:31:10Z")

</div>

Chris,

I loaded DifferentialEquations.jl ,and then listed all the subtypes of AbstractArray.  
DiffEqArray was not listed (I am in REPL within Atom). However, when I type:  
`supertype(DiffEqArray)`, the result is  
`AbstractDiffEqArray{T,N,A} where A where N where T`  
which is also not listed when using the `subtypes` command.

It would seem that what subtypes lists is dependent on the modules loaded. Is that the case? If so, then it is hard to see how operations on the subtype would work if the chain up to AbstractArray depends on the modules loaded. Obviously, dealing with types is rather complex.

Gordon

---

<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:** [May 28, 2020, 3:32pm UTC](https://discourse.julialang.org/t/operations-on-output-from-solve/40312/6 "2020-05-28T15:32:14Z")

</div>

> [@erlebach](#):
>
> What is the best way to find out the type hierarchy of a particular type?

`supertype(typeof(x))`, and keep hopping.

> [@erlebach](#):
>
> Where can I find a tutorial (or anything) that explains how to construct package in the “correct” style?

There’s a few styles around. I usually point to my video to first show the workflow:

[![](https://global.discourse-cdn.com/julialang/original/3X/a/9/a9eed041bdd2be4339d3e6dd3961b86bdc1511f3.jpeg "Developing Julia Packages") ](https://www.youtube.com/watch?v=QVmU29rCjaA)

and it demonstrates a few standard things you’ll see, like `export`s and `include` all at the top level file, the standard way to name variables, modules, functions, etc. But if you want more directly into the exact styles, there are three style guides that I know of:

- [Julia’s Style Guide](https://docs.julialang.org/en/latest/manual/style-guide/)
- [BlueStyle](https://github.com/invenia/BlueStyle)
- [YASGuide](https://github.com/jrevels/YASGuide)

They are more similar than different.

---

<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:** [May 28, 2020, 3:33pm UTC](https://discourse.julialang.org/t/operations-on-output-from-solve/40312/7 "2020-05-28T15:33:32Z")

</div>

> [@erlebach](#):
>
> It would seem that what subtypes lists is dependent on the modules loaded. Is that the case? If so, then it is hard to see how operations on the subtype would work if the chain up to AbstractArray depends on the modules loaded. Obviously, dealing with types is rather complex.

Yes, new packages can add subtypes to inherit their dispatch behaviors.
