basis.scm 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. ;;; The following give us dual bases
  21. ;;; Basis objects have a dimension, a basis, a dual basis.
  22. ;;; coordinate bases
  23. (define (coordinate-basis? x)
  24. (and (pair? x)
  25. (eq? (car x) '*coordinate-basis*)))
  26. (define (coordinate-system->basis coordinate-system)
  27. (list '*coordinate-basis*
  28. (coordinate-system->vector-basis coordinate-system)
  29. (coordinate-system->1form-basis coordinate-system)
  30. (coordinate-system 'dimension)
  31. coordinate-system))
  32. (define (basis->coordinate-system x)
  33. (assert (coordinate-basis? x) "Not a coordinate basis")
  34. (list-ref x 4))
  35. ;;; general bases
  36. (define (basis? x)
  37. (or (coordinate-basis? x)
  38. (and (pair? x)
  39. (eq? (car x) '*basis*))))
  40. (define (make-basis vector-basis 1form-basis)
  41. (let ((n (length (s:fringe vector-basis))))
  42. (assert (fix:= n (length (s:fringe 1form-basis))))
  43. (list '*basis* vector-basis 1form-basis n)))
  44. (define (basis->vector-basis x)
  45. (assert (basis? x) "Not a basis")
  46. (cadr x))
  47. (define (basis->1form-basis x)
  48. (assert (basis? x) "Not a basis")
  49. (caddr x))
  50. (define (basis->dimension x)
  51. (assert (basis? x) "Not a basis")
  52. (cadddr x))
  53. ;;; sigma (proc e_i w^i)
  54. (define (contract proc basis)
  55. (let ((vector-basis (basis->vector-basis basis))
  56. (1form-basis (basis->1form-basis basis)))
  57. (s:sigma/r proc
  58. vector-basis
  59. 1form-basis)))
  60. ;;; Has a dependence on flat basis sets. Experimental stuff kills system!
  61. (define (vector-basis->dual vector-basis coordinate-system)
  62. (let* ((typical-coords (coordinate-system 'typical-coords))
  63. (vector-basis-coefficient-functions
  64. #|
  65. (compose (vector-basis (coordinate-system '->coords))
  66. (coordinate-system '->point))
  67. |#
  68. (s:map/r (lambda (basis-vector)
  69. (vector-field->components basis-vector coordinate-system))
  70. vector-basis)
  71. )
  72. (guts
  73. (lambda (coords)
  74. (s:transpose (compatible-shape typical-coords)
  75. (s:inverse
  76. (compatible-shape typical-coords)
  77. (s:map (lambda (fn) (fn coords))
  78. vector-basis-coefficient-functions)
  79. typical-coords)
  80. typical-coords)))
  81. (1form-basis-coefficient-functions #| guts |#
  82. (c:generate (coordinate-system 'dimension)
  83. 'up
  84. (lambda (i)
  85. (compose (component i) guts))))
  86. (1form-basis
  87. (s:map/r (lambda (1form-basis-coefficient-function)
  88. (components->1form-field 1form-basis-coefficient-function
  89. coordinate-system))
  90. 1form-basis-coefficient-functions)))
  91. 1form-basis))
  92. #|
  93. (install-coordinates S2-spherical (up 'theta 'phi))
  94. (define e0
  95. (components->vector-field
  96. (up (literal-function 'e0t (-> (UP* Real) Real))
  97. (literal-function 'e0p (-> (UP* Real) Real)))
  98. S2-spherical))
  99. (define e1
  100. (components->vector-field
  101. (up (literal-function 'e1t (-> (UP* Real) Real))
  102. (literal-function 'e1p (-> (UP* Real) Real)))
  103. S2-spherical))
  104. (define edual
  105. (vector-basis->dual (down e0 e1) S2-spherical))
  106. (pec ((edual (down e0 e1))
  107. ((S2-spherical '->point)
  108. (up 'theta0 'phi0))))
  109. #| Result:
  110. (up (down 1 0) (down 0 1))
  111. |#
  112. |#
  113. (define* ((make-constant-vector-field basis m0) v)
  114. (lambda (f)
  115. (let ((vector-basis (basis->vector-basis basis))
  116. (1form-basis (basis->1form-basis basis)))
  117. (* (vector-basis f)
  118. (s:map/r (lambda (1fb) (lambda (m) ((1fb v) m0)))
  119. 1form-basis)))))
  120. ;;; Change of basis: The Jacobian is a structure of manifold
  121. ;;; functions. The outer index is the from-basis index, so this
  122. ;;; structure can be multiplied by tuple of component functions of a
  123. ;;; vector field relative to the from basis to get component functions
  124. ;;; for a vector field in the to basis.
  125. (define (Jacobian to-basis from-basis)
  126. (s:map/r (basis->1form-basis to-basis)
  127. (basis->vector-basis from-basis)))
  128. #|
  129. (define v (literal-vector-field 'v R2-rect))
  130. (define vjp
  131. (* (Jacobian (R2-polar 'coordinate-basis)
  132. (R2-rect 'coordinate-basis))
  133. ((R2-rect 'coordinate-basis-1form-fields)
  134. v)))
  135. (pe (vjp ((R2-rect '->point) (up 'x 'y))))
  136. (up
  137. (/ (+ (* x (v^0 (up x y))) (* y (v^1 (up x y))))
  138. (sqrt (+ (expt x 2) (expt y 2))))
  139. (/ (+ (* x (v^1 (up x y))) (* -1 y (v^0 (up x y))))
  140. (+ (expt x 2) (expt y 2))))
  141. |#