Type assertion?

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:

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

instead of

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

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

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

that’s not the right slicing:

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

does this.

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:

# 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):

# 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:

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.

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

Wouldn’t

for i in eachindex(v) 

be better style (for beginner, for performance) ?

3 Likes

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:

for i in eachindex(first(v))

?

2 Likes