Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: How to decompose units into sub-units? #707

Open
ejmeitz opened this issue Feb 19, 2024 · 1 comment
Open

Question: How to decompose units into sub-units? #707

ejmeitz opened this issue Feb 19, 2024 · 1 comment

Comments

@ejmeitz
Copy link
Contributor

ejmeitz commented Feb 19, 2024

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!

@sostock
Copy link
Collaborator

sostock commented May 27, 2024

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants