I have a huge expression that makes use of Mathematica's units support (Quantity[]) that I know evaluates to a dimensionless quantity. However, I'm unable to get Simplify[] nor UnitSimplify[] to simplify the expression to something not containing units.

Because the actual expression I'm trying to simplify is far too huge to include here, I've made a small example that reproduces this problem:

Quantity[a, "Ohms"] / (b Quantity[1. c, "Ohms"] + Quantity[d, "Ohms"])

Note the presence of variables, some within a Quantity[] and some outside, as well as a machine-precision number.
But clearly the "Ohms" unit is a common factor in both the numerator and denominator, so it can be cancelled away.

How can I get Mathematica to simplify this down to:

a / (1. b c + d)

Note: I'm using Mathematica 12.0

Edit: Additional test cases that includes slightly more of the original expression

Quantity[a, "Ohms"] / (Tanh@Sqrt[b] Quantity[1. c, "Ohms"] + Quantity[d, "Ohms"])
Quantity[a, "Ohms"] / (Sqrt[b] Quantity[1. c, "Ohms"] + Quantity[1 / (1 - b), "Ohms"])

3 Answers 3


Mathematica can't cancel the ohms units because the unit dimensions of $b$ is an unknown quantity. However, if $b$ is defined as a "DimensionlessUnit" quantity, the ohms units will cancel.

expr = Quantity[a, "Ohms"] / (b Quantity[1. c, "Ohms"] + Quantity[d, "Ohms"]);
expr /. b -> Quantity[b, "DimensionlessUnit"]
a / (1.*b*c + d)

As a general solution, use Variables to parse an expression, identify dimensionless variables with AtomQ, and define the variables as quantities with "DimensionlessUnit".

vars = Cases[Variables[expr], v_/;
  AtomQ@v -> v -> Quantity[v,"DimensionlessUnit"]];
expr /. vars
a / (1.*b*c + d)

Why units matter

Let me try to explain why units assigned to $b$ are significant. Obviously, when $b$ has a dimensionless value, the units cancel. However, it does matter what units $b$ has because incompatible units can't be canceled.

When $b$ has unit dimensions, Mathematica must decide if units in the expression are compatible. For example, expr /. b -> Quantity[1/"Feet"] causes an incompatible units error because "Ohms" and "Ohms/Feet" can't be added. Until $b$ has a value, Mathematica can't determine if the units in the expression are compatible, and indeterminate units don't cancel.

Referring to the expression in the question, it is not true that the units in the numerator and denominator are the same until $b$ is a known value.

  • 2
    $\begingroup$ I'm honestly not sure why it can't cancel out the Ohms, though. It doesn't matter what units b has; you can definitely cancel out all of the Ohm units regardless. $\endgroup$ Feb 21 at 10:23
  • $\begingroup$ @SjoerdSmit indeterminate units cannot cancel because Mathematica can't decide if units in the expression are compatible. I hope my edit explains the situation. $\endgroup$
    – creidhne
    Feb 21 at 12:57
  • 1
    $\begingroup$ Reduce[lhs === rhs] is invalid use of Reduce. SameQ immediately evaluates to True or False; there's nothing for Reduce to inspect or manipulate. $\endgroup$ Feb 21 at 13:32
  • 1
    $\begingroup$ @小太郎 Try this: replace AtomQ@v with Head[v] =!= Quantity. $\endgroup$
    – creidhne
    Feb 22 at 1:42
  • 1
    $\begingroup$ That version works quite well! It still ends up leaving a few Quantity[..., "DimensionlessUnit"] or Quantity[..., 1/"DimensionlessUnit"] hanging around in my expression, but they can be safely cleaned up with a simple ReplaceAll IMO. I've edited another example into my question if you want to keep fighting the hydra though :) $\endgroup$
    – 小太郎
    Feb 22 at 7:21
expr = Quantity[a, "Ohms"]/(b Quantity[1. c, "Ohms"] + Quantity[d, "Ohms"]);

Using ReplaceAll

expr /. Quantity[a_, _] :> a

enter image description here

  • 5
    $\begingroup$ This assumes the units do indeed cancel and the same quantities are expressed in the same unit, e.g., all Ohms and none in, say, Kiloohms. Safer would be for the replacement to be, e.g., q_ /; CompatibleUnitQ[q, "Ohms"] :> QuantityMagnitude[q, "Ohms"]. $\endgroup$
    – tad
    Feb 21 at 0:58

This isn't exactly an answer to your question, but it may be a clue (I suspect that Mathematica won't simplify the expression because it doesn't know if a, b, c, and d are dimensionless quantities):

a = Quantity[aa, "DimensionlessUnit"];
b = Quantity[bb, "DimensionlessUnit"];
c = Quantity[cc, "DimensionlessUnit"];
d = Quantity[dd, "DimensionlessUnit"];
Quantity[a, "Ohms"]/(b  Quantity[1.  c, "Ohms"] + Quantity[d, "Ohms"])


aa/(1. bb cc + dd)

Don't ask me why it assumes that aa, bb, cc, and dd are dimensionless. As I said, this is only a clue.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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