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