# Mathematica refuses to simplify units

Here is a minimal example:

Quantity[z, "m"]/(Quantity[x, "m"] Log[q] + Quantity[y, "m"])


where z,x,q,y are undefined variables. Mathematica refuses to cancel the meter unit. I tried UnitSimplify, FullSimplify, Simplify, but they don't work. I think the Log[q] term is culprit. If you remove it, the meter goes away.

This is problematic, because I cannot use this expression inside numerical methods. I need to find a way to simplify the units. I can't remove them by hand, because this expression is generated automatically from other routines I have in my code. Any ideas?

• @LouisB That's exactly what my answer below does. – Cassini Oct 3 '17 at 0:24

I think the issue is that Mathematica doesn't know the dimensions of Log[q]. You could tell Mathematica that it is dimensionless by using the unit "PureUnities". So:

Quantity[z, "m"]/(Quantity[x, "m"] Quantity[Log[q], "PureUnities"] + Quantity[y, "m"])


z/(y + x*Log[q])

• Is "PureUnities" the same as "DimensionlessUnit"? I've never heard of it. – Cassini Oct 3 '17 at 0:27
• By the way, I'm asking because this occurred to me as well but I assumed the OP would want to assign a unit (or, rather, no unit) to q instead of Log[q]. Unfortunately, that doesn't seem to work. – Cassini Oct 3 '17 at 0:30

This probably isn't what you were hoping for, but I'll offer it up anyway (and apologies in advance for my awkward use of patterns):

Quantity[z,
"m"]/(Quantity[x, "m"] Log[q] + Quantity[y, "m"]) //. {Times[
a_?(! QuantityQ[#] &), b_?QuantityQ] ->
Quantity[a QuantityMagnitude[b], QuantityUnit[b]]}


will yield the answer you expect:

z/(y + x Log[q])

• That rule simply replaces quantitities with their magnitude. What if I am summing meters to centimeters? This will miss a factor of 100. – becko Oct 3 '17 at 6:32
• @becko That's incorrect--try it and you'll see. All the rule does is look for terms which multiply a Quantity by something which isn't a Quantity and replaces it with a single Quantity in which the multiplication is performed in the magnitude of the Quantity. The units are preserved. – Cassini Oct 3 '17 at 12:59
• You are totally correct, my bad. I should have tested before commenting. – becko Oct 3 '17 at 13:18