ControlSystems.jl icon indicating copy to clipboard operation
ControlSystems.jl copied to clipboard

Use of promote_op problematic

Open baggepinnen opened this issue 3 years ago • 3 comments

See discussion in https://github.com/JuliaLang/julia/issues/38436#issuecomment-730200157

We should try to rid ourselves of the use of promote_op to avoid future situations like this.

baggepinnen avatar Nov 19 '20 09:11 baggepinnen

I agree that we should fix this. It should be fairly straight forward if we use one(T) and zero(T) to compute the promotion type instead. However, the following in promotion.jl will be trickier

function Base.promote_rule(::Type{StateSpace{TE, T1, MT}}, ::Type{T2}) where {TE, T1, MT, T2<:Number}
    NewMT = Base.promote_op(*, MT, T2)
    StateSpace{TE, eltype(NewMT), NewMT}
end

mfalt avatar Nov 20 '20 14:11 mfalt

Didn't we say that we should get rid of the Matrix type argument? Which would resolve this?

olof3 avatar Nov 20 '20 14:11 olof3

I assume we would have a similar problem for the corresponding HeteroStateSpace, but we seem to avoid defining promote rules with numbers by calling the constructors in those cases, so MAYBE that is the better workaround. But we seem to be inconsistent/wrong with the current implementations, e.g in types/promotion.jl

*(sys::ST, n::Number) where ST <: AbstractStateSpace = StateSpace(sys.A, sys.B, sys.C*n, sys.D*n, sys.timeevol)
/(sys::ST, n::Number) where ST <: AbstractStateSpace = ST(sys.A, sys.B, sys.C/n, sys.D/n, sys.timeevol)

and I can't see directly if we catch the matrix case anywhere.

mfalt avatar Nov 20 '20 14:11 mfalt