-
Notifications
You must be signed in to change notification settings - Fork 4
/
with-units.scm
323 lines (234 loc) · 9.53 KB
/
with-units.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#| -*-Scheme-*-
Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014 Massachusetts
Institute of Technology
This file is part of MIT/GNU Scheme.
MIT/GNU Scheme is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at
your option) any later version.
MIT/GNU Scheme is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with MIT/GNU Scheme; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301,
USA.
|#
;;;; Quantities with units
(declare (usual-integrations))
;;; If set to #t allows contageous no-unit combinations.
(define *permissive-units* #f)
;;; Quantities without explicit units are assumed unitless.
#|
;;; In types.scm
(define with-units-type-tag '*with-units*)
(define (with-units? x)
(and (pair? x)
(eq? (car x) with-units-type-tag)))
|#
(define (without-units? x)
(not (with-units? x)))
(define (unitless-quantity? x)
(unitless? (u:units x)))
(define (u:arity x)
(g:arity (u:value x)))
(define (u:value x)
(cond ((with-units? x) (cadr x))
((units? x) 1)
(else x)))
(define (u:units x)
(cond ((with-units? x) (caddr x))
((units? x) x)
(else &unitless)))
(define (units:= x y)
(or (g:zero? x)
(g:zero? y)
(equal? (u:units x) (u:units y))))
(define (angular? x)
(equal? (u:units x) angular))
(define (with-units value units)
(if (equal? units &unitless)
value
(list with-units-type-tag value units)))
(define (has-units? value unit)
(units:= value unit))
(define (u:type x)
(g:type (u:value x)))
(define (u:zero-like x) ;can add to anything with same units
(with-units (g:zero-like (u:value x))
(u:units x)))
(define (u:one-like x) ;can multiply anything with same units
(with-units (g:one-like (u:value x))
&unitless))
(define (u:zero? x)
(g:zero? (u:value x)))
(define (u:one? x)
(g:one? (u:value x)))
(define (u:= x y)
(and (units:= x y)
(g:= (u:value x) (u:value y))))
(define (u:< x y)
(and (units:= x y)
(g:< (u:value x) (u:value y))))
(define (u:<= x y)
(and (units:= x y)
(g:<= (u:value x) (u:value y))))
(define (u:> x y)
(and (units:= x y)
(g:> (u:value x) (u:value y))))
(define (u:>= x y)
(and (units:= x y)
(g:>= (u:value x) (u:value y))))
(define (u:negate x)
(with-units (g:negate (u:value x)) (u:units x)))
(define (u:invert x)
(with-units (g:invert (u:value x)) (invert-units (u:units x))))
(define (u:sqrt x)
(with-units (g:sqrt (u:value x)) (expt-units (u:units x) 1/2)))
(define (u:sin x)
(assert (unitless-quantity? x) "Arg to sin not dimensionless")
(with-units (g:sin (u:value x)) &unitless))
(define (u:cos x)
(assert (unitless-quantity? x) "Arg to cos not dimensionless")
(with-units (g:cos (u:value x)) &unitless))
(define (u:exp x)
(assert (unitless-quantity? x) "Arg to exp not dimensionless")
(with-units (g:exp (u:value x)) &unitless))
(define (u:+ x y)
(cond ((g:zero? x) y)
((g:zero? y) x)
((units:= x y)
(with-units (g:+ (u:value x) (u:value y)) (u:units x)))
((and *permissive-units*
(or (without-units? x) (without-units? y)))
(g:+ (u:value x) (u:value y)))
(else (error "Units do not match: +" x y))))
(define (u:- x y)
(cond ((g:zero? y) x)
((g:zero? x) (u:negate y))
((units:= x y)
(with-units (g:- (u:value x) (u:value y)) (u:units x)))
((and *permissive-units*
(or (without-units? x) (without-units? y)))
(g:- (u:value x) (u:value y)))
(else (error "Units do not match: -" x y))))
(define (u:* x y)
(with-units (g:* (u:value x) (u:value y))
(*units (u:units x) (u:units y))))
(define (u:/ x y)
(with-units (g:/ (u:value x) (u:value y))
(/units (u:units x) (u:units y))))
(define (u:*u x u)
(u:* x (with-units 1 u)))
(define (u:u* u x)
(u:* (with-units 1 u) x))
(define (u:t*u t u)
(u:*u (with-units t &unitless) u))
(define (u:u*t t u)
(u:u* u (with-units t &unitless)))
(define (u:/u x u)
(u:/ x (with-units 1 u)))
(define (u:u/ u x)
(u:/ (with-units 1 u) x))
(define (u:t/u t u)
(u:/u (with-units t &unitless) u))
(define (u:u/t t u)
(u:u/ u (with-units t &unitless)))
(define (u:expt x y)
(if (unitless-quantity? y)
(with-units (g:expt (u:value x) (u:value y))
(expt-units (u:units x) (u:value y)))
(error "Exponent must be unitless: expt" x y)))
(define (u:make-rectangular x y)
(cond ((g:zero? y) x)
((g:zero? x)
(with-units (g:make-rectangular (u:value x) (u:value y)) (u:units y)))
((units:= x y)
(with-units (g:make-rectangular (u:value x) (u:value y)) (u:units x)))
((and *permissive-units* (or (without-units? x) (without-units? y)))
(g:make-rectangular (u:value x) (u:value y)))
(else (error "Units do not match: make-rectangular" x y))))
(define (u:make-polar r theta)
(if (angular? theta)
(with-units
(g:make-polar (u:value r) (u:value theta))
(u:units r))
(error "Theta must be angular: make-polar" r theta)))
(define (u:real-part z)
(with-units (g:real-part (u:value z)) (u:units z)))
(define (u:imag-part z)
(with-units (g:imag-part (u:value z)) (u:units z)))
(define (u:magnitude z)
(with-units (g:magnitude (u:value z)) (u:units z)))
(define (u:angle z)
(with-units (g:angle (u:value z)) *angular*))
(define (u:conjugate z)
(with-units (g:conjugate (u:value z)) (u:units z)))
(define (u:atan2 y x)
(cond ((units:= x y)
(with-units (g:atan2 (u:value y) (u:value x)) angular))
((and *permissive-units* (or (without-units? x) (without-units? y)))
(g:atan2 (u:value y) (u:value x)))
(else (error "Units do not match: atan2" y x))))
(define (non-unit? x)
(not (and (pair? x) (eq? (car x) '*unit*))))
(assign-operation 'type u:type with-units?)
(assign-operation 'arity u:arity with-units?)
(assign-operation 'zero-like u:zero-like with-units?)
(assign-operation 'one-like u:one-like with-units?)
(assign-operation 'zero? u:zero? with-units?)
;;; The following causes (/ (& 1 &ere) (& 1 &volt)) to return
;Value 22: (*with-units* 1 #(0 0 0 1 0 0 0))
;(assign-operation 'one? u:one? with-units?)
(assign-operation 'negate u:negate with-units?)
(assign-operation 'invert u:invert with-units?)
(assign-operation 'sqrt u:sqrt with-units?)
#|
(assign-operation 'sin u:sin angular?)
(assign-operation 'cos u:cos angular?)
(assign-operation 'exp u:cos angular?)
|#
(assign-operation '= u:= with-units? with-units?)
(assign-operation '< u:< with-units? with-units?)
(assign-operation '<= u:<= with-units? with-units?)
(assign-operation '> u:> with-units? with-units?)
(assign-operation '>= u:>= with-units? with-units?)
(assign-operation '+ u:+ with-units? not-differential-or-compound?)
(assign-operation '+ u:+ not-differential-or-compound? with-units?)
(assign-operation '- u:- with-units? not-differential-or-compound?)
(assign-operation '- u:- not-differential-or-compound? with-units?)
(assign-operation '* u:* with-units? not-differential-or-compound?)
(assign-operation '* u:* not-differential-or-compound? with-units?)
(assign-operation '* u:*u with-units? units?)
(assign-operation '* u:u* units? with-units?)
(assign-operation '* u:t*u not-d-c-u? units?)
(assign-operation '* u:u*t units? not-d-c-u?)
(assign-operation '/ u:/ with-units? not-differential-or-compound?)
(assign-operation '/ u:/ not-differential-or-compound? with-units?)
(assign-operation '/ u:/u with-units? units?)
(assign-operation '/ u:u/ units? with-units?)
(assign-operation '/ u:t/u not-d-c-u? units?)
(assign-operation '/ u:u/t units? not-d-c-u?)
;(assign-operation 'dot-product u:dot-product with-units? with-units?)
(assign-operation 'expt u:expt with-units? not-differential-or-compound?)
;(assign-operation 'gcd u:gcd with-units? with-units?)
(assign-operation 'make-rectangular u:make-rectangular with-units? with-units?)
(assign-operation 'make-polar u:make-polar with-units? any?)
(assign-operation 'real-part u:real-part with-units?)
(assign-operation 'imag-part u:imag-part with-units?)
(assign-operation 'magnitude u:magnitude with-units?)
(assign-operation 'angle u:angle with-units?)
(assign-operation 'conjugate u:conjugate with-units?)
;(assign-operation 'atan1 u:atan with-units?)
(assign-operation 'atan2 u:atan2 with-units? with-units?)
#|
(pe (definite-integral
(lambda (r)
(/ (* :G earth-mass (& 1 &kilogram))
(square (+ earth-radius r))))
(& 0 &meter) (& 1 &meter)))
(& 9.824031599863007 &joule)
|#