4.6 Working with Physical Units

PyXPlot has extensive facilities for handling data with a range of physical units. These features make it a powerful desktop tool for converting measurements between different systems of units – for example, between imperial and metric units – and for doing simple back-of-the-envelope calculations.

All numerical variables in PyXPlot have not only a magnitude, but also a physical unit associated with them. In the case of a pure number such as 2, the quantity is said to be dimensionless. The special function unit() is used to specify the physical unit associated with a quantity. For example, the expression

print 2*unit(s)

takes the number 2 and multiplies it by the unit s, which is the SI abbreviation for seconds. The resulting quantity then has dimensions of time, and could, for example, be divided by the unit hr to find the dimensionless number of hours in two seconds:

print 2*unit(s)/unit(hr)

Compound units such as miles per hour, which is defined in terms of two other units, can be used as in

print 2*unit(miles/hour)

or, in many cases, have their own explicit abbreviations, in this case mph:

print 2*unit(mph)

As these examples demonstrate, the unit() function can be passed a string of units either multiplied together with the * operator, or divided using the / operator. Units may be raised to powers with the ** operator1, as in the example:

pyxplot> a = 2*unit(m**2)
pyxplot> print "An area of %f square feet"%(a/unit(ft**2))
An area of 21.527821 square feet

As these examples also demonstrate, units may be referred to by either their abbreviated or full names, and each of these may be used in either their singular or plural forms. A complete list of all of the units which PyXPlot recognises by default, together with all of their recognised names can be found in Appendix 4.

SI units may also be preceded with SI prefixes, as in the examples2:

a = 2*unit(um)
a = 2*unit(micrometres)

When quantities with physical units are substituted into algebraic expressions, PyXPlot automatically checks that the expression is dimensionally correct before evaluating it. For example, the following expression is not dimensionally correct and would return an error because the first term in the sum has dimensions of velocity, whereas the second term is a length:

\includegraphics{cross}

a = 2*unit(m)
b = 4*unit(s)
print a/b + a

PyXPlot continues to throw an error in this case, even when explicit numerical errors are turned off with the set numeric errors quiet command, since it is deemed a serious error: the above expression would never be correct for any values of a and b given their dimensions.

A large number of units are pre-defined in PyXPlot by default, a complete list of which can be found in Appendix 4. However, the need may occasionally arise to define new units. It is not possible to do this from an interactive PyXPlot terminal, but it is possible to do so from a configuration script which PyXPlot runs upon start-up. Such configuration scripts will be discussed in Chapter 8. New units may either be derived from existing SI units, alternative measures of existing quantities, or entirely new base units such as numbers of CPU cycles or man-hours of labour.

Footnotes

  1. The \^{} character may be used as an alias for the ** operator, though this notation is arguably confusing, since the same character is used for the binary exclusive or operator in PyXPlot’s normal arithmetic.
  2. As the first of these examples demonstrates, the letter u is used as a Roman-alphabet substitute for the Greek letter $\upmu $.