Krastanov:
Hence my question: For a completely arbitrary type (that permits 1D indexing and has an eltype
of some reasonable scalar type, but does not subclass AbstractArray
), what is the officially required interface for it to work with DifferentialEquations.jl
? Or are we advised to not use completely custom objects?
It depends on the solver. With OrdinaryDiffEq, you just need a valid broadcast. Here’s a tested type which doesn’t even have indexing and works with non-stiff solvers in OrdinaryDiffEq:
struct NoIndexArray{T, N} <: AbstractArray{T, N}
x::Array{T, N}
end
Base.size(x::NoIndexArray) = size(x.x)
Base.axes(x::NoIndexArray) = axes(x.x)
function Base.similar(x::NoIndexArray, dims::Union{Integer, AbstractUnitRange}...)
NoIndexArray(similar(x.x, dims...))
end
Base.copyto!(x::NoIndexArray, y::NoIndexArray) = NoIndexArray(copyto!(x.x, y.x))
Base.copy(x::NoIndexArray) = NoIndexArray(copy(x.x))
Base.zero(x::NoIndexArray) = NoIndexArray(zero(x.x))
Base.fill!(x::NoIndexArray, y) = NoIndexArray(fill!(x.x, y))
Base.mapreduce(f, op, x::NoIndexArray; kwargs...) = mapreduce(f, op, x.x; kwargs...)
Base.any(f::Function, x::NoIndexArray; kwargs...) = any(f, x.x; kwargs...)
Base.all(f::Function, x::NoIndexArray; kwargs...) = all(f, x.x; kwargs...)
Base.:(==)(x::NoIndexArray, y::NoIndexArray) = x.x == y.x
struct NoIndexStyle{N} <: Broadcast.AbstractArrayStyle{N} end
NoIndexStyle(::Val{N}) where {N} = NoIndexStyle{N}()
NoIndexStyle{M}(::Val{N}) where {N, M} = NoIndexStyle{N}()
This file has been truncated. show original
For implicit methods you need linear algebraic operations.
2 Likes