2 * Introduction to Units::
3 * Functions and Variables for Units::
6 @node Introduction to Units, Functions and Variables for Units, Package unit, Package unit
7 @section Introduction to Units
9 The @emph{unit} package enables the user to convert between arbitrary
10 units and work with dimensions in equations. The functioning of this package
11 is radically different from the original Maxima units package - whereas the
12 original was a basic list of definitions, this package uses rulesets to allow
13 the user to chose, on a per dimension basis, what unit final answers should be
14 rendered in. It will separate units instead of intermixing them in the display,
15 allowing the user to readily identify the units associated with a particular
16 answer. It will allow a user to simplify an expression to its fundamental Base
17 Units, as well as providing fine control over simplifying to derived units.
18 Dimensional analysis is possible, and a variety of tools are available to
19 manage conversion and simplification options. In addition to customizable
20 automatic conversion, @emph{units} also provides a traditional manual
23 Note - when unit conversions are inexact Maxima will make approximations resulting
24 in fractions. This is a consequence of the techniques used to simplify units.
25 The messages warning of this type of substitution are disabled by default in
26 the case of units (normally they are on) since this situation occurs frequently
27 and the warnings clutter the output. (The existing state of ratprint is restored
28 after unit conversions, so user changes to that setting will be preserved
29 otherwise.) If the user needs this information for units, they can set
30 @emph{unitverbose:on} to reactivate the printing of warnings from the unit
33 @emph{unit} is included in Maxima in the share/contrib/unit directory. It obeys
34 normal Maxima package loading conventions:
39 *******************************************************************
40 * Units version 0.50 *
41 * Definitions based on the NIST Reference on *
42 * Constants, Units, and Uncertainty *
43 * Conversion factors from various sources including *
44 * NIST and the GNU units package *
45 *******************************************************************
47 Redefining necessary functions...
48 WARNING: DEFUN/DEFMACRO: redefining function TOPLEVEL-MACSYMA-EVAL ...
49 WARNING: DEFUN/DEFMACRO: redefining function MSETCHK ...
50 WARNING: DEFUN/DEFMACRO: redefining function KILL1 ...
51 WARNING: DEFUN/DEFMACRO: redefining function NFORMAT ...
52 Initializing unit arrays...
57 The WARNING messages are expected and not a cause for concern - they indicate
58 the @emph{unit} package is redefining functions already defined in Maxima proper.
59 This is necessary in order to properly handle units. The user
60 should be aware that if other changes have been made to these functions by other
61 packages those changes will be overwritten by this loading process.
63 The @emph{unit.mac} file also loads a lisp file @emph{unit-functions.lisp} which
64 contains the lisp functions needed for the package.
66 Clifford Yapp is the primary author. He has received valuable assistance from
67 Barton Willis of the University of Nebraska at Kearney (UNK), Robert Dodier, and
68 other intrepid folk of the Maxima mailing list.
70 There are probably lots of bugs. Let me know. @code{float} and @code{numer}
71 don't do what is expected.
73 TODO : dimension functionality, handling of temperature,
74 showabbr and friends. Show examples with addition of quantities containing
77 @opencatbox{Categories:}
78 @category{Physical units}
79 @category{Share packages}
80 @category{Package unit}
84 @node Functions and Variables for Units, , Introduction to Units, Package unit
85 @section Functions and Variables for Units
88 @deffn {Function} setunits (@var{list})
89 By default, the @emph{unit} package does not use any derived dimensions, but will
90 convert all units to the seven fundamental dimensions using MKS units.
102 (%o3) (------) (----)
113 (%i5) centigram*inch/minutes^2;
115 (%o5) (-------------) (----)
121 In some cases this is the desired behavior. If the user wishes to use other
122 units, this is achieved with the @code{setunits} command:
125 (%i6) setunits([centigram,inch,minute]);
131 (%o7) (-------------) (------)
138 (%o8) (--------) (------)
147 (%i10) centigram*inch/minutes^2;
155 The setting of units is quite flexible. For example, if we want to
156 get back to kilograms, meters, and seconds as defaults for those
157 dimensions we can do:
160 (%i11) setunits([kg,m,s]);
164 (%i12) centigram*inch/minutes^2;
166 (%o12) (-------------) (----)
172 Derived units are also handled by this command:
193 (%i21) centigram*inch/minutes^2;
195 (%o21) (-------------) (N)
200 Notice that the @emph{unit} package recognized the non MKS combination
201 of mass, length, and inverse time squared as a force, and converted it
202 to Newtons. This is how Maxima works in general. If, for example, we
203 prefer dyne to Newtons, we simply do the following:
206 (%i22) setunits(dyn);
211 (%o23) (100000) (dyn)
214 (%i24) centigram*inch/minutes^2;
216 (%o24) (--------) (dyn)
221 To discontinue simplifying to any force, we use the uforget command:
235 (%i28) centigram*inch/minutes^2;
237 (%o28) (-------------) (----)
242 This would have worked equally well with @code{uforget(N)} or
243 @code{uforget(%force)}.
245 See also @mref{uforget}. To use this function write first @code{load("unit")}.
247 @opencatbox{Categories:}
248 @category{Package unit}
254 @deffn {Function} uforget (@var{list})
255 By default, the @emph{unit} package converts all units to the
256 seven fundamental dimensions using MKS units. This behavior can
257 be changed with the @code{setunits} command. After that, the
258 user can restore the default behavior for a particular dimension
259 by means of the @code{uforget} command:
262 (%i13) setunits([centigram,inch,minute]);
266 (%i14) centigram*inch/minutes^2;
273 (%i15) uforget([cg,%in,%min]);
274 (%o15) [false, false, false]
277 (%i16) centigram*inch/minutes^2;
279 (%o16) (-------------) (----)
285 @code{uforget} operates on dimensions,
286 not units, so any unit of a particular dimension will work. The
287 dimension itself is also a legal argument.
289 See also @mref{setunits}. To use this function write first @code{load("unit")}.
291 @opencatbox{Categories:}
292 @category{Package unit}
298 @deffn {Function} convert (@var{expr}, @var{list})
299 When resetting the global environment is overkill, there is the @code{convert}
300 command, which allows one time conversions. It can accept either a single
301 argument or a list of units to use in conversion. When a convert operation is
302 done, the normal global evaluation system is bypassed, in order to avoid the
303 desired result being converted again. As a consequence, for inexact calculations
304 "rat" warnings will be visible if the global environment controlling this behavior
305 (@code{ratprint}) is true. This is also useful for spot-checking the
306 accuracy of a global conversion. Another feature is @code{convert} will allow a
307 user to do Base Dimension conversions even if the global environment is set to
308 simplify to a Derived Dimension.
319 (%i3) convert(kg*m/s^2,[g,km,s]);
326 (%i4) convert(kg*m/s^2,[g,inch,minute]);
328 `rat' replaced 39.37007874015748 by 5000/127 = 39.37007874015748
330 (%o4) (-----------) (-----)
335 (%i5) convert(kg*m/s^2,[N]);
339 (%i6) convert(kg*m^2/s^2,[N]);
343 (%i7) setunits([N,J]);
347 (%i8) convert(kg*m^2/s^2,[N]);
351 (%i9) convert(kg*m^2/s^2,[N,inch]);
353 `rat' replaced 39.37007874015748 by 5000/127 = 39.37007874015748
359 (%i10) convert(kg*m^2/s^2,[J]);
367 (%i12) setunits([g,inch,s]);
381 (%o15) (-------) (-----)
386 (%i16) convert(kg*m/s^2,[g,inch,s]);
388 `rat' replaced 39.37007874015748 by 5000/127 = 39.37007874015748
390 (%o16) (-------) (-----)
396 See also @mref{setunits} and @mref{uforget}. To use this function write first @code{load("unit")}.
398 @opencatbox{Categories:}
399 @category{Package unit}
405 @anchor{usersetunits}
406 @defvr {Optional variable} usersetunits
409 If a user wishes to have a default unit behavior other than that described,
410 they can make use of @emph{maxima-init.mac} and the @emph{usersetunits}
411 variable. The @emph{unit} package will check on startup to see if this variable
412 has been assigned a list. If it has, it will use setunits on that list and take
413 the units from that list to be defaults. @code{uforget} will revert to the behavior
414 defined by usersetunits over its own defaults. For example, if we have a
415 @emph{maxima-init.mac} file containing:
417 usersetunits : [N,J];
419 we would see the following behavior:
423 *******************************************************************
424 * Units version 0.50 *
425 * Definitions based on the NIST Reference on *
426 * Constants, Units, and Uncertainty *
427 * Conversion factors from various sources including *
428 * NIST and the GNU units package *
429 *******************************************************************
431 Redefining necessary functions...
432 WARNING: DEFUN/DEFMACRO: redefining function
433 TOPLEVEL-MACSYMA-EVAL ...
434 WARNING: DEFUN/DEFMACRO: redefining function MSETCHK ...
435 WARNING: DEFUN/DEFMACRO: redefining function KILL1 ...
436 WARNING: DEFUN/DEFMACRO: redefining function NFORMAT ...
437 Initializing unit arrays...
439 User defaults found...
440 User defaults initialized.
459 (%i6) setunits([dyn,eV]);
468 (%o8) (6241509596477042688) (eV)
472 (%o9) (6241509596477042688) (eV m)
476 (%o10) (6241509596477042688000) (eV)
479 (%i11) uforget([dyn,eV]);
480 (%o11) [false, false]
499 Without @code{usersetunits}, the initial inputs would have been converted
500 to MKS, and uforget would have resulted in a return to MKS rules. Instead,
501 the user preferences are respected in both cases. Notice these can still
502 be overridden if desired. To completely eliminate this simplification - i.e.
503 to have the user defaults reset to factory defaults - the @code{dontusedimension}
504 command can be used. @code{uforget} can restore user settings again, but
505 only if @code{usedimension} frees it for use. Alternately,
506 @code{kill(usersetunits)} will completely remove all knowledge of the user defaults
507 from the session. Here are some examples of how these various options work.
518 (%i4) setunits([dyn,eV]);
527 (%o6) (6241509596477042688) (eV)
530 (%i7) uforget([dyn,eV]);
542 (%i10) dontusedimension(N);
546 (%i11) dontusedimension(J);
547 (%o11) [%energy, %force]
565 (%i14) setunits([dyn,eV]);
584 (%i17) uforget([dyn,eV]);
585 (%o17) [false, false]
603 (%i20) usedimension(N);
604 Done. To have Maxima simplify to this dimension, use
605 setunits([unit]) to select a unit.
609 (%i21) usedimension(J);
610 Done. To have Maxima simplify to this dimension, use
611 setunits([unit]) to select a unit.
630 (%i24) setunits([dyn,eV]);
635 (%o25) (100000) (dyn)
639 (%o26) (6241509596477042688) (eV)
642 (%i27) uforget([dyn,eV]);
643 (%o27) [false, false]
654 (%i30) kill(usersetunits);
658 (%i31) uforget([dyn,eV]);
659 (%o31) [false, false]
677 Unfortunately this wide variety of options is a little confusing at first,
678 but once the user grows used to them they should find they have very full
679 control over their working environment.
681 @c One other significant customization option available is the @code{setunitprefix}
682 @c command. Normally, abbreviations used in this package are as close to those
683 @c used in standard texts as possible. Some people, however, prefer to use those
684 @c symbols for normal work and have units labeled in some other fashion.
685 @c @code{setunitprefix} is provided for this case. Here is an example of its use:
687 @opencatbox{Categories:}
688 @category{Package unit}
694 @anchor{metricexpandall}
695 @deffn {Function} metricexpandall (@var{x})
696 Rebuilds global unit lists automatically creating all desired metric units.
697 @var{x} is a numerical argument which is used to specify how many metric
698 prefixes the user wishes defined. The arguments are as follows, with each
699 higher number defining all lower numbers' units:
702 0 - none. Only base units
703 1 - kilo, centi, milli
704 (default) 2 - giga, mega, kilo, hecto, deka, deci, centi, milli,
706 3 - peta, tera, giga, mega, kilo, hecto, deka, deci,
707 centi, milli, micro, nano, pico, femto
711 Normally, Maxima will not define the full expansion since this results in a
712 very large number of units, but @code{metricexpandall} can be used to
713 rebuild the list in a more or less complete fashion. The relevant variable
714 in the @emph{unit.mac} file is @var{%unitexpand}.
715 @c This should be made configurable as a maxima-init.mac controllable option.
717 @opencatbox{Categories:}
718 @category{Package unit}
723 @defvr {Variable} %unitexpand
724 Default value: @code{2}
726 This is the value supplied to @code{metricexpandall} during the initial loading
729 @opencatbox{Categories:}
730 @category{Package unit}
736 @c @deffn {Function} functionname (@var{arg1}, @var{arg2}, ..., @var{argn})
739 @c @defvr {Optional variable} variablename
740 @c Default value: @code{true}