So, right now, I am working on setting various quantities up as functions of R. R will be varying later on, so right now I just have it set to the variable radius. It's breaking my dimensional analysis later on in the notebook though, because Mathematica assumes radius has units, when it should just be a scalar. Is there a better way to say the following so that I can have dimensional analysis work in the meanwhile?

R = Quantity[radius, "Meters"]; 

This is the output:

 Sqrt[1 - (1.408969*10^6 radius^2)/(
   1.380123*10^7 + 1.408969*10^6 radius^2)] (Quantity[
    7.67349*10^-12 radius^2, (("Amperes")^2 ("Meters")^2 (
     "Seconds")^2 ("Teslas")^2)/("Kilograms")])]

It should just be in Joules! But, when I set R to a quantity, like so:

R = Quantity[1, "Meters"];

I get such a pretty result:

{7.30945*10^-12 J}

I think I need to strip the units off radius. I tried Quantity Magnitude, but Mathematica wasn't a fan.

  • 2
    $\begingroup$ Probably if you edit to include a minimum working example of your code and data in formatted form we could see how its breaking a suggest a solution. Otherwise we can only guess. $\endgroup$
    – rhermans
    Oct 14, 2015 at 14:47
  • 3
    $\begingroup$ MapAt[UnitSimplify, expr, -1] instead of UnitSimplify[expr]. $\endgroup$
    – march
    Oct 14, 2015 at 15:21
  • $\begingroup$ I am confused. In your sample expression R is never used, so its value cannot matter to the final result. What expression are you actually evaluating when you obtain the numerical result you show? $\endgroup$
    – MarcoB
    Oct 14, 2015 at 21:22
  • $\begingroup$ a = Solve[lorentz*m0*tempv/(q*B) == R, lorentz][[2]] T = ((lorentz /. a) - 1)*m0*c^2 // N $\endgroup$
    – laudiacay
    Oct 16, 2015 at 14:44
  • 1
    $\begingroup$ Perhaps you can make use of the available dimensionless unit: Quantity[radius, "DimensionlessUnit"]? $\endgroup$ Mar 18, 2016 at 16:57

1 Answer 1


General Remarks

UnitSimplify is expecting an expression with head Quantity, and the factor before the quantity prevents the evaluation. We have for instance:

UnitConvert[a Quantity[2, "Inches"]]
(* UnitConvert[a (Quantity[2, "Inches"])] *)

UnitConvert[Quantity[2, "Inches"]]
(* Quantity[127/2500, "Meters"] *)

Note that the expression a Quantity[2, "Inches"] does not evaluate itself to a Quantity object:

a Quantity[2, "Inches"]
(* a (Quantity[2, "Inches"]) *)

because a is not NumericQ. On the contrary, this returns a Quantity:

(1 + Sqrt[2]) Quantity[2, "Inches"]
(* Quantity[2 (1 + Sqrt[2]), "Inches"] *)

Some spelunking (<<GeneralUtilities`; PrintDefinitions[Quantity]) shows the definition yielding this behavior. It starts with

Quantity /: Condition[HoldPattern[Times][...]

OP's question

Back to OP's question, a possible approach is to make the non-NumericQ factor of the expression enter the magnitude of the quantity. This can be done as follows:

makeQuantity[HoldPattern[Times[fact_, q_Quantity ?QuantityQ]]] := 
        Quantity[fact QuantityMagnitude[q], QuantityUnit[q]]

Taking OP's expression

expr = Sqrt[1 - (1.408969*10^6 radius^2)/(1.380123*10^7 + 1.408969*10^6 radius^2)] *
          7.67349*10^-12 radius^2, 
          (("Amperes")^2 ("Meters")^2 ("Seconds")^2 ("Teslas")^2)/("Kilograms")

we get the expected simplification after processing:


enter image description here


enter image description here

This returns the mentioned result for the special case radius = 1:

% /. radius -> 1
(* Quantity[7.30944*10^-12, "Joules"] *)

The pattern I used for the definition of makeQuantity is a simple one, it may need to be enhanced depending on what the use is.


Your Answer

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

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