]> code.delx.au - gnu-emacs/blob - lisp/calc/calc-units.el
Add 2012 to FSF copyright years for Emacs files (do not merge to trunk)
[gnu-emacs] / lisp / calc / calc-units.el
1 ;;; calc-units.el --- unit conversion functions for Calc
2
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
5
6 ;; Author: David Gillespie <daveg@synaptics.com>
7 ;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com>
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 ;; This file is autoloaded from calc-ext.el.
29
30 (require 'calc-ext)
31 (require 'calc-macs)
32 (eval-when-compile
33 (require 'calc-alg))
34
35 ;;; Units operations.
36
37 ;;; Units table last updated 9-Jan-91 by Ulrich Mueller (ulm@vsnhd1.cern.ch)
38 ;;; with some additions by Przemek Klosowski (przemek@rrdstrad.nist.gov)
39 ;;; Updated April 2002 by Jochen Küpper
40
41 ;;; Updated August 2007, using
42 ;;; CODATA (http://physics.nist.gov/cuu/Constants/index.html)
43 ;;; NIST (http://physics.nist.gov/Pubs/SP811/appenB9.html)
44 ;;; ESUWM (Encyclopaedia of Scientific Units, Weights and
45 ;;; Measures, by François Cardarelli)
46 ;;; All conversions are exact unless otherwise noted.
47
48 (defvar math-standard-units
49 '( ;; Length
50 ( m nil "*Meter" )
51 ( in "254*10^(-2) cm" "Inch" nil
52 "2.54 cm")
53 ( ft "12 in" "Foot")
54 ( yd "3 ft" "Yard" )
55 ( mi "5280 ft" "Mile" )
56 ( au "149597870691. m" "Astronomical Unit" nil
57 "149597870691 m (*)")
58 ;; (approx) NASA JPL (http://neo.jpl.nasa.gov/glossary/au.html)
59 ( lyr "c yr" "Light Year" )
60 ( pc "3.0856775854*10^16 m" "Parsec" nil
61 "3.0856775854 10^16 m (*)") ;; (approx) ESUWM
62 ( nmi "1852 m" "Nautical Mile" )
63 ( fath "6 ft" "Fathom" )
64 ( fur "660 ft" "Furlong")
65 ( mu "1 um" "Micron" )
66 ( mil "(1/1000) in" "Mil" )
67 ( point "(1/72) in" "Point (1/72 inch)" )
68 ( Ang "10^(-10) m" "Angstrom" )
69 ( mfi "mi+ft+in" "Miles + feet + inches" )
70 ;; TeX lengths
71 ( texpt "(100/7227) in" "Point (TeX conventions)" )
72 ( texpc "12 texpt" "Pica" )
73 ( texbp "point" "Big point (TeX conventions)" )
74 ( texdd "(1238/1157) texpt" "Didot point" )
75 ( texcc "12 texdd" "Cicero" )
76 ( texsp "(1/65536) texpt" "Scaled TeX point" )
77
78 ;; Area
79 ( hect "10000 m^2" "*Hectare" )
80 ( a "100 m^2" "Are")
81 ( acre "(1/640) mi^2" "Acre" )
82 ( b "10^(-28) m^2" "Barn" )
83
84 ;; Volume
85 ( L "10^(-3) m^3" "*Liter" )
86 ( l "L" "Liter" )
87 ( gal "4 qt" "US Gallon" )
88 ( qt "2 pt" "Quart" )
89 ( pt "2 cup" "Pint" )
90 ( cup "8 ozfl" "Cup" )
91 ( ozfl "2 tbsp" "Fluid Ounce" )
92 ( floz "2 tbsp" "Fluid Ounce" )
93 ( tbsp "3 tsp" "Tablespoon" )
94 ;; ESUWM defines a US gallon as 231 in^3.
95 ;; That gives the following exact value for tsp.
96 ( tsp "492892159375*10^(-11) ml" "Teaspoon" nil
97 "4.92892159375 ml")
98 ( vol "tsp+tbsp+ozfl+cup+pt+qt+gal" "Gallons + ... + teaspoons" nil
99 "tsp+tbsp+ozfl+cup+pt+qt+gal")
100 ( galC "galUK" "Canadian Gallon" )
101 ( galUK "454609*10^(-5) L" "UK Gallon" nil
102 "4.54609 L") ;; NIST
103
104 ;; Time
105 ( s nil "*Second" )
106 ( sec "s" "Second" )
107 ( min "60 s" "Minute" )
108 ( hr "60 min" "Hour" )
109 ( day "24 hr" "Day" )
110 ( wk "7 day" "Week" )
111 ( hms "wk+day+hr+min+s" "Hours, minutes, seconds" )
112 ( yr "36525*10^(-2) day" "Year (Julian)" nil
113 "365.25 day")
114 ( Hz "1/s" "Hertz" )
115
116 ;; Speed
117 ( mph "mi/hr" "*Miles per hour" )
118 ( kph "km/hr" "Kilometers per hour" )
119 ( knot "nmi/hr" "Knot" )
120 ( c "299792458 m/s" "Speed of light" ) ;;; CODATA
121
122 ;; Acceleration
123 ( ga "980665*10^(-5) m/s^2" "*\"g\" acceleration" nil
124 "9.80665 m / s^2") ;; CODATA
125
126 ;; Mass
127 ( g nil "*Gram" )
128 ( lb "16 oz" "Pound (mass)" )
129 ( oz "28349523125*10^(-9) g" "Ounce (mass)" nil
130 "28.349523125 g") ;; ESUWM
131 ( ton "2000 lb" "Ton" )
132 ( tpo "ton+lb+oz" "Tons + pounds + ounces (mass)" )
133 ( t "1000 kg" "Metric ton" )
134 ( tonUK "10160469088*10^(-7) kg" "UK ton" nil
135 "1016.0469088 kg") ;; ESUWM
136 ( lbt "12 ozt" "Troy pound" )
137 ( ozt "311034768*10^(-7) g" "Troy ounce" nil
138 "31.10347680 g") ;; ESUWM, 1/12 exact value for lbt
139 ( ct "(2/10) g" "Carat" nil
140 "0.2 g") ;; ESUWM
141 ( u "1.660538782*10^(-27) kg" "Unified atomic mass" nil
142 "1.660538782 10^-27 kg (*)");;(approx) CODATA
143
144 ;; Force
145 ( N "m kg/s^2" "*Newton" )
146 ( dyn "10^(-5) N" "Dyne" )
147 ( gf "ga g" "Gram (force)" )
148 ( lbf "ga lb" "Pound (force)" )
149 ( kip "1000 lbf" "Kilopound (force)" )
150 ( pdl "138254954376*10^(-12) N" "Poundal" nil
151 "0.138254954376 N") ;; ESUWM
152
153 ;; Energy
154 ( J "N m" "*Joule" )
155 ( erg "10^(-7) J" "Erg" )
156 ( cal "41868*10^(-4) J" "International Table Calorie" nil
157 "4.1868 J") ;; NIST
158 ( calth "4184*10^(-3) J" "Thermochemical Calorie" nil
159 "4.184 J") ;; NIST
160 ( Cal "1000 cal" "Large Calorie")
161 ( Btu "105505585262*10^(-8) J" "International Table Btu" nil
162 "1055.05585262 J") ;; ESUWM
163 ( eV "ech V" "Electron volt" )
164 ( ev "eV" "Electron volt" )
165 ( therm "105506000 J" "EEC therm" )
166 ( invcm "h c/cm" "Energy in inverse centimeters" )
167 ( Kayser "invcm" "Kayser (inverse centimeter energy)" )
168 ( men "100/invcm" "Inverse energy in meters" )
169 ( Hzen "h Hz" "Energy in Hertz")
170 ( Ken "k K" "Energy in Kelvins")
171 ( Wh "W hr" "Watt hour")
172 ( Ws "W s" "Watt second")
173
174 ;; Power
175 ( W "J/s" "*Watt" )
176 ( hp "550 ft lbf/s" "Horsepower") ;;ESUWM
177 ( hpm "75 m kgf/s" "Metric Horsepower") ;;ESUWM
178
179 ;; Temperature
180 ( K nil "*Degree Kelvin" K )
181 ( dK "K" "Degree Kelvin" K )
182 ( degK "K" "Degree Kelvin" K )
183 ( dC "K" "Degree Celsius" C )
184 ( degC "K" "Degree Celsius" C )
185 ( dF "(5/9) K" "Degree Fahrenheit" F )
186 ( degF "(5/9) K" "Degree Fahrenheit" F )
187
188 ;; Pressure
189 ( Pa "N/m^2" "*Pascal" )
190 ( bar "10^5 Pa" "Bar" )
191 ( atm "101325 Pa" "Standard atmosphere" ) ;; CODATA
192 ( Torr "(1/760) atm" "Torr")
193 ( mHg "1000 Torr" "Meter of mercury" )
194 ( inHg "254*10^(-1) mmHg" "Inch of mercury" nil
195 "25.4 mmHg")
196 ( inH2O "2.490889*10^2 Pa" "Inch of water" nil
197 "2.490889 10^2 Pa (*)") ;;(approx) NIST
198 ( psi "lbf/in^2" "Pounds per square inch" )
199
200 ;; Viscosity
201 ( P "(1/10) Pa s" "*Poise" )
202 ( St "10^(-4) m^2/s" "Stokes" )
203
204 ;; Electromagnetism
205 ( A nil "*Ampere" )
206 ( C "A s" "Coulomb" )
207 ( Fdy "ech Nav" "Faraday" )
208 ( e "ech" "Elementary charge" )
209 ( ech "1.602176487*10^(-19) C" "Elementary charge" nil
210 "1.602176487 10^-19 C (*)") ;;(approx) CODATA
211 ( V "W/A" "Volt" )
212 ( ohm "V/A" "Ohm" )
213 ( mho "A/V" "Mho" )
214 ( S "A/V" "Siemens" )
215 ( F "C/V" "Farad" )
216 ( H "Wb/A" "Henry" )
217 ( T "Wb/m^2" "Tesla" )
218 ( Gs "10^(-4) T" "Gauss" )
219 ( Wb "V s" "Weber" )
220
221 ;; Luminous intensity
222 ( cd nil "*Candela" )
223 ( sb "10000 cd/m^2" "Stilb" )
224 ( lm "cd sr" "Lumen" )
225 ( lx "lm/m^2" "Lux" )
226 ( ph "10000 lx" "Phot" )
227 ( fc "lm/ft^2" "Footcandle") ;; ESUWM
228 ( lam "10000 lm/m^2" "Lambert" )
229 ( flam "(1/pi) cd/ft^2" "Footlambert") ;; ESUWM
230
231 ;; Radioactivity
232 ( Bq "1/s" "*Becquerel" )
233 ( Ci "37*10^9 Bq" "Curie" ) ;; ESUWM
234 ( Gy "J/kg" "Gray" )
235 ( Sv "Gy" "Sievert" )
236 ( R "258*10^(-6) C/kg" "Roentgen" ) ;; NIST
237 ( rd "(1/100) Gy" "Rad" )
238 ( rem "rd" "Rem" )
239
240 ;; Amount of substance
241 ( mol nil "*Mole" )
242
243 ;; Plane angle
244 ( rad nil "*Radian" )
245 ( circ "2 pi rad" "Full circle" )
246 ( rev "circ" "Full revolution" )
247 ( deg "circ/360" "Degree" )
248 ( arcmin "deg/60" "Arc minute" )
249 ( arcsec "arcmin/60" "Arc second" )
250 ( grad "circ/400" "Grade" )
251 ( rpm "rev/min" "Revolutions per minute" )
252
253 ;; Solid angle
254 ( sr nil "*Steradian" )
255
256 ;; Other physical quantities
257 ;; The values are from CODATA, and are approximate.
258 ( h "6.62606896*10^(-34) J s" "*Planck's constant" nil
259 "6.62606896 10^-34 J s (*)")
260 ( hbar "h / (2 pi)" "Planck's constant" ) ;; Exact
261 ( mu0 "4 pi 10^(-7) H/m" "Permeability of vacuum") ;; Exact
262 ( eps0 "1 / (mu0 c^2)" "Permittivity of vacuum" )
263 ( G "6.67428*10^(-11) m^3/(kg s^2)" "Gravitational constant" nil
264 "6.67428 10^-11 m^3/(kg s^2) (*)")
265 ( Nav "6.02214179*10^(23) / mol" "Avogadro's constant" nil
266 "6.02214179 10^23 / mol (*)")
267 ( me "9.10938215*10^(-31) kg" "Electron rest mass" nil
268 "9.10938215 10^-31 kg (*)")
269 ( mp "1.672621637*10^(-27) kg" "Proton rest mass" nil
270 "1.672621637 10^-27 kg (*)")
271 ( mn "1.674927211*10^(-27) kg" "Neutron rest mass" nil
272 "1.674927211 10^-27 kg (*)")
273 ( mmu "1.88353130*10^(-28) kg" "Muon rest mass" nil
274 "1.88353130 10^-28 kg (*)")
275 ( Ryd "10973731.568527 /m" "Rydberg's constant" nil
276 "10973731.568527 /m (*)")
277 ( k "1.3806504*10^(-23) J/K" "Boltzmann's constant" nil
278 "1.3806504 10^-23 J/K (*)")
279 ( alpha "7.2973525376*10^(-3)" "Fine structure constant" nil
280 "7.2973525376 10^-3 (*)")
281 ( muB "927.400915*10^(-26) J/T" "Bohr magneton" nil
282 "927.400915 10^-26 J/T (*)")
283 ( muN "5.05078324*10^(-27) J/T" "Nuclear magneton" nil
284 "5.05078324 10^-27 J/T (*)")
285 ( mue "-928.476377*10^(-26) J/T" "Electron magnetic moment" nil
286 "-928.476377 10^-26 J/T (*)")
287 ( mup "1.410606662*10^(-26) J/T" "Proton magnetic moment" nil
288 "1.410606662 10^-26 J/T (*)")
289 ( R0 "8.314472 J/(mol K)" "Molar gas constant" nil
290 "8.314472 J/(mol K) (*)")
291 ( V0 "22.710981*10^(-3) m^3/mol" "Standard volume of ideal gas" nil
292 "22.710981 10^-3 m^3/mol (*)")))
293
294
295 (defvar math-additional-units nil
296 "*Additional units table for user-defined units.
297 Must be formatted like `math-standard-units'.
298 If you change this, be sure to set `math-units-table' to nil to ensure
299 that the combined units table will be rebuilt.")
300
301 (defvar math-unit-prefixes
302 '( ( ?Y (^ 10 24) "Yotta" )
303 ( ?Z (^ 10 21) "Zetta" )
304 ( ?E (^ 10 18) "Exa" )
305 ( ?P (^ 10 15) "Peta" )
306 ( ?T (^ 10 12) "Tera" )
307 ( ?G (^ 10 9) "Giga" )
308 ( ?M (^ 10 6) "Mega" )
309 ( ?k (^ 10 3) "Kilo" )
310 ( ?K (^ 10 3) "Kilo" )
311 ( ?h (^ 10 2) "Hecto" )
312 ( ?H (^ 10 2) "Hecto" )
313 ( ?D (^ 10 1) "Deka" )
314 ( 0 (^ 10 0) nil )
315 ( ?d (^ 10 -1) "Deci" )
316 ( ?c (^ 10 -2) "Centi" )
317 ( ?m (^ 10 -3) "Milli" )
318 ( ?u (^ 10 -6) "Micro" )
319 ( ?n (^ 10 -9) "Nano" )
320 ( ?p (^ 10 -12) "Pico" )
321 ( ?f (^ 10 -15) "Femto" )
322 ( ?a (^ 10 -18) "Atto" )
323 ( ?z (^ 10 -21) "zepto" )
324 ( ?y (^ 10 -24) "yocto" )))
325
326 (defvar math-standard-units-systems
327 '( ( base nil )
328 ( si ( ( g '(/ (var kg var-kg) 1000) ) ) )
329 ( mks ( ( g '(/ (var kg var-kg) 1000) ) ) )
330 ( cgs ( ( m '(* (var cm var-cm) 100 ) ) ) )))
331
332 (defvar math-units-table nil
333 "Internal units table.
334 Derived from `math-standard-units' and `math-additional-units'.
335 Entries are (SYMBOL EXPR DOC-STRING TEMP-TYPE BASE-UNITS).")
336
337 (defvar math-units-table-buffer-valid nil)
338
339 ;;; Units commands.
340
341 (defun calc-base-units ()
342 (interactive)
343 (calc-slow-wrapper
344 (let ((calc-autorange-units nil))
345 (calc-enter-result 1 "bsun" (math-simplify-units
346 (math-to-standard-units (calc-top-n 1)
347 nil))))))
348
349 (defun calc-quick-units ()
350 (interactive)
351 (calc-slow-wrapper
352 (let* ((num (- last-command-event ?0))
353 (pos (if (= num 0) 10 num))
354 (units (calc-var-value 'var-Units))
355 (expr (calc-top-n 1)))
356 (unless (and (>= num 0) (<= num 9))
357 (error "Bad unit number"))
358 (unless (math-vectorp units)
359 (error "No \"quick units\" are defined"))
360 (unless (< pos (length units))
361 (error "Unit number %d not defined" pos))
362 (if (math-units-in-expr-p expr nil)
363 (calc-enter-result 1 (format "cun%d" num)
364 (math-convert-units expr (nth pos units)))
365 (calc-enter-result 1 (format "*un%d" num)
366 (math-simplify-units
367 (math-mul expr (nth pos units))))))))
368
369 (defun math-get-standard-units (expr)
370 "Return the standard units in EXPR."
371 (math-simplify-units
372 (math-extract-units
373 (math-to-standard-units expr nil))))
374
375 (defun math-get-units (expr)
376 "Return the units in EXPR."
377 (math-simplify-units
378 (math-extract-units expr)))
379
380 (defun math-make-unit-string (expr)
381 "Return EXPR in string form.
382 If EXPR is nil, return nil."
383 (if expr
384 (let ((cexpr (math-compose-expr expr 0)))
385 (replace-regexp-in-string
386 " / " "/"
387 (if (stringp cexpr)
388 cexpr
389 (math-composition-to-string cexpr))))))
390
391 (defvar math-default-units-table
392 (make-hash-table :test 'equal)
393 "A table storing previously converted units.")
394
395 (defun math-get-default-units (expr)
396 "Get default units to use when converting the units in EXPR."
397 (let* ((units (math-get-units expr))
398 (standard-units (math-get-standard-units expr))
399 (default-units (gethash
400 standard-units
401 math-default-units-table)))
402 (if (equal units (car default-units))
403 (math-make-unit-string (cadr default-units))
404 (math-make-unit-string (car default-units)))))
405
406 (defun math-put-default-units (expr)
407 "Put the units in EXPR in the default units table."
408 (let* ((units (math-get-units expr))
409 (standard-units (math-get-standard-units expr))
410 (default-units (gethash
411 standard-units
412 math-default-units-table)))
413 (cond
414 ((not default-units)
415 (puthash standard-units (list units) math-default-units-table))
416 ((not (equal units (car default-units)))
417 (puthash standard-units
418 (list units (car default-units))
419 math-default-units-table)))))
420
421
422 (defun calc-convert-units (&optional old-units new-units)
423 (interactive)
424 (calc-slow-wrapper
425 (let ((expr (calc-top-n 1))
426 (uoldname nil)
427 unew
428 units
429 defunits)
430 (unless (math-units-in-expr-p expr t)
431 (let ((uold (or old-units
432 (progn
433 (setq uoldname (read-string "Old units: "))
434 (if (equal uoldname "")
435 (progn
436 (setq uoldname "1")
437 1)
438 (if (string-match "\\` */" uoldname)
439 (setq uoldname (concat "1" uoldname)))
440 (math-read-expr uoldname))))))
441 (when (eq (car-safe uold) 'error)
442 (error "Bad format in units expression: %s" (nth 1 uold)))
443 (setq expr (math-mul expr uold))))
444 (unless new-units
445 (setq defunits (math-get-default-units expr))
446 (setq new-units
447 (read-string (concat
448 (if uoldname
449 (concat "Old units: "
450 uoldname
451 ", new units")
452 "New units")
453 (if defunits
454 (concat
455 " (default "
456 defunits
457 "): ")
458 ": "))))
459
460 (if (and
461 (string= new-units "")
462 defunits)
463 (setq new-units defunits)))
464 (when (string-match "\\` */" new-units)
465 (setq new-units (concat "1" new-units)))
466 (setq units (math-read-expr new-units))
467 (when (eq (car-safe units) 'error)
468 (error "Bad format in units expression: %s" (nth 2 units)))
469 (math-put-default-units units)
470 (let ((unew (math-units-in-expr-p units t))
471 (std (and (eq (car-safe units) 'var)
472 (assq (nth 1 units) math-standard-units-systems))))
473 (if std
474 (calc-enter-result 1 "cvun" (math-simplify-units
475 (math-to-standard-units expr
476 (nth 1 std))))
477 (unless unew
478 (error "No units specified"))
479 (calc-enter-result 1 "cvun"
480 (math-convert-units
481 expr units
482 (and uoldname (not (equal uoldname "1"))))))))))
483
484 (defun calc-autorange-units (arg)
485 (interactive "P")
486 (calc-wrapper
487 (calc-change-mode 'calc-autorange-units arg nil t)
488 (message (if calc-autorange-units
489 "Adjusting target unit prefix automatically"
490 "Using target units exactly"))))
491
492 (defun calc-convert-temperature (&optional old-units new-units)
493 (interactive)
494 (calc-slow-wrapper
495 (let ((expr (calc-top-n 1))
496 (uold nil)
497 (uoldname nil)
498 unew
499 defunits)
500 (setq uold (or old-units
501 (let ((units (math-single-units-in-expr-p expr)))
502 (if units
503 (if (consp units)
504 (list 'var (car units)
505 (intern (concat "var-"
506 (symbol-name
507 (car units)))))
508 (error "Not a pure temperature expression"))
509 (math-read-expr
510 (setq uoldname (read-string
511 "Old temperature units: ")))))))
512 (when (eq (car-safe uold) 'error)
513 (error "Bad format in units expression: %s" (nth 2 uold)))
514 (or (math-units-in-expr-p expr nil)
515 (setq expr (math-mul expr uold)))
516 (setq defunits (math-get-default-units expr))
517 (setq unew (or new-units
518 (read-string
519 (concat
520 (if uoldname
521 (concat "Old temperature units: "
522 uoldname
523 ", new units")
524 "New temperature units")
525 (if defunits
526 (concat " (default "
527 defunits
528 "): ")
529 ": ")))))
530 (setq unew (math-read-expr (if (string= unew "") defunits unew)))
531 (when (eq (car-safe unew) 'error)
532 (error "Bad format in units expression: %s" (nth 2 unew)))
533 (math-put-default-units unew)
534 (let ((ntemp (calc-normalize
535 (math-simplify-units
536 (math-convert-temperature expr uold unew
537 uoldname)))))
538 (if (Math-zerop ntemp)
539 (setq ntemp (list '* ntemp unew)))
540 (let ((calc-simplify-mode 'none))
541 (calc-enter-result 1 "cvtm" ntemp))))))
542
543 (defun calc-remove-units ()
544 (interactive)
545 (calc-slow-wrapper
546 (calc-enter-result 1 "rmun" (math-simplify-units
547 (math-remove-units (calc-top-n 1))))))
548
549 (defun calc-extract-units ()
550 (interactive)
551 (calc-slow-wrapper
552 (calc-enter-result 1 "rmun" (math-simplify-units
553 (math-extract-units (calc-top-n 1))))))
554
555 ;; The variables calc-num-units and calc-den-units are local to
556 ;; calc-explain-units, but are used by calc-explain-units-rec,
557 ;; which is called by calc-explain-units.
558 (defvar calc-num-units)
559 (defvar calc-den-units)
560
561 (defun calc-explain-units ()
562 (interactive)
563 (calc-wrapper
564 (let ((calc-num-units nil)
565 (calc-den-units nil))
566 (calc-explain-units-rec (calc-top-n 1) 1)
567 (and calc-den-units (string-match "^[^(].* .*[^)]$" calc-den-units)
568 (setq calc-den-units (concat "(" calc-den-units ")")))
569 (if calc-num-units
570 (if calc-den-units
571 (message "%s per %s" calc-num-units calc-den-units)
572 (message "%s" calc-num-units))
573 (if calc-den-units
574 (message "1 per %s" calc-den-units)
575 (message "No units in expression"))))))
576
577 (defun calc-explain-units-rec (expr pow)
578 (let ((u (math-check-unit-name expr))
579 pos)
580 (if (and u (not (math-zerop pow)))
581 (let ((name (or (nth 2 u) (symbol-name (car u)))))
582 (if (eq (aref name 0) ?\*)
583 (setq name (substring name 1)))
584 (if (string-match "[^a-zA-Z0-9']" name)
585 (if (string-match "^[a-zA-Z0-9' ()]*$" name)
586 (while (setq pos (string-match "[ ()]" name))
587 (setq name (concat (substring name 0 pos)
588 (if (eq (aref name pos) 32) "-" "")
589 (substring name (1+ pos)))))
590 (setq name (concat "(" name ")"))))
591 (or (eq (nth 1 expr) (car u))
592 (setq name (concat (nth 2 (assq (aref (symbol-name
593 (nth 1 expr)) 0)
594 math-unit-prefixes))
595 (if (and (string-match "[^a-zA-Z0-9']" name)
596 (not (memq (car u) '(mHg gf))))
597 (concat "-" name)
598 (downcase name)))))
599 (cond ((or (math-equal-int pow 1)
600 (math-equal-int pow -1)))
601 ((or (math-equal-int pow 2)
602 (math-equal-int pow -2))
603 (if (equal (nth 4 u) '((m . 1)))
604 (setq name (concat "Square-" name))
605 (setq name (concat name "-squared"))))
606 ((or (math-equal-int pow 3)
607 (math-equal-int pow -3))
608 (if (equal (nth 4 u) '((m . 1)))
609 (setq name (concat "Cubic-" name))
610 (setq name (concat name "-cubed"))))
611 (t
612 (setq name (concat name "^"
613 (math-format-number (math-abs pow))))))
614 (if (math-posp pow)
615 (setq calc-num-units (if calc-num-units
616 (concat calc-num-units " " name)
617 name))
618 (setq calc-den-units (if calc-den-units
619 (concat calc-den-units " " name)
620 name))))
621 (cond ((eq (car-safe expr) '*)
622 (calc-explain-units-rec (nth 1 expr) pow)
623 (calc-explain-units-rec (nth 2 expr) pow))
624 ((eq (car-safe expr) '/)
625 (calc-explain-units-rec (nth 1 expr) pow)
626 (calc-explain-units-rec (nth 2 expr) (- pow)))
627 ((memq (car-safe expr) '(neg + -))
628 (calc-explain-units-rec (nth 1 expr) pow))
629 ((and (eq (car-safe expr) '^)
630 (math-realp (nth 2 expr)))
631 (calc-explain-units-rec (nth 1 expr)
632 (math-mul pow (nth 2 expr))))))))
633
634 (defun calc-simplify-units ()
635 (interactive)
636 (calc-slow-wrapper
637 (calc-with-default-simplification
638 (calc-enter-result 1 "smun" (math-simplify-units (calc-top-n 1))))))
639
640 (defun calc-view-units-table (n)
641 (interactive "P")
642 (and n (setq math-units-table-buffer-valid nil))
643 (let ((win (get-buffer-window "*Units Table*")))
644 (if (and win
645 math-units-table
646 math-units-table-buffer-valid)
647 (progn
648 (bury-buffer (window-buffer win))
649 (let ((curwin (selected-window)))
650 (select-window win)
651 (switch-to-buffer nil)
652 (select-window curwin)))
653 (math-build-units-table-buffer nil))))
654
655 (defun calc-enter-units-table (n)
656 (interactive "P")
657 (and n (setq math-units-table-buffer-valid nil))
658 (math-build-units-table-buffer t)
659 (message "%s" (substitute-command-keys "Type \\[calc] to return to the Calculator")))
660
661 (defun calc-define-unit (uname desc &optional disp)
662 (interactive "SDefine unit name: \nsDescription: \nP")
663 (if disp (setq disp (read-string "Display definition: ")))
664 (calc-wrapper
665 (let ((form (calc-top-n 1))
666 (unit (assq uname math-additional-units)))
667 (or unit
668 (setq math-additional-units
669 (cons (setq unit (list uname nil nil nil nil))
670 math-additional-units)
671 math-units-table nil))
672 (setcar (cdr unit) (and (not (and (eq (car-safe form) 'var)
673 (eq (nth 1 form) uname)))
674 (not (math-equal-int form 1))
675 (math-format-flat-expr form 0)))
676 (setcar (cdr (cdr unit)) (and (not (equal desc ""))
677 desc))
678 (if disp
679 (setcar (cdr (cdr (cdr (cdr unit)))) disp))))
680 (calc-invalidate-units-table))
681
682 (defun calc-undefine-unit (uname)
683 (interactive "SUndefine unit name: ")
684 (calc-wrapper
685 (let ((unit (assq uname math-additional-units)))
686 (or unit
687 (if (assq uname math-standard-units)
688 (error "\"%s\" is a predefined unit name" uname)
689 (error "Unit name \"%s\" not found" uname)))
690 (setq math-additional-units (delq unit math-additional-units)
691 math-units-table nil)))
692 (calc-invalidate-units-table))
693
694 (defun calc-invalidate-units-table ()
695 (setq math-units-table nil)
696 (let ((buf (get-buffer "*Units Table*")))
697 (and buf
698 (with-current-buffer buf
699 (save-excursion
700 (goto-char (point-min))
701 (if (looking-at "Calculator Units Table")
702 (let ((inhibit-read-only t))
703 (insert "(Obsolete) "))))))))
704
705 (defun calc-get-unit-definition (uname)
706 (interactive "SGet definition for unit: ")
707 (calc-wrapper
708 (math-build-units-table)
709 (let ((unit (assq uname math-units-table)))
710 (or unit
711 (error "Unit name \"%s\" not found" uname))
712 (let ((msg (nth 2 unit)))
713 (if (stringp msg)
714 (if (string-match "^\\*" msg)
715 (setq msg (substring msg 1)))
716 (setq msg (symbol-name uname)))
717 (if (nth 1 unit)
718 (progn
719 (calc-enter-result 0 "ugdf" (nth 1 unit))
720 (message "Derived unit: %s" msg))
721 (calc-enter-result 0 "ugdf" (list 'var uname
722 (intern
723 (concat "var-"
724 (symbol-name uname)))))
725 (message "Base unit: %s" msg))))))
726
727 (defun calc-permanent-units ()
728 (interactive)
729 (calc-wrapper
730 (let (pos)
731 (set-buffer (find-file-noselect (substitute-in-file-name
732 calc-settings-file)))
733 (goto-char (point-min))
734 (if (and (search-forward ";;; Custom units stored by Calc" nil t)
735 (progn
736 (beginning-of-line)
737 (setq pos (point))
738 (search-forward "\n;;; End of custom units" nil t)))
739 (progn
740 (beginning-of-line)
741 (forward-line 1)
742 (delete-region pos (point)))
743 (goto-char (point-max))
744 (insert "\n\n")
745 (forward-char -1))
746 (insert ";;; Custom units stored by Calc on " (current-time-string) "\n")
747 (if math-additional-units
748 (progn
749 (insert "(setq math-additional-units '(\n")
750 (let ((list math-additional-units))
751 (while list
752 (insert " (" (symbol-name (car (car list))) " "
753 (if (nth 1 (car list))
754 (if (stringp (nth 1 (car list)))
755 (prin1-to-string (nth 1 (car list)))
756 (prin1-to-string (math-format-flat-expr
757 (nth 1 (car list)) 0)))
758 "nil")
759 " "
760 (prin1-to-string (nth 2 (car list)))
761 ")\n")
762 (setq list (cdr list))))
763 (insert "))\n"))
764 (insert ";;; (no custom units defined)\n"))
765 (insert ";;; End of custom units\n")
766 (save-buffer))))
767
768
769 ;; The variable math-cu-unit-list is local to math-build-units-table,
770 ;; but is used by math-compare-unit-names, which is called (indirectly)
771 ;; by math-build-units-table.
772 ;; math-cu-unit-list is also local to math-convert-units, but is used
773 ;; by math-convert-units-rec, which is called by math-convert-units.
774 (defvar math-cu-unit-list)
775
776 (defun math-build-units-table ()
777 (or math-units-table
778 (let* ((combined-units (append math-additional-units
779 math-standard-units))
780 (math-cu-unit-list (mapcar 'car combined-units))
781 tab)
782 (message "Building units table...")
783 (setq math-units-table-buffer-valid nil)
784 (setq tab (mapcar (function
785 (lambda (x)
786 (list (car x)
787 (and (nth 1 x)
788 (if (stringp (nth 1 x))
789 (let ((exp (math-read-plain-expr
790 (nth 1 x))))
791 (if (eq (car-safe exp) 'error)
792 (error "Format error in definition of %s in units table: %s"
793 (car x) (nth 2 exp))
794 exp))
795 (nth 1 x)))
796 (nth 2 x)
797 (nth 3 x)
798 (and (not (nth 1 x))
799 (list (cons (car x) 1)))
800 (nth 4 x))))
801 combined-units))
802 (let ((math-units-table tab))
803 (mapc 'math-find-base-units tab))
804 (message "Building units table...done")
805 (setq math-units-table tab))))
806
807 ;; The variables math-fbu-base and math-fbu-entry are local to
808 ;; math-find-base-units, but are used by math-find-base-units-rec,
809 ;; which is called by math-find-base-units.
810 (defvar math-fbu-base)
811 (defvar math-fbu-entry)
812
813 (defun math-find-base-units (math-fbu-entry)
814 (if (eq (nth 4 math-fbu-entry) 'boom)
815 (error "Circular definition involving unit %s" (car math-fbu-entry)))
816 (or (nth 4 math-fbu-entry)
817 (let (math-fbu-base)
818 (setcar (nthcdr 4 math-fbu-entry) 'boom)
819 (math-find-base-units-rec (nth 1 math-fbu-entry) 1)
820 '(or math-fbu-base
821 (error "Dimensionless definition for unit %s" (car math-fbu-entry)))
822 (while (eq (cdr (car math-fbu-base)) 0)
823 (setq math-fbu-base (cdr math-fbu-base)))
824 (let ((b math-fbu-base))
825 (while (cdr b)
826 (if (eq (cdr (car (cdr b))) 0)
827 (setcdr b (cdr (cdr b)))
828 (setq b (cdr b)))))
829 (setq math-fbu-base (sort math-fbu-base 'math-compare-unit-names))
830 (setcar (nthcdr 4 math-fbu-entry) math-fbu-base)
831 math-fbu-base)))
832
833 (defun math-compare-unit-names (a b)
834 (memq (car b) (cdr (memq (car a) math-cu-unit-list))))
835
836 (defun math-find-base-units-rec (expr pow)
837 (let ((u (math-check-unit-name expr)))
838 (cond (u
839 (let ((ulist (math-find-base-units u)))
840 (while ulist
841 (let ((p (* (cdr (car ulist)) pow))
842 (old (assq (car (car ulist)) math-fbu-base)))
843 (if old
844 (setcdr old (+ (cdr old) p))
845 (setq math-fbu-base
846 (cons (cons (car (car ulist)) p) math-fbu-base))))
847 (setq ulist (cdr ulist)))))
848 ((math-scalarp expr))
849 ((and (eq (car expr) '^)
850 (integerp (nth 2 expr)))
851 (math-find-base-units-rec (nth 1 expr) (* pow (nth 2 expr))))
852 ((eq (car expr) '*)
853 (math-find-base-units-rec (nth 1 expr) pow)
854 (math-find-base-units-rec (nth 2 expr) pow))
855 ((eq (car expr) '/)
856 (math-find-base-units-rec (nth 1 expr) pow)
857 (math-find-base-units-rec (nth 2 expr) (- pow)))
858 ((eq (car expr) 'neg)
859 (math-find-base-units-rec (nth 1 expr) pow))
860 ((eq (car expr) '+)
861 (math-find-base-units-rec (nth 1 expr) pow))
862 ((eq (car expr) 'var)
863 (or (eq (nth 1 expr) 'pi)
864 (error "Unknown name %s in defining expression for unit %s"
865 (nth 1 expr) (car math-fbu-entry))))
866 (t (error "Malformed defining expression for unit %s" (car math-fbu-entry))))))
867
868
869 (defun math-units-in-expr-p (expr sub-exprs)
870 (and (consp expr)
871 (if (eq (car expr) 'var)
872 (math-check-unit-name expr)
873 (and (or sub-exprs
874 (memq (car expr) '(* / ^)))
875 (or (math-units-in-expr-p (nth 1 expr) sub-exprs)
876 (math-units-in-expr-p (nth 2 expr) sub-exprs))))))
877
878 (defun math-only-units-in-expr-p (expr)
879 (and (consp expr)
880 (if (eq (car expr) 'var)
881 (math-check-unit-name expr)
882 (if (memq (car expr) '(* /))
883 (and (math-only-units-in-expr-p (nth 1 expr))
884 (math-only-units-in-expr-p (nth 2 expr)))
885 (and (eq (car expr) '^)
886 (and (math-only-units-in-expr-p (nth 1 expr))
887 (math-realp (nth 2 expr))))))))
888
889 (defun math-single-units-in-expr-p (expr)
890 (cond ((math-scalarp expr) nil)
891 ((eq (car expr) 'var)
892 (math-check-unit-name expr))
893 ((eq (car expr) '*)
894 (let ((u1 (math-single-units-in-expr-p (nth 1 expr)))
895 (u2 (math-single-units-in-expr-p (nth 2 expr))))
896 (or (and u1 u2 'wrong)
897 u1
898 u2)))
899 ((eq (car expr) '/)
900 (if (math-units-in-expr-p (nth 2 expr) nil)
901 'wrong
902 (math-single-units-in-expr-p (nth 1 expr))))
903 (t 'wrong)))
904
905 (defun math-check-unit-name (v)
906 (and (eq (car-safe v) 'var)
907 (or (assq (nth 1 v) (or math-units-table (math-build-units-table)))
908 (let ((name (symbol-name (nth 1 v))))
909 (and (> (length name) 1)
910 (assq (aref name 0) math-unit-prefixes)
911 (or (assq (intern (substring name 1)) math-units-table)
912 (and (eq (aref name 0) ?M)
913 (> (length name) 3)
914 (eq (aref name 1) ?e)
915 (eq (aref name 2) ?g)
916 (assq (intern (substring name 3))
917 math-units-table))))))))
918
919 ;; The variable math-which-standard is local to math-to-standard-units,
920 ;; but is used by math-to-standard-rec, which is called by
921 ;; math-to-standard-units.
922 (defvar math-which-standard)
923
924 (defun math-to-standard-units (expr math-which-standard)
925 (math-to-standard-rec expr))
926
927 (defun math-to-standard-rec (expr)
928 (if (eq (car-safe expr) 'var)
929 (let ((u (math-check-unit-name expr))
930 (base (nth 1 expr)))
931 (if u
932 (progn
933 (if (nth 1 u)
934 (setq expr (math-to-standard-rec (nth 1 u)))
935 (let ((st (assq (car u) math-which-standard)))
936 (if st
937 (setq expr (nth 1 st))
938 (setq expr (list 'var (car u)
939 (intern (concat "var-"
940 (symbol-name
941 (car u)))))))))
942 (or (null u)
943 (eq base (car u))
944 (setq expr (list '*
945 (nth 1 (assq (aref (symbol-name base) 0)
946 math-unit-prefixes))
947 expr)))
948 expr)
949 (if (eq base 'pi)
950 (math-pi)
951 expr)))
952 (if (Math-primp expr)
953 expr
954 (cons (car expr)
955 (mapcar 'math-to-standard-rec (cdr expr))))))
956
957 (defun math-apply-units (expr units ulist &optional pure)
958 (setq expr (math-simplify-units expr))
959 (if ulist
960 (let ((new 0)
961 value)
962 (or (math-numberp expr)
963 (error "Incompatible units"))
964 (while (cdr ulist)
965 (setq value (math-div expr (nth 1 (car ulist)))
966 value (math-floor (let ((calc-internal-prec
967 (1- calc-internal-prec)))
968 (math-normalize value)))
969 new (math-add new (math-mul value (car (car ulist))))
970 expr (math-sub expr (math-mul value (nth 1 (car ulist))))
971 ulist (cdr ulist)))
972 (math-add new (math-mul (math-div expr (nth 1 (car ulist)))
973 (car (car ulist)))))
974 (if pure
975 expr
976 (math-simplify-units (list '* expr units)))))
977
978 (defvar math-decompose-units-cache nil)
979 (defun math-decompose-units (units)
980 (let ((u (math-check-unit-name units)))
981 (and u (eq (car-safe (nth 1 u)) '+)
982 (setq units (nth 1 u))))
983 (setq units (calcFunc-expand units))
984 (and (eq (car-safe units) '+)
985 (let ((entry (list units calc-internal-prec calc-prefer-frac)))
986 (or (equal entry (car math-decompose-units-cache))
987 (let ((ulist nil)
988 (utemp units)
989 qty unit)
990 (while (eq (car-safe utemp) '+)
991 (setq ulist (cons (math-decompose-unit-part (nth 2 utemp))
992 ulist)
993 utemp (nth 1 utemp)))
994 (setq ulist (cons (math-decompose-unit-part utemp) ulist)
995 utemp ulist)
996 (while (setq utemp (cdr utemp))
997 (unless (equal (nth 2 (car utemp)) (nth 2 (car ulist)))
998 (error "Inconsistent units in sum")))
999 (setq math-decompose-units-cache
1000 (cons entry
1001 (sort ulist
1002 (function
1003 (lambda (x y)
1004 (not (Math-lessp (nth 1 x)
1005 (nth 1 y))))))))))
1006 (cdr math-decompose-units-cache))))
1007
1008 (defun math-decompose-unit-part (unit)
1009 (cons unit
1010 (math-is-multiple (math-simplify-units (math-to-standard-units
1011 unit nil))
1012 t)))
1013
1014 ;; The variable math-fcu-u is local to math-find-compatible-unit,
1015 ;; but is used by math-find-compatible-rec which is called by
1016 ;; math-find-compatible-unit.
1017 (defvar math-fcu-u)
1018
1019 (defun math-find-compatible-unit (expr unit)
1020 (let ((math-fcu-u (math-check-unit-name unit)))
1021 (if math-fcu-u
1022 (math-find-compatible-unit-rec expr 1))))
1023
1024 (defun math-find-compatible-unit-rec (expr pow)
1025 (cond ((eq (car-safe expr) '*)
1026 (or (math-find-compatible-unit-rec (nth 1 expr) pow)
1027 (math-find-compatible-unit-rec (nth 2 expr) pow)))
1028 ((eq (car-safe expr) '/)
1029 (or (math-find-compatible-unit-rec (nth 1 expr) pow)
1030 (math-find-compatible-unit-rec (nth 2 expr) (- pow))))
1031 ((and (eq (car-safe expr) '^)
1032 (integerp (nth 2 expr)))
1033 (math-find-compatible-unit-rec (nth 1 expr) (* pow (nth 2 expr))))
1034 (t
1035 (let ((u2 (math-check-unit-name expr)))
1036 (if (equal (nth 4 math-fcu-u) (nth 4 u2))
1037 (cons expr pow))))))
1038
1039 ;; The variables math-cu-new-units and math-cu-pure are local to
1040 ;; math-convert-units, but are used by math-convert-units-rec,
1041 ;; which is called by math-convert-units.
1042 (defvar math-cu-new-units)
1043 (defvar math-cu-pure)
1044
1045 (defun math-convert-units (expr math-cu-new-units &optional math-cu-pure)
1046 (if (eq (car-safe math-cu-new-units) 'var)
1047 (let ((unew (assq (nth 1 math-cu-new-units)
1048 (math-build-units-table))))
1049 (if (eq (car-safe (nth 1 unew)) '+)
1050 (setq math-cu-new-units (nth 1 unew)))))
1051 (math-with-extra-prec 2
1052 (let ((compat (and (not math-cu-pure)
1053 (math-find-compatible-unit expr math-cu-new-units)))
1054 (math-cu-unit-list nil)
1055 (math-combining-units nil))
1056 (if compat
1057 (math-simplify-units
1058 (math-mul (math-mul (math-simplify-units
1059 (math-div expr (math-pow (car compat)
1060 (cdr compat))))
1061 (math-pow math-cu-new-units (cdr compat)))
1062 (math-simplify-units
1063 (math-to-standard-units
1064 (math-pow (math-div (car compat) math-cu-new-units)
1065 (cdr compat))
1066 nil))))
1067 (when (setq math-cu-unit-list (math-decompose-units math-cu-new-units))
1068 (setq math-cu-new-units (nth 2 (car math-cu-unit-list))))
1069 (when (eq (car-safe expr) '+)
1070 (setq expr (math-simplify-units expr)))
1071 (if (math-units-in-expr-p expr t)
1072 (math-convert-units-rec expr)
1073 (math-apply-units (math-to-standard-units
1074 (list '/ expr math-cu-new-units) nil)
1075 math-cu-new-units math-cu-unit-list math-cu-pure))))))
1076
1077 (defun math-convert-units-rec (expr)
1078 (if (math-units-in-expr-p expr nil)
1079 (math-apply-units (math-to-standard-units
1080 (list '/ expr math-cu-new-units) nil)
1081 math-cu-new-units math-cu-unit-list math-cu-pure)
1082 (if (Math-primp expr)
1083 expr
1084 (cons (car expr)
1085 (mapcar 'math-convert-units-rec (cdr expr))))))
1086
1087 (defun math-convert-temperature (expr old new &optional pure)
1088 (let* ((units (math-single-units-in-expr-p expr))
1089 (uold (if old
1090 (if (or (null units)
1091 (equal (nth 1 old) (car units)))
1092 (math-check-unit-name old)
1093 (error "Inconsistent temperature units"))
1094 units))
1095 (unew (math-check-unit-name new)))
1096 (unless (and (consp unew) (nth 3 unew))
1097 (error "Not a valid temperature unit"))
1098 (unless (and (consp uold) (nth 3 uold))
1099 (error "Not a pure temperature expression"))
1100 (let ((v (car uold)))
1101 (setq expr (list '/ expr (list 'var v
1102 (intern (concat "var-"
1103 (symbol-name v)))))))
1104 (or (eq (nth 3 uold) (nth 3 unew))
1105 (cond ((eq (nth 3 uold) 'K)
1106 (setq expr (list '- expr '(/ 27315 100)))
1107 (if (eq (nth 3 unew) 'F)
1108 (setq expr (list '+ (list '* expr '(/ 9 5)) 32))))
1109 ((eq (nth 3 uold) 'C)
1110 (if (eq (nth 3 unew) 'F)
1111 (setq expr (list '+ (list '* expr '(/ 9 5)) 32))
1112 (setq expr (list '+ expr '(/ 27315 100)))))
1113 (t
1114 (setq expr (list '* (list '- expr 32) '(/ 5 9)))
1115 (if (eq (nth 3 unew) 'K)
1116 (setq expr (list '+ expr '(/ 27315 100)))))))
1117 (if pure
1118 expr
1119 (list '* expr new))))
1120
1121
1122
1123 (defun math-simplify-units (a)
1124 (let ((math-simplifying-units t)
1125 (calc-matrix-mode 'scalar))
1126 (math-simplify a)))
1127 (defalias 'calcFunc-usimplify 'math-simplify-units)
1128
1129 ;; The function created by math-defsimplify uses the variable
1130 ;; math-simplify-expr, and so is used by functions in math-defsimplify
1131 (defvar math-simplify-expr)
1132
1133 (math-defsimplify (+ -)
1134 (and math-simplifying-units
1135 (math-units-in-expr-p (nth 1 math-simplify-expr) nil)
1136 (let* ((units (math-extract-units (nth 1 math-simplify-expr)))
1137 (ratio (math-simplify (math-to-standard-units
1138 (list '/ (nth 2 math-simplify-expr) units) nil))))
1139 (if (math-units-in-expr-p ratio nil)
1140 (progn
1141 (calc-record-why "*Inconsistent units" math-simplify-expr)
1142 math-simplify-expr)
1143 (list '* (math-add (math-remove-units (nth 1 math-simplify-expr))
1144 (if (eq (car math-simplify-expr) '-)
1145 (math-neg ratio) ratio))
1146 units)))))
1147
1148 (math-defsimplify *
1149 (math-simplify-units-prod))
1150
1151 (defun math-simplify-units-prod ()
1152 (and math-simplifying-units
1153 calc-autorange-units
1154 (Math-realp (nth 1 math-simplify-expr))
1155 (let* ((num (math-float (nth 1 math-simplify-expr)))
1156 (xpon (calcFunc-xpon num))
1157 (unitp (cdr (cdr math-simplify-expr)))
1158 (unit (car unitp))
1159 (pow (if (eq (car math-simplify-expr) '*) 1 -1))
1160 u)
1161 (and (eq (car-safe unit) '*)
1162 (setq unitp (cdr unit)
1163 unit (car unitp)))
1164 (and (eq (car-safe unit) '^)
1165 (integerp (nth 2 unit))
1166 (setq pow (* pow (nth 2 unit))
1167 unitp (cdr unit)
1168 unit (car unitp)))
1169 (and (setq u (math-check-unit-name unit))
1170 (integerp xpon)
1171 (or (< xpon 0)
1172 (>= xpon (if (eq (car u) 'm) 1 3)))
1173 (let* ((uxpon 0)
1174 (pref (if (< pow 0)
1175 (reverse math-unit-prefixes)
1176 math-unit-prefixes))
1177 (p pref)
1178 pxpon pname)
1179 (or (eq (car u) (nth 1 unit))
1180 (setq uxpon (* pow
1181 (nth 2 (nth 1 (assq
1182 (aref (symbol-name
1183 (nth 1 unit)) 0)
1184 math-unit-prefixes))))))
1185 (setq xpon (+ xpon uxpon))
1186 (while (and p
1187 (or (memq (car (car p)) '(?d ?D ?h ?H))
1188 (and (eq (car (car p)) ?c)
1189 (not (eq (car u) 'm)))
1190 (< xpon (setq pxpon (* (nth 2 (nth 1 (car p)))
1191 pow)))
1192 (progn
1193 (setq pname (math-build-var-name
1194 (if (eq (car (car p)) 0)
1195 (car u)
1196 (concat (char-to-string
1197 (car (car p)))
1198 (symbol-name
1199 (car u))))))
1200 (and (/= (car (car p)) 0)
1201 (assq (nth 1 pname)
1202 math-units-table)))))
1203 (setq p (cdr p)))
1204 (and p
1205 (/= pxpon uxpon)
1206 (or (not (eq p pref))
1207 (< xpon (+ pxpon (* (math-abs pow) 3))))
1208 (progn
1209 (setcar (cdr math-simplify-expr)
1210 (let ((calc-prefer-frac nil))
1211 (calcFunc-scf (nth 1 math-simplify-expr)
1212 (- uxpon pxpon))))
1213 (setcar unitp pname)
1214 math-simplify-expr)))))))
1215
1216 (defvar math-try-cancel-units)
1217
1218 (math-defsimplify /
1219 (and math-simplifying-units
1220 (let ((np (cdr math-simplify-expr))
1221 (math-try-cancel-units 0)
1222 n nn)
1223 (setq n (if (eq (car-safe (nth 2 math-simplify-expr)) '*)
1224 (cdr (nth 2 math-simplify-expr))
1225 (nthcdr 2 math-simplify-expr)))
1226 (if (math-realp (car n))
1227 (progn
1228 (setcar (cdr math-simplify-expr) (math-mul (nth 1 math-simplify-expr)
1229 (let ((calc-prefer-frac nil))
1230 (math-div 1 (car n)))))
1231 (setcar n 1)))
1232 (while (eq (car-safe (setq n (car np))) '*)
1233 (math-simplify-units-divisor (cdr n) (cdr (cdr math-simplify-expr)))
1234 (setq np (cdr (cdr n))))
1235 (math-simplify-units-divisor np (cdr (cdr math-simplify-expr)))
1236 (if (eq math-try-cancel-units 0)
1237 (let* ((math-simplifying-units nil)
1238 (base (math-simplify
1239 (math-to-standard-units math-simplify-expr nil))))
1240 (if (Math-numberp base)
1241 (setq math-simplify-expr base))))
1242 (if (eq (car-safe math-simplify-expr) '/)
1243 (math-simplify-units-prod))
1244 math-simplify-expr)))
1245
1246 (defun math-simplify-units-divisor (np dp)
1247 (let ((n (car np))
1248 d dd temp)
1249 (while (eq (car-safe (setq d (car dp))) '*)
1250 (when (setq temp (math-simplify-units-quotient n (nth 1 d)))
1251 (setcar np (setq n temp))
1252 (setcar (cdr d) 1))
1253 (setq dp (cdr (cdr d))))
1254 (when (setq temp (math-simplify-units-quotient n d))
1255 (setcar np (setq n temp))
1256 (setcar dp 1))))
1257
1258 ;; Simplify, e.g., "in / cm" to "2.54" in a units expression.
1259 (defun math-simplify-units-quotient (n d)
1260 (let ((pow1 1)
1261 (pow2 1))
1262 (when (and (eq (car-safe n) '^)
1263 (integerp (nth 2 n)))
1264 (setq pow1 (nth 2 n) n (nth 1 n)))
1265 (when (and (eq (car-safe d) '^)
1266 (integerp (nth 2 d)))
1267 (setq pow2 (nth 2 d) d (nth 1 d)))
1268 (let ((un (math-check-unit-name n))
1269 (ud (math-check-unit-name d)))
1270 (and un ud
1271 (if (and (equal (nth 4 un) (nth 4 ud))
1272 (eq pow1 pow2))
1273 (if (eq pow1 1)
1274 (math-to-standard-units (list '/ n d) nil)
1275 (list '^ (math-to-standard-units (list '/ n d) nil) pow1))
1276 (let (ud1)
1277 (setq un (nth 4 un)
1278 ud (nth 4 ud))
1279 (while un
1280 (setq ud1 ud)
1281 (while ud1
1282 (and (eq (car (car un)) (car (car ud1)))
1283 (setq math-try-cancel-units
1284 (+ math-try-cancel-units
1285 (- (* (cdr (car un)) pow1)
1286 (* (cdr (car ud)) pow2)))))
1287 (setq ud1 (cdr ud1)))
1288 (setq un (cdr un)))
1289 nil))))))
1290
1291 (math-defsimplify ^
1292 (and math-simplifying-units
1293 (math-realp (nth 2 math-simplify-expr))
1294 (if (memq (car-safe (nth 1 math-simplify-expr)) '(* /))
1295 (list (car (nth 1 math-simplify-expr))
1296 (list '^ (nth 1 (nth 1 math-simplify-expr))
1297 (nth 2 math-simplify-expr))
1298 (list '^ (nth 2 (nth 1 math-simplify-expr))
1299 (nth 2 math-simplify-expr)))
1300 (math-simplify-units-pow (nth 1 math-simplify-expr)
1301 (nth 2 math-simplify-expr)))))
1302
1303 (math-defsimplify calcFunc-sqrt
1304 (and math-simplifying-units
1305 (if (memq (car-safe (nth 1 math-simplify-expr)) '(* /))
1306 (list (car (nth 1 math-simplify-expr))
1307 (list 'calcFunc-sqrt (nth 1 (nth 1 math-simplify-expr)))
1308 (list 'calcFunc-sqrt (nth 2 (nth 1 math-simplify-expr))))
1309 (math-simplify-units-pow (nth 1 math-simplify-expr) '(frac 1 2)))))
1310
1311 (math-defsimplify (calcFunc-floor
1312 calcFunc-ceil
1313 calcFunc-round
1314 calcFunc-rounde
1315 calcFunc-roundu
1316 calcFunc-trunc
1317 calcFunc-float
1318 calcFunc-frac
1319 calcFunc-abs
1320 calcFunc-clean)
1321 (and math-simplifying-units
1322 (= (length math-simplify-expr) 2)
1323 (if (math-only-units-in-expr-p (nth 1 math-simplify-expr))
1324 (nth 1 math-simplify-expr)
1325 (if (and (memq (car-safe (nth 1 math-simplify-expr)) '(* /))
1326 (or (math-only-units-in-expr-p
1327 (nth 1 (nth 1 math-simplify-expr)))
1328 (math-only-units-in-expr-p
1329 (nth 2 (nth 1 math-simplify-expr)))))
1330 (list (car (nth 1 math-simplify-expr))
1331 (cons (car math-simplify-expr)
1332 (cons (nth 1 (nth 1 math-simplify-expr))
1333 (cdr (cdr math-simplify-expr))))
1334 (cons (car math-simplify-expr)
1335 (cons (nth 2 (nth 1 math-simplify-expr))
1336 (cdr (cdr math-simplify-expr)))))))))
1337
1338 (defun math-simplify-units-pow (a pow)
1339 (if (and (eq (car-safe a) '^)
1340 (math-check-unit-name (nth 1 a))
1341 (math-realp (nth 2 a)))
1342 (list '^ (nth 1 a) (math-mul pow (nth 2 a)))
1343 (let* ((u (math-check-unit-name a))
1344 (pf (math-to-simple-fraction pow))
1345 (d (and (eq (car-safe pf) 'frac) (nth 2 pf))))
1346 (and u d
1347 (math-units-are-multiple u d)
1348 (list '^ (math-to-standard-units a nil) pow)))))
1349
1350
1351 (defun math-units-are-multiple (u n)
1352 (setq u (nth 4 u))
1353 (while (and u (= (% (cdr (car u)) n) 0))
1354 (setq u (cdr u)))
1355 (null u))
1356
1357 (math-defsimplify calcFunc-sin
1358 (and math-simplifying-units
1359 (math-units-in-expr-p (nth 1 math-simplify-expr) nil)
1360 (let ((rad (math-simplify-units
1361 (math-evaluate-expr
1362 (math-to-standard-units (nth 1 math-simplify-expr) nil))))
1363 (calc-angle-mode 'rad))
1364 (and (eq (car-safe rad) '*)
1365 (math-realp (nth 1 rad))
1366 (eq (car-safe (nth 2 rad)) 'var)
1367 (eq (nth 1 (nth 2 rad)) 'rad)
1368 (list 'calcFunc-sin (nth 1 rad))))))
1369
1370 (math-defsimplify calcFunc-cos
1371 (and math-simplifying-units
1372 (math-units-in-expr-p (nth 1 math-simplify-expr) nil)
1373 (let ((rad (math-simplify-units
1374 (math-evaluate-expr
1375 (math-to-standard-units (nth 1 math-simplify-expr) nil))))
1376 (calc-angle-mode 'rad))
1377 (and (eq (car-safe rad) '*)
1378 (math-realp (nth 1 rad))
1379 (eq (car-safe (nth 2 rad)) 'var)
1380 (eq (nth 1 (nth 2 rad)) 'rad)
1381 (list 'calcFunc-cos (nth 1 rad))))))
1382
1383 (math-defsimplify calcFunc-tan
1384 (and math-simplifying-units
1385 (math-units-in-expr-p (nth 1 math-simplify-expr) nil)
1386 (let ((rad (math-simplify-units
1387 (math-evaluate-expr
1388 (math-to-standard-units (nth 1 math-simplify-expr) nil))))
1389 (calc-angle-mode 'rad))
1390 (and (eq (car-safe rad) '*)
1391 (math-realp (nth 1 rad))
1392 (eq (car-safe (nth 2 rad)) 'var)
1393 (eq (nth 1 (nth 2 rad)) 'rad)
1394 (list 'calcFunc-tan (nth 1 rad))))))
1395
1396 (math-defsimplify calcFunc-sec
1397 (and math-simplifying-units
1398 (math-units-in-expr-p (nth 1 math-simplify-expr) nil)
1399 (let ((rad (math-simplify-units
1400 (math-evaluate-expr
1401 (math-to-standard-units (nth 1 math-simplify-expr) nil))))
1402 (calc-angle-mode 'rad))
1403 (and (eq (car-safe rad) '*)
1404 (math-realp (nth 1 rad))
1405 (eq (car-safe (nth 2 rad)) 'var)
1406 (eq (nth 1 (nth 2 rad)) 'rad)
1407 (list 'calcFunc-sec (nth 1 rad))))))
1408
1409 (math-defsimplify calcFunc-csc
1410 (and math-simplifying-units
1411 (math-units-in-expr-p (nth 1 math-simplify-expr) nil)
1412 (let ((rad (math-simplify-units
1413 (math-evaluate-expr
1414 (math-to-standard-units (nth 1 math-simplify-expr) nil))))
1415 (calc-angle-mode 'rad))
1416 (and (eq (car-safe rad) '*)
1417 (math-realp (nth 1 rad))
1418 (eq (car-safe (nth 2 rad)) 'var)
1419 (eq (nth 1 (nth 2 rad)) 'rad)
1420 (list 'calcFunc-csc (nth 1 rad))))))
1421
1422 (math-defsimplify calcFunc-cot
1423 (and math-simplifying-units
1424 (math-units-in-expr-p (nth 1 math-simplify-expr) nil)
1425 (let ((rad (math-simplify-units
1426 (math-evaluate-expr
1427 (math-to-standard-units (nth 1 math-simplify-expr) nil))))
1428 (calc-angle-mode 'rad))
1429 (and (eq (car-safe rad) '*)
1430 (math-realp (nth 1 rad))
1431 (eq (car-safe (nth 2 rad)) 'var)
1432 (eq (nth 1 (nth 2 rad)) 'rad)
1433 (list 'calcFunc-cot (nth 1 rad))))))
1434
1435
1436 (defun math-remove-units (expr)
1437 (if (math-check-unit-name expr)
1438 1
1439 (if (Math-primp expr)
1440 expr
1441 (cons (car expr)
1442 (mapcar 'math-remove-units (cdr expr))))))
1443
1444 (defun math-extract-units (expr)
1445 (if (memq (car-safe expr) '(* /))
1446 (cons (car expr)
1447 (mapcar 'math-extract-units (cdr expr)))
1448 (if (math-check-unit-name expr) expr 1)))
1449
1450 (defun math-build-units-table-buffer (enter-buffer)
1451 (if (not (and math-units-table math-units-table-buffer-valid
1452 (get-buffer "*Units Table*")))
1453 (let ((buf (get-buffer-create "*Units Table*"))
1454 (uptr (math-build-units-table))
1455 (calc-language (if (eq calc-language 'big) nil calc-language))
1456 (calc-float-format '(float 0))
1457 (calc-group-digits nil)
1458 (calc-number-radix 10)
1459 (calc-twos-complement-mode nil)
1460 (calc-point-char ".")
1461 (std nil)
1462 u name shadowed)
1463 (save-excursion
1464 (message "Formatting units table...")
1465 (set-buffer buf)
1466 (let ((inhibit-read-only t))
1467 (erase-buffer)
1468 (insert "Calculator Units Table:\n\n")
1469 (insert "(All definitions are exact unless marked with an asterisk (*).)\n\n")
1470 (insert "Unit Type Definition Description\n\n")
1471 (while uptr
1472 (setq u (car uptr)
1473 name (nth 2 u))
1474 (when (eq (car u) 'm)
1475 (setq std t))
1476 (setq shadowed (and std (assq (car u) math-additional-units)))
1477 (when (and name
1478 (> (length name) 1)
1479 (eq (aref name 0) ?\*))
1480 (unless (eq uptr math-units-table)
1481 (insert "\n"))
1482 (setq name (substring name 1)))
1483 (insert " ")
1484 (and shadowed (insert "("))
1485 (insert (symbol-name (car u)))
1486 (and shadowed (insert ")"))
1487 (if (nth 3 u)
1488 (progn
1489 (indent-to 10)
1490 (insert (symbol-name (nth 3 u))))
1491 (or std
1492 (progn
1493 (indent-to 10)
1494 (insert "U"))))
1495 (indent-to 14)
1496 (and shadowed (insert "("))
1497 (if (nth 5 u)
1498 (insert (nth 5 u))
1499 (if (nth 1 u)
1500 (insert (math-format-value (nth 1 u) 80))
1501 (insert (symbol-name (car u)))))
1502 (and shadowed (insert ")"))
1503 (indent-to 41)
1504 (insert " ")
1505 (when name
1506 (insert name))
1507 (if shadowed
1508 (insert " (redefined above)")
1509 (unless (nth 1 u)
1510 (insert " (base unit)")))
1511 (insert "\n")
1512 (setq uptr (cdr uptr)))
1513 (insert "\n\nUnit Prefix Table:\n\n")
1514 (setq uptr math-unit-prefixes)
1515 (while uptr
1516 (setq u (car uptr))
1517 (insert " " (char-to-string (car u)))
1518 (if (equal (nth 1 u) (nth 1 (nth 1 uptr)))
1519 (insert " " (char-to-string (car (car (setq uptr (cdr uptr)))))
1520 " ")
1521 (insert " "))
1522 (insert "10^" (int-to-string (nth 2 (nth 1 u))))
1523 (indent-to 15)
1524 (insert " " (nth 2 u) "\n")
1525 (while (eq (car (car (setq uptr (cdr uptr)))) 0)))
1526 (insert "\n"))
1527 (view-mode)
1528 (message "Formatting units table...done"))
1529 (setq math-units-table-buffer-valid t)
1530 (let ((oldbuf (current-buffer)))
1531 (set-buffer buf)
1532 (goto-char (point-min))
1533 (set-buffer oldbuf))
1534 (if enter-buffer
1535 (pop-to-buffer buf)
1536 (display-buffer buf)))
1537 (if enter-buffer
1538 (pop-to-buffer (get-buffer "*Units Table*"))
1539 (display-buffer (get-buffer "*Units Table*")))))
1540
1541 (provide 'calc-units)
1542
1543 ;; Local Variables:
1544 ;; coding: iso-latin-1
1545 ;; End:
1546
1547 ;; arch-tag: e993314f-3adc-4191-be61-4ef8874881c4
1548 ;;; calc-units.el ends here