Skip to content

Commit a1cb849

Browse files
committed
ControlSystems compat part 2: Promote. Closes #87
1 parent fcf3316 commit a1cb849

File tree

4 files changed

+36
-24
lines changed

4 files changed

+36
-24
lines changed

src/broadcasting.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ function fill_flat(Ax1, Ax2, N1, N2)
104104
N = N1
105105
ax1, ax2 = Ax1, Ax2
106106
end
107-
Ax = promote.(getaxes(ax1), getaxes(ax2)) |> typeof
107+
Ax = Base.promote_typeof(getaxes(ax1), getaxes(ax2))
108108
return Ax, N
109109
end
110110
fill_flat(Ax::Type{<:VarAxes}, N) = fill_flat(getaxes(Ax), N) |> typeof

src/componentarray.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -273,13 +273,13 @@ julia> getaxes(ca)
273273
@inline getaxes(x::AdjOrTrans{T, <:ComponentVector}) where T = (FlatAxis(), getaxes(x.parent)[1])
274274
@inline getaxes(x::AdjOrTrans{T, <:ComponentMatrix}) where T = reverse(getaxes(x.parent))
275275

276-
@inline getaxes(::Type{<:ComponentArray{T,N,A,<:Axes}}) where {T,N,A,Axes} = map(x->x(), (Axes.types...,))
277-
@inline getaxes(::Type{<:AdjOrTrans{T,<:CA}}) where {T,CA<:ComponentVector} = (FlatAxis(), getaxes(CA)[1]) |> typeof
278-
@inline getaxes(::Type{<:AdjOrTrans{T,<:CA}}) where {T,CA<:ComponentMatrix} = reverse(getaxes(CA)) |> typeof
276+
@inline getaxes(::Type{<:ComponentArray{T,N,A,Axes}}) where {T,N,A,Axes} = map(x->x(), (Axes.types...,))
277+
@inline getaxes(::Type{<:AdjOrTrans{T,CA}}) where {T,CA<:ComponentVector} = (FlatAxis(), getaxes(CA)[1]) |> typeof
278+
@inline getaxes(::Type{<:AdjOrTrans{T,CA}}) where {T,CA<:ComponentMatrix} = reverse(getaxes(CA)) |> typeof
279279

280280
## Field access through these functions to reserve dot-getting for keys
281281
@inline getaxes(x::VarAxes) = getaxes(typeof(x))
282-
@inline getaxes(Ax::Type{<:Axes}) where {Axes<:VarAxes} = map(x->x(), (Ax.types...,))
282+
@inline getaxes(Ax::Type{Axes}) where {Axes<:VarAxes} = map(x->x(), (Ax.types...,))
283283

284284
getaxes(x) = ()
285285

src/similar_convert_copy.jl

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,15 @@ end
4949
Base.deepcopy(x::ComponentArray) = ComponentArray(deepcopy(getdata(x)), getaxes(x))
5050

5151

52-
Base.convert(::Type{CA}, A::AbstractArray) where CA<:ComponentArray = ComponentArray(A, getaxes(CA))
53-
Base.convert(::Type{CA}, x::ComponentArray) where CA<:ComponentArray = ComponentArray(getdata(x), getaxes(CA))
52+
function Base.convert(::Type{ComponentArray{T,N,AA,Ax}}, A::AbstractArray) where {T,N,AA,Ax}
53+
return ComponentArray{Ax}(A)
54+
end
55+
function Base.convert(::Type{ComponentArray{T,N,A,Ax1}}, x::ComponentArray{T,N,A,Ax2}) where {T,N,A,Ax1,Ax2}
56+
return x
57+
end
58+
function Base.convert(::Type{ComponentArray{T1,N,A1,Ax1}}, x::ComponentArray{T2,N,A2,Ax2}) where {T1,T2,N,A1,A2,Ax1,Ax2}
59+
return T1.(x)
60+
end
5461
Base.convert(::Type{<:Array}, x::ComponentArray) = convert(Array, getdata(x))
5562

5663

@@ -78,20 +85,14 @@ Base.NamedTuple(x::ComponentVector) = _namedtuple(x)
7885

7986

8087
## AbstractAxis conversion and promotion
81-
# Base.convert(TypeAx::Type{<:Ax1}, ::Ax2) where {Ax1<:AbstractAxis,Ax2<:AbstractAxis} = promote_type(TypeAx,Ax2)()
82-
# Base.convert(::Type{Ax1}, ax::Ax2) where {Ax1<:AbstractAxis,Ax2<:AbstractAxis} = Ax1()
83-
84-
# Could not figure out promote_rule for these.
85-
Base.promote(ax::AbstractAxis, ::NullAxis) = ax
86-
Base.promote(ax::AbstractAxis, ::FlatAxis) = ax
87-
Base.promote(::NullAxis, ax::AbstractAxis) = ax
88-
Base.promote(::FlatAxis, ax::AbstractAxis) = ax
89-
Base.promote(::FlatAxis, ::NullAxis) = FlatAxis()
90-
Base.promote(::NullAxis, ::FlatAxis) = FlatAxis()
91-
Base.promote(::FlatAxis, ::FlatAxis) = FlatAxis()
92-
Base.promote(::NullAxis, ::NullAxis) = NullAxis()
93-
Base.promote(ax::Ax, ::Ax) where {Ax<:AbstractAxis} = ax
94-
function Base.promote(ax1::AbstractAxis, ax2::AbstractAxis)
95-
@warn "Tried to promote dissimilar axes of types $(typeof(ax1)) and $(typeof(ax2)). Falling back to FlatAxis."
96-
return FlatAxis()
97-
end
88+
Base.convert(::Type{Ax}, ::AbstractAxis) where {Ax<:AbstractAxis} = Ax()
89+
Base.convert(::Type{Ax}, ax::AbstractAxis) where {Ax<:VarAxes} = convert.(typeof.(getaxes(Ax)), (ax,))
90+
91+
Base.promote_rule(AA::Type{<:Axis}, ::Type{<:NullAxis}) = AA
92+
Base.promote_rule(AA::Type{<:Axis}, ::Type{<:FlatAxis}) = AA
93+
Base.promote_rule(FA::Type{FlatAxis}, ::Type{NullAxis}) = FA
94+
Base.promote_rule(FA::Type{FlatAxis}, ::Type{FlatAxis}) = FA
95+
Base.promote_rule(NA::Type{NullAxis}, ::Type{NullAxis}) = NA
96+
function Base.promote_rule(A1::Type{<:VarAxes}, A2::Type{<:VarAxes})
97+
ax = first.(promote.(getaxes(A1), getaxes(A2))) |> typeof
98+
end

test/runtests.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,17 @@ end
206206
ca = ComponentVector(a=1, b=2, c=3)
207207
@test_throws BoundsError ca[:a, :b]
208208
end
209+
210+
# Issue # 87: Conversion/promotion
211+
let
212+
ax1 = Axis((; x1=1))
213+
ax2 = Axis((; x2=1))
214+
A1 = ComponentMatrix(zeros(1, 1), ax1, ax1)
215+
A2 = ComponentMatrix(zeros(1, 1), ax2, ax2)
216+
A = [A for A in [A1, A2]]
217+
@test A[1] == A1
218+
@test A[2] == A2
219+
end
209220
end
210221

211222
@testset "Set" begin

0 commit comments

Comments
 (0)