Skip to content

QuantityArray convenience #166

Open
@icweaver

Description

@icweaver

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions