Hi,
I’m trying to define promotion rules for a type representing multicomplex numbers (Multicomplex number - Wikipedia). These have a series of complex numbers, im1
, im2
, etc. Internally, these are represented as static arrays of size 2^N:
using StaticArrays
struct Multicomplex{T,N,C} <: Number
value::SVector{C,T}
end
im1 = Multicomplex{Bool,1,2}(SVector{2}(false, true))
im2 = Multicomplex{Bool,2,4}(SVector{4}(false, false, true, false))
I’m trying to define a promotion rule so that Multicomplex numbers (e.g. im1
) get promoted to larger sizes when needed in calculations:
promote_rule(::Type{<:Multicomplex{T,N1,C1}},
::Type{<:Multicomplex{S,N2,C2}}) where
{T<:Real,S<:Real,N1,C1,N2,C2} =
Multicomplex{promote_type(T,S),maximum(N1,N2),maximum(C1,C2)}
# constructors are also defined for conversion, e.g.
#Multicomplex{T,2,4}(m::Multicomplex{S,1,2}) where {T,S} =
# Multicomplex{2}(SVector{4,T}(m.value[1], m.value[2], zero(T), zero(T)))
However, this rule doesn’t seem to work, as type parameters are just dropped when they don’t match in promote_type
:
julia> promote_type(Multicomplex{Float64,1,2}, Multicomplex{Float64,1,2})
Multicomplex{Float64, 1, 2}
julia> promote_type(Multicomplex{Float64,1,2}, Multicomplex{Float64,1,4})
Multicomplex{Float64, 1}
julia> promote_type(Multicomplex{Float64,1,2}, Multicomplex{Float64,2,4})
Multicomplex{Float64}
julia> promote_type(Multicomplex{Float64,1,2}, Multicomplex{Bool,2,4})
Multicomplex
Any advice would be appreciated. Thank you!