with-units-defs.scm 9.8 KB

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