JuliaSymbolics / Symbolics.jl

Symbolic programming for the next generation of numerical software

Home Page:https://symbolics.juliasymbolics.org/stable/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Array function registration doesn't overload promote types correctly

YingboMa opened this issue · comments

MWE:

using Symbolics
@register_array_symbolic bar(x::AbstractVector, p::AbstractMatrix) begin
    size = size(x)
    eltype = promote_type(eltype(x), eltype(p))
end
@variables x[1:3] p[1:3, 1:3];
ex = bar(x, p)
ex2 = similarterm(Symbolics.unwrap(ex), bar, [Symbolics.unwrap(x), Symbolics.unwrap(p)]; metadata=Symbolics.metadata(ex))
typeof(ex2)

outputs

julia> typeof(ex2)
SymbolicUtils.BasicSymbolic{Any}

@AayushSabharwal can you take a look at this?

It looks like @register_array_symbolic doesn't overload promote types at all, which is not ideal

promote_symtype is difficult. The best that seems to be possible is container_type{etype} where container_type is either specified in the macrocall or defaults to promote_atype(f, args...) and etype is the eltype in the macrocall or promote_eltype(f, args...). The dimensionality cannot be generically inferred, since promote_symtype only gets the types of arguments as input so it can't run the size provided in the macrocall, and relying in promote_ndims would break every existing @register_array_symbolic since it errors by default.