Skip to content

Fix symbol indexing by checking that observed !== nothing#241

Merged
ChrisRackauckas merged 4 commits into
SciML:masterfrom
chriselrod:fixsymindexing
Jan 10, 2023
Merged

Fix symbol indexing by checking that observed !== nothing#241
ChrisRackauckas merged 4 commits into
SciML:masterfrom
chriselrod:fixsymindexing

Conversation

@chriselrod

Copy link
Copy Markdown
Contributor

Fixes #240

@chriselrod

Copy link
Copy Markdown
Contributor Author

Sorry about the big diff. I have my editor auto-format files on save.
The change was just adding the A.observed !== nothing checks before dispatching to a method that calls it:

Base.@propagate_inbounds function Base.getindex(A::AbstractDiffEqArray{T, N},
                                                sym) where {T, N}
    if issymbollike(sym) && !isnothing(A.sc)
        if is_indep_sym(A.sc, sym)
            return A.t
        elseif is_state_sym(A.sc, sym)
            return getindex.(A.u, state_sym_to_index(A.sc, sym))
        elseif is_param_sym(A.sc, sym)
            return A.p[param_sym_to_index(A.sc, sym)]
        elseif A.observed !== nothing
            return observed(A, sym, :)
        end
    elseif all(issymbollike, sym) && !isnothing(A.sc)
        if all(Base.Fix1(is_param_sym, A.sc), sym)
            return getindex.((A,), sym)
        else
            return [getindex.((A,), sym, i) for i in eachindex(A.t)]
        end
    end
    return getindex.(A.u, sym)
end
Base.@propagate_inbounds function Base.getindex(A::AbstractDiffEqArray{T, N}, sym,
                                                args...) where {T, N}
    Main._a[] = (A, sym, args)
    if issymbollike(sym) && !isnothing(A.sc)
        if is_indep_sym(A.sc, sym)
            return A.t[args...]
        elseif is_state_sym(A.sc, sym)
            return A[sym][args...]
        elseif A.observed !== nothing
            return observed(A, sym, args...)
        end
    elseif all(issymbollike, sym) && !isnothing(A.sc)
        return reduce(vcat, map(s -> A[s, args...]', sym))
    end
    return getindex.(A.u, sym)
end

I could instead place such a check inside the observed function, in case people are meant to overload RecursiveArrayTools.observed.

@ChrisRackauckas
ChrisRackauckas merged commit 389c550 into SciML:master Jan 10, 2023
@chriselrod
chriselrod deleted the fixsymindexing branch January 10, 2023 13:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

getindex(::DiffEqArray, ::Symbol) is broken on recent releases

2 participants