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