gram-schmidt.scm 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. ;;; Gram-Schmidt orthonormalization process...
  21. (define (Gram-Schmidt vector-basis metric)
  22. (define (make-positive x)
  23. (sqrt (square x)))
  24. #|
  25. ;;; Fails in some symbolic situations!
  26. (define (make-positive x)
  27. (if (and (number? x) (negative? x))
  28. (- x)
  29. x))
  30. |#
  31. (define (normalize v)
  32. (* (/ 1 (sqrt (make-positive (metric v v)))) v))
  33. (let* ((vects (ultra-flatten vector-basis))
  34. (e0 (normalize (car vects))))
  35. (let lp ((ins (cdr vects)) (outs (list e0)))
  36. (if (null? ins)
  37. (apply down (reverse outs))
  38. (lp (cdr ins)
  39. (cons (normalize
  40. (- (car ins)
  41. (apply +
  42. (map (lambda (outv)
  43. (* (metric (car ins) outv)
  44. outv))
  45. outs))))
  46. outs))))))
  47. #|
  48. (define (Gram-Schmidt vector-basis metric)
  49. (define (make-positive x)
  50. (sqrt (square x)))
  51. (define (normalize v)
  52. (* (/ 1 (sqrt (make-positive (metric v v)))) v))
  53. (let* ((vects (ultra-flatten vector-basis))
  54. (e0 (car vects)))
  55. (let lp ((ins (cdr vects)) (outs (list e0)))
  56. (if (null? ins)
  57. (apply down (map normalize (reverse outs)))
  58. (lp (cdr ins)
  59. (cons (- (car ins)
  60. (apply +
  61. (map (lambda (outv)
  62. (* (metric (car ins) outv)
  63. outv))
  64. outs)))
  65. outs))))))
  66. |#
  67. #|
  68. ;;; Orthonormalizing with respect to the Lorentz metric in 2 dimensions.
  69. (install-coordinates R2-rect (up 't 'x))
  70. (define R2-point ((R2-rect '->point) (up 't0 'x0)))
  71. (define R2-basis (coordinate-system->basis R2-rect))
  72. (define ((L2-metric c) u v)
  73. (+ (* -1 c c (dt u) (dt v))
  74. (* 1 (dx u) (dx v))))
  75. (define L2-vector-basis
  76. (Gram-Schmidt (basis->vector-basis R2-basis) (L2-metric 'c)))
  77. (s:foreach (lambda (v)
  78. (pec ((v (literal-manifold-function 'f R2-rect))
  79. R2-point)))
  80. L2-vector-basis)
  81. #| Result:
  82. (/ (((partial 0) f) (up t0 x0)) c)
  83. |#
  84. #| Result:
  85. (((partial 1) f) (up t0 x0))
  86. |#
  87. ;Value: done
  88. (s:foreach (lambda (omega)
  89. (pec ((omega (literal-vector-field 'v R2-rect))
  90. R2-point)))
  91. (vector-basis->dual L2-vector-basis R2-rect))
  92. #| Result:
  93. (* c (v^0 (up t0 x0)))
  94. |#
  95. #| Result:
  96. (v^1 (up t0 x0))
  97. |#
  98. |#
  99. #|
  100. ;;; 4-dimensional Lorentz metric.
  101. (define SR R4-rect)
  102. (install-coordinates SR (up 't 'x 'y 'z))
  103. (define ((g-Lorentz c) u v)
  104. (+ (* (dx u) (dx v))
  105. (* (dy u) (dy v))
  106. (* (dz u) (dz v))
  107. (* -1 (square c) (dt u) (dt v))))
  108. (define SR-basis (coordinate-system->basis SR))
  109. (define an-event ((SR '->point) (up 't0 'x0 'y0 'z0)))
  110. (define SR-V (basis->vector-basis SR-basis))
  111. (define SR-V1 (ultra-flatten (Gram-Schmidt SR-V (g-Lorentz 'c))))
  112. ;;; SR-V1 is orthogonal
  113. (for-each (lambda (v1)
  114. (for-each (lambda (v2)
  115. (pe (((g-Lorentz 'c) v1 v2) an-event)))
  116. (cdr (memq v1 SR-V1))))
  117. SR-V1)
  118. 0
  119. 0
  120. 0
  121. 0
  122. 0
  123. 0
  124. ;;; SR-V1 is normal
  125. (for-each (lambda (v)
  126. (pe (((g-Lorentz 'c) v v) an-event)))
  127. SR-V1)
  128. -1
  129. 1
  130. 1
  131. 1
  132. (for-each (lambda (v)
  133. (pe ((v (SR '->coords))
  134. an-event)))
  135. SR-V1)
  136. (up (/ 1 c) 0 0 0)
  137. (up 0 1 0 0)
  138. (up 0 0 1 0)
  139. (up 0 0 0 1)
  140. |#
  141. #|
  142. (install-coordinates R3-rect (up 'x 'y 'z))
  143. (define R3-point ((R3-rect '->point) (up 'x0 'y0 'z0)))
  144. (define R3-basis (coordinate-system->basis R3-rect))
  145. (define ((g3-maker a b c d e f) v1 v2)
  146. (+ (* a (dx v1) (dx v2))
  147. (* b (dx v1) (dy v2))
  148. (* c (dx v1) (dz v2))
  149. (* b (dx v2) (dy v1))
  150. (* d (dy v1) (dy v2))
  151. (* e (dy v1) (dz v2))
  152. (* c (dx v2) (dz v1))
  153. (* e (dy v2) (dz v1))
  154. (* f (dz v1) (dz v2))))
  155. (define g3 (g3-maker 'a 'b 'c 'd 'e 'f))
  156. (for-each (lambda (v)
  157. (pe ((v (R3-rect '->coords))
  158. R3-point)))
  159. (ultra-flatten
  160. (Gram-Schmidt
  161. (basis->vector-basis R3-basis)
  162. g3)))
  163. (up (/ 1 (sqrt a)) 0 0)
  164. (up (/ (* -1 b) (sqrt (+ (* (expt a 2) d) (* -1 a (expt b 2)))))
  165. (/ a (sqrt (+ (* (expt a 2) d) (* -1 a (expt b 2)))))
  166. 0)
  167. ;;; Third one is something awful...
  168. |#
  169. ;;; This may be needed... ugh!
  170. (define (completely-antisymmetric indices)
  171. (permutation-parity indices (iota (length indices))))