Hot answers tagged

33

If we pre-evaluate the expression: x1 B[Quantity[ll, "Micrometers"], Quantity[1000, "Kelvins"]] then the run time can be reduced by about factor 10. We can do this by hoisting the expression out of the loop and pre-evaluating using With: p = Module[{ll} , With[{v = x1 B[Quantity[ll, "Micrometers"], Quantity[1000, "Kelvins"]]} , Table[{Quantity[...


29

This is certainly the optimal way of obtaining the list you are looking for Quantity;QuantityUnits`Private`$UnitReplacementRules[[1, All, 1]] EDIT for v10 (thanks @DavidCreech) In v10 this undocumented variable format has been changed into an association, whose keys are the units. Quantity; Keys[QuantityUnits`Private`$UnitReplacementRules]


21

This should list you all available units in Mathematica. Needs["QuantityUnits`"] Keys[QuantityUnits`Private`$UnitReplacementRules] Inspired by eldo I made a little dynamic interface: Needs["QuantityUnits`"] table = Keys[QuantityUnits`Private`$UnitReplacementRules]; Panel[DynamicModule[{f = ""}, Column[{Text[Style["Mathematica Unit Search:", Bold]]...


20

I believe you can use "DimensionlessUnit" to get the desired result: In[6]:= Quantity[3, "DimensionlessUnit"] Out[6]= 3 (note this is the unit produced by QuantityUnit on a dimensionless value): In[7]:= QuantityUnit[3] Out[7]= "DimensionlessUnit"


19

In physics, the Planck constant may be used as a natural unit. If you want to switch to another unit system, use UnitConvert[]. For example, you can switch to standard SI units this way: UnitConvert[Quantity[1, "PlanckConstant"], "SIBase"] which will give you: Quantity[6.626070*10^-34, ("Kilograms" ("Meters")^2)/("Seconds")] This can be done at the ...


18

This appears to be a bug. The dimensions of the Boltzmann constant are incorrect. In fact, all the physical constants I checked have TemperatureUnit where they should have TemperatureDifferenceUnit. You should only have to make a substitution when making calls to physical constants in Quantity: q = UnitConvert[Quantity["elementary charge"]]; k = (...


16

Just some analysis to try to find where the slow down. On my PC, it took 25 seconds to build the table. ps. I never used Units before. Your main loop: x = UnitConvert@Quantity["PlanckConstant" "SpeedOfLight"/"BoltzmannConstant"] x1 = UnitConvert@Quantity[2, "PlanckConstant" ("SpeedOfLight")^2] B[L_, T_] := (L^(-5))/(Exp[x/(L T)] - 1) c = Quantity[1000, "...


16

x is not in mL..it is just a pure number. Quantity[x, "ml"] is the 'thing' that is in mL. To get what you want, you need to recast your Solve command as Solve[x Quantity[5, "mol"] + (Quantity[250, "ml"] - x) Quantity[7, "mol"] == Quantity[250, "ml"] Quantity[6, "mol"], x] However, the result is a bit strange looking. {{x -> Quantity[1/8000, ("...


15

While it would've been nice if the package handled it automatically, it can be fixed with a simple overloading of Quantity: Unprotect@Quantity; Quantity /: (0 | 0.) Quantity[_, unit_] := Quantity[0, unit] Protect@Quantity; You can add this to your init.m, so that you don't have to define it each time. You can test your examples with this: 0. Quantity[1, "...


15

Here is a cheap way which does not involve WA, but will only be as good as you make it to be (so that you'd have to customize it yourself): create a dynamic environment: ClearAll[withUnits]; SetAttributes[withUnits, HoldAll]; withUnits[code_] := Function[Null, Block[{Quantity}, SetAttributes[Quantity, HoldRest]; Quantity /: UnitConvert[...


15

I have mined the Units package for the names of all units defined therein and correlated them to the built-in strings recognized by Quantity(referenced here). I then define a new function Quantify to convert the old school units into Quantity objects. unitRules = Dispatch[{Abampere -> Quantity[1, "ABAmperes"], Abcoulomb -> Quantity[1, "...


15

At the beginning of your notebook set the Metric system as default. $UnitSystem = "Metric" This works for me. If not try below suggestion Remember you are reading the data from wolfram alpha! it's a regional thing so if the upper solution didn't work, you can try something like (I don't remember exactly though) SetOptions[WolframAlpha, PodStates -> {"...


13

That code should work and it does work on my machine. The problem could be the following. You have only one part that requires Wolfram|Alpha interpretation: Quantity[24, "1/Seconds"] It is not built in unit - so it goes to Wolfram|Alpha for interpretation (how cool is that? ;-) ). This works almost always - unless something is wrong with internet ...


11

UnitConvert[Quantity[179., "Centimeters"], MixedRadix["Feet", "Inches"]] returns Quantity[MixedRadix[5, 10.472440944881885`], MixedRadix["Feet", "Inches"]] which formats as 5'10.4724"


10

The CGS units are available. Out of the need to ensure dimensional consistency, the different things which are all called ESUs must be carefully distinguished. In[59]:= Quantity[1, "ESU of charge"] Out[59]= Quantity[1, "ESUsOfCharge"] In[60]:= Quantity[1, "ESU"] Out[60]= Quantity[1, "ESUOfDielectricDisplacement"] Also, when a unit has a special name, ...


10

The following works for V10. First we define some abbreviation rules: rule = {"Newtons" :> N, "Meters" :> m, "Pascals" :> Pa, "Farads" :> F}; (* add more rules here *) Then: unit = TextString[QuantityUnit[Quantity[1, "Newtons/Meters^2"]] /. rule] "N/m^2" StringQ[unit] True TextString[QuantityUnit[Quantity[1, "Meters*Pascals/...


10

The difference between 0 °C and 100 °C in Fahrenheit is 180 °F. The difference between 0 K and 100 K in Fahrenheit is 180 °F. But 100 °C is 212 °F, while 100 K is 279.688 °F. The underlying reason are, of course, the differing scale origins (zero points); the conversions are therefore not direct proportions (not homogeneous functions). Instead they are ...


9

I'm not sure whether this is what you seek, but you can use Trace to investigate in a call to Quantity. Then you extract the essence Quantity["Newtons"]; StringReplace[Names["CalculateUnits`UnitCommonSymbols`*"], "CalculateUnits`UnitCommonSymbols`" ~~ r_ :> r] and you get some kind of list ;-)


9

You could set an input alias such as With[{rules = {"m" -> "Meters", "km" -> "Kilometers"}}, AppendTo[CurrentValue[InputNotebook[], InputAliases], "qu" -> TemplateBox[{"\[SelectionPlaceholder]", "\[Placeholder]"}, "QuantityUnit", DisplayFunction -> (PanelBox[RowBox[{##}], FrameMargins -> 2] &), InterpretationFunction -&...


9

Oops - found it! QuantityMagnitude[quantity] does the job. For example, In[1]:= QuantityMagnitude[Quantity[1, "Feet"]] Out[1]= 1


9

Expanding a little bit on paw's nice discovery: Needs["QuantityUnits`"] table = Keys[QuantityUnits`Private`$UnitReplacementRules]; Since this table is very long one can restrict the output, f.e. with Union @ Flatten[StringCases[#, "Feet" ~~ ___] & /@ table] // TableForm UPDATE A similar question could arise with the more than 1000 inbuilt ...


8

Below is some code I use to work with units. I am aware that the unit system I am calling "CGS" is only semi-CGS, since I am keeping the SI electromagnetic units, but this is the flavor of consistent unit system we sometimes use in our lab. Really, though, this is a recipe for choosing your own set of base units. The method works by applying ...


8

As noted in the documentation for Quantity, you can use ctrl-= to input units. This uses Wolfram|Alpha, so needs an internet connection. Quantity will also use Wolfram|Alpha to try to interpret strings, so you could also use: In[8]:= UnitConvert[Quantity["1 m/s^2*(1 min)^2"], Quantity["km"]] Out[8]= Quantity[18/5, "Kilometers"]


8

This is an addendum to John Conor Cosnett's answer. There is a degree of flexibility as long as you enter your unit expression as a single string. Thus, Quantity[1.3 10^17, "Watt/Centimeters^2"] Quantity[1.3 10^17, "Watts/Centimeter^2"] Quantity[1.3 10^17, "Watt/Centimeter^2"] Quantity[1.3*10^17, "W/cm^2"] all work and give Quantity[1.3*^17, "Watts"/"...


7

As a partial answer the documentation says: Supported units include all those specified by NIST Special Publication 811. This is repeated in Unit Discovery. It also states: Unit interpretation requires internet connectivity, and can entail additional evaluation time. If speed is a concern, it is advisable to use the canonical unit specification, ...


7

Mathematica arrives at the particular precision that it does using significance arithmetic and associated propagation of precision. This answer describes how to double check that. Oleksandr's answer makes a good case for the assertion that significance arithmetic is insufficient for this problem as it fails to account for the correlation in error between ...


7

EDITED to include Mr.Wizard's replacement for Switch EDITED to cover additional cases Roll your own: quantityWithAppropriatePrefix[quant_Quantity] := Fold[UnitConvert[#1, #2] &, quant, {"Imperial", "SI"}]; EDIT: As Kuba pointed out in a comment, this can be written more compactly as quantityWithAppropriatePrefix[quant_Quantity] := Fold[...


7

Defer[Quantity][Placeholder["tip"],"Meters"] or HoldForm[Quantity][Placeholder["tip"],"Meters"] The second method requires ReleaseHold to evaluate.


7

Yes, this was a bug. It has been fixed as of 10.0.2. In[1]:= Quantity[2, "meters"] Out[1]= 2 m


7

A hint about what is going on is provided by searching on MixedRadix in V10.2 or later. You will find the symbol was overloaded to allow the user to specify a sequence number bases for integers represented in mixed-base form. This has produced the unfortunate consequences that I mentioned in the question. I brought this to the attention of Wolfram tech ...



Only top voted, non community-wiki answers of a minimum length are eligible