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

Question: How to decompose units into sub-units?

Open ejmeitz opened this issue 1 year ago • 1 comments

If I have a unit that I know is energy/length^3 is there a way I can pull out the unit for energy and the unit for length separately?

Thanks!

ejmeitz avatar Feb 19 '24 18:02 ejmeitz

There is no easy way built into this package, but you could use the following function to extract the individual units of a Units object into a Tuple:

function unit_tuple(u::Unitful.Units{N}) where N
    length(N) == 1 ? (Unitful.FreeUnits(u),) : map(x -> Unitful.FreeUnits{(x,),dimension(x),nothing}(), N)
end

The branch for length(N) == 1 is only necessary for affine units (without it, unittuple(°C) would return K).

sostock avatar May 27 '24 12:05 sostock