aher

Lisp functions for elementary vector and matrix products

Mar 24th, 2013
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 2.86 KB | None | 0 0
  1. ;; Lisp functions for elementary vector and matrix products
  2.  
  3. ;; (dot U V) == dot product of two vectors == U.V
  4. ;; (transpose A) == transpose matrix A == A^T
  5. ;; (mult A B) === multiply two matrices AxB
  6. ;; (outer f U V) == outer product of vectors U and V wrt function f
  7. ;;   == matrix of elements [f(U[i], V[j])]
  8. ;; (inner f g U V) == generalized inner product of vectors U and V
  9. ;;   == fold(f, [g(U[i],V[i])])
  10. ;; (poly U x) == evaluate polynomial w/ coefficients U at point x
  11.  
  12. ;; The goal here is to define these operations as simply as possible,
  13. ;; in order to demonstrate basic principles,
  14. ;; so we don't do any input validation or error catching.
  15.  
  16. ;; N.B. to use the following examples you must define RANGE and IOTA
  17. ;; as described here: http://pastebin.com/h1dNCaiQ
  18. ;; If this file is called range.lsp then do this:
  19. (load "range.lsp")
  20.  
  21. ;; vector is just a list
  22. ;; example vectors
  23. (defvar U '(1 3 -5))
  24. (defvar V '(4 -2 -1))
  25.  
  26. ;; dot product of two vectors U.V
  27. (defun dot (U V) (apply #'+ (mapcar #'* U V)))
  28.  
  29. ;; example dot product
  30. (dot U V) ; U.V == 3
  31.  
  32. ;; matrix is a list of lists
  33. ;; example matrices
  34. (defvar A '((1 3 -5) (4 -2 -1) (0 7 2)))
  35. (defvar B '((-3 2 5) (1 3 -7) (-1 -2 8)))
  36.  
  37. ;; transpose matrix: exchange rows and columns
  38. (defun transpose (M) (apply #'mapcar #'list M))
  39.  
  40. ;; matrix multiplication
  41. (defun mult (A B)
  42.   (mapcar (lambda (row)
  43.     (mapcar (lambda (col)
  44.       (dot row col))
  45.     (transpose B)))
  46.   A))
  47.  
  48. ;; example matrix multiplication
  49. (mult A B) ;; AxB == ((5 21 -56) (-13 4 26) (5 17 -33))
  50.  
  51. ;; remember, vector is list and matrix is list of lists
  52. ;; so to mult vector by matrix, nest vector in parens like this...
  53. (mult (list U) B) ; == 1x3 matrix: ((5 21 -56))
  54. (mult A (transpose (list V))) ; == 3x1 matrix: ((3) (21) (-16))
  55.  
  56. ;; dot product U.V == UxV^T
  57. ;; except we'll get nested parens, b/c this is matrix, not vector
  58. (mult (list U) (transpose (list V))) ; == ((3))
  59.  
  60. ;; outer product of two vectors
  61. (defun outer (fn U V)
  62.   (mapcar (lambda (x)
  63.     (mapcar (lambda (y)
  64.       (funcall fn x y))
  65.     V))
  66.   U))
  67.  
  68. ;; example outer product
  69. (outer #'* (iota 12) (iota 12)) ; 1-12 times tables
  70.  
  71. ;; inner product of two vectors
  72. (defun inner (f g U V) (apply f (mapcar g U V)))
  73.  
  74. ;; same as (dot U V)
  75. (inner #'+ #'* U V) ; == 3
  76.  
  77. ;; (1^1 2^2 3^3) == (1 4 27)
  78. (inner #'list #'expt (iota 3) (iota 3))
  79.  
  80. ;; 1^1 + 2^2 + 3^3 == 32
  81. (inner #'+ #'expt (iota 3) (iota 3))
  82.  
  83. ;; 1^1 * 2^2 * 3^3 == 108
  84. (inner #'* #'expt (iota 3) (iota 3))
  85.  
  86. ;; polynomial evaluation
  87. ;; coefficient list a == (a_0 a_1 a_2 ... a_n)
  88. ;; point to eval at == x
  89. ;; want: a_0 + a_1 * x + a_2 * x^2 + ... + a_n * x^n
  90. (defun poly (a x)
  91.   (dot a (inner #'list #'expt (make-list (length a) :initial-element x) (range 0 (1- (length a))))))
  92.  
  93. ;; 4*(-3)^0 - 2*(-3)^1 - 1*(-3)^2 == 1
  94. (poly V -3)
  95.  
  96. ;; 1*(1/2)^0 + 3*(1/2)^1 - 5*(1/2)^2 == 1.25 == 5/4
  97. (poly U 1/2)
Advertisement
Add Comment
Please, Sign In to add comment