3
$\begingroup$

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?

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

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

$\endgroup$
  • 3
    $\begingroup$ Is "PureUnities" the same as "DimensionlessUnit"? I've never heard of it. $\endgroup$ – Cassini Oct 3 '17 at 0:27
  • $\begingroup$ 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. $\endgroup$ – Cassini Oct 3 '17 at 0:30
1
$\begingroup$

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])
$\endgroup$
  • $\begingroup$ That rule simply replaces quantitities with their magnitude. What if I am summing meters to centimeters? This will miss a factor of 100. $\endgroup$ – becko Oct 3 '17 at 6:32
  • 1
    $\begingroup$ @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. $\endgroup$ – Cassini Oct 3 '17 at 12:59
  • $\begingroup$ You are totally correct, my bad. I should have tested before commenting. $\endgroup$ – becko Oct 3 '17 at 13:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.