Tag Info

Hot answers tagged

28

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


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

This is certainly the optimal way of obtaining the list you are looking for Quantity;QuantityUnits`Private`$UnitReplacementRules[[1, All, 1]]


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

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"


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


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


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

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


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

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

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


4

This appears to work in the 9.0.1 release: DateListPlot[gasPrices]


4

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


4

If you want something that displays in abbreviated from, you can do the following: Type Quantity[number, "unitstring"], for example Quantity[5, "m"]. Select that expression (pressing Ctrl. twice will do that if you haven't moved the cursor). Perform Evaluate In Place (CtrlShiftEnter on Windows and Linux, CmdEnter on OS X). This will give you something ...


4

QuantityMagnitude@UnitConvert[Quantity["GravitationalConstant"]] 6.67*10^-11


4

A properly formatted Quantity expression should work just fine, without trying to connect to WolframAlpha servers for conversion. For example, Quantity[1, "Feet"] + Quantity[2, "Inches"] should run with or without internet connectivity. In contrast, Quantity[1, "Foot"] + Quantity[2, "Inches"] will attempt to contact WA servers in an attempt to ...


3

Why not use Square Meters just like you used Square Angstroms? e.g.: UnitConvert[Quantity[2*^10, "Square Angstroms"], "Square Meters"] Gives: 1/5000000000 ((m)^2)


3

I would use UnitConvert: UnitConvert[Quantity[1, ("Kilograms"*"Meters"^2)/ "Seconds"^2], Quantity["Newton Meters"]]


3

Here's my extensions to Rojo's answer. I've moved the replacement rules into a global variable, to make them easier to modify on the fly $UnitReplacementRules = {"fm"->"Femtometers","nm"->"Nanometers","\[Mu]m"->"Micrometers","mm"->"Millimeters","cm"->"Centimeters","m"->"Meters","km"|"kms"->"Kilometers","mi"->"Miles", ...



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