PageRenderTime 27ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Examples/guile/matrix/matrix.scm

#
Lisp | 210 lines | 142 code | 34 blank | 34 comment | 0 complexity | 6a02dced5b9bcb328d4c1c8050dda5e2 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. #!./matrix \
  2. -e do-test -s
  3. !#
  4. ;;; Authors: David Beazley <beazley@cs.uchicago.edu>, 1999
  5. ;;; Martin Froehlich <MartinFroehlich@ACM.org>, 2000
  6. ;;;
  7. ;;; PURPOSE OF THIS FILE: This file is an example for how to use the guile
  8. ;;; scripting options with a little more than trivial script. Example
  9. ;;; derived from David Beazley's matrix evaluation example. David
  10. ;;; Beazley's annotation: >>Guile script for testing out matrix
  11. ;;; operations. Disclaimer : I'm not a very good scheme
  12. ;;; programmer<<. Martin Froehlich's annotation: >>I'm not a very good
  13. ;;; scheme programmer, too<<.
  14. ;;;
  15. ;;; Explanation: The three lines at the beginning of this script are
  16. ;;; telling the kernel to load the enhanced guile interpreter named
  17. ;;; "matrix"; to execute the function "do-test" (-e option) after loading
  18. ;;; this script (-s option). There are a lot more options wich allow for
  19. ;;; even finer tuning. SEE ALSO: Section "Guile Scripts" in the "Guile
  20. ;;; reference manual -- Part I: Preliminaries".
  21. ;;;
  22. ;;;
  23. ;;; This program is distributed in the hope that it will be useful, but
  24. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  25. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  26. ;;; Create a zero matrix
  27. (define (zero M)
  28. (define (zero-loop M i j)
  29. (if (< i 4)
  30. (if (< j 4) (begin
  31. (set-m M i j 0.0)
  32. (zero-loop M i (+ j 1)))
  33. (zero-loop M (+ i 1) 0))))
  34. (zero-loop M 0 0))
  35. ;;; Create an identity matrix
  36. (define (identity M)
  37. (define (iloop M i)
  38. (if (< i 4) (begin
  39. (set-m M i i 1.0)
  40. (iloop M (+ i 1)))))
  41. (zero M)
  42. (iloop M 0))
  43. ;;; Rotate around x axis
  44. (define (rotx M r)
  45. (define temp (new-matrix))
  46. (define rd (/ (* r 3.14159) 180.0))
  47. (zero temp)
  48. (set-m temp 0 0 1.0)
  49. (set-m temp 1 1 (cos rd))
  50. (set-m temp 1 2 (- 0 (sin rd)))
  51. (set-m temp 2 1 (sin rd))
  52. (set-m temp 2 2 (cos rd))
  53. (set-m temp 3 3 1.0)
  54. (mat-mult M temp M)
  55. (destroy-matrix temp))
  56. ;;; Rotate around y axis
  57. (define (roty M r)
  58. (define temp (new-matrix))
  59. (define rd (/ (* r 3.14159) 180.0))
  60. (zero temp)
  61. (set-m temp 1 1 1.0)
  62. (set-m temp 0 0 (cos rd))
  63. (set-m temp 0 2 (sin rd))
  64. (set-m temp 2 0 (- 0 (sin rd)))
  65. (set-m temp 2 2 (cos rd))
  66. (set-m temp 3 3 1.0)
  67. (mat-mult M temp M)
  68. (destroy-matrix temp))
  69. ;;; Rotate around z axis
  70. (define (rotz M r)
  71. (define temp (new-matrix))
  72. (define rd (/ (* r 3.14159) 180.0))
  73. (zero temp)
  74. (set-m temp 0 0 (cos rd))
  75. (set-m temp 0 1 (- 0 (sin rd)))
  76. (set-m temp 1 0 (sin rd))
  77. (set-m temp 1 1 (cos rd))
  78. (set-m temp 2 2 1.0)
  79. (set-m temp 3 3 1.0)
  80. (mat-mult M temp M)
  81. (destroy-matrix temp))
  82. ;;; Scale a matrix
  83. (define (scale M s)
  84. (define temp (new-matrix))
  85. (define (sloop m i s)
  86. (if (< i 4) (begin
  87. (set-m m i i s)
  88. (sloop m (+ i 1) s))))
  89. (zero temp)
  90. (sloop temp 0 s)
  91. (mat-mult M temp M)
  92. (destroy-matrix temp))
  93. ;;; Make a matrix with random elements
  94. (define (randmat M)
  95. (define (rand-loop M i j)
  96. (if (< i 4)
  97. (if (< j 4)
  98. (begin
  99. (set-m M i j (drand48))
  100. (rand-loop M i (+ j 1)))
  101. (rand-loop M (+ i 1) 0))))
  102. (rand-loop M 0 0))
  103. ;;; stray definitions collected here
  104. (define (rot-test M v t i)
  105. (if (< i 360) (begin
  106. (rotx M 1)
  107. (rotz M -0.5)
  108. (transform M v t)
  109. (rot-test M v t (+ i 1)))))
  110. (define (create-matrix) ; Create some matrices
  111. (let loop ((i 0) (result '()))
  112. (if (< i 200)
  113. (loop (+ i 1) (cons (new-matrix) result))
  114. result)))
  115. (define (add-mat M ML)
  116. (define (add-two m1 m2 i j)
  117. (if (< i 4)
  118. (if (< j 4)
  119. (begin
  120. (set-m m1 i j (+ (get-m m1 i j) (get-m m2 i j)))
  121. (add-two m1 m2 i (+ j 1)))
  122. (add-two m1 m2 (+ i 1) 0))))
  123. (if (null? ML) () (begin
  124. (add-two M (car ML) 0 0)
  125. (add-mat M (cdr ML)))))
  126. (define (cleanup ML)
  127. (if (null? ML) () (begin
  128. (destroy-matrix (car ML))
  129. (cleanup (cdr ML)))))
  130. (define (make-random ML) ; Put random values in them
  131. (if (null? ML) () (begin
  132. (randmat (car ML))
  133. (make-random (cdr ML)))))
  134. (define (mul-mat m ML)
  135. (if (null? ML) () (begin
  136. (mat-mult m (car ML) m)
  137. (mul-mat m (cdr ML)))))
  138. ;;; Now we'll hammer on things a little bit just to make
  139. ;;; sure everything works.
  140. (define M1 (new-matrix)) ; a matrix
  141. (define v (createv 1 2 3 4)) ; a vector
  142. (define t (createv 0 0 0 0)) ; the zero-vector
  143. (define M-list (create-matrix)) ; get list of marices
  144. (define M (new-matrix)) ; yet another matrix
  145. (display "variables defined\n")
  146. (define (do-test x)
  147. (display "Testing matrix program...\n")
  148. (identity M1)
  149. (print-matrix M1)
  150. (display "Rotate-x 45 degrees\n")
  151. (rotx M1 45)
  152. (print-matrix M1)
  153. (display "Rotate y 30 degrees\n")
  154. (roty M1 30)
  155. (print-matrix M1)
  156. (display "Rotate z 15 degrees\n")
  157. (rotz M1 15)
  158. (print-matrix M1)
  159. (display "Scale 0.5\n")
  160. (scale M1 0.5)
  161. (print-matrix M1)
  162. ;; Rotating ...
  163. (display "Rotating...\n")
  164. (rot-test M1 v t 0)
  165. (printv t)
  166. (make-random M-list)
  167. (zero M1)
  168. (display "Adding them together (in Guile)\n")
  169. (add-mat M1 M-list)
  170. (print-matrix M1)
  171. (display "Doing 200 multiplications (mostly in C)\n")
  172. (randmat M)
  173. (mul-mat M M-list)
  174. (display "Cleaning up\n")
  175. (cleanup M-list))
  176. ;;; matrix.scm ends here