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