Spin`Algebra

Spin`Algebra — Magnetic Resonance with Mathematica
 

Unit` Guide

© Yuri E. Kandrashkin, 2007

Summary

The package Unit` grants an elegant method to work with units. It uses the definition of Mathematica standard packages Miscellaneous`Units` , Miscellaneous`SIUnits`, and Miscellaneous`PhysicalConstants` and expands its definitions to give:
1. Compact typing and reading of units 5◦kg
2. Unit transformation rules (2∘kg)^23◦m→12◦kg^2 m
3. Subscripts might be used as symbols 1◦N_A
4. Work with fundamental constants as with units
5. Evaluation protection of units, so unit m in expression m=1; length=1◦m is treated correctly
6. The results are presented in globally defined format which can be easily changed
7. The ability of incorporation a set of notation for units of specific field of research

Introduction

The package Unit` allows to use acronym form of fundamental units with no harm even if the corresponding symbols have values. Some other properties of the package will be described below.

This loads a package

[Graphics:img/guide_4.gif]

The input and output of units is designed as arguments of function Unit:

[Graphics:img/guide_5.gif]

[Graphics:img/guide_6.gif]

Although the output looks as a function it is not:

[Graphics:img/guide_7.gif]

[Graphics:img/guide_8.gif]

The convenient input of symbol is typing :sc:. Thus the next example can be entered as 5:sc:m

[Graphics:img/guide_9.gif]

[Graphics:img/guide_10.gif]

This expression is not a function Unit anymore but a product:

[Graphics:img/guide_11.gif]

[Graphics:img/guide_12.gif]

Often symbols like m are used as names of variables. By default the definition of a symbol acts globally in any expression.

[Graphics:img/guide_13.gif]

[Graphics:img/guide_14.gif]

To prevent this unlikely behavior function Unit has an attribute HoldAll. This property allows to use short acronyms in SI notation with no harm:

[Graphics:img/guide_15.gif]

[Graphics:img/guide_16.gif]

This clears the value of m

[Graphics:img/guide_17.gif]

Product and Power of units

The power of an expression is transformed to product of a value and its unit:

[Graphics:img/guide_18.gif]

[Graphics:img/guide_19.gif]

The product of two or more expressions is automatically simplified:

[Graphics:img/guide_20.gif]

[Graphics:img/guide_21.gif]

The internal form of the expression is

[Graphics:img/guide_22.gif]

[Graphics:img/guide_23.gif]

Transformation to unitless form

If there is necessity of removing the head of function Unit one might use the function ReplaceAll:

[Graphics:img/guide_24.gif]

[Graphics:img/guide_25.gif]

The result doesn't contain a function Unit

[Graphics:img/guide_26.gif]

[Graphics:img/guide_27.gif]

The simple way to remove units is using the following form:

[Graphics:img/guide_28.gif]

[Graphics:img/guide_29.gif]

The resulting table now can be displayed in a graphic

[Graphics:img/guide_30.gif]

[Graphics:img/guide_31.gif]

[Graphics:img/guide_32.gif]

Conversion between different systems of units

Global variable $UnitFormat determines the output system of units. By default it is set as SI:

[Graphics:img/guide_33.gif]

[Graphics:img/guide_34.gif]

Other values include MKS, CGS, SIAcronym, SIFundamental, and SIConstants.

[Graphics:img/guide_35.gif]

[Graphics:img/guide_36.gif]

With the values SI, MKS, CGS the output is similar to using the corresponding functions from the package Miscellaneous`Units`

[Graphics:img/guide_37.gif]

[Graphics:img/guide_38.gif]

The values "SIAcronym", SIFundamental and SIConstants give a compact form of SI system of units:

[Graphics:img/guide_39.gif]

[Graphics:img/guide_40.gif]

SIAcronym sets the rules for acronyms of SI system of units, SIConstants adds additional transformations for physical constants, and SIFundamental can be used to convert all values in unique representation of fundamental SI units.

If there is no action on Unit its argument is not evaluated:  

[Graphics:img/guide_41.gif]

[Graphics:img/guide_42.gif]

The function UnitUpdate forces recomputation of function Unit

[Graphics:img/guide_43.gif]

[Graphics:img/guide_44.gif]

This reevaluates expr in the CGS system of units:

[Graphics:img/guide_45.gif]

[Graphics:img/guide_46.gif]

But acronyms like m for Meter are unknown while $UnitFormat has the value CGS:

[Graphics:img/guide_47.gif]

[Graphics:img/guide_48.gif]

To add extra conversion rules one can list their names after the output format:

[Graphics:img/guide_49.gif]

[Graphics:img/guide_50.gif]

The value All can be used to apply all transformation rules: $UnitFormat={"CGS",All}. The second argument is used here to convert the expression in the specific format.

[Graphics:img/guide_51.gif]

[Graphics:img/guide_52.gif]

This clears the value of expr

[Graphics:img/guide_53.gif]

Work with constants as with units

Often the expressions are presented in simpler form if some fundamental constants are treated as units. For example, one can work in units of speed of the light:

[Graphics:img/guide_54.gif]

[Graphics:img/guide_55.gif]

Because of the value of constant c is undefined the evaluation of Unit doesn't change the expression

[Graphics:img/guide_56.gif]

[Graphics:img/guide_57.gif]

But deriving the value of v or computing and comparing it with other expressions occasionally needs the conversion to standard system of units. The package supports a format SIConstants which allows to convert the constants in SI system of units during the evaluation of function Unit

[Graphics:img/guide_58.gif]

[Graphics:img/guide_59.gif]

Although the value of v is still given in units of c:

[Graphics:img/guide_60.gif]

[Graphics:img/guide_61.gif]

Another example is a definition of electron magnetogyric ratio:

[Graphics:img/guide_62.gif]

[Graphics:img/guide_63.gif]

Its value in SI is

[Graphics:img/guide_64.gif]

[Graphics:img/guide_65.gif]

The standard value of magnetic field in electron spin resonance experiments is 350 mT and the resonance frequency is  

[Graphics:img/guide_66.gif]

[Graphics:img/guide_67.gif]

This corresponds to wavelength ~3cm^(-1)

[Graphics:img/guide_69.gif]

[Graphics:img/guide_70.gif]

The computation of the next line gives the complete set of notations of the format SIConstants

[Graphics:img/guide_71.gif]

[Graphics:img/guide_72.gif]

This clears the used values

[Graphics:img/guide_73.gif]

Definition of a new system of units

In many areas of research there are special conventions on the units. For instance wavenumber, cm^(-1), is used to define an energy or frequency in spectroscopy. In molecular and chemical physics the energy is often is defined in units of temperature. In such cases it is convenient to set a new system of units supporting the traditional notations. A function UnitFormat can be used to set of new rules of definitions.

Every time during the transformation of the value of Unit the value of $UnitFormat is checked. The possible value of $UnitFormat should match to one of arguments of function UnitFormat:

[Graphics:img/guide_75.gif]

[Graphics:img/guide_76.gif]

Except the cases of SI, CGS and MKS all other definitions of function UnitFormat must be sets of rules of transformations to SI system of units and from SI to a new system of units.

Let's define a new system of energy units:

[Graphics:img/guide_77.gif]

The functions HoldPattern and RuleDelayed (:→) in the definition above used to prevent evaluation of symbols cm, kT and eV even if they have values:

[Graphics:img/guide_78.gif]

[Graphics:img/guide_79.gif]



   Last modified: April 10, 2007