Skip to content

Commit

Permalink
Merge pull request #88 from sharanry/sy/indexing_error
Browse files Browse the repository at this point in the history
Throw error with invalid symbol indexing
  • Loading branch information
ChrisRackauckas authored Jul 20, 2021
2 parents a3a1ac6 + 23ce98d commit cac7ffa
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/solutions/solution_interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,17 @@ Base.@propagate_inbounds function Base.getindex(A::AbstractTimeseriesSolution,sy
i = sym
end

indepsym = getindepsym(A)
if i === nothing
indepsym = getindepsym(A)
if i === nothing
if issymbollike(sym) && indepsym !== nothing && Symbol(sym) == indepsym
A.t
else
observed(A,sym,:)
end
else
elseif i isa Base.Integer || i isa AbstractRange || i isa AbstractVector{<:Base.Integer}
A[i,:]
else
error("Invalid indexing of solution")
end
end

Expand All @@ -75,8 +77,10 @@ Base.@propagate_inbounds function Base.getindex(A::AbstractTimeseriesSolution,sy
else
observed(A,sym,args...)
end
else
elseif i isa Base.Integer || i isa AbstractRange || i isa AbstractVector{<:Base.Integer}
A[i,args...]
else
error("Invalid indexing of solution")
end
end

Expand Down Expand Up @@ -424,7 +428,7 @@ function interpret_vars(vars,sol,syms)
end
end

if typeof(vars) <: Integer
if typeof(vars) <: Base.Integer
vars = [(DEFAULT_PLOT_FUNC,0, vars)]
end

Expand Down
18 changes: 18 additions & 0 deletions test/downstream/symbol_indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,24 @@ tspan = (0.0,100.0)
prob = ODEProblem(sys_simplified,u0,tspan,p)
sol = solve(prob,Rodas4())

@test_throws UndefVarError sol[b]
@test_throws UndefVarError sol[b, 1]
@test_throws UndefVarError sol[b, 1:5]
@test_throws UndefVarError sol[b, [1,2,3]]
@test_throws ErrorException sol['a']
@test_throws ErrorException sol['a', 1]
@test_throws ErrorException sol['a', 1:5]
@test_throws ErrorException sol['a', [1,2,3]]

@test sol[a] isa AbstractVector
@test sol[a, 1] isa Real
@test sol[a, 1:5] isa AbstractVector
@test sol[a, [1,2,3]] isa AbstractVector

@test sol[1] isa AbstractVector
@test sol[1:2] isa AbstractArray
@test sol[[1,2]] isa AbstractArray

@test sol[lorenz1.x] isa Vector
@test sol[lorenz1.x,2] isa Float64
@test sol[lorenz1.x,:] isa Vector
Expand Down

0 comments on commit cac7ffa

Please sign in to comment.