Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/arrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,13 @@ function Base.similar(bc::Broadcast.Broadcasted{Broadcast.ArrayStyle{QA}}, ::Typ
first_output::ElType = materialize_first(bc)
return QuantityArray(output_array, dimension(first_output)::dim_type(ElType), ElType)
end
function Base.similar(bc::Broadcast.Broadcasted{Broadcast.ArrayStyle{QuantityArray{T,N,D,Q,V}}}, ::Type{ElType}) where {T,N,D,Q,V<:Array{T,N},ElType}
return similar(Array{ElType}, axes(bc))
for VSupertype in (:(Array{T,N}), :(AbstractRange{T}))
@eval function Base.similar(
bc::Broadcast.Broadcasted{Broadcast.ArrayStyle{QuantityArray{T,N,D,Q,V}}},
::Type{ElType},
) where {T,N,D,Q,V<:$VSupertype,ElType}
return similar(Array{ElType}, axes(bc))
end
end
function Base.similar(bc::Broadcast.Broadcasted{Broadcast.ArrayStyle{QuantityArray{T,N,D,Q,V}}}, ::Type{ElType}) where {T,N,D,Q,V,ElType}
# To deal with things like StaticArrays, we need to rely on
Expand Down
13 changes: 13 additions & 0 deletions test/unittests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,19 @@ end
@test RealQuantity(us"inch") * (1.0:4.0) isa QuantityArray
end

@testset "Broadcasting over QuantityArray with StepRangeLen backing" begin
struct WrappedQuantity{T}
value::T
end

t = QuantityArray(range(1.0, step=0.1, length=10), u"s")
wrapped = WrappedQuantity.(t)

@test wrapped isa Vector{WrappedQuantity{Quantity{Float64,typeof(dimension(u"s"))}}}
@test first(wrapped).value == first(t)
@test last(wrapped).value == last(t)
end

@testset "Array of quantities multiplication behavior" begin
# Test that array of quantities * quantity does NOT create nested QuantityArray
# but instead broadcasts properly
Expand Down
Loading