vectors.scm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. ;;;; Vectors
  21. (declare (usual-integrations))
  22. (define (v:type v) vector-type-tag)
  23. (define (v:type-predicate v) vector-quantity?)
  24. ;;; This file makes the identification of the Scheme VECTOR data
  25. ;;; type with mathematical n-dimensional vectors. These are
  26. ;;; interpreted as column vectors by the matrix package.
  27. ;;; Thus we inherit the constructors VECTOR and MAKE-VECTOR,
  28. ;;; the selectors VECTOR-LENGTH and VECTOR-REF,
  29. ;;; the mutator VECTOR-SET!, and zero-based indexing.
  30. ;;; We also get the iterator MAKE-INITIALIZED-VECTOR,
  31. ;;; and the predicate VECTOR?
  32. (define-integrable v:generate make-initialized-vector)
  33. (define-integrable vector:generate make-initialized-vector)
  34. (define-integrable v:dimension vector-length)
  35. (define* ((v:elementwise f) . vectors)
  36. (assert (and (not (null? vectors))
  37. (for-all? vectors vector?)))
  38. (let ((n (v:dimension (car vectors))))
  39. (assert (for-all? (cdr vectors)
  40. (lambda (m)
  41. (fix:= (v:dimension m) n))))
  42. (v:generate
  43. (vector-length (car vectors))
  44. (lambda (i)
  45. (g:apply f
  46. (map (lambda (v) (vector-ref v i))
  47. vectors))))))
  48. (define vector:elementwise v:elementwise)
  49. (define (v:zero? v)
  50. (vector-forall g:zero? v))
  51. (define (v:make-zero n)
  52. (make-vector n :zero))
  53. (define (v:zero-like v)
  54. (v:generate (vector-length v)
  55. (lambda (i)
  56. (g:zero-like (vector-ref v i)))))
  57. (define (literal-vector name dimension)
  58. (v:generate dimension
  59. (lambda (i)
  60. (string->symbol
  61. (string-append (symbol->string name)
  62. "^"
  63. (number->string i))))))
  64. (define (v:make-basis-unit n i) ; #(0 0 ... 1 ... 0) n long, 1 in ith position
  65. (v:generate n (lambda (j) (if (fix:= j i) :one :zero))))
  66. (define (v:basis-unit? v)
  67. (let ((n (vector-length v)))
  68. (let lp ((i 0) (ans #f))
  69. (cond ((fix:= i n) ans)
  70. ((g:zero? (vector-ref v i))
  71. (lp (fix:+ i 1) ans))
  72. ((and (g:one? (vector-ref v i)) (not ans))
  73. (lp (fix:+ i 1) i))
  74. (else #f)))))
  75. (define (vector=vector v1 v2)
  76. (vector-forall g:= v1 v2))
  77. (define (vector+vector v1 v2)
  78. (v:generate (vector-length v1)
  79. (lambda (i)
  80. (g:+ (vector-ref v1 i)
  81. (vector-ref v2 i)))))
  82. (define (vector-vector v1 v2)
  83. (v:generate (vector-length v1)
  84. (lambda (i)
  85. (g:- (vector-ref v1 i)
  86. (vector-ref v2 i)))))
  87. (define (v:negate v)
  88. (v:generate (vector-length v)
  89. (lambda (i)
  90. (g:negate (vector-ref v i)))))
  91. (define (v:scale s)
  92. (lambda (v)
  93. (v:generate (vector-length v)
  94. (lambda (i)
  95. (g:* s (vector-ref v i))))))
  96. (define (scalar*vector s v)
  97. (v:generate (vector-length v)
  98. (lambda (i)
  99. (g:* s (vector-ref v i)))))
  100. (define (vector*scalar v s)
  101. (v:generate (vector-length v)
  102. (lambda (i)
  103. (g:* (vector-ref v i) s))))
  104. (define (vector/scalar v s)
  105. (v:generate (vector-length v)
  106. (lambda (i)
  107. (g:/ (vector-ref v i) s))))
  108. (define (v:inner-product v1 v2)
  109. (assert (and (vector? v1) (vector? v2))
  110. "Not vectors -- INNER-PRODUCT" (list v1 v2))
  111. (let ((n (v:dimension v1)))
  112. (assert (fix:= n (v:dimension v2))
  113. "Not same dimension -- INNER-PRODUCT" (list v1 v2))
  114. (let lp ((i 0) (ans :zero))
  115. (if (fix:= i n)
  116. ans
  117. (lp (fix:+ i 1)
  118. (g:+ ans
  119. (g:* (g:conjugate (vector-ref v1 i))
  120. (vector-ref v2 i))))))))
  121. (define (v:dot-product v1 v2)
  122. (assert (and (vector? v1) (vector? v2))
  123. "Not vectors -- V:DOT-PRODUCT" (list v1 v2))
  124. (let ((n (v:dimension v1)))
  125. (assert (fix:= n (v:dimension v2))
  126. "Not same dimension -- V:DOT-PRODUCT" (list v1 v2))
  127. (let lp ((i 0) (ans :zero))
  128. (if (fix:= i n)
  129. ans
  130. (lp (fix:+ i 1)
  131. (g:+ ans
  132. (g:* (vector-ref v1 i)
  133. (vector-ref v2 i))))))))
  134. (define (v:square v)
  135. (v:dot-product v v))
  136. (define (v:cube v)
  137. (scalar*vector (v:dot-product v v) v))
  138. (define (euclidean-norm v)
  139. (g:sqrt (v:dot-product v v)))
  140. (define (complex-norm v)
  141. (g:sqrt (v:inner-product v v)))
  142. (define (maxnorm v)
  143. (vector-accumulate max g:magnitude :zero v))
  144. (define (v:make-unit v)
  145. (scalar*vector (g:invert (euclidean-norm v))
  146. v))
  147. (define (v:unit? v)
  148. (g:one? (v:dot-product v v)))
  149. (define (v:conjugate v)
  150. ((v:elementwise g:conjugate) v))
  151. (define (v:cross-product v w)
  152. (assert (and (fix:= (vector-length v) 3)
  153. (fix:= (vector-length w) 3))
  154. "Cross product of non-3-dimensional vectors?"
  155. (list v w))
  156. (let ((v0 (vector-ref v 0))
  157. (v1 (vector-ref v 1))
  158. (v2 (vector-ref v 2))
  159. (w0 (vector-ref w 0))
  160. (w1 (vector-ref w 1))
  161. (w2 (vector-ref w 2)))
  162. (vector (g:- (g:* v1 w2) (g:* v2 w1))
  163. (g:- (g:* v2 w0) (g:* v0 w2))
  164. (g:- (g:* v0 w1) (g:* v1 w0)))))
  165. (define (general-inner-product addition multiplication :zero)
  166. (define (ip v1 v2)
  167. (let ((n (vector-length v1)))
  168. (if (not (fix:= n (vector-length v2)))
  169. (error "Unequal dimensions -- INNER-PRODUCT" v1 v2))
  170. (if (fix:= n 0)
  171. :zero
  172. (let loop ((i 1)
  173. (ans (multiplication (vector-ref v1 0)
  174. (vector-ref v2 0))))
  175. (if (fix:= i n)
  176. ans
  177. (loop (fix:+ i 1)
  178. (addition ans
  179. (multiplication (vector-ref v1 i)
  180. (vector-ref v2 i)))))))))
  181. ip)
  182. (define (v:apply vec args)
  183. (v:generate (vector-length vec)
  184. (lambda (i)
  185. (g:apply (vector-ref vec i) args))))
  186. (define (v:arity v)
  187. (let ((n (vector-length v)))
  188. (cond ((fix:= n 0)
  189. (error "I don't know the arity of the empty vector"))
  190. (else
  191. (let lp ((i 1) (a (g:arity (vector-ref v 0))))
  192. (if (fix:= i n)
  193. a
  194. (let ((b (joint-arity a (g:arity (vector-ref v i)))))
  195. (if b
  196. (lp (+ i 1) b)
  197. #f))))))))
  198. (define (v:partial-derivative vector varspecs)
  199. ((v:elementwise
  200. (lambda (f)
  201. (generic:partial-derivative f varspecs)))
  202. vector))
  203. (define (v:inexact? v)
  204. (vector-exists g:inexact? v))
  205. (define %kernel-vectors-dummy-1
  206. (begin
  207. (assign-operation 'type v:type vector?)
  208. (assign-operation 'type-predicate v:type-predicate vector?)
  209. (assign-operation 'arity v:arity vector?)
  210. (assign-operation 'inexact? v:inexact? vector?)
  211. (assign-operation 'zero-like v:zero-like vector?)
  212. (assign-operation 'zero? v:zero? vector?)
  213. (assign-operation 'negate v:negate vector?)
  214. (assign-operation 'magnitude complex-norm vector?)
  215. (assign-operation 'abs euclidean-norm vector?)
  216. (assign-operation 'conjugate v:conjugate vector?)
  217. (assign-operation '= vector=vector vector? vector?)
  218. (assign-operation '+ vector+vector vector? vector?)
  219. (assign-operation '- vector-vector vector? vector?)
  220. (assign-operation '* scalar*vector scalar? vector?)
  221. (assign-operation '* vector*scalar vector? scalar?)
  222. (assign-operation '/ vector/scalar vector? scalar?)
  223. ;;; subsumed by s:dot-product
  224. ;;; (assign-operation 'dot-product v:dot-product vector? vector?)
  225. (assign-operation 'cross-product v:cross-product vector? vector?)
  226. (assign-operation 'dimension v:dimension vector?)))
  227. #| ;;; Should be subsumed by deriv:pd in deriv.scm.
  228. (assign-operation 'partial-derivative
  229. v:partial-derivative
  230. vector? any?)
  231. |#
  232. #| ;;; Should be subsumed by s:apply in structs.scm.
  233. (assign-operation 'apply v:apply vector? any?)
  234. |#
  235. ;;; Abstract vectors generalize vector quantities.
  236. (define (abstract-vector symbol)
  237. (make-literal vector-type-tag symbol))
  238. (define (av:arity v)
  239. ;; Default is vector of numbers.
  240. (get-property v 'arity *at-least-zero*))
  241. (define (av:zero-like v)
  242. (let ((z (abstract-vector (list 'zero-like v))))
  243. (add-property! z 'zero #t)
  244. z))
  245. (define* (make-vector-combination operator #:optional reverse?)
  246. (if (default-object? reverse?)
  247. (lambda operands
  248. (make-combination vector-type-tag
  249. operator operands))
  250. (lambda operands
  251. (make-combination vector-type-tag
  252. operator (reverse operands)))))
  253. (define %kernel-vectors-dummy-2
  254. (begin
  255. (assign-operation 'type v:type abstract-vector?)
  256. (assign-operation 'type-predicate v:type-predicate abstract-vector?)
  257. (assign-operation 'arity av:arity abstract-vector?)
  258. (assign-operation 'inexact? (has-property? 'inexact) abstract-vector?)
  259. (assign-operation 'zero-like av:zero-like abstract-vector?)
  260. (assign-operation 'zero? (has-property? 'zero) abstract-vector?)
  261. (assign-operation
  262. 'negate (make-vector-combination 'negate) abstract-vector?)
  263. (assign-operation
  264. 'magnitude (make-vector-combination 'magnitude) abstract-vector?)
  265. (assign-operation
  266. 'abs (make-vector-combination 'abs) abstract-vector?)
  267. (assign-operation
  268. 'conjugate (make-vector-combination 'conjugate) abstract-vector?)
  269. #|
  270. (assign-operation
  271. 'derivative (make-vector-combination 'derivative) abstract-vector?)
  272. |#
  273. ;(assign-operation '= vector=vector abstract-vector? abstract-vector?)
  274. (assign-operation
  275. '+ (make-vector-combination '+) abstract-vector? abstract-vector?)
  276. (assign-operation
  277. '+ (make-vector-combination '+) vector? abstract-vector?)
  278. (assign-operation
  279. '+ (make-vector-combination '+ 'r) abstract-vector? vector?)
  280. (assign-operation
  281. '- (make-vector-combination '-) abstract-vector? abstract-vector?)
  282. (assign-operation
  283. '- (make-vector-combination '-) vector? abstract-vector?)
  284. (assign-operation
  285. '- (make-vector-combination '-) abstract-vector? vector?)
  286. (assign-operation
  287. '* (make-numerical-combination '*) abstract-vector? abstract-vector?)
  288. (assign-operation
  289. '* (make-numerical-combination '*) vector? abstract-vector?)
  290. (assign-operation
  291. '* (make-numerical-combination '* 'r) abstract-vector? vector?)
  292. (assign-operation
  293. '* (make-vector-combination '*) scalar? abstract-vector?)
  294. (assign-operation
  295. '* (make-vector-combination '* 'r) abstract-vector? scalar?)
  296. (assign-operation
  297. '/ (make-vector-combination '/) abstract-vector? scalar?)
  298. (assign-operation
  299. 'dot-product (make-vector-combination 'dot-product)
  300. abstract-vector? abstract-vector?)
  301. (assign-operation 'partial-derivative
  302. (make-vector-combination 'partial-derivative)
  303. abstract-vector? any?)))
  304. ;;; I don't know what to do here -- GJS. This is part of the literal
  305. ;;; function problem.
  306. ;(assign-operation 'apply v:apply abstract-vector? any?)