indexed.scm 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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. ;;;; Minimal support for Indexed Objects
  21. ;;; e.g. the components of tensors relative to a basis.
  22. (declare (usual-integrations + *))
  23. ;;; A minimal interface for multi-index stuff.
  24. (define (argument-types proc)
  25. (eq-get proc 'argument-types))
  26. (define has-argument-types? argument-types)
  27. (define (declare-argument-types! proc argument-types)
  28. (assert (procedure? proc))
  29. (eq-put! proc 'argument-types argument-types))
  30. ;;; argument-types are, for example
  31. ;;; (list 1form-field? vector-field? vector-field?),
  32. ;;; for a Christoffel-2: it takes one 1form field and two vector fields.
  33. (define (index-types proc)
  34. (eq-get proc 'index-types))
  35. (define has-index-types? index-types)
  36. (define (declare-index-types! proc index-types)
  37. (assert (procedure? proc))
  38. (eq-put! proc 'index-types index-types))
  39. ;;; argument-types are, for example
  40. ;;; (list up down down),
  41. ;;; for a Christoffel-2: it takes one 1form field and two vector fields.
  42. ;;; An argument-typed function of type (n . m) takes n 1form fields
  43. ;;; and m vector-fields, in that order, and produces a function on a
  44. ;;; manifold point. An indexed function of type (n . m) takes n+m
  45. ;;; indices and gives a function on a manifold point.
  46. ;;; For each argument-typed function and basis there is an indexed
  47. ;;; function that gives the function resulting from applying the
  48. ;;; argument-typed function to the basis elements specified by the
  49. ;;; corresponding indices.
  50. (define (typed->indexed function basis)
  51. (let ((arg-types (argument-types function))
  52. (vector-basis (basis->vector-basis basis))
  53. (1form-basis (basis->1form-basis basis)))
  54. ;;; FBE move after define
  55. ;; (assert (and arg-types
  56. ;; (every (lambda (arg-type)
  57. ;; (or (eq? arg-type 1form-field?)
  58. ;; (eq? arg-type vector-field?)))
  59. ;; arg-types)
  60. ;; (not (any (lambda (arg-type) ;forms then vectors.
  61. ;; (eq? arg-type 1form-field?))
  62. ;; (or (memq vector-field? arg-types)
  63. ;; '()))))
  64. ;; "Bad arg types")
  65. (define (indexed indices)
  66. (assert (fix:= (length indices) (length arg-types)))
  67. (apply function
  68. (map (lambda (arg-type arg-index)
  69. (cond ((eq? arg-type vector-field?)
  70. (g:ref vector-basis arg-index))
  71. ((eq? arg-type 1form-field?)
  72. (g:ref 1form-basis arg-index))))
  73. arg-types indices)))
  74. (assert (and arg-types
  75. (every (lambda (arg-type)
  76. (or (eq? arg-type 1form-field?)
  77. (eq? arg-type vector-field?)))
  78. arg-types)
  79. (not (any (lambda (arg-type) ;forms then vectors.
  80. (eq? arg-type 1form-field?))
  81. (or (memq vector-field? arg-types)
  82. '()))))
  83. "Bad arg types")
  84. (declare-index-types! indexed
  85. (map (lambda (arg-type)
  86. (cond ((eq? arg-type vector-field?)
  87. down)
  88. ((eq? arg-type 1form-field?)
  89. up)))
  90. arg-types))
  91. indexed))
  92. (define (indexed->typed indexed basis)
  93. (let ((index-types (index-types indexed))
  94. (vector-basis (basis->vector-basis basis))
  95. (1form-basis (basis->1form-basis basis))
  96. (n (basis->dimension basis)))
  97. ;;; FBE move after define
  98. ;; (assert (and index-types
  99. ;; (every (lambda (index-type)
  100. ;; (or (eq? index-type up) (eq? index-type down)))
  101. ;; index-types)
  102. ;; (not (any (lambda (index-type) ;ups before downs
  103. ;; (eq? index-type up))
  104. ;; (or (memq down index-types)
  105. ;; '()))))
  106. ;; "Bad index types")
  107. (define (function . args)
  108. (assert (fix:= (length index-types) (length args)))
  109. (assert (every (lambda (index-type arg)
  110. (or (and (eq? index-type up) (1form-field? arg))
  111. (and (eq? index-type down) (vector-field? arg))))
  112. index-types args)
  113. "Args do not match indices")
  114. (let ((sum 0)) ;Ugh! an accumulator...
  115. (let aloop ((args args) (term 1) (indices '()))
  116. (if (null? args)
  117. (set! sum (g:+ (g:* (indexed (reverse indices)) term) sum))
  118. (let ((arg (car args)))
  119. (let dloop ((i 0))
  120. (if (fix:< i n)
  121. (begin
  122. (aloop (cdr args)
  123. (g:* (cond ((vector-field? arg)
  124. ((g:ref 1form-basis i) arg))
  125. ((1form-field? arg)
  126. (arg (g:ref vector-basis i))))
  127. term)
  128. (cons i indices))
  129. (dloop (fix:+ i 1))))))))
  130. sum))
  131. (assert (and index-types
  132. (every (lambda (index-type)
  133. (or (eq? index-type up) (eq? index-type down)))
  134. index-types)
  135. (not (any (lambda (index-type) ;ups before downs
  136. (eq? index-type up))
  137. (or (memq down index-types)
  138. '()))))
  139. "Bad index types")
  140. (declare-argument-types! function
  141. (map (lambda (index-type)
  142. (cond ((eq? index-type up) 1form-field?)
  143. ((eq? index-type down) vector-field?)))
  144. index-types))
  145. function))
  146. #|
  147. (define-coordinates (up x y) R2-rect)
  148. (define (T w1 w2 v1)
  149. (+ (* 'a (dx v1) (w1 d/dx) (w2 d/dy))
  150. (* 'b (dy v1) (w1 d/dy) (w2 d/dx))
  151. (* 'c (dy v1) (w1 d/dy) (w2 d/dy))))
  152. (declare-argument-types! T
  153. (list 1form-field? 1form-field? vector-field?))
  154. (((indexed->typed
  155. (typed->indexed T (coordinate-system->basis R2-rect))
  156. (coordinate-system->basis R2-rect))
  157. (literal-1form-field 'w1 R2-rect)
  158. (literal-1form-field 'w2 R2-rect)
  159. (literal-vector-field 'v1 R2-rect))
  160. ((point R2-rect) (up 'x 'y)))
  161. #|
  162. (+ (* a (w2_1 (up x y)) (w1_0 (up x y)) (v1^0 (up x y)))
  163. (* b (w2_0 (up x y)) (w1_1 (up x y)) (v1^1 (up x y)))
  164. (* c (w2_1 (up x y)) (w1_1 (up x y)) (v1^1 (up x y))))
  165. |#
  166. ;;; Seems to work!
  167. |#
  168. (define (i:outer-product T1 T2)
  169. (let ((i1 (index-types T1)) (i2 (index-types T2)))
  170. (assert i1 "T1 not index typed")
  171. (assert i2 "T2 not index typed")
  172. (let ((nu1 (count-occurrences up i1)) (nd1 (count-occurrences down i1))
  173. (nu2 (count-occurrences up i2)) (nd2 (count-occurrences down i2)))
  174. (let ((nup (fix:+ nu1 nu2)) (ndp (fix:+ nd1 nd2)))
  175. (let ((np (fix:+ nup ndp)) (n1 (fix:+ nup nd1)))
  176. (define (product args)
  177. (assert (fix:= (length args) np)
  178. "Wrong number of args to i:outer-product")
  179. (g:* (T1 (append (sublist args 0 nu1)
  180. (sublist args nup n1)))
  181. (T2 (append (sublist args nu1 nup)
  182. (sublist args n1 np)))))
  183. (declare-index-types! product
  184. (append (make-list nup up) (make-list ndp down)))
  185. product)))))
  186. (define (count-occurrences element list)
  187. (count-elements (lambda (x) (eq? element x)) list))
  188. #|
  189. (define-coordinates (up x y) R2-rect)
  190. (define (T1 w1 w2 v1)
  191. (+ (* 'a (dx v1) (w1 d/dx) (w2 d/dy))
  192. (* 'b (dy v1) (w1 d/dy) (w2 d/dx))
  193. (* 'c (dy v1) (w1 d/dy) (w2 d/dy))))
  194. (declare-argument-types! T1
  195. (list 1form-field? 1form-field? vector-field?))
  196. (define iT1
  197. (typed->indexed T1 (coordinate-system->basis R2-rect)))
  198. (define (T2 w1 w2)
  199. (+ (* (w1 d/dx) (w2 d/dx))
  200. (* (w1 d/dy) (w2 d/dy))
  201. (* (w1 d/dy) (w2 d/dx))))
  202. (declare-argument-types! T2
  203. (list 1form-field? 1form-field?))
  204. (define iT2
  205. (typed->indexed T2 (coordinate-system->basis R2-rect)))
  206. (define iT3 (i:outer-product iT1 iT2))
  207. (pe (((indexed->typed iT3 (coordinate-system->basis R2-rect))
  208. (literal-1form-field 'w1 R2-rect)
  209. (literal-1form-field 'w2 R2-rect)
  210. (literal-1form-field 'w3 R2-rect)
  211. (literal-1form-field 'w4 R2-rect)
  212. (literal-vector-field 'v1 R2-rect))
  213. ((point R2-rect) (up 'x 'y))))
  214. #|
  215. (+ (* a (w1_0 (up x y)) (v1^0 (up x y)) (w2_1 (up x y)) (w3_0 (up x y)) (w4_0 (up x y)))
  216. (* a (w1_0 (up x y)) (v1^0 (up x y)) (w2_1 (up x y)) (w4_1 (up x y)) (w3_1 (up x y)))
  217. (* a (w1_0 (up x y)) (v1^0 (up x y)) (w2_1 (up x y)) (w4_0 (up x y)) (w3_1 (up x y)))
  218. (* b (w2_0 (up x y)) (w1_1 (up x y)) (v1^1 (up x y)) (w3_0 (up x y)) (w4_0 (up x y)))
  219. (* b (w2_0 (up x y)) (w1_1 (up x y)) (v1^1 (up x y)) (w4_1 (up x y)) (w3_1 (up x y)))
  220. (* b (w2_0 (up x y)) (w1_1 (up x y)) (v1^1 (up x y)) (w4_0 (up x y)) (w3_1 (up x y)))
  221. (* c (w2_1 (up x y)) (w1_1 (up x y)) (v1^1 (up x y)) (w3_0 (up x y)) (w4_0 (up x y)))
  222. (* c (w2_1 (up x y)) (w1_1 (up x y)) (v1^1 (up x y)) (w4_1 (up x y)) (w3_1 (up x y)))
  223. (* c (w2_1 (up x y)) (w1_1 (up x y)) (v1^1 (up x y)) (w4_0 (up x y)) (w3_1 (up x y))))
  224. |#
  225. |#
  226. (define (i:contract T u d n)
  227. (let ((i-types (index-types T)))
  228. (assert i-types "T not index typed")
  229. (let ((nu (count-occurrences up i-types))
  230. (nd (count-occurrences down i-types)))
  231. (assert (and (fix:<= 0 u) (fix:< u nu)
  232. (fix:<= 0 d) (fix:< d nd))
  233. "Contraction indices not in range")
  234. (let ((nuc (fix:- nu 1)) (ndc (fix:- nd 1)))
  235. (define (contraction args)
  236. (sigma (lambda (i)
  237. (T (append
  238. (list-with-inserted-coord (list-head args nuc) u i)
  239. (list-with-inserted-coord (list-tail args nuc) d i))))
  240. 0 (fix:- n 1)))
  241. (declare-index-types! contraction
  242. (append (make-list nuc up) (make-list ndc down)))
  243. contraction))))
  244. (define (list-with-inserted-coord list index coord)
  245. (append (list-head list index) (cons coord (list-tail list index))))
  246. #|
  247. (((indexed->typed (i:contract iT1 0 0 2)
  248. (coordinate-system->basis R2-rect))
  249. (literal-1form-field 'w1 R2-rect))
  250. ((point R2-rect) (up 'x 'y)))
  251. #|
  252. (+ (* a (w1_1 (up x y)))
  253. (* b (w1_0 (up x y)))
  254. (* c (w1_1 (up x y))))
  255. |#
  256. (((indexed->typed (i:contract iT1 1 0 2)
  257. (coordinate-system->basis R2-rect))
  258. (literal-1form-field 'w1 R2-rect))
  259. ((point R2-rect) (up 'x 'y)))
  260. #| (* c (w1_1 (up x y))) |#
  261. (((indexed->typed (i:contract iT3 1 0 0)
  262. (coordinate-system->basis R2-rect))
  263. (literal-1form-field 'w1 R2-rect)
  264. (literal-1form-field 'w2 R2-rect)
  265. (literal-1form-field 'w3 R2-rect))
  266. ((point R2-rect) (up 'x 'y)))
  267. #| 0 |#
  268. |#
  269. (define (typed->structure T basis)
  270. (let ((vector-basis (basis->vector-basis basis))
  271. (1form-basis (basis->1form-basis basis)))
  272. (define (iterate arg-types argl)
  273. (if (null? arg-types)
  274. (apply T (reverse argl))
  275. (s:map/r (lambda (e) (iterate (cdr arg-types) (cons e argl)))
  276. (cond ((eq? (car arg-types) vector-field?) vector-basis)
  277. ((eq? (car arg-types) 1form-field?) 1form-basis)
  278. (else (error "Bad arg-type"))))))
  279. (iterate (argument-types T) '())))
  280. (define (structure->typed coeff-functions basis)
  281. (let ((vector-basis (basis->vector-basis basis))
  282. (1form-basis (basis->1form-basis basis))
  283. (arg-types
  284. (let lp ((cf coeff-functions))
  285. (if (structure? cf)
  286. (cons (let ((shape (s:opposite cf)))
  287. (cond ((eq? shape 'up) vector-field?)
  288. ((eq? shape 'down) 1form-field?)
  289. (else (error "Bad Shape"))))
  290. (lp (ref cf 0)))
  291. '())))
  292. (coeff-functions
  293. (maybe-simplify-coeff-functions coeff-functions basis)))
  294. (define (indexed-function . args)
  295. (assert (fix:= (length args) (length arg-types)))
  296. (for-each (lambda (arg-type arg) (assert (arg-type arg)))
  297. arg-types args)
  298. (g:* (let lp ((args args) (arg-types arg-types))
  299. (if (null? args)
  300. one-manifold-function
  301. (let ((arg (car args)) (arg-type (car arg-types)))
  302. (cond ((eq? arg-type vector-field?)
  303. (s:map/r (lambda (etilde)
  304. (g:* (etilde arg)
  305. (lp (cdr args)
  306. (cdr arg-types))))
  307. 1form-basis))
  308. ((eq? arg-type 1form-field?)
  309. (s:map/r (lambda (e)
  310. (g:* (arg e)
  311. (lp (cdr args)
  312. (cdr arg-types))))
  313. vector-basis))))))
  314. coeff-functions))
  315. (declare-argument-types! indexed-function arg-types)
  316. indexed-function))
  317. #|
  318. (define-coordinates (up x y) R2-rect)
  319. (define (T v1 w1 w2)
  320. (+ (* 'a (dx v1) (w1 d/dx) (w2 d/dy))
  321. (* 'b (dy v1) (w1 d/dy) (w2 d/dx))
  322. (* 'c (dy v1) (w1 d/dy) (w2 d/dy))))
  323. (declare-argument-types! T
  324. (list vector-field? 1form-field? 1form-field?))
  325. ((typed->structure T (coordinate-system->basis R2-rect))
  326. ((point R2-rect) (up 'x 'y)))
  327. #|
  328. (down (up (up 0 a) (up 0 0)) (up (up 0 0) (up b c)))
  329. |#
  330. ;;; Outer index is first argument. Inner index is last argument.
  331. (((structure->typed
  332. (typed->structure T (coordinate-system->basis R2-rect))
  333. (coordinate-system->basis R2-rect))
  334. (literal-vector-field 'v1 R2-rect)
  335. (literal-1form-field 'w1 R2-rect)
  336. (literal-1form-field 'w2 R2-rect))
  337. ((point R2-rect) (up 'x 'y)))
  338. #|
  339. (+ (* a (w2_1 (up x y)) (w1_0 (up x y)) (v1^0 (up x y)))
  340. (* b (w2_0 (up x y)) (w1_1 (up x y)) (v1^1 (up x y)))
  341. (* c (w2_1 (up x y)) (w1_1 (up x y)) (v1^1 (up x y))))
  342. |#
  343. ((typed->structure
  344. (structure->typed
  345. (typed->structure T (coordinate-system->basis R2-rect))
  346. (coordinate-system->basis R2-rect))
  347. (coordinate-system->basis R2-rect))
  348. ((point R2-rect) (up 'x 'y)))
  349. #|
  350. (down (up (up 0 a) (up 0 0)) (up (up 0 0) (up b c)))
  351. |#
  352. ;;; Seems to work!
  353. |#
  354. (define (maybe-simplify-coeff-functions coeff-functions basis)
  355. (if (and simplify-coeff-functions? (coordinate-basis? basis))
  356. (s:map/r (simplify-coeff-function
  357. (typical-point (basis->coordinate-system basis)))
  358. coeff-functions)
  359. coeff-functions))
  360. (define* ((simplify-coeff-function m) f)
  361. ;; a stub. see einstein/speedup.scm for ideas
  362. f)
  363. (define simplify-coeff-functions? #t)
  364. ;;; The following are universally ok.
  365. (define (zero-manifold-function? f)
  366. (eq? f zero-manifold-function))
  367. (define (one-manifold-function? f)
  368. (eq? f one-manifold-function))
  369. (define (manifold-function-cofunction? x)
  370. (or (function-quantity? x)
  371. (numerical-quantity? x)))
  372. (define %calculus-indexed-dummy-1
  373. (begin
  374. (defhandler '* (lambda (x y) zero-manifold-function)
  375. zero-manifold-function? manifold-function-cofunction?)
  376. (defhandler '* (lambda (x y) zero-manifold-function)
  377. manifold-function-cofunction? zero-manifold-function?)
  378. (defhandler '* (lambda (x y) y)
  379. one-manifold-function? manifold-function-cofunction?)
  380. (defhandler '* (lambda (x y) x)
  381. manifold-function-cofunction? one-manifold-function?)
  382. (defhandler '+ (lambda (x y) y)
  383. zero-manifold-function? manifold-function-cofunction?)
  384. (defhandler '+ (lambda (x y) x)
  385. manifold-function-cofunction? zero-manifold-function?)))