4
$\begingroup$

Why is that Power[Quantity["ElectricConstant"],-1] is represent different of Power[Pi,-1] What I am doing wrong ?

$\endgroup$
1
  • $\begingroup$ The technical "why" can be seen here: Power[Quantity["ElectricConstant"], -1] // FullForm. I'm unsure how to prevent this though. $\endgroup$
    – mfvonh
    Aug 25, 2014 at 19:06

2 Answers 2

2
$\begingroup$

This is not yet a real answer as I offer no solution, only observations.

Attempting to answer this by spelunking Quantity formatting is like falling down the rabbit hole. One internal function leads to another ad infinitum. I shall stop for now with the observation that the Box formatting comes from this function:

CalculateScan`UnitScanner`Private`UnitInformation

For example:

Quantity["ElectricConstant"]^-2 // FullForm
Quantity[1,Power["ElectricConstant",-2]]

Which in the course of box formatting eventually becomes a call like:

CalculateScan`UnitScanner`Private`UnitInformation[
 1, 
 QuantityUnits`Private`toAlphaUnit["ElectricConstant"^-2], 
 "SingularTest" -> (MatchQ[#1, 1] &), 
 "Output" -> {"PrefixBoxes", "PostfixBoxes", "UnitBoxes", "DefaultStyle"}
]
{"", RowBox[{"\"/\"", "\[InvisibleSpace]", 
   SubsuperscriptBox["\"\[CurlyEpsilon]\"", "0", "2"]}], "", Automatic}

Observe the RowBox with the "/". This is used in the higher level call:

QuantityUnits`Private`QuantityAbbreviation[1, "ElectricConstant"^-1]
StyleBox[RowBox[{#1, "\[InvisibleSpace]", 
   StyleBox[RowBox[{"\"/\"", "\[InvisibleSpace]", 
      SubscriptBox["\"\[CurlyEpsilon]\"", "0"]}], "QuantityUnitTraditionalLabel"]}], 
 ShowStringCharacters -> False]

Which defines (part of) a formatting function for the expression, which can be observed with:

Quantity["ElectricConstant"]^-2 // ToBoxes
TemplateBox[{"1"}, "QuantityUnit", 
 DisplayFunction -> (TooltipBox[
     StyleBox[RowBox[{#1, "\[InvisibleSpace]", 
        StyleBox[RowBox[{"\"/\"", "\[InvisibleSpace]", 
           SubsuperscriptBox["\"\[CurlyEpsilon]\"", "0", "2"]}], 
         "QuantityUnitTraditionalLabel"]}], ShowStringCharacters -> False], 
     "Unit: per electric constants squared"] &), 
 InterpretationFunction -> (RowBox[{"Quantity", "[", 
      RowBox[{#1, ",", FractionBox["1", SuperscriptBox["\"ElectricConstant\"", "2"]]}], 
      "]"}] &), SyntaxForm -> Mod]

As you can see this is all rather complicated. Perhaps later I will continue spelunking to trace the source of the formatting produced by CalculateScan`UnitScanner`Private`UnitInformation but at the moment I am fatigued.

$\endgroup$
1
  • $\begingroup$ It looks like the formatting for reciprocal units comes from ToPerBox and ToSlashBox in CalculateScan`UnitScanner`Private. $\endgroup$ Aug 26, 2014 at 15:25
2
$\begingroup$

You aren't doing anything wrong, it's just that Mathematica uses a linear form for the units in Quantity expressions. As mfvonh commented, the expression

Power[Quantity["ElectricConstant"], -1]

is evaluated to

Quantity[1, Power["ElectricConstant", -1]]

i.e. a Quantity with a magnitude of 1 and units of "reciprocal electric constants". The unit "reciprocal electric constants" is displayed as $1/\varepsilon _0$ just as units like "meters per second" are displayed as $m/s$ rather than the 2D form $\frac{m}{s}$

I am not aware of any simple method to change the way quantity units are formatted (though I would be delighted to be wrong). In principle you could modify the internal code for generating boxes for Quantity expressions but, as Mr Wizard has shown, the code is rather complex and it would not be easy.

$\endgroup$

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.