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

uconvert a vector or tuple

Open JakeZw opened this issue 2 years ago • 1 comments

When I try to do a units conversion uconvert, or upreferred on a vector or tuple I get an error. One example of use might be to perform a units conversion on the output of the extrema function.

julia> a = [1,2,3]u"ft"
3-element Vector{Quantity{Int64, 𝐋 , Unitful.FreeUnits{(ft,), 𝐋 , nothing}}}:
 1 ft
 2 ft
 3 ft

julia> uconvert(u"inch", a)
ERROR: MethodError: no method matching uconvert(::Unitful.FreeUnits{(inch,), 𝐋 , nothing}, ::Vector{Quantity{Int64, 𝐋 , Unitful.FreeUnits{(ft,), 𝐋 , nothing}}})
Closest candidates are:
  uconvert(::Any, ::Base.TwicePrecision) at C:\Users\jakez\.julia\packages\Unitful\SUQzL\src\range.jl:117
  uconvert(::Unitful.Units, ::Missing) at C:\Users\jakez\.julia\packages\Unitful\SUQzL\src\conversion.jl:90
  uconvert(::Unitful.Units, ::Level) at C:\Users\jakez\.julia\packages\Unitful\SUQzL\src\logarithm.jl:122
  ...
Stacktrace:
 [1] top-level scope
   @ REPL[74]:1

julia>

JakeZw avatar Jun 22 '22 13:06 JakeZw

You need to broadcast the function:

julia> using Unitful

julia> a = [1,2,3]u"ft"
3-element Vector{Quantity{Int64, 𝐋, Unitful.FreeUnits{(ft,), 𝐋, nothing}}}:
 1 ft
 2 ft
 3 ft

julia> uconvert.(u"inch", a)
3-element Vector{Quantity{Int64, 𝐋, Unitful.FreeUnits{(inch,), 𝐋, nothing}}}:
 12 inch
 24 inch
 36 inch

julia> a .|> u"inch"
3-element Vector{Quantity{Int64, 𝐋, Unitful.FreeUnits{(inch,), 𝐋, nothing}}}:
 12 inch
 24 inch
 36 inch

giordano avatar Jun 22 '22 13:06 giordano