1.4.10 Using Symbols to Tag Objects
There are many ways to use symbols in Mathematica. So far, we have concentrated on using symbols to store values and to represent mathematical variables. This section describes another way to use symbols in Mathematica.
The idea is to use symbols as "tags" for different types of objects.
Working with physical units gives one simple example. When you specify the length of an object, you want to give not only a number, but also the units in which the length is measured. In standard notation, you might write a length as 12 meters.
You can imitate this notation almost directly in Mathematica. You can for example simply use a symbol meters to indicate the units of our measurement.
The symbol meters here acts as a tag, which indicates the units used.
In:= 12 meters
You can add lengths like this.
In:= % + 5.3 meters
This gives a speed.
In:= % / (25 seconds)
This converts to a speed in feet per second.
In:= % /. meters -> 3.28084 feet
There is in fact a standard Mathematica package that allows you to work with units. The package defines many symbols that represent standard types of units.
Load the Mathematica package for handling units.
The package uses standardized names for units.
In:= 12 Meter/Second
The function Convert[expr, units] converts to the specified units.
In:= Convert[ %, Mile/Hour ]
Usually you have to give prefixes for units as separate words.
In:= Convert[ 3 Kilo Meter / Hour, Inch / Minute ]