Skip to content

Commit 94b6cd5

Browse files
committed
Broadcasting on CompositionArrays returns CompositionArrays
1 parent 9448c31 commit 94b6cd5

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

src/utilities/CompositionArrays.jl

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ CompositionArray{C}(args...; kwargs...) where {C<:AbstractComposition} = Composi
88
CompositionArray(d::Dict) = CompositionArray(TupleDataset(d))
99
CompositionArray{C}(d::Dict) where {C<:AbstractComposition} = CompositionArray{C}(TupleDataset(d))
1010
# Convert CompositionArrays to other CompositionArrays
11-
CompositionArray{C}(a::CompositionArray) where {C<:AbstractComposition} = CompositionArray(C.(a))
11+
CompositionArray(a::CompositionArray) = a
12+
CompositionArray{C}(a::CompositionArray) where {C<:AbstractComposition} = C.(a)
1213
export CompositionArray
1314

1415
# Type aliases for vectors and matrices
@@ -54,10 +55,10 @@ Base.sizehint!(A::CompositionArray, i::Integer) = sizehint!(getfield(A, :data))
5455
Base.copy(A::CompositionArray) = CompositionArray(copy(getfield(A, :data)))
5556

5657
# # Broadcasting interface (enable this if we want broadcasting on CompositionArrays to return CompositionArrays)
57-
# struct CompositionArrayStyle <: Broadcast.BroadcastStyle end
58-
# Broadcast.BroadcastStyle(::Type{CompositionArray}) = CompositionArrayStyle()
59-
# Broadcast.BroadcastStyle(::CompositionArrayStyle, ::Broadcast.DefaultArrayStyle) = CompositionArrayStyle()
60-
# Base.similar(bc::Base.Broadcast.Broadcasted{CompositionArrayStyle}, ::Type{T}) where {T} = similar(CompositionArray{T}, axes(bc))
58+
struct CompositionArrayStyle <: Base.Broadcast.BroadcastStyle end
59+
Base.Broadcast.BroadcastStyle(::Type{<:CompositionArray}) = CompositionArrayStyle()
60+
Base.Broadcast.BroadcastStyle(::CompositionArrayStyle, ::Base.Broadcast.DefaultArrayStyle) = CompositionArrayStyle()
61+
Base.similar(bc::Base.Broadcast.Broadcasted{CompositionArrayStyle}, ::Type{T}) where {T} = similar(CompositionArray{T}, axes(bc))
6162

6263
# Other Base functions
6364
function Base.isapprox(x::CompositionArray, y::CompositionArray; kwargs...)

test/testComposition.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,8 @@ renormalize!(ca; anhydrous=true)
300300
@test ca[1] + ca[1] 2 * ca[1]
301301
@test (ca[1]*ca[1])/ca[1] ca[1]
302302
# Test dot-broadcasting on compositions / compositionarrays
303+
@test (ca .+ ca)::CompositionVector{NCKFMASHTOlogtrace{Float64}} (2 .* ca)::CompositionVector{NCKFMASHTOlogtrace{Float64}}
304+
@test ((ca .* ca)./ca)::CompositionVector{NCKFMASHTOlogtrace{Float64}} ca
303305
@test CompositionArray(ca .+ ca) CompositionArray(2 .* ca)
304306
@test CompositionArray((ca .* ca)./ca) ca
305307

0 commit comments

Comments
 (0)