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