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

QuantityArray convenience

Open icweaver opened this issue 11 months ago • 0 comments

Hey again Miles. Sort of following up to your comment on Discourse, were you thinking of just having an extra method or two like:

Base.:*(A::AbstractArray, q::UnionAbstractQuantity) = QuantityArray(A, q)
Base.:*(q::UnionAbstractQuantity, A::AbstractArray) = A * q

so that things like this will work?

julia> (0:1.5:10)u"km"
7-element QuantityArray(::StepRangeLen{Float64, Base.TwicePrecision{Float64}, Base.TwicePrecision{Float64}, Int64}, ::Quantity{Float64, Dimensions{FixedRational{Int32, 25200}}}):
    0.0 m
 1500.0 m
 3000.0 m
 4500.0 m
 6000.0 m
 7500.0 m
 9000.0 m

julia> u"km" * [3, 4, 5]
3-element QuantityArray(::Vector{Float64}, ::Quantity{Float64, Dimensions{FixedRational{Int32, 25200}}}):
 3000.0 m
 4000.0 m
 5000.0 m

julia> [3, 4, 5] * u"km"
3-element QuantityArray(::Vector{Float64}, ::Quantity{Float64, Dimensions{FixedRational{Int32, 25200}}}):
 3000.0 m
 4000.0 m
 5000.0 m

julia> [3, 4, 5] .* u"km" # Maintain original behavior
3-element Vector{Quantity{Float64, Dimensions{FixedRational{Int32, 25200}}}}:
 3000.0 m
 4000.0 m
 5000.0 m

Happy to get a quick PR going with some docs and tests if so!

icweaver avatar Mar 09 '25 23:03 icweaver