1 ;; Calculator for GNU Emacs, part II [calc-units.el]
2 ;; Copyright (C) 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
3 ;; Written by Dave Gillespie, daveg@synaptics.com.
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is distributed in the hope that it will be useful,
8 ;; but WITHOUT ANY WARRANTY. No author or distributor
9 ;; accepts responsibility to anyone for the consequences of using it
10 ;; or for whether it serves any particular purpose or works at all,
11 ;; unless he says so in writing. Refer to the GNU Emacs General Public
12 ;; License for full details.
14 ;; Everyone is granted permission to copy, modify and redistribute
15 ;; GNU Emacs, but only under the conditions described in the
16 ;; GNU Emacs General Public License. A copy of this license is
17 ;; supposed to have been given to you along with GNU Emacs so you
18 ;; can know your rights and responsibilities. It should be in a
19 ;; file named COPYING. Among other things, the copyright notice
20 ;; and this notice must be preserved on all copies.
24 ;; This file is autoloaded from calc-ext.el.
29 (defun calc-Need-calc-units () nil)
34 (defun calc-base-units ()
37 (let ((calc-autorange-units nil))
38 (calc-enter-result 1 "bsun" (math-simplify-units
39 (math-to-standard-units (calc-top-n 1)
43 (defun calc-quick-units ()
46 (let* ((num (- last-command-char ?0))
47 (pos (if (= num 0) 10 num))
48 (units (calc-var-value 'var-Units))
49 (expr (calc-top-n 1)))
50 (or (and (>= num 0) (<= num 9))
51 (error "Bad unit number"))
52 (or (math-vectorp units)
53 (error "No \"quick units\" are defined"))
54 (or (< pos (length units))
55 (error "Unit number %d not defined" pos))
56 (if (math-units-in-expr-p expr nil)
57 (calc-enter-result 1 (format "cun%d" num)
58 (math-convert-units expr (nth pos units)))
59 (calc-enter-result 1 (format "*un%d" num)
61 (math-mul expr (nth pos units)))))))
64 (defun calc-convert-units (&optional old-units new-units)
67 (let ((expr (calc-top-n 1))
70 (or (math-units-in-expr-p expr t)
71 (let ((uold (or old-units
73 (setq uoldname (read-string "Old units: "))
74 (if (equal uoldname "")
78 (if (string-match "\\` */" uoldname)
79 (setq uoldname (concat "1" uoldname)))
80 (math-read-expr uoldname))))))
81 (if (eq (car-safe uold) 'error)
82 (error "Bad format in units expression: %s" (nth 1 uold)))
83 (setq expr (math-mul expr uold))))
85 (setq new-units (read-string (if uoldname
90 (if (string-match "\\` */" new-units)
91 (setq new-units (concat "1" new-units)))
92 (setq units (math-read-expr new-units))
93 (if (eq (car-safe units) 'error)
94 (error "Bad format in units expression: %s" (nth 2 units)))
95 (let ((unew (math-units-in-expr-p units t))
96 (std (and (eq (car-safe units) 'var)
97 (assq (nth 1 units) math-standard-units-systems))))
99 (calc-enter-result 1 "cvun" (math-simplify-units
100 (math-to-standard-units expr
103 (error "No units specified"))
104 (calc-enter-result 1 "cvun"
107 (and uoldname (not (equal uoldname "1")))))))))
110 (defun calc-autorange-units (arg)
113 (calc-change-mode 'calc-autorange-units arg nil t)
114 (message (if calc-autorange-units
115 "Adjusting target unit prefix automatically."
116 "Using target units exactly.")))
119 (defun calc-convert-temperature (&optional old-units new-units)
122 (let ((expr (calc-top-n 1))
126 (setq uold (or old-units
127 (let ((units (math-single-units-in-expr-p expr)))
130 (list 'var (car units)
131 (intern (concat "var-"
134 (error "Not a pure temperature expression"))
136 (setq uoldname (read-string
137 "Old temperature units: ")))))))
138 (if (eq (car-safe uold) 'error)
139 (error "Bad format in units expression: %s" (nth 2 uold)))
140 (or (math-units-in-expr-p expr nil)
141 (setq expr (math-mul expr uold)))
142 (setq unew (or new-units
144 (read-string (if uoldname
145 (concat "Old temperature units: "
148 "New temperature units: ")))))
149 (if (eq (car-safe unew) 'error)
150 (error "Bad format in units expression: %s" (nth 2 unew)))
151 (calc-enter-result 1 "cvtm" (math-simplify-units
152 (math-convert-temperature expr uold unew
156 (defun calc-remove-units ()
159 (calc-enter-result 1 "rmun" (math-simplify-units
160 (math-remove-units (calc-top-n 1)))))
163 (defun calc-extract-units ()
166 (calc-enter-result 1 "rmun" (math-simplify-units
167 (math-extract-units (calc-top-n 1)))))
170 (defun calc-explain-units ()
173 (let ((num-units nil)
175 (calc-explain-units-rec (calc-top-n 1) 1)
176 (and den-units (string-match "^[^(].* .*[^)]$" den-units)
177 (setq den-units (concat "(" den-units ")")))
180 (message "%s per %s" num-units den-units)
181 (message "%s" num-units))
183 (message "1 per %s" den-units)
184 (message "No units in expression")))))
187 (defun calc-explain-units-rec (expr pow)
188 (let ((u (math-check-unit-name expr))
190 (if (and u (not (math-zerop pow)))
191 (let ((name (or (nth 2 u) (symbol-name (car u)))))
192 (if (eq (aref name 0) ?\*)
193 (setq name (substring name 1)))
194 (if (string-match "[^a-zA-Z0-9']" name)
195 (if (string-match "^[a-zA-Z0-9' ()]*$" name)
196 (while (setq pos (string-match "[ ()]" name))
197 (setq name (concat (substring name 0 pos)
198 (if (eq (aref name pos) 32) "-" "")
199 (substring name (1+ pos)))))
200 (setq name (concat "(" name ")"))))
201 (or (eq (nth 1 expr) (car u))
202 (setq name (concat (nth 2 (assq (aref (symbol-name
205 (if (and (string-match "[^a-zA-Z0-9']" name)
206 (not (memq (car u) '(mHg gf))))
209 (cond ((or (math-equal-int pow 1)
210 (math-equal-int pow -1)))
211 ((or (math-equal-int pow 2)
212 (math-equal-int pow -2))
213 (if (equal (nth 4 u) '((m . 1)))
214 (setq name (concat "Square-" name))
215 (setq name (concat name "-squared"))))
216 ((or (math-equal-int pow 3)
217 (math-equal-int pow -3))
218 (if (equal (nth 4 u) '((m . 1)))
219 (setq name (concat "Cubic-" name))
220 (setq name (concat name "-cubed"))))
222 (setq name (concat name "^"
223 (math-format-number (math-abs pow))))))
225 (setq num-units (if num-units
226 (concat num-units " " name)
228 (setq den-units (if den-units
229 (concat den-units " " name)
231 (cond ((eq (car-safe expr) '*)
232 (calc-explain-units-rec (nth 1 expr) pow)
233 (calc-explain-units-rec (nth 2 expr) pow))
234 ((eq (car-safe expr) '/)
235 (calc-explain-units-rec (nth 1 expr) pow)
236 (calc-explain-units-rec (nth 2 expr) (- pow)))
237 ((memq (car-safe expr) '(neg + -))
238 (calc-explain-units-rec (nth 1 expr) pow))
239 ((and (eq (car-safe expr) '^)
240 (math-realp (nth 2 expr)))
241 (calc-explain-units-rec (nth 1 expr)
242 (math-mul pow (nth 2 expr)))))))
245 (defun calc-simplify-units ()
248 (calc-with-default-simplification
249 (calc-enter-result 1 "smun" (math-simplify-units (calc-top-n 1)))))
252 (defun calc-view-units-table (n)
254 (and n (setq math-units-table-buffer-valid nil))
255 (let ((win (get-buffer-window "*Units Table*")))
258 math-units-table-buffer-valid)
260 (bury-buffer (window-buffer win))
261 (let ((curwin (selected-window)))
263 (switch-to-buffer nil)
264 (select-window curwin)))
265 (math-build-units-table-buffer nil)))
268 (defun calc-enter-units-table (n)
270 (and n (setq math-units-table-buffer-valid nil))
271 (math-build-units-table-buffer t)
272 (message (substitute-command-keys "Type \\[calc] to return to the Calculator."))
275 (defun calc-define-unit (uname desc)
276 (interactive "SDefine unit name: \nsDescription: ")
278 (let ((form (calc-top-n 1))
279 (unit (assq uname math-additional-units)))
281 (setq math-additional-units
282 (cons (setq unit (list uname nil nil))
283 math-additional-units)
284 math-units-table nil))
285 (setcar (cdr unit) (and (not (and (eq (car-safe form) 'var)
286 (eq (nth 1 form) uname)))
287 (not (math-equal-int form 1))
288 (math-format-flat-expr form 0)))
289 (setcar (cdr (cdr unit)) (and (not (equal desc ""))
291 (calc-invalidate-units-table)
294 (defun calc-undefine-unit (uname)
295 (interactive "SUndefine unit name: ")
297 (let ((unit (assq uname math-additional-units)))
299 (if (assq uname math-standard-units)
300 (error "\"%s\" is a predefined unit name" uname)
301 (error "Unit name \"%s\" not found" uname)))
302 (setq math-additional-units (delq unit math-additional-units)
303 math-units-table nil)))
304 (calc-invalidate-units-table)
307 (defun calc-invalidate-units-table ()
308 (setq math-units-table nil)
309 (let ((buf (get-buffer "*Units Table*")))
314 (goto-char (point-min))
315 (if (looking-at "Calculator Units Table")
316 (let ((buffer-read-only nil))
317 (insert "(Obsolete) ")))))))
320 (defun calc-get-unit-definition (uname)
321 (interactive "SGet definition for unit: ")
323 (math-build-units-table)
324 (let ((unit (assq uname math-units-table)))
326 (error "Unit name \"%s\" not found" uname))
327 (let ((msg (nth 2 unit)))
329 (if (string-match "^\\*" msg)
330 (setq msg (substring msg 1)))
331 (setq msg (symbol-name uname)))
334 (calc-enter-result 0 "ugdf" (nth 1 unit))
335 (message "Derived unit: %s" msg))
336 (calc-enter-result 0 "ugdf" (list 'var uname
339 (symbol-name uname)))))
340 (message "Base unit: %s" msg)))))
343 (defun calc-permanent-units ()
347 (set-buffer (find-file-noselect (substitute-in-file-name
348 calc-settings-file)))
349 (goto-char (point-min))
350 (if (and (search-forward ";;; Custom units stored by Calc" nil t)
354 (search-forward "\n;;; End of custom units" nil t)))
358 (delete-region pos (point)))
359 (goto-char (point-max))
362 (insert ";;; Custom units stored by Calc on " (current-time-string) "\n")
363 (if math-additional-units
365 (insert "(setq math-additional-units '(\n")
366 (let ((list math-additional-units))
368 (insert " (" (symbol-name (car (car list))) " "
369 (if (nth 1 (car list))
370 (if (stringp (nth 1 (car list)))
371 (prin1-to-string (nth 1 (car list)))
372 (prin1-to-string (math-format-flat-expr
373 (nth 1 (car list)) 0)))
376 (prin1-to-string (nth 2 (car list)))
378 (setq list (cdr list))))
380 (insert ";;; (no custom units defined)\n"))
381 (insert ";;; End of custom units\n")
389 ;;; Units operations.
391 ;;; Units table last updated 9-Jan-91 by Ulrich Mueller (ulm@vsnhd1.cern.ch)
392 ;;; with some additions by Przemek Klosowski (przemek@rrdstrad.nist.gov)
394 (defvar math-standard-units
397 ( in "2.54 cm" "Inch" )
398 ( ft "12 in" "Foot" )
400 ( mi "5280 ft" "Mile" )
401 ( au "1.495979e11 m" "Astronomical Unit" )
402 ( lyr "9460536207068016 m" "Light Year" )
403 ( pc "206264.80625 au" "Parsec" )
404 ( nmi "1852 m" "Nautical Mile" )
405 ( fath "6 ft" "Fathom" )
406 ( u "1 um" "Micron" )
407 ( mil "in/1000" "Mil" )
408 ( point "in/72" "Point (1/72 inch)" )
409 ( tpt "in/72.27" "Point (TeX conventions)" )
410 ( Ang "1e-10 m" "Angstrom" )
411 ( mfi "mi+ft+in" "Miles + feet + inches" )
414 ( hect "10000 m^2" "*Hectare" )
415 ( acre "mi^2 / 640" "Acre" )
416 ( b "1e-28 m^2" "Barn" )
419 ( l "1e-3 m^3" "*Liter" )
420 ( L "1e-3 m^3" "Liter" )
421 ( gal "4 qt" "US Gallon" )
422 ( qt "2 pt" "Quart" )
423 ( pt "2 cup" "Pint" )
424 ( cup "8 ozfl" "Cup" )
425 ( ozfl "2 tbsp" "Fluid Ounce" )
426 ( floz "2 tbsp" "Fluid Ounce" )
427 ( tbsp "3 tsp" "Tablespoon" )
428 ( tsp "4.92892159375 ml" "Teaspoon" )
429 ( vol "tsp+tbsp+ozfl+cup+pt+qt+gal" "Gallons + ... + teaspoons" )
430 ( galC "4.54609 l" "Canadian Gallon" )
431 ( galUK "4.546092 l" "UK Gallon" )
436 ( min "60 s" "Minute" )
437 ( hr "60 min" "Hour" )
438 ( day "24 hr" "Day" )
439 ( wk "7 day" "Week" )
440 ( hms "wk+day+hr+min+s" "Hours, minutes, seconds" )
441 ( yr "365.25 day" "Year" )
445 ( mph "mi/hr" "*Miles per hour" )
446 ( kph "km/hr" "Kilometers per hour" )
447 ( knot "nmi/hr" "Knot" )
448 ( c "2.99792458e8 m/s" "Speed of light" )
451 ( ga "9.80665 m/s^2" "*\"g\" acceleration" )
455 ( lb "16 oz" "Pound (mass)" )
456 ( oz "28.349523125 g" "Ounce (mass)" )
457 ( ton "2000 lb" "Ton" )
458 ( tpo "ton+lb+oz" "Tons + pounds + ounces (mass)" )
459 ( t "1000 kg" "Metric ton" )
460 ( tonUK "1016.0469088 kg" "UK ton" )
461 ( lbt "12 ozt" "Troy pound" )
462 ( ozt "31.103475 g" "Troy ounce" )
463 ( ct ".2 g" "Carat" )
464 ( amu "1.6605402e-24 g" "Unified atomic mass" )
467 ( N "m kg/s^2" "*Newton" )
468 ( dyn "1e-5 N" "Dyne" )
469 ( gf "ga g" "Gram (force)" )
470 ( lbf "4.44822161526 N" "Pound (force)" )
471 ( kip "1000 lbf" "Kilopound (force)" )
472 ( pdl "0.138255 N" "Poundal" )
476 ( erg "1e-7 J" "Erg" )
477 ( cal "4.1868 J" "International Table Calorie" )
478 ( Btu "1055.05585262 J" "International Table Btu" )
479 ( eV "ech V" "Electron volt" )
480 ( ev "eV" "Electron volt" )
481 ( therm "105506000 J" "EEC therm" )
482 ( invcm "h c/cm" "Energy in inverse centimeters" )
483 ( Kayser "invcm" "Kayser (inverse centimeter energy)" )
484 ( men "100/invcm" "Inverse energy in meters" )
485 ( Hzen "h Hz" "Energy in Hertz")
486 ( Ken "k K" "Energy in Kelvins")
487 ;; ( invcm "eV / 8065.47835185" "Energy in inverse centimeters" )
488 ;; ( Hzen "eV / 2.41796958004e14" "Energy in Hertz")
489 ;; ( Ken "eV / 11604.7967327" "Energy in Kelvins")
493 ( hp "745.7 W" "Horsepower" )
496 ( K nil "*Degree Kelvin" K )
497 ( dK "K" "Degree Kelvin" K )
498 ( degK "K" "Degree Kelvin" K )
499 ( dC "K" "Degree Celsius" C )
500 ( degC "K" "Degree Celsius" C )
501 ( dF "(5/9) K" "Degree Fahrenheit" F )
502 ( degF "(5/9) K" "Degree Fahrenheit" F )
505 ( Pa "N/m^2" "*Pascal" )
506 ( bar "1e5 Pa" "Bar" )
507 ( atm "101325 Pa" "Standard atmosphere" )
508 ( torr "atm/760" "Torr" )
509 ( mHg "1000 torr" "Meter of mercury" )
510 ( inHg "25.4 mmHg" "Inch of mercury" )
511 ( inH2O "248.84 Pa" "Inch of water" )
512 ( psi "6894.75729317 Pa" "Pound per square inch" )
515 ( P "0.1 Pa s" "*Poise" )
516 ( St "1e-4 m^2/s" "Stokes" )
520 ( C "A s" "Coulomb" )
521 ( Fdy "ech Nav" "Faraday" )
522 ( e "1.60217733e-19 C" "Elementary charge" )
523 ( ech "1.60217733e-19 C" "Elementary charge" )
527 ( S "A/V" "Siemens" )
530 ( T "Wb/m^2" "Tesla" )
531 ( G "1e-4 T" "Gauss" )
534 ;; Luminous intensity
535 ( cd nil "*Candela" )
536 ( sb "1e4 cd/m^2" "Stilb" )
537 ( lm "cd sr" "Lumen" )
538 ( lx "lm/m^2" "Lux" )
539 ( ph "1e4 lx" "Phot" )
540 ( fc "10.76 lx" "Footcandle" )
541 ( lam "1e4 lm/m^2" "Lambert" )
542 ( flam "1.07639104e-3 lam" "Footlambert" )
545 ( Bq "1/s" "*Becquerel" )
546 ( Ci "3.7e10 Bq" "Curie" )
548 ( Sv "Gy" "Sievert" )
549 ( R "2.58e-4 C/kg" "Roentgen" )
550 ( rd ".01 Gy" "Rad" )
553 ;; Amount of substance
557 ( rad nil "*Radian" )
558 ( circ "2 pi rad" "Full circle" )
559 ( rev "circ" "Full revolution" )
560 ( deg "circ/360" "Degree" )
561 ( arcmin "deg/60" "Arc minute" )
562 ( arcsec "arcmin/60" "Arc second" )
563 ( grad "circ/400" "Grade" )
564 ( rpm "rev/min" "Revolutions per minute" )
567 ( sr nil "*Steradian" )
569 ;; Other physical quantities (Physics Letters B239, 1 (1990))
570 ( h "6.6260755e-34 J s" "*Planck's constant" )
571 ( hbar "h / 2 pi" "Planck's constant" )
572 ( mu0 "4 pi 1e-7 H/m" "Permeability of vacuum" )
573 ( Grav "6.67259e-11 N m^2/kg^2" "Gravitational constant" )
574 ( Nav "6.0221367e23 / mol" "Avagadro's constant" )
575 ( me "0.51099906 MeV/c^2" "Electron rest mass" )
576 ( mp "1.007276470 amu" "Proton rest mass" )
577 ( mn "1.008664904 amu" "Neutron rest mass" )
578 ( mu "0.113428913 amu" "Muon rest mass" )
579 ( Ryd "1.0973731571e5 invcm" "Rydberg's constant" )
580 ( k "1.3806513e-23 J/K" "Boltzmann's constant" )
581 ( fsc "1 / 137.0359895" "Fine structure constant" )
582 ( muB "5.78838263e-11 MeV/T" "Bohr magneton" )
583 ( muN "3.15245166e-14 MeV/T" "Nuclear magneton" )
584 ( mue "1.001159652193 muB" "Electron magnetic moment" )
585 ( mup "2.792847386 muN" "Proton magnetic moment" )
586 ( R0 "Nav k" "Molar gas constant" )
587 ( V0 "22.413992 L/mol" "Standard volume of ideal gas" )
591 (defvar math-additional-units nil
592 "*Additional units table for user-defined units.
593 Must be formatted like math-standard-units.
594 If this is changed, be sure to set math-units-table to nil to ensure
595 that the combined units table will be rebuilt.")
597 (defvar math-unit-prefixes
598 '( ( ?E (float 1 18) "Exa" )
599 ( ?P (float 1 15) "Peta" )
600 ( ?T (float 1 12) "Tera" )
601 ( ?G (float 1 9) "Giga" )
602 ( ?M (float 1 6) "Mega" )
603 ( ?k (float 1 3) "Kilo" )
604 ( ?K (float 1 3) "Kilo" )
605 ( ?h (float 1 2) "Hecto" )
606 ( ?H (float 1 2) "Hecto" )
607 ( ?D (float 1 1) "Deka" )
608 ( 0 (float 1 0) nil )
609 ( ?d (float 1 -1) "Deci" )
610 ( ?c (float 1 -2) "Centi" )
611 ( ?m (float 1 -3) "Milli" )
612 ( ?u (float 1 -6) "Micro" )
613 ( ?n (float 1 -9) "Nano" )
614 ( ?p (float 1 -12) "Pico" )
615 ( ?f (float 1 -15) "Femto" )
616 ( ?a (float 1 -18) "Atto" )
619 (defvar math-standard-units-systems
621 ( si ( ( g '(* (var kg var-kg) (float 1 -3)) ) ) )
622 ( mks ( ( g '(* (var kg var-kg) (float 1 -3)) ) ) )
623 ( cgs ( ( m '(* (var cm var-cm) 100 ) ) ) )
626 (defvar math-units-table nil
627 "Internal units table derived from math-defined-units.
628 Entries are (SYMBOL EXPR DOC-STRING TEMP-TYPE BASE-UNITS).")
630 (defvar math-units-table-buffer-valid nil)
633 (defun math-build-units-table ()
635 (let* ((combined-units (append math-additional-units
636 math-standard-units))
637 (unit-list (mapcar 'car combined-units))
639 (message "Building units table...")
640 (setq math-units-table-buffer-valid nil)
641 (setq tab (mapcar (function
645 (if (stringp (nth 1 x))
646 (let ((exp (math-read-plain-expr
648 (if (eq (car-safe exp) 'error)
649 (error "Format error in definition of %s in units table: %s"
656 (list (cons (car x) 1))))))
658 (let ((math-units-table tab))
659 (mapcar 'math-find-base-units tab))
660 (message "Building units table...done")
661 (setq math-units-table tab)))
664 (defun math-find-base-units (entry)
665 (if (eq (nth 4 entry) 'boom)
666 (error "Circular definition involving unit %s" (car entry)))
669 (setcar (nthcdr 4 entry) 'boom)
670 (math-find-base-units-rec (nth 1 entry) 1)
672 (error "Dimensionless definition for unit %s" (car entry)))
673 (while (eq (cdr (car base)) 0)
674 (setq base (cdr base)))
677 (if (eq (cdr (car (cdr b))) 0)
678 (setcdr b (cdr (cdr b)))
680 (setq base (sort base 'math-compare-unit-names))
681 (setcar (nthcdr 4 entry) base)
685 (defun math-compare-unit-names (a b)
686 (memq (car b) (cdr (memq (car a) unit-list)))
689 (defun math-find-base-units-rec (expr pow)
690 (let ((u (math-check-unit-name expr)))
692 (let ((ulist (math-find-base-units u)))
694 (let ((p (* (cdr (car ulist)) pow))
695 (old (assq (car (car ulist)) base)))
697 (setcdr old (+ (cdr old) p))
698 (setq base (cons (cons (car (car ulist)) p) base))))
699 (setq ulist (cdr ulist)))))
700 ((math-scalarp expr))
701 ((and (eq (car expr) '^)
702 (integerp (nth 2 expr)))
703 (math-find-base-units-rec (nth 1 expr) (* pow (nth 2 expr))))
705 (math-find-base-units-rec (nth 1 expr) pow)
706 (math-find-base-units-rec (nth 2 expr) pow))
708 (math-find-base-units-rec (nth 1 expr) pow)
709 (math-find-base-units-rec (nth 2 expr) (- pow)))
710 ((eq (car expr) 'neg)
711 (math-find-base-units-rec (nth 1 expr) pow))
713 (math-find-base-units-rec (nth 1 expr) pow))
714 ((eq (car expr) 'var)
715 (or (eq (nth 1 expr) 'pi)
716 (error "Unknown name %s in defining expression for unit %s"
717 (nth 1 expr) (car entry))))
718 (t (error "Malformed defining expression for unit %s" (car entry)))))
722 (defun math-units-in-expr-p (expr sub-exprs)
724 (if (eq (car expr) 'var)
725 (math-check-unit-name expr)
727 (memq (car expr) '(* / ^)))
728 (or (math-units-in-expr-p (nth 1 expr) sub-exprs)
729 (math-units-in-expr-p (nth 2 expr) sub-exprs)))))
732 (defun math-only-units-in-expr-p (expr)
734 (if (eq (car expr) 'var)
735 (math-check-unit-name expr)
736 (if (memq (car expr) '(* /))
737 (and (math-only-units-in-expr-p (nth 1 expr))
738 (math-only-units-in-expr-p (nth 2 expr)))
739 (and (eq (car expr) '^)
740 (and (math-only-units-in-expr-p (nth 1 expr))
741 (math-realp (nth 2 expr)))))))
744 (defun math-single-units-in-expr-p (expr)
745 (cond ((math-scalarp expr) nil)
746 ((eq (car expr) 'var)
747 (math-check-unit-name expr))
749 (let ((u1 (math-single-units-in-expr-p (nth 1 expr)))
750 (u2 (math-single-units-in-expr-p (nth 2 expr))))
751 (or (and u1 u2 'wrong)
755 (if (math-units-in-expr-p (nth 2 expr) nil)
757 (math-single-units-in-expr-p (nth 1 expr))))
761 (defun math-check-unit-name (v)
762 (and (eq (car-safe v) 'var)
763 (or (assq (nth 1 v) (or math-units-table (math-build-units-table)))
764 (let ((name (symbol-name (nth 1 v))))
765 (and (> (length name) 1)
766 (assq (aref name 0) math-unit-prefixes)
767 (or (assq (intern (substring name 1)) math-units-table)
768 (and (eq (aref name 0) ?M)
770 (eq (aref name 1) ?e)
771 (eq (aref name 2) ?g)
772 (assq (intern (substring name 3))
773 math-units-table)))))))
777 (defun math-to-standard-units (expr which-standard)
778 (math-to-standard-rec expr)
781 (defun math-to-standard-rec (expr)
782 (if (eq (car-safe expr) 'var)
783 (let ((u (math-check-unit-name expr))
788 (setq expr (math-to-standard-rec (nth 1 u)))
789 (let ((st (assq (car u) which-standard)))
791 (setq expr (nth 1 st))
792 (setq expr (list 'var (car u)
793 (intern (concat "var-"
799 (nth 1 (assq (aref (symbol-name base) 0)
806 (if (Math-primp expr)
809 (mapcar 'math-to-standard-rec (cdr expr)))))
812 (defun math-apply-units (expr units ulist &optional pure)
816 (setq expr (math-simplify-units expr))
817 (or (math-numberp expr)
818 (error "Incompatible units"))
820 (setq value (math-div expr (nth 1 (car ulist)))
821 value (math-floor (let ((calc-internal-prec
822 (1- calc-internal-prec)))
823 (math-normalize value)))
824 new (math-add new (math-mul value (car (car ulist))))
825 expr (math-sub expr (math-mul value (nth 1 (car ulist))))
827 (math-add new (math-mul (math-div expr (nth 1 (car ulist)))
829 (math-simplify-units (if pure
831 (list '* expr units))))
834 (defun math-decompose-units (units)
835 (let ((u (math-check-unit-name units)))
836 (and u (eq (car-safe (nth 1 u)) '+)
837 (setq units (nth 1 u))))
838 (setq units (calcFunc-expand units))
839 (and (eq (car-safe units) '+)
840 (let ((entry (list units calc-internal-prec calc-prefer-frac)))
841 (or (equal entry (car math-decompose-units-cache))
845 (while (eq (car-safe utemp) '+)
846 (setq ulist (cons (math-decompose-unit-part (nth 2 utemp))
848 utemp (nth 1 utemp)))
849 (setq ulist (cons (math-decompose-unit-part utemp) ulist)
851 (while (setq utemp (cdr utemp))
852 (or (equal (nth 2 (car utemp)) (nth 2 (car ulist)))
853 (error "Inconsistent units in sum")))
854 (setq math-decompose-units-cache
859 (not (Math-lessp (nth 1 x)
861 (cdr math-decompose-units-cache)))
863 (setq math-decompose-units-cache nil)
865 (defun math-decompose-unit-part (unit)
867 (math-is-multiple (math-simplify-units (math-to-standard-units
872 (defun math-find-compatible-unit (expr unit)
873 (let ((u (math-check-unit-name unit)))
875 (math-find-compatible-unit-rec expr 1)))
878 (defun math-find-compatible-unit-rec (expr pow)
879 (cond ((eq (car-safe expr) '*)
880 (or (math-find-compatible-unit-rec (nth 1 expr) pow)
881 (math-find-compatible-unit-rec (nth 2 expr) pow)))
882 ((eq (car-safe expr) '/)
883 (or (math-find-compatible-unit-rec (nth 1 expr) pow)
884 (math-find-compatible-unit-rec (nth 2 expr) (- pow))))
885 ((and (eq (car-safe expr) '^)
886 (integerp (nth 2 expr)))
887 (math-find-compatible-unit-rec (nth 1 expr) (* pow (nth 2 expr))))
889 (let ((u2 (math-check-unit-name expr)))
890 (if (equal (nth 4 u) (nth 4 u2))
894 (defun math-convert-units (expr new-units &optional pure)
895 (math-with-extra-prec 2
896 (let ((compat (and (not pure) (math-find-compatible-unit expr new-units)))
898 (math-combining-units nil))
901 (math-mul (math-mul (math-simplify-units
902 (math-div expr (math-pow (car compat)
904 (math-pow new-units (cdr compat)))
906 (math-to-standard-units
907 (math-pow (math-div (car compat) new-units)
910 (if (setq unit-list (math-decompose-units new-units))
911 (setq new-units (nth 2 (car unit-list))))
912 (if (eq (car-safe expr) '+)
913 (setq expr (math-simplify-units expr)))
914 (if (math-units-in-expr-p expr t)
915 (math-convert-units-rec expr)
916 (math-apply-units (math-to-standard-units
917 (list '/ expr new-units) nil)
918 new-units unit-list pure)))))
921 (defun math-convert-units-rec (expr)
922 (if (math-units-in-expr-p expr nil)
923 (math-apply-units (math-to-standard-units (list '/ expr new-units) nil)
924 new-units unit-list pure)
925 (if (Math-primp expr)
928 (mapcar 'math-convert-units-rec (cdr expr)))))
931 (defun math-convert-temperature (expr old new &optional pure)
932 (let* ((units (math-single-units-in-expr-p expr))
935 (equal (nth 1 old) (car units)))
936 (math-check-unit-name old)
937 (error "Inconsistent temperature units"))
939 (unew (math-check-unit-name new)))
940 (or (and (consp unew) (nth 3 unew))
941 (error "Not a valid temperature unit"))
942 (or (and (consp uold) (nth 3 uold))
943 (error "Not a pure temperature expression"))
944 (let ((v (car uold)))
945 (setq expr (list '/ expr (list 'var v
946 (intern (concat "var-"
947 (symbol-name v)))))))
948 (or (eq (nth 3 uold) (nth 3 unew))
949 (cond ((eq (nth 3 uold) 'K)
950 (setq expr (list '- expr '(float 27315 -2)))
951 (if (eq (nth 3 unew) 'F)
952 (setq expr (list '+ (list '* expr '(frac 9 5)) 32))))
953 ((eq (nth 3 uold) 'C)
954 (if (eq (nth 3 unew) 'F)
955 (setq expr (list '+ (list '* expr '(frac 9 5)) 32))
956 (setq expr (list '+ expr '(float 27315 -2)))))
958 (setq expr (list '* (list '- expr 32) '(frac 5 9)))
959 (if (eq (nth 3 unew) 'K)
960 (setq expr (list '+ expr '(float 27315 -2)))))))
968 (defun math-simplify-units (a)
969 (let ((math-simplifying-units t)
970 (calc-matrix-mode 'scalar))
973 (fset 'calcFunc-usimplify (symbol-function 'math-simplify-units))
975 (math-defsimplify (+ -)
976 (and math-simplifying-units
977 (math-units-in-expr-p (nth 1 expr) nil)
978 (let* ((units (math-extract-units (nth 1 expr)))
979 (ratio (math-simplify (math-to-standard-units
980 (list '/ (nth 2 expr) units) nil))))
981 (if (math-units-in-expr-p ratio nil)
983 (calc-record-why "*Inconsistent units" expr)
985 (list '* (math-add (math-remove-units (nth 1 expr))
986 (if (eq (car expr) '-) (math-neg ratio) ratio))
991 (math-simplify-units-prod)
994 (defun math-simplify-units-prod ()
995 (and math-simplifying-units
997 (Math-realp (nth 1 expr))
998 (let* ((num (math-float (nth 1 expr)))
999 (xpon (calcFunc-xpon num))
1000 (unitp (cdr (cdr expr)))
1002 (pow (if (eq (car expr) '*) 1 -1))
1004 (and (eq (car-safe unit) '*)
1005 (setq unitp (cdr unit)
1007 (and (eq (car-safe unit) '^)
1008 (integerp (nth 2 unit))
1009 (setq pow (* pow (nth 2 unit))
1012 (and (setq u (math-check-unit-name unit))
1015 (>= xpon (if (eq (car u) 'm) 1 3)))
1018 (reverse math-unit-prefixes)
1019 math-unit-prefixes))
1022 (or (eq (car u) (nth 1 unit))
1027 math-unit-prefixes))))))
1028 (setq xpon (+ xpon uxpon))
1030 (or (memq (car (car p)) '(?d ?D ?h ?H))
1031 (and (eq (car (car p)) ?c)
1032 (not (eq (car u) 'm)))
1033 (< xpon (setq pxpon (* (nth 2 (nth 1 (car p)))
1036 (setq pname (math-build-var-name
1037 (if (eq (car (car p)) 0)
1039 (concat (char-to-string
1043 (and (/= (car (car p)) 0)
1045 math-units-table)))))
1049 (or (not (eq p pref))
1050 (< xpon (+ pxpon (* (math-abs pow) 3))))
1053 (let ((calc-prefer-frac nil))
1054 (calcFunc-scf (nth 1 expr)
1056 (setcar unitp pname)
1061 (and math-simplifying-units
1062 (let ((np (cdr expr))
1063 (try-cancel-units 0)
1065 (setq n (if (eq (car-safe (nth 2 expr)) '*)
1068 (if (math-realp (car n))
1070 (setcar (cdr expr) (math-mul (nth 1 expr)
1071 (let ((calc-prefer-frac nil))
1072 (math-div 1 (car n)))))
1074 (while (eq (car-safe (setq n (car np))) '*)
1075 (math-simplify-units-divisor (cdr n) (cdr (cdr expr)))
1076 (setq np (cdr (cdr n))))
1077 (math-simplify-units-divisor np (cdr (cdr expr)))
1078 (if (eq try-cancel-units 0)
1079 (let* ((math-simplifying-units nil)
1080 (base (math-simplify (math-to-standard-units expr nil))))
1081 (if (Math-numberp base)
1083 (if (eq (car-safe expr) '/)
1084 (math-simplify-units-prod))
1088 (defun math-simplify-units-divisor (np dp)
1091 (while (eq (car-safe (setq d (car dp))) '*)
1092 (if (setq temp (math-simplify-units-quotient n (nth 1 d)))
1094 (setcar np (setq n temp))
1095 (setcar (cdr d) 1)))
1096 (setq dp (cdr (cdr d))))
1097 (if (setq temp (math-simplify-units-quotient n d))
1099 (setcar np (setq n temp))
1103 ;; Simplify, e.g., "in / cm" to "2.54" in a units expression.
1104 (defun math-simplify-units-quotient (n d)
1107 (and (eq (car-safe n) '^)
1108 (integerp (nth 2 n))
1109 (setq pow1 (nth 2 n) n (nth 1 n)))
1110 (and (eq (car-safe d) '^)
1111 (integerp (nth 2 d))
1112 (setq pow2 (nth 2 d) d (nth 1 d)))
1113 (let ((un (math-check-unit-name n))
1114 (ud (math-check-unit-name d)))
1116 (if (and (equal (nth 4 un) (nth 4 ud))
1118 (math-to-standard-units (list '/ n d) nil)
1125 (and (eq (car (car un)) (car (car ud1)))
1126 (setq try-cancel-units
1128 (- (* (cdr (car un)) pow1)
1129 (* (cdr (car ud)) pow2)))))
1130 (setq ud1 (cdr ud1)))
1136 (and math-simplifying-units
1137 (math-realp (nth 2 expr))
1138 (if (memq (car-safe (nth 1 expr)) '(* /))
1139 (list (car (nth 1 expr))
1140 (list '^ (nth 1 (nth 1 expr)) (nth 2 expr))
1141 (list '^ (nth 2 (nth 1 expr)) (nth 2 expr)))
1142 (math-simplify-units-pow (nth 1 expr) (nth 2 expr))))
1145 (math-defsimplify calcFunc-sqrt
1146 (and math-simplifying-units
1147 (if (memq (car-safe (nth 1 expr)) '(* /))
1148 (list (car (nth 1 expr))
1149 (list 'calcFunc-sqrt (nth 1 (nth 1 expr)))
1150 (list 'calcFunc-sqrt (nth 2 (nth 1 expr))))
1151 (math-simplify-units-pow (nth 1 expr) '(frac 1 2))))
1154 (math-defsimplify (calcFunc-floor
1164 (and math-simplifying-units
1166 (if (math-only-units-in-expr-p (nth 1 expr))
1168 (if (and (memq (car-safe (nth 1 expr)) '(* /))
1169 (or (math-only-units-in-expr-p
1170 (nth 1 (nth 1 expr)))
1171 (math-only-units-in-expr-p
1172 (nth 2 (nth 1 expr)))))
1173 (list (car (nth 1 expr))
1175 (cons (nth 1 (nth 1 expr))
1178 (cons (nth 2 (nth 1 expr))
1179 (cdr (cdr expr)))))))))
1181 (defun math-simplify-units-pow (a pow)
1182 (if (and (eq (car-safe a) '^)
1183 (math-check-unit-name (nth 1 a))
1184 (math-realp (nth 2 a)))
1185 (list '^ (nth 1 a) (math-mul pow (nth 2 a)))
1186 (let* ((u (math-check-unit-name a))
1187 (pf (math-to-simple-fraction pow))
1188 (d (and (eq (car-safe pf) 'frac) (nth 2 pf))))
1190 (math-units-are-multiple u d)
1191 (list '^ (math-to-standard-units a nil) pow))))
1195 (defun math-units-are-multiple (u n)
1197 (while (and u (= (% (cdr (car u)) n) 0))
1202 (math-defsimplify calcFunc-sin
1203 (and math-simplifying-units
1204 (math-units-in-expr-p (nth 1 expr) nil)
1205 (let ((rad (math-simplify-units
1207 (math-to-standard-units (nth 1 expr) nil))))
1208 (calc-angle-mode 'rad))
1209 (and (eq (car-safe rad) '*)
1210 (math-realp (nth 1 rad))
1211 (eq (car-safe (nth 2 rad)) 'var)
1212 (eq (nth 1 (nth 2 rad)) 'rad)
1213 (list 'calcFunc-sin (nth 1 rad)))))
1216 (math-defsimplify calcFunc-cos
1217 (and math-simplifying-units
1218 (math-units-in-expr-p (nth 1 expr) nil)
1219 (let ((rad (math-simplify-units
1221 (math-to-standard-units (nth 1 expr) nil))))
1222 (calc-angle-mode 'rad))
1223 (and (eq (car-safe rad) '*)
1224 (math-realp (nth 1 rad))
1225 (eq (car-safe (nth 2 rad)) 'var)
1226 (eq (nth 1 (nth 2 rad)) 'rad)
1227 (list 'calcFunc-cos (nth 1 rad)))))
1230 (math-defsimplify calcFunc-tan
1231 (and math-simplifying-units
1232 (math-units-in-expr-p (nth 1 expr) nil)
1233 (let ((rad (math-simplify-units
1235 (math-to-standard-units (nth 1 expr) nil))))
1236 (calc-angle-mode 'rad))
1237 (and (eq (car-safe rad) '*)
1238 (math-realp (nth 1 rad))
1239 (eq (car-safe (nth 2 rad)) 'var)
1240 (eq (nth 1 (nth 2 rad)) 'rad)
1241 (list 'calcFunc-tan (nth 1 rad)))))
1245 (defun math-remove-units (expr)
1246 (if (math-check-unit-name expr)
1248 (if (Math-primp expr)
1251 (mapcar 'math-remove-units (cdr expr)))))
1254 (defun math-extract-units (expr)
1255 (if (memq (car-safe expr) '(* /))
1257 (mapcar 'math-extract-units (cdr expr)))
1258 (if (math-check-unit-name expr) expr 1))
1261 (defun math-build-units-table-buffer (enter-buffer)
1262 (if (not (and math-units-table math-units-table-buffer-valid
1263 (get-buffer "*Units Table*")))
1264 (let ((buf (get-buffer-create "*Units Table*"))
1265 (uptr (math-build-units-table))
1266 (calc-language (if (eq calc-language 'big) nil calc-language))
1267 (calc-float-format '(float 0))
1268 (calc-group-digits nil)
1269 (calc-number-radix 10)
1270 (calc-point-char ".")
1274 (message "Formatting units table...")
1276 (setq buffer-read-only nil)
1278 (insert "Calculator Units Table:\n\n")
1279 (insert "Unit Type Definition Description\n\n")
1285 (setq shadowed (and std (assq (car u) math-additional-units)))
1288 (eq (aref name 0) ?\*))
1290 (or (eq uptr math-units-table)
1292 (setq name (substring name 1))))
1294 (and shadowed (insert "("))
1295 (insert (symbol-name (car u)))
1296 (and shadowed (insert ")"))
1300 (insert (symbol-name (nth 3 u))))
1306 (and shadowed (insert "("))
1308 (insert (math-format-value (nth 1 u) 80))
1309 (insert (symbol-name (car u))))
1310 (and shadowed (insert ")"))
1316 (insert " (redefined above)")
1318 (insert " (base unit)")))
1320 (setq uptr (cdr uptr)))
1321 (insert "\n\nUnit Prefix Table:\n\n")
1322 (setq uptr math-unit-prefixes)
1325 (insert " " (char-to-string (car u)))
1326 (if (equal (nth 1 u) (nth 1 (nth 1 uptr)))
1327 (insert " " (char-to-string (car (car (setq uptr (cdr uptr)))))
1330 (insert "10^" (int-to-string (nth 2 (nth 1 u))))
1332 (insert " " (nth 2 u) "\n")
1333 (while (eq (car (car (setq uptr (cdr uptr)))) 0)))
1335 (setq buffer-read-only t)
1336 (message "Formatting units table...done"))
1337 (setq math-units-table-buffer-valid t)
1338 (let ((oldbuf (current-buffer)))
1340 (goto-char (point-min))
1341 (set-buffer oldbuf))
1344 (display-buffer buf)))
1346 (pop-to-buffer (get-buffer "*Units Table*"))
1347 (display-buffer (get-buffer "*Units Table*"))))