# Type assertion?

**URL:** <https://discourse.julialang.org/t/type-assertion/35950>\
**Category:** New to Julia\
**Created:** [March 13, 2020, 10:42pm UTC](https://discourse.julialang.org/t/type-assertion/35950 "2020-03-13T22:42:19Z")\
**Posts on this page:** 6\
**Page:** 2

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [March 15, 2020, 5:53pm UTC](https://discourse.julialang.org/t/type-assertion/35950/21 "2020-03-15T17:53:58Z")

</div>

I’ll look into that. My intended use is to, e.g., convert results from DifferentialEquations.jl solvers, say `sol`, to something that is easier to plot, i.e., schematically:

```julia
Sol = vec2vec(sol[:])
plot(sol.t,Sol[1])

```

instead of

```julia
plot(sol,vars=(0,1))

```

for cases when the latter is unsuitable.  
Perhaps this case requires vectors that are too big for “static vectors”?

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [March 15, 2020, 6:32pm UTC](https://discourse.julialang.org/t/type-assertion/35950/22 "2020-03-15T18:32:35Z")

</div>

I expect there’s a better way to do this. cc @ChrisRackauckas

---

<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:** [March 15, 2020, 7:18pm UTC](https://discourse.julialang.org/t/type-assertion/35950/23 "2020-03-15T19:18:05Z")

</div>

> [@BLI](#):
>
> Sol = vec2vec(sol[:]); plot(sol.t,Sol[1])

that’s not the right slicing:

```julia
plot(sol.t,sol[:,1]

```

does this.

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [March 15, 2020, 8:51pm UTC](https://discourse.julialang.org/t/type-assertion/35950/24 "2020-03-15T20:51:11Z")

</div>

That’s because `sol` is a special data structure (I think it should be `plot(sol.t,sol[1,:])` ).

In my case, I have played with 2 (or 3?) utility functions:

```julia
# Utility functions
# -- map solution function to output
function sol_2_out(sol,out,time=sol.t)
    return [out(s) for s in sol.(time)]
end
# -- convert vector of samples to vector of timeseries
equal_lengths(v) = all(x->length(x)==length(first(v)), v)
#
function vec2vec(v)
    equal_lengths(v) || error("The element vectors must have equal lengths.")
    return [getindex.(v,i) for i in firstindex(v[1]):lastindex(v[1])]
end
;

```

This makes it simple to compute algebraic combinations of the solution of differential equations (without using DAE solvers):

```julia
# Output
out = s -> [s[1] + s[2],s[1]*s[2]]
#
y = sol_2_out(sol,out) |> vec2vec;
#
plot(sol.t,y)

```

Here, `y` doesn’t have the special data structure that `sol` has. If I want to use another sampling rate:

```julia
tm = range(0.,10,length=10)
y = sol_2_out(sol,out,tm) |> vec2vec
plot(tm,y)

```

This is convenient for, e.g., control engineers who want to plot the feedback control signal or an output, or if one wants to know, e.g., the reaction rate in a chemical reactor where the rate is a function of the states.

Maybe there is a simpler way to do this.

---

<div class="post-metadata">

**Author:** ![samusz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samusz/32/7972_2.png) [@samusz](https://discourse.julialang.org/u/samusz)\
**Post date:** [June 11, 2020, 12:08pm UTC](https://discourse.julialang.org/t/type-assertion/35950/25 "2020-06-11T12:08:26Z")

</div>

```julia
for i in 1:lastindex(v[1])]

```

Wouldn’t

```julia
for i in eachindex(v) 

```

be better style (for beginner, for performance) ?

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [June 18, 2020, 12:51pm UTC](https://discourse.julialang.org/t/type-assertion/35950/26 "2020-06-18T12:51:48Z")

</div>

I think the two loops do different things. The first iterate the indexes of the _inner_ array inside the first element of `v`, the second iterate the indexes of `v`. Did you mean:

```julia
for i in eachindex(first(v))

```

?

[Previous page](https://discourse.julialang.org/t/type-assertion/35950.md?page=1)
