-
Notifications
You must be signed in to change notification settings - Fork 1
/
with-units.scm
167 lines (133 loc) · 4.33 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
;; -*-Hen-*-
;;
;; Modified for Chicken Scheme by Ivan Raikov and André Sá (2009-2020).
;;
;; Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
;; 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
;; 2006, 2007 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 3 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
(module with-units
(
*permissive-units*
with-units?
without-units?
unitless-quantity?
val-with-units
u:value
u:units
u:equal?
u:zero?
u:=
u:negate
u:invert
u:+ u:- u:* u:/
u:sqrt
u:sin
u:cos
u:expt
)
(import scheme (chicken base) (only (chicken format) fprintf) unitconv)
;;; If set to #t allows contageous no-unit combinations.
(define *permissive-units* (make-parameter #f))
;;; Quantities without explicit units are assumed unitless.
(define-record-type with-units
(make-with-units unit value)
with-units?
(unit with-units-unit )
(value with-units-value )
)
(set-record-printer! with-units
(lambda (x out)
(fprintf out "#(~S ~S)"
(with-units-value x)
(with-units-unit x))))
(define (u:units x)
(cond ((with-units? x) (with-units-unit x))
(else unitless)))
(define (u:value x)
(cond ((with-units? x) (with-units-value x))
(else x)))
(define (without-units? x)
(not (with-units? x)))
(define (unitless-quantity? x)
(cond ((with-units? x) (unitless? (u:units x)))
(else #t)))
(define (u:equal? x y)
(cond ((and (with-units? x) (with-units? y))
(equal? (u:units x) (u:units y)))
(else #t)))
(define (val-with-units value unit)
(and (unit? unit) (number? value)
(make-with-units unit value)))
(define (u:zero? x) (zero? (u:value x)))
(define (u:= x y)
(and (u:equal? x y)
(= (u:value x) (u:value y))))
(define (u:negate x)
(val-with-units (- (u:value x)) (u:units x)))
(define (invert x) (/ 1 x))
(define (u:invert x)
(val-with-units (invert (u:value x)) (unit-invert (u:units x))))
(define (u:sqrt x)
(val-with-units (sqrt (u:value x)) (unit-expt (u:units x) 1/2)))
(define (u:sin x)
(val-with-units (sin (u:value x)) unitless))
(define (u:cos x)
(val-with-units (cos (u:value x)) unitless))
(define (u:+ x y)
(if (and (with-units? x) (with-units? y))
(cond ((u:zero? x) y)
((u:zero? y) x)
((u:equal? x y)
(val-with-units (+ (u:value x) (u:value y)) (u:units x) ))
((and (*permissive-units*)
(or (without-units? x) (without-units? y)))
(+ (u:value x) (u:value y)))
(else (error 'u:+ "units do not match: " x y)))
(val-with-units(+ x y) unitless )))
(define (u:- x y)
(if (and (with-units? x) (with-units? y))
(cond ((u:zero? y) x)
((u:zero? x) (u:negate y))
((u:equal? x y)
(val-with-units (- (u:value x) (u:value y)) (u:units x) ))
((and (*permissive-units*)
(or (without-units? x) (without-units? y)))
(- (u:value x) (u:value y)))
(else (error 'u:- "units do not match: " x y)))
(val-with-units (- x y) unitless)))
(define (u:* x y)
(if (and (with-units? x) (with-units? y))
(val-with-units (* (u:value x) (u:value y))
(unit* (u:units x) (u:units y)))
(val-with-units(* x y) unitless )))
(define (u:/ x y)
(if (and (with-units? x) (with-units? y))
(val-with-units (/ (u:value x) (u:value y))
(unit/ (u:units x) (u:units y)))
(val-with-units (/ x y) unitless )))
(define (u:expt x y)
(if (with-units? x)
(if (unitless-quantity? y)
(val-with-units (expt (u:value x) (u:value y))
(unit-expt (u:units x) (u:value y)))
(error 'u:expt "exponent must be unitless:" x y))
(val-with-units (expt x y) unitless)))
)