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