diff options
| -rw-r--r-- | lisp/calc/calc-units.el | 164 |
1 files changed, 55 insertions, 109 deletions
diff --git a/lisp/calc/calc-units.el b/lisp/calc/calc-units.el index 80c30622b38..65640c701de 100644 --- a/lisp/calc/calc-units.el +++ b/lisp/calc/calc-units.el | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | ;; Calculator for GNU Emacs, part II [calc-units.el] | 1 | ;; Calculator for GNU Emacs, part II [calc-units.el] |
| 2 | ;; Copyright (C) 1990, 1991, 1992, 1993 Free Software Foundation, Inc. | 2 | ;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc. |
| 3 | ;; Written by Dave Gillespie, daveg@synaptics.com. | 3 | ;; Written by Dave Gillespie, daveg@synaptics.com. |
| 4 | 4 | ||
| 5 | ;; This file is part of GNU Emacs. | 5 | ;; This file is part of GNU Emacs. |
| @@ -37,8 +37,7 @@ | |||
| 37 | (let ((calc-autorange-units nil)) | 37 | (let ((calc-autorange-units nil)) |
| 38 | (calc-enter-result 1 "bsun" (math-simplify-units | 38 | (calc-enter-result 1 "bsun" (math-simplify-units |
| 39 | (math-to-standard-units (calc-top-n 1) | 39 | (math-to-standard-units (calc-top-n 1) |
| 40 | nil))))) | 40 | nil)))))) |
| 41 | ) | ||
| 42 | 41 | ||
| 43 | (defun calc-quick-units () | 42 | (defun calc-quick-units () |
| 44 | (interactive) | 43 | (interactive) |
| @@ -58,8 +57,7 @@ | |||
| 58 | (math-convert-units expr (nth pos units))) | 57 | (math-convert-units expr (nth pos units))) |
| 59 | (calc-enter-result 1 (format "*un%d" num) | 58 | (calc-enter-result 1 (format "*un%d" num) |
| 60 | (math-simplify-units | 59 | (math-simplify-units |
| 61 | (math-mul expr (nth pos units))))))) | 60 | (math-mul expr (nth pos units)))))))) |
| 62 | ) | ||
| 63 | 61 | ||
| 64 | (defun calc-convert-units (&optional old-units new-units) | 62 | (defun calc-convert-units (&optional old-units new-units) |
| 65 | (interactive) | 63 | (interactive) |
| @@ -104,8 +102,7 @@ | |||
| 104 | (calc-enter-result 1 "cvun" | 102 | (calc-enter-result 1 "cvun" |
| 105 | (math-convert-units | 103 | (math-convert-units |
| 106 | expr units | 104 | expr units |
| 107 | (and uoldname (not (equal uoldname "1"))))))))) | 105 | (and uoldname (not (equal uoldname "1")))))))))) |
| 108 | ) | ||
| 109 | 106 | ||
| 110 | (defun calc-autorange-units (arg) | 107 | (defun calc-autorange-units (arg) |
| 111 | (interactive "P") | 108 | (interactive "P") |
| @@ -113,8 +110,7 @@ | |||
| 113 | (calc-change-mode 'calc-autorange-units arg nil t) | 110 | (calc-change-mode 'calc-autorange-units arg nil t) |
| 114 | (message (if calc-autorange-units | 111 | (message (if calc-autorange-units |
| 115 | "Adjusting target unit prefix automatically." | 112 | "Adjusting target unit prefix automatically." |
| 116 | "Using target units exactly."))) | 113 | "Using target units exactly.")))) |
| 117 | ) | ||
| 118 | 114 | ||
| 119 | (defun calc-convert-temperature (&optional old-units new-units) | 115 | (defun calc-convert-temperature (&optional old-units new-units) |
| 120 | (interactive) | 116 | (interactive) |
| @@ -150,22 +146,19 @@ | |||
| 150 | (error "Bad format in units expression: %s" (nth 2 unew))) | 146 | (error "Bad format in units expression: %s" (nth 2 unew))) |
| 151 | (calc-enter-result 1 "cvtm" (math-simplify-units | 147 | (calc-enter-result 1 "cvtm" (math-simplify-units |
| 152 | (math-convert-temperature expr uold unew | 148 | (math-convert-temperature expr uold unew |
| 153 | uoldname))))) | 149 | uoldname)))))) |
| 154 | ) | ||
| 155 | 150 | ||
| 156 | (defun calc-remove-units () | 151 | (defun calc-remove-units () |
| 157 | (interactive) | 152 | (interactive) |
| 158 | (calc-slow-wrapper | 153 | (calc-slow-wrapper |
| 159 | (calc-enter-result 1 "rmun" (math-simplify-units | 154 | (calc-enter-result 1 "rmun" (math-simplify-units |
| 160 | (math-remove-units (calc-top-n 1))))) | 155 | (math-remove-units (calc-top-n 1)))))) |
| 161 | ) | ||
| 162 | 156 | ||
| 163 | (defun calc-extract-units () | 157 | (defun calc-extract-units () |
| 164 | (interactive) | 158 | (interactive) |
| 165 | (calc-slow-wrapper | 159 | (calc-slow-wrapper |
| 166 | (calc-enter-result 1 "rmun" (math-simplify-units | 160 | (calc-enter-result 1 "rmun" (math-simplify-units |
| 167 | (math-extract-units (calc-top-n 1))))) | 161 | (math-extract-units (calc-top-n 1)))))) |
| 168 | ) | ||
| 169 | 162 | ||
| 170 | (defun calc-explain-units () | 163 | (defun calc-explain-units () |
| 171 | (interactive) | 164 | (interactive) |
| @@ -181,8 +174,7 @@ | |||
| 181 | (message "%s" num-units)) | 174 | (message "%s" num-units)) |
| 182 | (if den-units | 175 | (if den-units |
| 183 | (message "1 per %s" den-units) | 176 | (message "1 per %s" den-units) |
| 184 | (message "No units in expression"))))) | 177 | (message "No units in expression")))))) |
| 185 | ) | ||
| 186 | 178 | ||
| 187 | (defun calc-explain-units-rec (expr pow) | 179 | (defun calc-explain-units-rec (expr pow) |
| 188 | (let ((u (math-check-unit-name expr)) | 180 | (let ((u (math-check-unit-name expr)) |
| @@ -239,15 +231,13 @@ | |||
| 239 | ((and (eq (car-safe expr) '^) | 231 | ((and (eq (car-safe expr) '^) |
| 240 | (math-realp (nth 2 expr))) | 232 | (math-realp (nth 2 expr))) |
| 241 | (calc-explain-units-rec (nth 1 expr) | 233 | (calc-explain-units-rec (nth 1 expr) |
| 242 | (math-mul pow (nth 2 expr))))))) | 234 | (math-mul pow (nth 2 expr)))))))) |
| 243 | ) | ||
| 244 | 235 | ||
| 245 | (defun calc-simplify-units () | 236 | (defun calc-simplify-units () |
| 246 | (interactive) | 237 | (interactive) |
| 247 | (calc-slow-wrapper | 238 | (calc-slow-wrapper |
| 248 | (calc-with-default-simplification | 239 | (calc-with-default-simplification |
| 249 | (calc-enter-result 1 "smun" (math-simplify-units (calc-top-n 1))))) | 240 | (calc-enter-result 1 "smun" (math-simplify-units (calc-top-n 1)))))) |
| 250 | ) | ||
| 251 | 241 | ||
| 252 | (defun calc-view-units-table (n) | 242 | (defun calc-view-units-table (n) |
| 253 | (interactive "P") | 243 | (interactive "P") |
| @@ -262,15 +252,13 @@ | |||
| 262 | (select-window win) | 252 | (select-window win) |
| 263 | (switch-to-buffer nil) | 253 | (switch-to-buffer nil) |
| 264 | (select-window curwin))) | 254 | (select-window curwin))) |
| 265 | (math-build-units-table-buffer nil))) | 255 | (math-build-units-table-buffer nil)))) |
| 266 | ) | ||
| 267 | 256 | ||
| 268 | (defun calc-enter-units-table (n) | 257 | (defun calc-enter-units-table (n) |
| 269 | (interactive "P") | 258 | (interactive "P") |
| 270 | (and n (setq math-units-table-buffer-valid nil)) | 259 | (and n (setq math-units-table-buffer-valid nil)) |
| 271 | (math-build-units-table-buffer t) | 260 | (math-build-units-table-buffer t) |
| 272 | (message (substitute-command-keys "Type \\[calc] to return to the Calculator.")) | 261 | (message (substitute-command-keys "Type \\[calc] to return to the Calculator."))) |
| 273 | ) | ||
| 274 | 262 | ||
| 275 | (defun calc-define-unit (uname desc) | 263 | (defun calc-define-unit (uname desc) |
| 276 | (interactive "SDefine unit name: \nsDescription: ") | 264 | (interactive "SDefine unit name: \nsDescription: ") |
| @@ -288,8 +276,7 @@ | |||
| 288 | (math-format-flat-expr form 0))) | 276 | (math-format-flat-expr form 0))) |
| 289 | (setcar (cdr (cdr unit)) (and (not (equal desc "")) | 277 | (setcar (cdr (cdr unit)) (and (not (equal desc "")) |
| 290 | desc)))) | 278 | desc)))) |
| 291 | (calc-invalidate-units-table) | 279 | (calc-invalidate-units-table)) |
| 292 | ) | ||
| 293 | 280 | ||
| 294 | (defun calc-undefine-unit (uname) | 281 | (defun calc-undefine-unit (uname) |
| 295 | (interactive "SUndefine unit name: ") | 282 | (interactive "SUndefine unit name: ") |
| @@ -301,8 +288,7 @@ | |||
| 301 | (error "Unit name \"%s\" not found" uname))) | 288 | (error "Unit name \"%s\" not found" uname))) |
| 302 | (setq math-additional-units (delq unit math-additional-units) | 289 | (setq math-additional-units (delq unit math-additional-units) |
| 303 | math-units-table nil))) | 290 | math-units-table nil))) |
| 304 | (calc-invalidate-units-table) | 291 | (calc-invalidate-units-table)) |
| 305 | ) | ||
| 306 | 292 | ||
| 307 | (defun calc-invalidate-units-table () | 293 | (defun calc-invalidate-units-table () |
| 308 | (setq math-units-table nil) | 294 | (setq math-units-table nil) |
| @@ -314,8 +300,7 @@ | |||
| 314 | (goto-char (point-min)) | 300 | (goto-char (point-min)) |
| 315 | (if (looking-at "Calculator Units Table") | 301 | (if (looking-at "Calculator Units Table") |
| 316 | (let ((buffer-read-only nil)) | 302 | (let ((buffer-read-only nil)) |
| 317 | (insert "(Obsolete) "))))))) | 303 | (insert "(Obsolete) ")))))))) |
| 318 | ) | ||
| 319 | 304 | ||
| 320 | (defun calc-get-unit-definition (uname) | 305 | (defun calc-get-unit-definition (uname) |
| 321 | (interactive "SGet definition for unit: ") | 306 | (interactive "SGet definition for unit: ") |
| @@ -337,8 +322,7 @@ | |||
| 337 | (intern | 322 | (intern |
| 338 | (concat "var-" | 323 | (concat "var-" |
| 339 | (symbol-name uname))))) | 324 | (symbol-name uname))))) |
| 340 | (message "Base unit: %s" msg))))) | 325 | (message "Base unit: %s" msg)))))) |
| 341 | ) | ||
| 342 | 326 | ||
| 343 | (defun calc-permanent-units () | 327 | (defun calc-permanent-units () |
| 344 | (interactive) | 328 | (interactive) |
| @@ -379,8 +363,7 @@ | |||
| 379 | (insert "))\n")) | 363 | (insert "))\n")) |
| 380 | (insert ";;; (no custom units defined)\n")) | 364 | (insert ";;; (no custom units defined)\n")) |
| 381 | (insert ";;; End of custom units\n") | 365 | (insert ";;; End of custom units\n") |
| 382 | (save-buffer))) | 366 | (save-buffer)))) |
| 383 | ) | ||
| 384 | 367 | ||
| 385 | 368 | ||
| 386 | 369 | ||
| @@ -658,8 +641,7 @@ Entries are (SYMBOL EXPR DOC-STRING TEMP-TYPE BASE-UNITS).") | |||
| 658 | (let ((math-units-table tab)) | 641 | (let ((math-units-table tab)) |
| 659 | (mapcar 'math-find-base-units tab)) | 642 | (mapcar 'math-find-base-units tab)) |
| 660 | (message "Building units table...done") | 643 | (message "Building units table...done") |
| 661 | (setq math-units-table tab))) | 644 | (setq math-units-table tab)))) |
| 662 | ) | ||
| 663 | 645 | ||
| 664 | (defun math-find-base-units (entry) | 646 | (defun math-find-base-units (entry) |
| 665 | (if (eq (nth 4 entry) 'boom) | 647 | (if (eq (nth 4 entry) 'boom) |
| @@ -679,12 +661,10 @@ Entries are (SYMBOL EXPR DOC-STRING TEMP-TYPE BASE-UNITS).") | |||
| 679 | (setq b (cdr b))))) | 661 | (setq b (cdr b))))) |
| 680 | (setq base (sort base 'math-compare-unit-names)) | 662 | (setq base (sort base 'math-compare-unit-names)) |
| 681 | (setcar (nthcdr 4 entry) base) | 663 | (setcar (nthcdr 4 entry) base) |
| 682 | base)) | 664 | base))) |
| 683 | ) | ||
| 684 | 665 | ||
| 685 | (defun math-compare-unit-names (a b) | 666 | (defun math-compare-unit-names (a b) |
| 686 | (memq (car b) (cdr (memq (car a) unit-list))) | 667 | (memq (car b) (cdr (memq (car a) unit-list)))) |
| 687 | ) | ||
| 688 | 668 | ||
| 689 | (defun math-find-base-units-rec (expr pow) | 669 | (defun math-find-base-units-rec (expr pow) |
| 690 | (let ((u (math-check-unit-name expr))) | 670 | (let ((u (math-check-unit-name expr))) |
| @@ -715,8 +695,7 @@ Entries are (SYMBOL EXPR DOC-STRING TEMP-TYPE BASE-UNITS).") | |||
| 715 | (or (eq (nth 1 expr) 'pi) | 695 | (or (eq (nth 1 expr) 'pi) |
| 716 | (error "Unknown name %s in defining expression for unit %s" | 696 | (error "Unknown name %s in defining expression for unit %s" |
| 717 | (nth 1 expr) (car entry)))) | 697 | (nth 1 expr) (car entry)))) |
| 718 | (t (error "Malformed defining expression for unit %s" (car entry))))) | 698 | (t (error "Malformed defining expression for unit %s" (car entry)))))) |
| 719 | ) | ||
| 720 | 699 | ||
| 721 | 700 | ||
| 722 | (defun math-units-in-expr-p (expr sub-exprs) | 701 | (defun math-units-in-expr-p (expr sub-exprs) |
| @@ -726,8 +705,7 @@ Entries are (SYMBOL EXPR DOC-STRING TEMP-TYPE BASE-UNITS).") | |||
| 726 | (and (or sub-exprs | 705 | (and (or sub-exprs |
| 727 | (memq (car expr) '(* / ^))) | 706 | (memq (car expr) '(* / ^))) |
| 728 | (or (math-units-in-expr-p (nth 1 expr) sub-exprs) | 707 | (or (math-units-in-expr-p (nth 1 expr) sub-exprs) |
| 729 | (math-units-in-expr-p (nth 2 expr) sub-exprs))))) | 708 | (math-units-in-expr-p (nth 2 expr) sub-exprs)))))) |
| 730 | ) | ||
| 731 | 709 | ||
| 732 | (defun math-only-units-in-expr-p (expr) | 710 | (defun math-only-units-in-expr-p (expr) |
| 733 | (and (consp expr) | 711 | (and (consp expr) |
| @@ -738,8 +716,7 @@ Entries are (SYMBOL EXPR DOC-STRING TEMP-TYPE BASE-UNITS).") | |||
| 738 | (math-only-units-in-expr-p (nth 2 expr))) | 716 | (math-only-units-in-expr-p (nth 2 expr))) |
| 739 | (and (eq (car expr) '^) | 717 | (and (eq (car expr) '^) |
| 740 | (and (math-only-units-in-expr-p (nth 1 expr)) | 718 | (and (math-only-units-in-expr-p (nth 1 expr)) |
| 741 | (math-realp (nth 2 expr))))))) | 719 | (math-realp (nth 2 expr)))))))) |
| 742 | ) | ||
| 743 | 720 | ||
| 744 | (defun math-single-units-in-expr-p (expr) | 721 | (defun math-single-units-in-expr-p (expr) |
| 745 | (cond ((math-scalarp expr) nil) | 722 | (cond ((math-scalarp expr) nil) |
| @@ -755,8 +732,7 @@ Entries are (SYMBOL EXPR DOC-STRING TEMP-TYPE BASE-UNITS).") | |||
| 755 | (if (math-units-in-expr-p (nth 2 expr) nil) | 732 | (if (math-units-in-expr-p (nth 2 expr) nil) |
| 756 | 'wrong | 733 | 'wrong |
| 757 | (math-single-units-in-expr-p (nth 1 expr)))) | 734 | (math-single-units-in-expr-p (nth 1 expr)))) |
| 758 | (t 'wrong)) | 735 | (t 'wrong))) |
| 759 | ) | ||
| 760 | 736 | ||
| 761 | (defun math-check-unit-name (v) | 737 | (defun math-check-unit-name (v) |
| 762 | (and (eq (car-safe v) 'var) | 738 | (and (eq (car-safe v) 'var) |
| @@ -770,13 +746,11 @@ Entries are (SYMBOL EXPR DOC-STRING TEMP-TYPE BASE-UNITS).") | |||
| 770 | (eq (aref name 1) ?e) | 746 | (eq (aref name 1) ?e) |
| 771 | (eq (aref name 2) ?g) | 747 | (eq (aref name 2) ?g) |
| 772 | (assq (intern (substring name 3)) | 748 | (assq (intern (substring name 3)) |
| 773 | math-units-table))))))) | 749 | math-units-table)))))))) |
| 774 | ) | ||
| 775 | 750 | ||
| 776 | 751 | ||
| 777 | (defun math-to-standard-units (expr which-standard) | 752 | (defun math-to-standard-units (expr which-standard) |
| 778 | (math-to-standard-rec expr) | 753 | (math-to-standard-rec expr)) |
| 779 | ) | ||
| 780 | 754 | ||
| 781 | (defun math-to-standard-rec (expr) | 755 | (defun math-to-standard-rec (expr) |
| 782 | (if (eq (car-safe expr) 'var) | 756 | (if (eq (car-safe expr) 'var) |
| @@ -806,8 +780,7 @@ Entries are (SYMBOL EXPR DOC-STRING TEMP-TYPE BASE-UNITS).") | |||
| 806 | (if (Math-primp expr) | 780 | (if (Math-primp expr) |
| 807 | expr | 781 | expr |
| 808 | (cons (car expr) | 782 | (cons (car expr) |
| 809 | (mapcar 'math-to-standard-rec (cdr expr))))) | 783 | (mapcar 'math-to-standard-rec (cdr expr)))))) |
| 810 | ) | ||
| 811 | 784 | ||
| 812 | (defun math-apply-units (expr units ulist &optional pure) | 785 | (defun math-apply-units (expr units ulist &optional pure) |
| 813 | (if ulist | 786 | (if ulist |
| @@ -828,8 +801,7 @@ Entries are (SYMBOL EXPR DOC-STRING TEMP-TYPE BASE-UNITS).") | |||
| 828 | (car (car ulist))))) | 801 | (car (car ulist))))) |
| 829 | (math-simplify-units (if pure | 802 | (math-simplify-units (if pure |
| 830 | expr | 803 | expr |
| 831 | (list '* expr units)))) | 804 | (list '* expr units))))) |
| 832 | ) | ||
| 833 | 805 | ||
| 834 | (defun math-decompose-units (units) | 806 | (defun math-decompose-units (units) |
| 835 | (let ((u (math-check-unit-name units))) | 807 | (let ((u (math-check-unit-name units))) |
| @@ -858,22 +830,19 @@ Entries are (SYMBOL EXPR DOC-STRING TEMP-TYPE BASE-UNITS).") | |||
| 858 | (lambda (x y) | 830 | (lambda (x y) |
| 859 | (not (Math-lessp (nth 1 x) | 831 | (not (Math-lessp (nth 1 x) |
| 860 | (nth 1 y)))))))))) | 832 | (nth 1 y)))))))))) |
| 861 | (cdr math-decompose-units-cache))) | 833 | (cdr math-decompose-units-cache)))) |
| 862 | ) | ||
| 863 | (setq math-decompose-units-cache nil) | 834 | (setq math-decompose-units-cache nil) |
| 864 | 835 | ||
| 865 | (defun math-decompose-unit-part (unit) | 836 | (defun math-decompose-unit-part (unit) |
| 866 | (cons unit | 837 | (cons unit |
| 867 | (math-is-multiple (math-simplify-units (math-to-standard-units | 838 | (math-is-multiple (math-simplify-units (math-to-standard-units |
| 868 | unit nil)) | 839 | unit nil)) |
| 869 | t)) | 840 | t))) |
| 870 | ) | ||
| 871 | 841 | ||
| 872 | (defun math-find-compatible-unit (expr unit) | 842 | (defun math-find-compatible-unit (expr unit) |
| 873 | (let ((u (math-check-unit-name unit))) | 843 | (let ((u (math-check-unit-name unit))) |
| 874 | (if u | 844 | (if u |
| 875 | (math-find-compatible-unit-rec expr 1))) | 845 | (math-find-compatible-unit-rec expr 1)))) |
| 876 | ) | ||
| 877 | 846 | ||
| 878 | (defun math-find-compatible-unit-rec (expr pow) | 847 | (defun math-find-compatible-unit-rec (expr pow) |
| 879 | (cond ((eq (car-safe expr) '*) | 848 | (cond ((eq (car-safe expr) '*) |
| @@ -888,8 +857,7 @@ Entries are (SYMBOL EXPR DOC-STRING TEMP-TYPE BASE-UNITS).") | |||
| 888 | (t | 857 | (t |
| 889 | (let ((u2 (math-check-unit-name expr))) | 858 | (let ((u2 (math-check-unit-name expr))) |
| 890 | (if (equal (nth 4 u) (nth 4 u2)) | 859 | (if (equal (nth 4 u) (nth 4 u2)) |
| 891 | (cons expr pow))))) | 860 | (cons expr pow)))))) |
| 892 | ) | ||
| 893 | 861 | ||
| 894 | (defun math-convert-units (expr new-units &optional pure) | 862 | (defun math-convert-units (expr new-units &optional pure) |
| 895 | (math-with-extra-prec 2 | 863 | (math-with-extra-prec 2 |
| @@ -915,8 +883,7 @@ Entries are (SYMBOL EXPR DOC-STRING TEMP-TYPE BASE-UNITS).") | |||
| 915 | (math-convert-units-rec expr) | 883 | (math-convert-units-rec expr) |
| 916 | (math-apply-units (math-to-standard-units | 884 | (math-apply-units (math-to-standard-units |
| 917 | (list '/ expr new-units) nil) | 885 | (list '/ expr new-units) nil) |
| 918 | new-units unit-list pure))))) | 886 | new-units unit-list pure)))))) |
| 919 | ) | ||
| 920 | 887 | ||
| 921 | (defun math-convert-units-rec (expr) | 888 | (defun math-convert-units-rec (expr) |
| 922 | (if (math-units-in-expr-p expr nil) | 889 | (if (math-units-in-expr-p expr nil) |
| @@ -925,8 +892,7 @@ Entries are (SYMBOL EXPR DOC-STRING TEMP-TYPE BASE-UNITS).") | |||
| 925 | (if (Math-primp expr) | 892 | (if (Math-primp expr) |
| 926 | expr | 893 | expr |
| 927 | (cons (car expr) | 894 | (cons (car expr) |
| 928 | (mapcar 'math-convert-units-rec (cdr expr))))) | 895 | (mapcar 'math-convert-units-rec (cdr expr)))))) |
| 929 | ) | ||
| 930 | 896 | ||
| 931 | (defun math-convert-temperature (expr old new &optional pure) | 897 | (defun math-convert-temperature (expr old new &optional pure) |
| 932 | (let* ((units (math-single-units-in-expr-p expr)) | 898 | (let* ((units (math-single-units-in-expr-p expr)) |
| @@ -960,17 +926,15 @@ Entries are (SYMBOL EXPR DOC-STRING TEMP-TYPE BASE-UNITS).") | |||
| 960 | (setq expr (list '+ expr '(float 27315 -2))))))) | 926 | (setq expr (list '+ expr '(float 27315 -2))))))) |
| 961 | (if pure | 927 | (if pure |
| 962 | expr | 928 | expr |
| 963 | (list '* expr new))) | 929 | (list '* expr new)))) |
| 964 | ) | ||
| 965 | 930 | ||
| 966 | 931 | ||
| 967 | 932 | ||
| 968 | (defun math-simplify-units (a) | 933 | (defun math-simplify-units (a) |
| 969 | (let ((math-simplifying-units t) | 934 | (let ((math-simplifying-units t) |
| 970 | (calc-matrix-mode 'scalar)) | 935 | (calc-matrix-mode 'scalar)) |
| 971 | (math-simplify a)) | 936 | (math-simplify a))) |
| 972 | ) | 937 | (defalias calcFunc-usimplify 'math-simplify-units) |
| 973 | (fset 'calcFunc-usimplify (symbol-function 'math-simplify-units)) | ||
| 974 | 938 | ||
| 975 | (math-defsimplify (+ -) | 939 | (math-defsimplify (+ -) |
| 976 | (and math-simplifying-units | 940 | (and math-simplifying-units |
| @@ -984,12 +948,10 @@ Entries are (SYMBOL EXPR DOC-STRING TEMP-TYPE BASE-UNITS).") | |||
| 984 | expr) | 948 | expr) |
| 985 | (list '* (math-add (math-remove-units (nth 1 expr)) | 949 | (list '* (math-add (math-remove-units (nth 1 expr)) |
| 986 | (if (eq (car expr) '-) (math-neg ratio) ratio)) | 950 | (if (eq (car expr) '-) (math-neg ratio) ratio)) |
| 987 | units)))) | 951 | units))))) |
| 988 | ) | ||
| 989 | 952 | ||
| 990 | (math-defsimplify * | 953 | (math-defsimplify * |
| 991 | (math-simplify-units-prod) | 954 | (math-simplify-units-prod)) |
| 992 | ) | ||
| 993 | 955 | ||
| 994 | (defun math-simplify-units-prod () | 956 | (defun math-simplify-units-prod () |
| 995 | (and math-simplifying-units | 957 | (and math-simplifying-units |
| @@ -1054,8 +1016,7 @@ Entries are (SYMBOL EXPR DOC-STRING TEMP-TYPE BASE-UNITS).") | |||
| 1054 | (calcFunc-scf (nth 1 expr) | 1016 | (calcFunc-scf (nth 1 expr) |
| 1055 | (- uxpon pxpon)))) | 1017 | (- uxpon pxpon)))) |
| 1056 | (setcar unitp pname) | 1018 | (setcar unitp pname) |
| 1057 | expr)))))) | 1019 | expr))))))) |
| 1058 | ) | ||
| 1059 | 1020 | ||
| 1060 | (math-defsimplify / | 1021 | (math-defsimplify / |
| 1061 | (and math-simplifying-units | 1022 | (and math-simplifying-units |
| @@ -1082,8 +1043,7 @@ Entries are (SYMBOL EXPR DOC-STRING TEMP-TYPE BASE-UNITS).") | |||
| 1082 | (setq expr base)))) | 1043 | (setq expr base)))) |
| 1083 | (if (eq (car-safe expr) '/) | 1044 | (if (eq (car-safe expr) '/) |
| 1084 | (math-simplify-units-prod)) | 1045 | (math-simplify-units-prod)) |
| 1085 | expr)) | 1046 | expr))) |
| 1086 | ) | ||
| 1087 | 1047 | ||
| 1088 | (defun math-simplify-units-divisor (np dp) | 1048 | (defun math-simplify-units-divisor (np dp) |
| 1089 | (let ((n (car np)) | 1049 | (let ((n (car np)) |
| @@ -1097,8 +1057,7 @@ Entries are (SYMBOL EXPR DOC-STRING TEMP-TYPE BASE-UNITS).") | |||
| 1097 | (if (setq temp (math-simplify-units-quotient n d)) | 1057 | (if (setq temp (math-simplify-units-quotient n d)) |
| 1098 | (progn | 1058 | (progn |
| 1099 | (setcar np (setq n temp)) | 1059 | (setcar np (setq n temp)) |
| 1100 | (setcar dp 1)))) | 1060 | (setcar dp 1))))) |
| 1101 | ) | ||
| 1102 | 1061 | ||
| 1103 | ;; Simplify, e.g., "in / cm" to "2.54" in a units expression. | 1062 | ;; Simplify, e.g., "in / cm" to "2.54" in a units expression. |
| 1104 | (defun math-simplify-units-quotient (n d) | 1063 | (defun math-simplify-units-quotient (n d) |
| @@ -1129,8 +1088,7 @@ Entries are (SYMBOL EXPR DOC-STRING TEMP-TYPE BASE-UNITS).") | |||
| 1129 | (* (cdr (car ud)) pow2))))) | 1088 | (* (cdr (car ud)) pow2))))) |
| 1130 | (setq ud1 (cdr ud1))) | 1089 | (setq ud1 (cdr ud1))) |
| 1131 | (setq un (cdr un))) | 1090 | (setq un (cdr un))) |
| 1132 | nil))))) | 1091 | nil)))))) |
| 1133 | ) | ||
| 1134 | 1092 | ||
| 1135 | (math-defsimplify ^ | 1093 | (math-defsimplify ^ |
| 1136 | (and math-simplifying-units | 1094 | (and math-simplifying-units |
| @@ -1139,8 +1097,7 @@ Entries are (SYMBOL EXPR DOC-STRING TEMP-TYPE BASE-UNITS).") | |||
| 1139 | (list (car (nth 1 expr)) | 1097 | (list (car (nth 1 expr)) |
| 1140 | (list '^ (nth 1 (nth 1 expr)) (nth 2 expr)) | 1098 | (list '^ (nth 1 (nth 1 expr)) (nth 2 expr)) |
| 1141 | (list '^ (nth 2 (nth 1 expr)) (nth 2 expr))) | 1099 | (list '^ (nth 2 (nth 1 expr)) (nth 2 expr))) |
| 1142 | (math-simplify-units-pow (nth 1 expr) (nth 2 expr)))) | 1100 | (math-simplify-units-pow (nth 1 expr) (nth 2 expr))))) |
| 1143 | ) | ||
| 1144 | 1101 | ||
| 1145 | (math-defsimplify calcFunc-sqrt | 1102 | (math-defsimplify calcFunc-sqrt |
| 1146 | (and math-simplifying-units | 1103 | (and math-simplifying-units |
| @@ -1148,8 +1105,7 @@ Entries are (SYMBOL EXPR DOC-STRING TEMP-TYPE BASE-UNITS).") | |||
| 1148 | (list (car (nth 1 expr)) | 1105 | (list (car (nth 1 expr)) |
| 1149 | (list 'calcFunc-sqrt (nth 1 (nth 1 expr))) | 1106 | (list 'calcFunc-sqrt (nth 1 (nth 1 expr))) |
| 1150 | (list 'calcFunc-sqrt (nth 2 (nth 1 expr)))) | 1107 | (list 'calcFunc-sqrt (nth 2 (nth 1 expr)))) |
| 1151 | (math-simplify-units-pow (nth 1 expr) '(frac 1 2)))) | 1108 | (math-simplify-units-pow (nth 1 expr) '(frac 1 2))))) |
| 1152 | ) | ||
| 1153 | 1109 | ||
| 1154 | (math-defsimplify (calcFunc-floor | 1110 | (math-defsimplify (calcFunc-floor |
| 1155 | calcFunc-ceil | 1111 | calcFunc-ceil |
| @@ -1188,16 +1144,14 @@ Entries are (SYMBOL EXPR DOC-STRING TEMP-TYPE BASE-UNITS).") | |||
| 1188 | (d (and (eq (car-safe pf) 'frac) (nth 2 pf)))) | 1144 | (d (and (eq (car-safe pf) 'frac) (nth 2 pf)))) |
| 1189 | (and u d | 1145 | (and u d |
| 1190 | (math-units-are-multiple u d) | 1146 | (math-units-are-multiple u d) |
| 1191 | (list '^ (math-to-standard-units a nil) pow)))) | 1147 | (list '^ (math-to-standard-units a nil) pow))))) |
| 1192 | ) | ||
| 1193 | 1148 | ||
| 1194 | 1149 | ||
| 1195 | (defun math-units-are-multiple (u n) | 1150 | (defun math-units-are-multiple (u n) |
| 1196 | (setq u (nth 4 u)) | 1151 | (setq u (nth 4 u)) |
| 1197 | (while (and u (= (% (cdr (car u)) n) 0)) | 1152 | (while (and u (= (% (cdr (car u)) n) 0)) |
| 1198 | (setq u (cdr u))) | 1153 | (setq u (cdr u))) |
| 1199 | (null u) | 1154 | (null u)) |
| 1200 | ) | ||
| 1201 | 1155 | ||
| 1202 | (math-defsimplify calcFunc-sin | 1156 | (math-defsimplify calcFunc-sin |
| 1203 | (and math-simplifying-units | 1157 | (and math-simplifying-units |
| @@ -1210,8 +1164,7 @@ Entries are (SYMBOL EXPR DOC-STRING TEMP-TYPE BASE-UNITS).") | |||
| 1210 | (math-realp (nth 1 rad)) | 1164 | (math-realp (nth 1 rad)) |
| 1211 | (eq (car-safe (nth 2 rad)) 'var) | 1165 | (eq (car-safe (nth 2 rad)) 'var) |
| 1212 | (eq (nth 1 (nth 2 rad)) 'rad) | 1166 | (eq (nth 1 (nth 2 rad)) 'rad) |
| 1213 | (list 'calcFunc-sin (nth 1 rad))))) | 1167 | (list 'calcFunc-sin (nth 1 rad)))))) |
| 1214 | ) | ||
| 1215 | 1168 | ||
| 1216 | (math-defsimplify calcFunc-cos | 1169 | (math-defsimplify calcFunc-cos |
| 1217 | (and math-simplifying-units | 1170 | (and math-simplifying-units |
| @@ -1224,8 +1177,7 @@ Entries are (SYMBOL EXPR DOC-STRING TEMP-TYPE BASE-UNITS).") | |||
| 1224 | (math-realp (nth 1 rad)) | 1177 | (math-realp (nth 1 rad)) |
| 1225 | (eq (car-safe (nth 2 rad)) 'var) | 1178 | (eq (car-safe (nth 2 rad)) 'var) |
| 1226 | (eq (nth 1 (nth 2 rad)) 'rad) | 1179 | (eq (nth 1 (nth 2 rad)) 'rad) |
| 1227 | (list 'calcFunc-cos (nth 1 rad))))) | 1180 | (list 'calcFunc-cos (nth 1 rad)))))) |
| 1228 | ) | ||
| 1229 | 1181 | ||
| 1230 | (math-defsimplify calcFunc-tan | 1182 | (math-defsimplify calcFunc-tan |
| 1231 | (and math-simplifying-units | 1183 | (and math-simplifying-units |
| @@ -1238,8 +1190,7 @@ Entries are (SYMBOL EXPR DOC-STRING TEMP-TYPE BASE-UNITS).") | |||
| 1238 | (math-realp (nth 1 rad)) | 1190 | (math-realp (nth 1 rad)) |
| 1239 | (eq (car-safe (nth 2 rad)) 'var) | 1191 | (eq (car-safe (nth 2 rad)) 'var) |
| 1240 | (eq (nth 1 (nth 2 rad)) 'rad) | 1192 | (eq (nth 1 (nth 2 rad)) 'rad) |
| 1241 | (list 'calcFunc-tan (nth 1 rad))))) | 1193 | (list 'calcFunc-tan (nth 1 rad)))))) |
| 1242 | ) | ||
| 1243 | 1194 | ||
| 1244 | 1195 | ||
| 1245 | (defun math-remove-units (expr) | 1196 | (defun math-remove-units (expr) |
| @@ -1248,15 +1199,13 @@ Entries are (SYMBOL EXPR DOC-STRING TEMP-TYPE BASE-UNITS).") | |||
| 1248 | (if (Math-primp expr) | 1199 | (if (Math-primp expr) |
| 1249 | expr | 1200 | expr |
| 1250 | (cons (car expr) | 1201 | (cons (car expr) |
| 1251 | (mapcar 'math-remove-units (cdr expr))))) | 1202 | (mapcar 'math-remove-units (cdr expr)))))) |
| 1252 | ) | ||
| 1253 | 1203 | ||
| 1254 | (defun math-extract-units (expr) | 1204 | (defun math-extract-units (expr) |
| 1255 | (if (memq (car-safe expr) '(* /)) | 1205 | (if (memq (car-safe expr) '(* /)) |
| 1256 | (cons (car expr) | 1206 | (cons (car expr) |
| 1257 | (mapcar 'math-extract-units (cdr expr))) | 1207 | (mapcar 'math-extract-units (cdr expr))) |
| 1258 | (if (math-check-unit-name expr) expr 1)) | 1208 | (if (math-check-unit-name expr) expr 1))) |
| 1259 | ) | ||
| 1260 | 1209 | ||
| 1261 | (defun math-build-units-table-buffer (enter-buffer) | 1210 | (defun math-build-units-table-buffer (enter-buffer) |
| 1262 | (if (not (and math-units-table math-units-table-buffer-valid | 1211 | (if (not (and math-units-table math-units-table-buffer-valid |
| @@ -1344,9 +1293,6 @@ Entries are (SYMBOL EXPR DOC-STRING TEMP-TYPE BASE-UNITS).") | |||
| 1344 | (display-buffer buf))) | 1293 | (display-buffer buf))) |
| 1345 | (if enter-buffer | 1294 | (if enter-buffer |
| 1346 | (pop-to-buffer (get-buffer "*Units Table*")) | 1295 | (pop-to-buffer (get-buffer "*Units Table*")) |
| 1347 | (display-buffer (get-buffer "*Units Table*")))) | 1296 | (display-buffer (get-buffer "*Units Table*"))))) |
| 1348 | ) | ||
| 1349 | |||
| 1350 | |||
| 1351 | |||
| 1352 | 1297 | ||
| 1298 | ;;; calc-units.el ends here | ||