# Unitful - derived unit simplification?

**URL:** <https://discourse.julialang.org/t/unitful-derived-unit-simplification/71186>\
**Category:** General Usage\
**Tags:** unitful\
**Created:** [November 9, 2021, 8:04am UTC](https://discourse.julialang.org/t/unitful-derived-unit-simplification/71186 "2021-11-09T08:04:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mde](https://avatars.discourse-cdn.com/v4/letter/m/839c29/32.png) [@mde](https://discourse.julialang.org/u/mde)\
**Post date:** [November 9, 2021, 8:04am UTC](https://discourse.julialang.org/t/unitful-derived-unit-simplification/71186/1 "2021-11-09T08:04:15Z")

</div>

I am fairly new to Unitful and am running into an issue when I multiply a derived unit by one of its component units and want the resulting unit to simplify into the other component unit.

Example:

```julia
> 1u"M" * 1u"L"
1 L M # This should equal "1 mol"

> 1u"mol/L" * 1u"L"
1 mol # The desired behavior works with base units.

```

```julia
> typeof(1u"M")
Quantity{Int64, 𝐍 𝐋⁻³, Unitful.FreeUnits{(M,), 𝐍 𝐋⁻³, nothing}}

> typeof(1u"L")
Quantity{Int64, 𝐋³, Unitful.FreeUnits{(L,), 𝐋³, nothing}}

```

Is there an easy way to fix this instead of resorting to only using base units?

Edit: I run into the same issue when multiplying liters and cm3, where they should cancel out.

```julia
> 1u"L" * 1u"1/cm^3"
1 L cm⁻³

```

---

<div class="post-metadata">

**Author:** ![MarcMush](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marcmush/32/18006_2.png) [@MarcMush](https://discourse.julialang.org/u/MarcMush)\
**Post date:** [November 9, 2021, 10:15am UTC](https://discourse.julialang.org/t/unitful-derived-unit-simplification/71186/2 "2021-11-09T10:15:22Z")

</div>

if you know what unit you expect, you can use `uconvert` or call the units as functions:

```julia-repl
julia> uconvert(u"mol", 1u"M" * 1u"L")
1 mol

julia> uconvert(NoUnits, 1u"L" * 1u"1/cm^3")
1000

julia> 1u"M" * 1u"L" |> u"mol"
1 mol

julia> 1u"L" * 1u"1/cm^3" |> NoUnits
1000

```

if you don’t know the dimension, you can use `upreferred`, which by default shows SI units (this is configurable with `preferunits`)

```julia-repl
julia> upreferred(3u"J/kg")
3 m^2 s^-2

```

see the relevant documentation for more: [Conversion/promotion · Unitful.jl](https://painterqubits.github.io/Unitful.jl/stable/conversion/)

---

<div class="post-metadata">

**Author:** ![mde](https://avatars.discourse-cdn.com/v4/letter/m/839c29/32.png) [@mde](https://discourse.julialang.org/u/mde)\
**Post date:** [November 23, 2021, 7:32am UTC](https://discourse.julialang.org/t/unitful-derived-unit-simplification/71186/3 "2021-11-23T07:32:38Z")

</div>

Thank you! `upreferred` simplifies the units perfectly.

Now my examples become:

```julia
julia> 1u"M" * 1u"L" |> upreferred
1 mol

julia> 1u"L" * 1u"1/cm^3" |> upreferred
1000

```

A related post recently came up with the same issue: [Make Unitful simplify expressions like 1μm/1m to a unitless value by default](https://discourse.julialang.org/t/make-unitful-simplify-expressions-like-1-m-1m-to-a-unitless-value-by-default/71866/)
