Extending Symbolics.jl, How to dispatch all functions to the underlying Num

Hello, I want to extend Symbolics.jl Variables to encode bounds and default value of parameters in the “variable”; Currently something like this:

using Symbolics: _parse_vars, Num, @variables

import SciMLBase: issymbollike

import Base: +, -, *, /

struct Parameter
    var::Num
    lower::Real
    upper::Real
    default::Real
end

# Extended Functions:
Base.:+(p::Parameter, x::Any) = Base.:+(p.var, x)
Base.:-(p::Parameter, x::Any) = Base.:-(p.var, x)
Base.:/(p::Parameter, x::Any) = Base.:/(p.var, x)
Base.:*(p::Parameter, x::Any) = Base.:*(p.var, x)

issymbollike(::Parameter) = true

macro parameter(x, lower, upper, default)
    var = (@variables $x)[1]
    esc(Parameter(var, lower, upper, default))
end

function bounds(p::Parameter)
    return (p.lower, p.upper)
end

I want all functions (except a few pre-determined ones like bounds) to act directly on p.var without having to extend everything by hand like the Base.: block.

Maybe you could get some inspiration from here which already implemented bounds Symbolic Metadata · ModelingToolkit.jl (sciml.ai).

1 Like

That should do!

Though I may need “mandatory” metadata for my case, will see how it goes.