Built-in Data and Units
Mathematica provides access to a lot of data, most useful for chemistry is the built-in chemical, element, and isotope data and the physical constants it knows about
ElementData
ElementData is a function that gets data on an element from Mathematica’s built in servers. For example:
(*Out:*)
Quantity[1.00794, "AtomicMassUnit"]
(*Out:*)
74
Here is the full list of properties to query for each element:
(*Out:*)
And here is a function to apply that to a given element
(*Out:*)
A list of all the elements can be generated via ElementData[] and I used that to generate a template for a periodic table. So finally here is a function that uses this template and the previously defined function to get all the possible information from ElementData.
Since this function calls elementProperties be sure to evaluate the definition for elementProperties before evaluating the following.
(*Out:*)
periodicTable[]
IsotopeData
IsotopeData is similar to ElementData except one supplies an isotope instead. Known isotopes for a given element can be gotten via the following function
(*Out:*)
{"H1", "H2", "H3", "H4", "H5", "H6", "H7"}
We can similarly get all the properties for an isotope in the same way as for an element:
(*Out:*)
ChemicalData
The final useful data set is the built in ChemicalData which can return properties of chemicals generally. At this point it should be clear how it works, but it’s worth pointing out that the list of all the known chemicals can take a long time to load so only call ChemicalData without any arguments if you’re sure your internet connection is good and your computer can handle the load.
Quantity
The interface to the units Mathematica knows about is called Quantity
This was used previously to get the value of the molar gas constant, but there is a host of useful units built in. For example let’s convert between a wavelength and a frequency for electromagnetic radiation:
Let’s start out with green light. Let’s find a frequency that works using Mathematica’s built-in ColorData , which knows the frequencies of the visible spectrum.
Unfortunately this can only convert from a wavelength to a color, but we can write a function to get close to the appropriate wavelength, knowing that in Mathematica a color is just RGBColor[redPercentage,greenPercentage,bluePercentage] .
First we write a distance function:
Then we get the minimum distance to our given color of all the wavelengths in the visible spectrum: (for convenience I use a more complicated function declaration syntax, which is explained later)
(*Out:*)
Quantity[541, "Nanometers"]
We can check that this is in fact a wavelength that corresponds to green light:
(*Out:*)
Then we’ll convert this to a frequency by recalling c=λν
(*Out:*)
Quantity[5.54145*10^14, "Hertz"]
Another place this is incredibly useful is for simple unit conversion problems, such as a classic PV=nRT application. Here’s a sample problem I found online:
- Determine the volume of occupied by 2.34 grams of carbon dioxide gas at STP.
We can solve this trivially using this built-in data:
(*Out:*)
Quantity[1.1911, "Liters"]
It performs all the tedious conversions and has all the data. All we need to do is supply the formula.