Tag Info

Hot answers tagged

29

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"]]} , ...


20

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]


19

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:", ...


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, ...


14

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, ...


14

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 /: ...


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 ...


13

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, ...


13

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 = ...


12

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"


12

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, ...


11

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, ...


9

In physics 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 end ...


8

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, ...


8

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 ...


7

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


7

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 ...


7

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


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

The problem is that you have to type the units exactly right or they don't work. Quantity[2,("Electronvolts")/("Grams"*("Centimeters")^2)] Quantity[3,("Grams"*("Centimeters")^2)/("Electronvolts"*("Centimeters")^2)] To get a list of the available units, type Quantity; Keys[QuantityUnits`Private`$UnitReplacementRules] which I got from this post. edit: ...


6

This is not an answer, but more of a comment to help motivate the question. Apparently the phrasing of the question, as it currently is, is not convincing for many people. However, this is not really related to Quantity. Perhaps it can be an "answer" in the sense that it provides an alternative to relying on the built-in methods. Definitions The values ...


6

You can see the very same effect already without Quantity: a = 1.0`7 (* ==> 1.000000 *) b = 1.0`14 (* ==> 1.0000000000000 *) a/Sqrt[a]-Sqrt[a]+b (* 1.000000 *) You might argue that there should be more precision here because a cancels out completely. But the point is that as soon as a is evaluated, all Mathematica has is a value of 1.0`7, and ...


5

Not a full answer since I need to sleep :) but more of an observation, which might help. It seems to have to do with the fact that 0 and 0. are not the same in Mathematica. This simple example shows it UnitConvert[0. + Quantity[5, "Meters"], "Inches"] (*--> UnitConvert[0. + Quantity[5, "Meters"], "Inches"] *) while UnitConvert[0 + ...


5

It seems, if you are not going to use the "proper long name", you need to use Quantity Try UnitConvert[Quantity[55, "mi/hour"], Quantity["m/s"]]


5

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, ...


5

You might use Assumptions in one form or another. Block[{$Assumptions = Liters > 0 && Mols > 0 && Kelvins > 0}, Simplify[Wideal[5 Liters, 10 Liters, 1 Mols, 298 Kelvins]] ] (* -> 298 Kelvins Mols R Log[2] *) One disappointment for me is that the following doesn't work: Block[{$Assumptions = Liters > 0 && Mols ...


5

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


5

If you don't want Mathematica to use Wolfram | Alpha for correcting incorrect units, you can just set $AllowInternet = False. The downside of this is that it also blocks internet access for the curated data functions such as WeatherData, FinancialData, etc. You can also use Block to disallow internet only within your function.


5

How about m = Quantity[AstronomicalData["Earth", "Mass"], AstronomicalData["Earth", "Mass", "Units"]] 5.9721986*10^24 kg m // FullForm Quantity[5.9721985999999999999999999999999999999999202`8.*^24,"Kilograms"]


5

stringUnits = Module[{qf = QuantityForm[QuantityUnit[#], "Abbreviation"], boxes}, boxes = Cases[ToBoxes[qf], StyleBox[RowBox[{x___}], sty_] :> x]; StringJoin[ boxes /. x_String :> ToExpression[x] /. Null -> " " /. SuperscriptBox[a_, b_] :> StringJoin[ToString@a, "^", ToString@b]]] &; stringUnits @ Quantity["Newton/Meters^2"] (* ...



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