/[emacs]/emacs/lisp/calc/calc-units.el
ViewVC logotype

Diff of /emacs/lisp/calc/calc-units.el

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.2 by walters, Wed Nov 14 09:08:03 2001 UTC revision 1.3 by walters, Mon Nov 19 07:42:20 2001 UTC
# Line 1  Line 1 
1  ;; Calculator for GNU Emacs, part II [calc-units.el]  ;;; calc-units.el --- unit conversion functions for Calc
2    
3  ;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc.  ;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc.
4  ;; Written by Dave Gillespie, daveg@synaptics.com.  
5    ;; Author: David Gillespie <daveg@synaptics.com>
6    ;; Maintainer: Colin Walters <walters@debian.org>
7    
8  ;; This file is part of GNU Emacs.  ;; This file is part of GNU Emacs.
9    
# Line 19  Line 22 
22  ;; file named COPYING.  Among other things, the copyright notice  ;; file named COPYING.  Among other things, the copyright notice
23  ;; and this notice must be preserved on all copies.  ;; and this notice must be preserved on all copies.
24    
25    ;;; Commentary:
26    
27    ;;; Code:
28    
29  ;; This file is autoloaded from calc-ext.el.  ;; This file is autoloaded from calc-ext.el.
30  (require 'calc-ext)  (require 'calc-ext)
# Line 28  Line 33 
33    
34  (defun calc-Need-calc-units () nil)  (defun calc-Need-calc-units () nil)
35    
36    ;;; Units operations.
37    
38    ;;; Units table last updated 9-Jan-91 by Ulrich Mueller (ulm@vsnhd1.cern.ch)
39    ;;; with some additions by Przemek Klosowski (przemek@rrdstrad.nist.gov)
40    
41    (defvar math-standard-units
42      '( ;; Length
43         ( m       nil                   "*Meter" )
44         ( in      "2.54 cm"             "Inch" )
45         ( ft      "12 in"               "Foot" )
46         ( yd      "3 ft"                "Yard" )
47         ( mi      "5280 ft"             "Mile" )
48         ( au      "1.495979e11 m"       "Astronomical Unit" )
49         ( lyr     "9460536207068016 m"  "Light Year" )
50         ( pc      "206264.80625 au"     "Parsec" )
51         ( nmi     "1852 m"              "Nautical Mile" )
52         ( fath    "6 ft"                "Fathom" )
53         ( u       "1 um"                "Micron" )
54         ( mil     "in/1000"             "Mil" )
55         ( point   "in/72"               "Point (1/72 inch)" )
56         ( tpt     "in/72.27"            "Point (TeX conventions)" )
57         ( Ang     "1e-10 m"             "Angstrom" )
58         ( mfi     "mi+ft+in"            "Miles + feet + inches" )
59        
60         ;; Area
61         ( hect    "10000 m^2"           "*Hectare" )
62         ( acre    "mi^2 / 640"          "Acre" )
63         ( b       "1e-28 m^2"           "Barn" )
64        
65         ;; Volume
66         ( l       "1e-3 m^3"            "*Liter" )
67         ( L       "1e-3 m^3"            "Liter" )
68         ( gal     "4 qt"                "US Gallon" )
69         ( qt      "2 pt"                "Quart" )
70         ( pt      "2 cup"               "Pint" )
71         ( cup     "8 ozfl"              "Cup" )
72         ( ozfl    "2 tbsp"              "Fluid Ounce" )
73         ( floz    "2 tbsp"              "Fluid Ounce" )
74         ( tbsp    "3 tsp"               "Tablespoon" )
75         ( tsp     "4.92892159375 ml"    "Teaspoon" )
76         ( vol     "tsp+tbsp+ozfl+cup+pt+qt+gal" "Gallons + ... + teaspoons" )
77         ( galC    "4.54609 l"           "Canadian Gallon" )
78         ( galUK   "4.546092 l"          "UK Gallon" )
79        
80         ;; Time
81         ( s       nil                   "*Second" )
82         ( sec     "s"                   "Second" )
83         ( min     "60 s"                "Minute" )
84         ( hr      "60 min"              "Hour" )
85         ( day     "24 hr"               "Day" )
86         ( wk      "7 day"               "Week" )
87         ( hms     "wk+day+hr+min+s"     "Hours, minutes, seconds" )
88         ( yr      "365.25 day"          "Year" )
89         ( Hz      "1/s"                 "Hertz" )
90    
91         ;; Speed
92         ( mph     "mi/hr"               "*Miles per hour" )
93         ( kph     "km/hr"               "Kilometers per hour" )
94         ( knot    "nmi/hr"              "Knot" )
95         ( c       "2.99792458e8 m/s"    "Speed of light" )    
96        
97         ;; Acceleration
98         ( ga      "9.80665 m/s^2"       "*\"g\" acceleration" )
99    
100         ;; Mass
101         ( g       nil                   "*Gram" )
102         ( lb      "16 oz"               "Pound (mass)" )
103         ( oz      "28.349523125 g"      "Ounce (mass)" )
104         ( ton     "2000 lb"             "Ton" )
105         ( tpo     "ton+lb+oz"           "Tons + pounds + ounces (mass)" )
106         ( t       "1000 kg"             "Metric ton" )
107         ( tonUK   "1016.0469088 kg"     "UK ton" )
108         ( lbt     "12 ozt"              "Troy pound" )
109         ( ozt     "31.103475 g"         "Troy ounce" )
110         ( ct      ".2 g"                "Carat" )
111         ( amu     "1.6605402e-24 g"     "Unified atomic mass" )
112    
113         ;; Force
114         ( N       "m kg/s^2"            "*Newton" )
115         ( dyn     "1e-5 N"              "Dyne" )
116         ( gf      "ga g"                "Gram (force)" )
117         ( lbf     "4.44822161526 N"     "Pound (force)" )
118         ( kip     "1000 lbf"            "Kilopound (force)" )
119         ( pdl     "0.138255 N"          "Poundal" )
120    
121         ;; Energy
122         ( J       "N m"                 "*Joule" )
123         ( erg     "1e-7 J"              "Erg" )
124         ( cal     "4.1868 J"            "International Table Calorie" )
125         ( Btu     "1055.05585262 J"     "International Table Btu" )
126         ( eV      "ech V"               "Electron volt" )
127         ( ev      "eV"                  "Electron volt" )
128         ( therm   "105506000 J"         "EEC therm" )
129         ( invcm   "h c/cm"              "Energy in inverse centimeters" )
130         ( Kayser  "invcm"               "Kayser (inverse centimeter energy)" )
131         ( men     "100/invcm"           "Inverse energy in meters" )
132         ( Hzen    "h Hz"                "Energy in Hertz")
133         ( Ken     "k K"                 "Energy in Kelvins")
134         ;; ( invcm   "eV / 8065.47835185"    "Energy in inverse centimeters" )
135         ;; ( Hzen    "eV / 2.41796958004e14" "Energy in Hertz")
136         ;; ( Ken     "eV / 11604.7967327"    "Energy in Kelvins")
137    
138         ;; Power
139         ( W       "J/s"                 "*Watt" )
140         ( hp      "745.7 W"             "Horsepower" )
141    
142         ;; Temperature
143         ( K       nil                   "*Degree Kelvin"     K )
144         ( dK      "K"                   "Degree Kelvin"      K )
145         ( degK    "K"                   "Degree Kelvin"      K )
146         ( dC      "K"                   "Degree Celsius"     C )
147         ( degC    "K"                   "Degree Celsius"     C )
148         ( dF      "(5/9) K"             "Degree Fahrenheit"  F )
149         ( degF    "(5/9) K"             "Degree Fahrenheit"  F )
150    
151         ;; Pressure
152         ( Pa      "N/m^2"               "*Pascal" )
153         ( bar     "1e5 Pa"              "Bar" )
154         ( atm     "101325 Pa"           "Standard atmosphere" )
155         ( torr    "atm/760"             "Torr" )
156         ( mHg     "1000 torr"           "Meter of mercury" )
157         ( inHg    "25.4 mmHg"           "Inch of mercury" )
158         ( inH2O   "248.84 Pa"           "Inch of water" )
159         ( psi     "6894.75729317 Pa"    "Pound per square inch" )
160    
161         ;; Viscosity
162         ( P       "0.1 Pa s"            "*Poise" )
163         ( St      "1e-4 m^2/s"          "Stokes" )
164    
165         ;; Electromagnetism
166         ( A       nil                   "*Ampere" )
167         ( C       "A s"                 "Coulomb" )
168         ( Fdy     "ech Nav"             "Faraday" )
169         ( e       "1.60217733e-19 C"    "Elementary charge" )
170         ( ech     "1.60217733e-19 C"    "Elementary charge" )
171         ( V       "W/A"                 "Volt" )
172         ( ohm     "V/A"                 "Ohm" )
173         ( mho     "A/V"                 "Mho" )
174         ( S       "A/V"                 "Siemens" )
175         ( F       "C/V"                 "Farad" )
176         ( H       "Wb/A"                "Henry" )
177         ( T       "Wb/m^2"              "Tesla" )
178         ( G       "1e-4 T"              "Gauss" )
179         ( Wb      "V s"                 "Weber" )
180    
181         ;; Luminous intensity
182         ( cd      nil                   "*Candela" )
183         ( sb      "1e4 cd/m^2"          "Stilb" )
184         ( lm      "cd sr"               "Lumen" )
185         ( lx      "lm/m^2"              "Lux" )
186         ( ph      "1e4 lx"              "Phot" )
187         ( fc      "10.76 lx"            "Footcandle" )
188         ( lam     "1e4 lm/m^2"          "Lambert" )
189         ( flam    "1.07639104e-3 lam"   "Footlambert" )
190    
191         ;; Radioactivity
192         ( Bq      "1/s"                 "*Becquerel" )
193         ( Ci      "3.7e10 Bq"           "Curie" )
194         ( Gy      "J/kg"                "Gray" )
195         ( Sv      "Gy"                  "Sievert" )
196         ( R       "2.58e-4 C/kg"        "Roentgen" )
197         ( rd      ".01 Gy"              "Rad" )
198         ( rem     "rd"                  "Rem" )
199    
200         ;; Amount of substance
201         ( mol     nil                   "*Mole" )
202    
203         ;; Plane angle
204         ( rad     nil                   "*Radian" )
205         ( circ    "2 pi rad"            "Full circle" )
206         ( rev     "circ"                "Full revolution" )
207         ( deg     "circ/360"            "Degree" )
208         ( arcmin  "deg/60"              "Arc minute" )
209         ( arcsec  "arcmin/60"           "Arc second" )
210         ( grad    "circ/400"            "Grade" )
211         ( rpm     "rev/min"             "Revolutions per minute" )
212    
213         ;; Solid angle
214         ( sr      nil                   "*Steradian" )
215    
216         ;; Other physical quantities (Physics Letters B239, 1 (1990))
217         ( h       "6.6260755e-34 J s"   "*Planck's constant" )
218         ( hbar    "h / 2 pi"            "Planck's constant" )
219         ( mu0     "4 pi 1e-7 H/m"       "Permeability of vacuum" )
220         ( Grav    "6.67259e-11 N m^2/kg^2"  "Gravitational constant" )
221         ( Nav     "6.0221367e23 / mol"  "Avagadro's constant" )
222         ( me      "0.51099906 MeV/c^2"  "Electron rest mass" )
223         ( mp      "1.007276470 amu"     "Proton rest mass" )
224         ( mn      "1.008664904 amu"     "Neutron rest mass" )
225         ( mu      "0.113428913 amu"     "Muon rest mass" )
226         ( Ryd     "1.0973731571e5 invcm" "Rydberg's constant" )
227         ( k       "1.3806513e-23 J/K"   "Boltzmann's constant" )
228         ( fsc     "1 / 137.0359895"     "Fine structure constant" )
229         ( muB     "5.78838263e-11 MeV/T"  "Bohr magneton" )
230         ( muN     "3.15245166e-14 MeV/T"  "Nuclear magneton" )
231         ( mue     "1.001159652193 muB"  "Electron magnetic moment" )
232         ( mup     "2.792847386 muN"     "Proton magnetic moment" )
233         ( R0      "Nav k"               "Molar gas constant" )
234         ( V0      "22.413992 L/mol"     "Standard volume of ideal gas" )))
235    
236    
237    (defvar math-additional-units nil
238      "*Additional units table for user-defined units.
239    Must be formatted like math-standard-units.
240    If this is changed, be sure to set math-units-table to nil to ensure
241    that the combined units table will be rebuilt.")
242    
243    (defvar math-unit-prefixes
244      '( ( ?E  (float 1 18)  "Exa"    )
245         ( ?P  (float 1 15)  "Peta"   )
246         ( ?T  (float 1 12)  "Tera"   )
247         ( ?G  (float 1 9)   "Giga"   )
248         ( ?M  (float 1 6)   "Mega"   )
249         ( ?k  (float 1 3)   "Kilo"   )
250         ( ?K  (float 1 3)   "Kilo"   )
251         ( ?h  (float 1 2)   "Hecto"  )
252         ( ?H  (float 1 2)   "Hecto"  )
253         ( ?D  (float 1 1)   "Deka"   )
254         ( 0   (float 1 0)   nil      )
255         ( ?d  (float 1 -1)  "Deci"   )
256         ( ?c  (float 1 -2)  "Centi"  )
257         ( ?m  (float 1 -3)  "Milli"  )
258         ( ?u  (float 1 -6)  "Micro"  )
259         ( ?n  (float 1 -9)  "Nano"   )
260         ( ?p  (float 1 -12) "Pico"   )
261         ( ?f  (float 1 -15) "Femto"  )
262         ( ?a  (float 1 -18) "Atto"   )))
263    
264    (defvar math-standard-units-systems
265      '( ( base  nil )
266         ( si    ( ( g   '(* (var kg var-kg) (float 1 -3)) ) ) )
267         ( mks   ( ( g   '(* (var kg var-kg) (float 1 -3)) ) ) )
268         ( cgs   ( ( m   '(* (var cm var-cm) 100         ) ) ) )))
269    
270    (defvar math-units-table nil
271      "Internal units table derived from math-defined-units.
272    Entries are (SYMBOL EXPR DOC-STRING TEMP-TYPE BASE-UNITS).")
273    
274    (defvar math-units-table-buffer-valid nil)
275    
276  ;;; Units commands.  ;;; Units commands.
277    
# Line 46  Line 290 
290            (pos (if (= num 0) 10 num))            (pos (if (= num 0) 10 num))
291            (units (calc-var-value 'var-Units))            (units (calc-var-value 'var-Units))
292            (expr (calc-top-n 1)))            (expr (calc-top-n 1)))
293       (or (and (>= num 0) (<= num 9))       (unless (and (>= num 0) (<= num 9))
294           (error "Bad unit number"))         (errunless "Bad unit number"))
295       (or (math-vectorp units)       (unless (math-vectorp units)
296           (error "No \"quick units\" are defined"))         (errunless "No \"quick units\" are defined"))
297       (or (< pos (length units))       (unless (< pos (length units))
298           (error "Unit number %d not defined" pos))         (errunless "Unit number %d not defined" pos))
299       (if (math-units-in-expr-p expr nil)       (if (math-units-in-expr-p expr nil)
300           (calc-enter-result 1 (format "cun%d" num)           (calc-enter-result 1 (format "cun%d" num)
301                              (math-convert-units expr (nth pos units)))                              (math-convert-units expr (nth pos units)))
# Line 65  Line 309 
309     (let ((expr (calc-top-n 1))     (let ((expr (calc-top-n 1))
310           (uoldname nil)           (uoldname nil)
311           unew)           unew)
312       (or (math-units-in-expr-p expr t)       (unless (math-units-in-expr-p expr t)
313           (let ((uold (or old-units         (let ((uold (or old-units
314                           (progn                         (progn
315                             (setq uoldname (read-string "Old units: "))                           (setq uoldname (read-string "Old units: "))
316                             (if (equal uoldname "")                           (if (equal uoldname "")
317                                 (progn                               (progn
318                                   (setq uoldname "1")                                 (setq uoldname "1")
319                                   1)                                 1)
320                               (if (string-match "\\` */" uoldname)                             (if (string-match "\\` */" uoldname)
321                                   (setq uoldname (concat "1" uoldname)))                                 (setq uoldname (concat "1" uoldname)))
322                               (math-read-expr uoldname))))))                             (math-read-expr uoldname))))))
323             (if (eq (car-safe uold) 'error)           (when (eq (car-safe uold) 'error)
324                 (error "Bad format in units expression: %s" (nth 1 uold)))             (error "Bad format in units expression: %s" (nth 1 uold)))
325             (setq expr (math-mul expr uold))))           (setq expr (math-mul expr uold))))
326       (or new-units       (unless new-units
327           (setq new-units (read-string (if uoldname         (setq new-units (read-string (if uoldname
328                                            (concat "Old units: "                                          (concat "Old units: "
329                                                    uoldname                                                  uoldname
330                                                    ", new units: ")                                                  ", new units: ")
331                                          "New units: "))))                                        "New units: "))))
332       (if (string-match "\\` */" new-units)       (when (string-match "\\` */" new-units)
333           (setq new-units (concat "1" new-units)))         (setq new-units (concat "1" new-units)))
334       (setq units (math-read-expr new-units))       (setq units (math-read-expr new-units))
335       (if (eq (car-safe units) 'error)       (when (eq (car-safe units) 'error)
336           (error "Bad format in units expression: %s" (nth 2 units)))         (error "Bad format in units expression: %s" (nth 2 units)))
337       (let ((unew (math-units-in-expr-p units t))       (let ((unew (math-units-in-expr-p units t))
338             (std (and (eq (car-safe units) 'var)             (std (and (eq (car-safe units) 'var)
339                       (assq (nth 1 units) math-standard-units-systems))))                       (assq (nth 1 units) math-standard-units-systems))))
# Line 97  Line 341 
341             (calc-enter-result 1 "cvun" (math-simplify-units             (calc-enter-result 1 "cvun" (math-simplify-units
342                                          (math-to-standard-units expr                                          (math-to-standard-units expr
343                                                                  (nth 1 std))))                                                                  (nth 1 std))))
344           (or unew           (unless unew
345               (error "No units specified"))             (error "No units specified"))
346           (calc-enter-result 1 "cvun"           (calc-enter-result 1 "cvun"
347                              (math-convert-units                              (math-convert-units
348                               expr units                               expr units
# Line 109  Line 353 
353    (calc-wrapper    (calc-wrapper
354     (calc-change-mode 'calc-autorange-units arg nil t)     (calc-change-mode 'calc-autorange-units arg nil t)
355     (message (if calc-autorange-units     (message (if calc-autorange-units
356                  "Adjusting target unit prefix automatically."                  "Adjusting target unit prefix automatically"
357                "Using target units exactly."))))                "Using target units exactly"))))
358    
359  (defun calc-convert-temperature (&optional old-units new-units)  (defun calc-convert-temperature (&optional old-units new-units)
360    (interactive)    (interactive)
# Line 131  Line 375 
375                          (math-read-expr                          (math-read-expr
376                           (setq uoldname (read-string                           (setq uoldname (read-string
377                                           "Old temperature units: ")))))))                                           "Old temperature units: ")))))))
378       (if (eq (car-safe uold) 'error)       (when (eq (car-safe uold) 'error)
379           (error "Bad format in units expression: %s" (nth 2 uold)))         (error "Bad format in units expression: %s" (nth 2 uold)))
380       (or (math-units-in-expr-p expr nil)       (or (math-units-in-expr-p expr nil)
381           (setq expr (math-mul expr uold)))           (setq expr (math-mul expr uold)))
382       (setq unew (or new-units       (setq unew (or new-units
# Line 142  Line 386 
386                                                uoldname                                                uoldname
387                                                ", new units: ")                                                ", new units: ")
388                                      "New temperature units: ")))))                                      "New temperature units: ")))))
389       (if (eq (car-safe unew) 'error)       (when (eq (car-safe unew) 'error)
390           (error "Bad format in units expression: %s" (nth 2 unew)))         (error "Bad format in units expression: %s" (nth 2 unew)))
391       (calc-enter-result 1 "cvtm" (math-simplify-units       (calc-enter-result 1 "cvtm" (math-simplify-units
392                                    (math-convert-temperature expr uold unew                                    (math-convert-temperature expr uold unew
393                                                              uoldname))))))                                                              uoldname))))))
# Line 258  Line 502 
502    (interactive "P")    (interactive "P")
503    (and n (setq math-units-table-buffer-valid nil))    (and n (setq math-units-table-buffer-valid nil))
504    (math-build-units-table-buffer t)    (math-build-units-table-buffer t)
505    (message (substitute-command-keys "Type \\[calc] to return to the Calculator.")))    (message (substitute-command-keys "Type \\[calc] to return to the Calculator")))
506    
507  (defun calc-define-unit (uname desc)  (defun calc-define-unit (uname desc)
508    (interactive "SDefine unit name: \nsDescription: ")    (interactive "SDefine unit name: \nsDescription: ")
# Line 367  Line 611 
611    
612    
613    
   
   
 ;;; Units operations.  
   
 ;;; Units table last updated 9-Jan-91 by Ulrich Mueller (ulm@vsnhd1.cern.ch)  
 ;;; with some additions by Przemek Klosowski (przemek@rrdstrad.nist.gov)  
   
 (defvar math-standard-units  
   '( ;; Length  
      ( m       nil                   "*Meter" )  
      ( in      "2.54 cm"             "Inch" )  
      ( ft      "12 in"               "Foot" )  
      ( yd      "3 ft"                "Yard" )  
      ( mi      "5280 ft"             "Mile" )  
      ( au      "1.495979e11 m"       "Astronomical Unit" )  
      ( lyr     "9460536207068016 m"  "Light Year" )  
      ( pc      "206264.80625 au"     "Parsec" )  
      ( nmi     "1852 m"              "Nautical Mile" )  
      ( fath    "6 ft"                "Fathom" )  
      ( u       "1 um"                "Micron" )  
      ( mil     "in/1000"             "Mil" )  
      ( point   "in/72"               "Point (1/72 inch)" )  
      ( tpt     "in/72.27"            "Point (TeX conventions)" )  
      ( Ang     "1e-10 m"             "Angstrom" )  
      ( mfi     "mi+ft+in"            "Miles + feet + inches" )  
       
      ;; Area  
      ( hect    "10000 m^2"           "*Hectare" )  
      ( acre    "mi^2 / 640"          "Acre" )  
      ( b       "1e-28 m^2"           "Barn" )  
       
      ;; Volume  
      ( l       "1e-3 m^3"            "*Liter" )  
      ( L       "1e-3 m^3"            "Liter" )  
      ( gal     "4 qt"                "US Gallon" )  
      ( qt      "2 pt"                "Quart" )  
      ( pt      "2 cup"               "Pint" )  
      ( cup     "8 ozfl"              "Cup" )  
      ( ozfl    "2 tbsp"              "Fluid Ounce" )  
      ( floz    "2 tbsp"              "Fluid Ounce" )  
      ( tbsp    "3 tsp"               "Tablespoon" )  
      ( tsp     "4.92892159375 ml"    "Teaspoon" )  
      ( vol     "tsp+tbsp+ozfl+cup+pt+qt+gal" "Gallons + ... + teaspoons" )  
      ( galC    "4.54609 l"           "Canadian Gallon" )  
      ( galUK   "4.546092 l"          "UK Gallon" )  
       
      ;; Time  
      ( s       nil                   "*Second" )  
      ( sec     "s"                   "Second" )  
      ( min     "60 s"                "Minute" )  
      ( hr      "60 min"              "Hour" )  
      ( day     "24 hr"               "Day" )  
      ( wk      "7 day"               "Week" )  
      ( hms     "wk+day+hr+min+s"     "Hours, minutes, seconds" )  
      ( yr      "365.25 day"          "Year" )  
      ( Hz      "1/s"                 "Hertz" )  
   
      ;; Speed  
      ( mph     "mi/hr"               "*Miles per hour" )  
      ( kph     "km/hr"               "Kilometers per hour" )  
      ( knot    "nmi/hr"              "Knot" )  
      ( c       "2.99792458e8 m/s"    "Speed of light" )      
       
      ;; Acceleration  
      ( ga      "9.80665 m/s^2"       "*\"g\" acceleration" )  
   
      ;; Mass  
      ( g       nil                   "*Gram" )  
      ( lb      "16 oz"               "Pound (mass)" )  
      ( oz      "28.349523125 g"      "Ounce (mass)" )  
      ( ton     "2000 lb"             "Ton" )  
      ( tpo     "ton+lb+oz"           "Tons + pounds + ounces (mass)" )  
      ( t       "1000 kg"             "Metric ton" )  
      ( tonUK   "1016.0469088 kg"     "UK ton" )  
      ( lbt     "12 ozt"              "Troy pound" )  
      ( ozt     "31.103475 g"         "Troy ounce" )  
      ( ct      ".2 g"                "Carat" )  
      ( amu     "1.6605402e-24 g"     "Unified atomic mass" )  
   
      ;; Force  
      ( N       "m kg/s^2"            "*Newton" )  
      ( dyn     "1e-5 N"              "Dyne" )  
      ( gf      "ga g"                "Gram (force)" )  
      ( lbf     "4.44822161526 N"     "Pound (force)" )  
      ( kip     "1000 lbf"            "Kilopound (force)" )  
      ( pdl     "0.138255 N"          "Poundal" )  
   
      ;; Energy  
      ( J       "N m"                 "*Joule" )  
      ( erg     "1e-7 J"              "Erg" )  
      ( cal     "4.1868 J"            "International Table Calorie" )  
      ( Btu     "1055.05585262 J"     "International Table Btu" )  
      ( eV      "ech V"               "Electron volt" )  
      ( ev      "eV"                  "Electron volt" )  
      ( therm   "105506000 J"         "EEC therm" )  
      ( invcm   "h c/cm"              "Energy in inverse centimeters" )  
      ( Kayser  "invcm"               "Kayser (inverse centimeter energy)" )  
      ( men     "100/invcm"           "Inverse energy in meters" )  
      ( Hzen    "h Hz"                "Energy in Hertz")  
      ( Ken     "k K"                 "Energy in Kelvins")  
      ;; ( invcm   "eV / 8065.47835185"    "Energy in inverse centimeters" )  
      ;; ( Hzen    "eV / 2.41796958004e14" "Energy in Hertz")  
      ;; ( Ken     "eV / 11604.7967327"    "Energy in Kelvins")  
   
      ;; Power  
      ( W       "J/s"                 "*Watt" )  
      ( hp      "745.7 W"             "Horsepower" )  
   
      ;; Temperature  
      ( K       nil                   "*Degree Kelvin"     K )  
      ( dK      "K"                   "Degree Kelvin"      K )  
      ( degK    "K"                   "Degree Kelvin"      K )  
      ( dC      "K"                   "Degree Celsius"     C )  
      ( degC    "K"                   "Degree Celsius"     C )  
      ( dF      "(5/9) K"             "Degree Fahrenheit"  F )  
      ( degF    "(5/9) K"             "Degree Fahrenheit"  F )  
   
      ;; Pressure  
      ( Pa      "N/m^2"               "*Pascal" )  
      ( bar     "1e5 Pa"              "Bar" )  
      ( atm     "101325 Pa"           "Standard atmosphere" )  
      ( torr    "atm/760"             "Torr" )  
      ( mHg     "1000 torr"           "Meter of mercury" )  
      ( inHg    "25.4 mmHg"           "Inch of mercury" )  
      ( inH2O   "248.84 Pa"           "Inch of water" )  
      ( psi     "6894.75729317 Pa"    "Pound per square inch" )  
   
      ;; Viscosity  
      ( P       "0.1 Pa s"            "*Poise" )  
      ( St      "1e-4 m^2/s"          "Stokes" )  
   
      ;; Electromagnetism  
      ( A       nil                   "*Ampere" )  
      ( C       "A s"                 "Coulomb" )  
      ( Fdy     "ech Nav"             "Faraday" )  
      ( e       "1.60217733e-19 C"    "Elementary charge" )  
      ( ech     "1.60217733e-19 C"    "Elementary charge" )  
      ( V       "W/A"                 "Volt" )  
      ( ohm     "V/A"                 "Ohm" )  
      ( mho     "A/V"                 "Mho" )  
      ( S       "A/V"                 "Siemens" )  
      ( F       "C/V"                 "Farad" )  
      ( H       "Wb/A"                "Henry" )  
      ( T       "Wb/m^2"              "Tesla" )  
      ( G       "1e-4 T"              "Gauss" )  
      ( Wb      "V s"                 "Weber" )  
   
      ;; Luminous intensity  
      ( cd      nil                   "*Candela" )  
      ( sb      "1e4 cd/m^2"          "Stilb" )  
      ( lm      "cd sr"               "Lumen" )  
      ( lx      "lm/m^2"              "Lux" )  
      ( ph      "1e4 lx"              "Phot" )  
      ( fc      "10.76 lx"            "Footcandle" )  
      ( lam     "1e4 lm/m^2"          "Lambert" )  
      ( flam    "1.07639104e-3 lam"   "Footlambert" )  
   
      ;; Radioactivity  
      ( Bq      "1/s"                 "*Becquerel" )  
      ( Ci      "3.7e10 Bq"           "Curie" )  
      ( Gy      "J/kg"                "Gray" )  
      ( Sv      "Gy"                  "Sievert" )  
      ( R       "2.58e-4 C/kg"        "Roentgen" )  
      ( rd      ".01 Gy"              "Rad" )  
      ( rem     "rd"                  "Rem" )  
   
      ;; Amount of substance  
      ( mol     nil                   "*Mole" )  
   
      ;; Plane angle  
      ( rad     nil                   "*Radian" )  
      ( circ    "2 pi rad"            "Full circle" )  
      ( rev     "circ"                "Full revolution" )  
      ( deg     "circ/360"            "Degree" )  
      ( arcmin  "deg/60"              "Arc minute" )  
      ( arcsec  "arcmin/60"           "Arc second" )  
      ( grad    "circ/400"            "Grade" )  
      ( rpm     "rev/min"             "Revolutions per minute" )  
   
      ;; Solid angle  
      ( sr      nil                   "*Steradian" )  
   
      ;; Other physical quantities (Physics Letters B239, 1 (1990))  
      ( h       "6.6260755e-34 J s"   "*Planck's constant" )  
      ( hbar    "h / 2 pi"            "Planck's constant" )  
      ( mu0     "4 pi 1e-7 H/m"       "Permeability of vacuum" )  
      ( Grav    "6.67259e-11 N m^2/kg^2"  "Gravitational constant" )  
      ( Nav     "6.0221367e23 / mol"  "Avagadro's constant" )  
      ( me      "0.51099906 MeV/c^2"  "Electron rest mass" )  
      ( mp      "1.007276470 amu"     "Proton rest mass" )  
      ( mn      "1.008664904 amu"     "Neutron rest mass" )  
      ( mu      "0.113428913 amu"     "Muon rest mass" )  
      ( Ryd     "1.0973731571e5 invcm" "Rydberg's constant" )  
      ( k       "1.3806513e-23 J/K"   "Boltzmann's constant" )  
      ( fsc     "1 / 137.0359895"     "Fine structure constant" )  
      ( muB     "5.78838263e-11 MeV/T"  "Bohr magneton" )  
      ( muN     "3.15245166e-14 MeV/T"  "Nuclear magneton" )  
      ( mue     "1.001159652193 muB"  "Electron magnetic moment" )  
      ( mup     "2.792847386 muN"     "Proton magnetic moment" )  
      ( R0      "Nav k"               "Molar gas constant" )  
      ( V0      "22.413992 L/mol"     "Standard volume of ideal gas" )  
 ))  
   
   
 (defvar math-additional-units nil  
   "*Additional units table for user-defined units.  
 Must be formatted like math-standard-units.  
 If this is changed, be sure to set math-units-table to nil to ensure  
 that the combined units table will be rebuilt.")  
   
 (defvar math-unit-prefixes  
   '( ( ?E  (float 1 18)  "Exa"    )  
      ( ?P  (float 1 15)  "Peta"   )  
      ( ?T  (float 1 12)  "Tera"   )  
      ( ?G  (float 1 9)   "Giga"   )  
      ( ?M  (float 1 6)   "Mega"   )  
      ( ?k  (float 1 3)   "Kilo"   )  
      ( ?K  (float 1 3)   "Kilo"   )  
      ( ?h  (float 1 2)   "Hecto"  )  
      ( ?H  (float 1 2)   "Hecto"  )  
      ( ?D  (float 1 1)   "Deka"   )  
      ( 0   (float 1 0)   nil      )  
      ( ?d  (float 1 -1)  "Deci"   )  
      ( ?c  (float 1 -2)  "Centi"  )  
      ( ?m  (float 1 -3)  "Milli"  )  
      ( ?u  (float 1 -6)  "Micro"  )  
      ( ?n  (float 1 -9)  "Nano"   )  
      ( ?p  (float 1 -12) "Pico"   )  
      ( ?f  (float 1 -15) "Femto"  )  
      ( ?a  (float 1 -18) "Atto"   )  
 ))  
   
 (defvar math-standard-units-systems  
   '( ( base  nil )  
      ( si    ( ( g   '(* (var kg var-kg) (float 1 -3)) ) ) )  
      ( mks   ( ( g   '(* (var kg var-kg) (float 1 -3)) ) ) )  
      ( cgs   ( ( m   '(* (var cm var-cm) 100         ) ) ) )  
 ))  
   
 (defvar math-units-table nil  
   "Internal units table derived from math-defined-units.  
 Entries are (SYMBOL EXPR DOC-STRING TEMP-TYPE BASE-UNITS).")  
   
 (defvar math-units-table-buffer-valid nil)  
   
   
614  (defun math-build-units-table ()  (defun math-build-units-table ()
615    (or math-units-table    (or math-units-table
616        (let* ((combined-units (append math-additional-units        (let* ((combined-units (append math-additional-units
# Line 803  Entries are (SYMBOL EXPR DOC-STRING TEMP Line 801  Entries are (SYMBOL EXPR DOC-STRING TEMP
801                               expr                               expr
802                             (list '* expr units)))))                             (list '* expr units)))))
803    
804    (defvar math-decompose-units-cache nil)
805  (defun math-decompose-units (units)  (defun math-decompose-units (units)
806    (let ((u (math-check-unit-name units)))    (let ((u (math-check-unit-name units)))
807      (and u (eq (car-safe (nth 1 u)) '+)      (and u (eq (car-safe (nth 1 u)) '+)
# Line 821  Entries are (SYMBOL EXPR DOC-STRING TEMP Line 820  Entries are (SYMBOL EXPR DOC-STRING TEMP
820                 (setq ulist (cons (math-decompose-unit-part utemp) ulist)                 (setq ulist (cons (math-decompose-unit-part utemp) ulist)
821                       utemp ulist)                       utemp ulist)
822                 (while (setq utemp (cdr utemp))                 (while (setq utemp (cdr utemp))
823                   (or (equal (nth 2 (car utemp)) (nth 2 (car ulist)))                   (unless (equal (nth 2 (car utemp)) (nth 2 (car ulist)))
824                       (error "Inconsistent units in sum")))                     (error "Inconsistent units in sum")))
825                 (setq math-decompose-units-cache                 (setq math-decompose-units-cache
826                       (cons entry                       (cons entry
827                             (sort ulist                             (sort ulist
# Line 831  Entries are (SYMBOL EXPR DOC-STRING TEMP Line 830  Entries are (SYMBOL EXPR DOC-STRING TEMP
830                                      (not (Math-lessp (nth 1 x)                                      (not (Math-lessp (nth 1 x)
831                                                       (nth 1 y))))))))))                                                       (nth 1 y))))))))))
832           (cdr math-decompose-units-cache))))           (cdr math-decompose-units-cache))))
 (setq math-decompose-units-cache nil)  
833    
834  (defun math-decompose-unit-part (unit)  (defun math-decompose-unit-part (unit)
835    (cons unit    (cons unit
# Line 875  Entries are (SYMBOL EXPR DOC-STRING TEMP Line 873  Entries are (SYMBOL EXPR DOC-STRING TEMP
873                         (math-pow (math-div (car compat) new-units)                         (math-pow (math-div (car compat) new-units)
874                                   (cdr compat))                                   (cdr compat))
875                         nil))))                         nil))))
876          (if (setq unit-list (math-decompose-units new-units))          (when (setq unit-list (math-decompose-units new-units))
877              (setq new-units (nth 2 (car unit-list))))            (setq new-units (nth 2 (car unit-list))))
878          (if (eq (car-safe expr) '+)          (when (eq (car-safe expr) '+)
879              (setq expr (math-simplify-units expr)))            (setq expr (math-simplify-units expr)))
880          (if (math-units-in-expr-p expr t)          (if (math-units-in-expr-p expr t)
881              (math-convert-units-rec expr)              (math-convert-units-rec expr)
882            (math-apply-units (math-to-standard-units            (math-apply-units (math-to-standard-units
# Line 903  Entries are (SYMBOL EXPR DOC-STRING TEMP Line 901  Entries are (SYMBOL EXPR DOC-STRING TEMP
901                       (error "Inconsistent temperature units"))                       (error "Inconsistent temperature units"))
902                   units))                   units))
903           (unew (math-check-unit-name new)))           (unew (math-check-unit-name new)))
904      (or (and (consp unew) (nth 3 unew))      (unless (and (consp unew) (nth 3 unew))
905          (error "Not a valid temperature unit"))        (error "Not a valid temperature unit"))
906      (or (and (consp uold) (nth 3 uold))      (unless (and (consp uold) (nth 3 uold))
907          (error "Not a pure temperature expression"))        (error "Not a pure temperature expression"))
908      (let ((v (car uold)))      (let ((v (car uold)))
909        (setq expr (list '/ expr (list 'var v        (setq expr (list '/ expr (list 'var v
910                                       (intern (concat "var-"                                       (intern (concat "var-"
# Line 934  Entries are (SYMBOL EXPR DOC-STRING TEMP Line 932  Entries are (SYMBOL EXPR DOC-STRING TEMP
932    (let ((math-simplifying-units t)    (let ((math-simplifying-units t)
933          (calc-matrix-mode 'scalar))          (calc-matrix-mode 'scalar))
934      (math-simplify a)))      (math-simplify a)))
935  (defalias calcFunc-usimplify 'math-simplify-units)  (defalias 'calcFunc-usimplify 'math-simplify-units)
936    
937  (math-defsimplify (+ -)  (math-defsimplify (+ -)
938    (and math-simplifying-units    (and math-simplifying-units
# Line 1049  Entries are (SYMBOL EXPR DOC-STRING TEMP Line 1047  Entries are (SYMBOL EXPR DOC-STRING TEMP
1047    (let ((n (car np))    (let ((n (car np))
1048          d dd temp)          d dd temp)
1049      (while (eq (car-safe (setq d (car dp))) '*)      (while (eq (car-safe (setq d (car dp))) '*)
1050        (if (setq temp (math-simplify-units-quotient n (nth 1 d)))        (when (setq temp (math-simplify-units-quotient n (nth 1 d)))
1051            (progn          (setcar np (setq n temp))
1052              (setcar np (setq n temp))          (setcar (cdr d) 1))
             (setcar (cdr d) 1)))  
1053        (setq dp (cdr (cdr d))))        (setq dp (cdr (cdr d))))
1054      (if (setq temp (math-simplify-units-quotient n d))      (when (setq temp (math-simplify-units-quotient n d))
1055          (progn        (setcar np (setq n temp))
1056            (setcar np (setq n temp))        (setcar dp 1))))
           (setcar dp 1)))))  
1057    
1058  ;; Simplify, e.g., "in / cm" to "2.54" in a units expression.  ;; Simplify, e.g., "in / cm" to "2.54" in a units expression.
1059  (defun math-simplify-units-quotient (n d)  (defun math-simplify-units-quotient (n d)
1060    (let ((pow1 1)    (let ((pow1 1)
1061          (pow2 1))          (pow2 1))
1062      (and (eq (car-safe n) '^)      (when (and (eq (car-safe n) '^)
1063           (integerp (nth 2 n))                 (integerp (nth 2 n)))
1064           (setq pow1 (nth 2 n) n (nth 1 n)))        (setq pow1 (nth 2 n) n (nth 1 n)))
1065      (and (eq (car-safe d) '^)      (when (and (eq (car-safe d) '^)
1066           (integerp (nth 2 d))                 (integerp (nth 2 d)))
1067           (setq pow2 (nth 2 d) d (nth 1 d)))        (setq pow2 (nth 2 d) d (nth 1 d)))
1068      (let ((un (math-check-unit-name n))      (let ((un (math-check-unit-name n))
1069            (ud (math-check-unit-name d)))            (ud (math-check-unit-name d)))
1070        (and un ud        (and un ud
# Line 1229  Entries are (SYMBOL EXPR DOC-STRING TEMP Line 1225  Entries are (SYMBOL EXPR DOC-STRING TEMP
1225            (while uptr            (while uptr
1226              (setq u (car uptr)              (setq u (car uptr)
1227                    name (nth 2 u))                    name (nth 2 u))
1228              (if (eq (car u) 'm)              (when (eq (car u) 'm)
1229                  (setq std t))                (setq std t))
1230              (setq shadowed (and std (assq (car u) math-additional-units)))              (setq shadowed (and std (assq (car u) math-additional-units)))
1231              (if (and name              (when (and name
1232                       (> (length name) 1)                         (> (length name) 1)
1233                       (eq (aref name 0) ?\*))                         (eq (aref name 0) ?\*))
1234                  (progn                (unless (eq uptr math-units-table)
1235                    (or (eq uptr math-units-table)                  (insert "\n"))
1236                        (insert "\n"))                (setq name (substring name 1)))
                   (setq name (substring name 1))))  
1237              (insert " ")              (insert " ")
1238              (and shadowed (insert "("))              (and shadowed (insert "("))
1239              (insert (symbol-name (car u)))              (insert (symbol-name (car u)))
# Line 1259  Entries are (SYMBOL EXPR DOC-STRING TEMP Line 1254  Entries are (SYMBOL EXPR DOC-STRING TEMP
1254              (and shadowed (insert ")"))              (and shadowed (insert ")"))
1255              (indent-to 41)              (indent-to 41)
1256              (insert " ")              (insert " ")
1257              (if name              (when name
1258                  (insert name))                (insert name))
1259              (if shadowed              (if shadowed
1260                  (insert " (redefined above)")                  (insert " (redefined above)")
1261                (or (nth 1 u)                (unless (nth 1 u)
1262                    (insert " (base unit)")))                  (insert " (base unit)")))
1263              (insert "\n")              (insert "\n")
1264              (setq uptr (cdr uptr)))              (setq uptr (cdr uptr)))
1265            (insert "\n\nUnit Prefix Table:\n\n")            (insert "\n\nUnit Prefix Table:\n\n")

Legend:
Removed from v.1.2  
changed lines
  Added in v.1.3

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26