3 ;;; Copyright (c) 2005--2007, by A.J. Rossini <blindglobe@gmail.com>
4 ;;; See COPYRIGHT file for any additional restrictions (BSD license).
5 ;;; Since 1991, ANSI was finally finished. Modified to match ANSI
8 ;;;; matrices -- Basic matrix operations
10 ;;;; Copyright (c) 1991, by Luke Tierney. Permission is granted for
11 ;;;; unrestricted use.
14 ;;; Need to extend to use CLEM
22 (defpackage :lisp-stat-matrix
25 :lisp-stat-compound-data
)
26 (:export matrixp num-rows num-cols matmult identity-matrix diagonal
27 row-list column-list inner-product outer-product
28 cross-product transpose bind-columns bind-rows
29 array-data-vector vector-to-array
31 check-matrix check-square-matrix
33 copy-array copy-vector
36 (in-package :lisp-stat-matrix
)
38 (deftype matrix
() 'array
) ;; temp fix
40 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
42 ;;;; Array to Row-Major Data Vector Conversion Functions
44 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
46 (defun array-data-vector (a)
48 Displaces array A to a vector"
49 (make-array (array-total-size a
)
51 :element-type
(array-element-type a
)))
53 (defun vector-to-array (v dims
)
55 Displaces vector V to array with dimensions DIMS"
58 :element-type
(array-element-type v
)))
62 (defun check-matrix (a)
63 (if (not (and (arrayp a
) (= (array-rank a
) 2)))
64 (error "not a matrix - ~s" a
)
67 (defun check-square-matrix (a)
68 (if (and (check-matrix a
)
69 (/= (array-dimension a
0) (array-dimension a
1))
70 (error "matrix not square - ~s" a
))
75 Returns T if X is a matrix, NIL otherwise."
76 (and (arrayp x
) (= (array-rank x
) 2)))
80 Returns number of rows in X."
81 (array-dimension x
0))
85 Returns number of columns in X."
86 (array-dimension x
1))
88 (defun matmult (a b
&rest args
)
89 "Args: (a b &rest args)
90 Returns the matrix product of matrices a, b, etc. If a is a vector it is
91 treated as a row vector; if b is a vector it is treated as a column vector."
92 ;; fixme: why does SBCL claim this is unreachable?
93 (let ((rtype (cond ((and (typep a
'matrix
)
94 (typep b
'matrix
)) 'matrix
)
95 ((and (typep a
'sequence
)
96 (typep b
'sequence
)) 'number
)
98 (if (consp a
) 'list
'vector
))
100 (if (consp b
) 'list
'vector
)))))
103 (setf a
(vector-to-array (coerce a
'vector
) (list 1 (length a
)))))
105 (setf b
(vector-to-array (coerce b
'vector
) (list (length b
) 1))))
106 (if (not (= (array-dimension a
1) (array-dimension b
0)))
107 (error "dimensions do not match"))
109 (reduce #'matmult args
:initial-value
(matmult a b
))
110 (let* ((n (array-dimension a
0))
111 (m (array-dimension b
1))
112 (p (array-dimension a
1))
113 (c (make-array (list n m
)))
115 (declare (fixnum n m p
))
124 (* (aref a i k
) (aref b k j
)))))
125 (setf (aref c i j
) x
)))
128 (number (aref c
0 0))
129 (t (coerce (compound-data-seq c
) rtype
)))))))
131 (defun identity-matrix (n)
133 Returns the identity matrix of rank N."
134 (let ((result (make-array (list n n
) :initial-element
0)))
135 (dotimes (i n result
)
137 (setf (aref result i i
) 1))))
139 ;; this thing is not very efficient at this point - too much coercing
142 If X is a matrix, returns the diagonal of X. If X is a sequence, returns a
143 diagonal matrix of rank (length X) with diagonal elements eq to the elements
145 (cond ((typep x
'matrix
)
146 (let* ((n (min (num-rows x
) (num-cols x
)))
147 (result (make-array n
)))
148 (dotimes (i n
(coerce result
'list
))
149 (setf (aref result i
) (aref x i i
)))))
151 (let* ((x (coerce x
'vector
))
153 (result (make-array (list n n
) :initial-element
0)))
154 (dotimes (i n result
)
155 (setf (aref result i i
) (aref x i
)))))
156 (t (error "argument must be a matrix or a sequence"))))
160 Returns a list of the rows of M as vectors"
162 (let ((m (num-rows x
))
165 (declare (fixnum m n
))
168 (let ((row (make-array n
)))
171 (setf (aref row i
) (aref x k i
))))))
172 (dotimes (i m result
)
174 (setf result
(cons (get-row (- m i
1)) result
))))))
176 (defun column-list (x)
178 Returns a list of the columns of M as vectors"
180 (let ((m (num-rows x
))
183 (declare (fixnum m n
))
186 (let ((col (make-array m
)))
189 (setf (aref col i
) (aref x i k
))))))
190 (dotimes (i n result
)
192 (setf result
(cons (get-col (- n i
1)) result
))))))
194 (defun inner-product (x y
)
196 Returns inner product of sequences X and Y."
200 (cx (make-next-element x
))
201 (cy (make-next-element y
))
204 (if (/= n
(length y
)) (error "sequence lengths do not match"))
205 (dotimes (i n result
)
208 (+ result
(* (get-next-element cx i
) (get-next-element cy i
)))))))
210 (defun outer-product (x y
&optional
(f #'*))
211 "Args: (x y &optional (fcn #'*))
212 Returns the generalized outer product of x and y, using fcn. Tat is, the result
213 is a matrix of dimension ((length x) (length y)) and the (i j) element of the
214 result is computed as (apply fcn (aref x i) (aref y j))."
215 (let* ((x (coerce x
'vector
))
216 (y (coerce y
'vector
))
219 (a (make-array (list m n
))))
220 (declare (fixnum m n
))
225 (setf (aref a i j
) (funcall f
(aref x i
) (aref y j
)))))))
227 (defun cross-product (x)
229 If X is a matrix returns (matmult (transpose X) X). If X is a vector returns
230 (inner-product X X)."
232 (let* ((n (num-rows x
))
234 (c (make-array (list p p
))))
235 (declare (fixnum n p
))
243 (incf val
(* (aref x k i
) (aref x k j
))))
244 (setf (aref c i j
) val
)
245 (setf (aref c j i
) val
))))))
247 (defun transpose-list (x)
248 (let ((m (length (first x
))))
250 (if (not (consp next
)) (error "not a list - ~a" x
))
251 (if (/= m
(length next
)) (error "sublists not the same length")))
252 (do* ((cx (copy-list x
))
253 (result (make-list m
))
254 (next result
(cdr next
)))
256 (setf (first next
) (mapcar #'first cx
))
257 (do ((next cx
(cdr next
)))
259 (setf (first next
) (rest (first next
)))))))
263 Returns the transpose of the matrix M."
265 ((consp x
) (transpose-list x
))
268 (let* ((m (num-rows x
))
270 (tx (make-array (list n m
))))
271 (declare (fixnum m n
))
276 (setf (aref tx j i
) (aref x i j
))))))))
278 (defun bind-columns (&rest args
)
280 The ARGS can be matrices, vectors, or lists. Arguments are bound into a matrix
282 Example: (bind-columns #2a((1 2)(3 4)) #(5 6)) returns #2a((1 2 5)(3 4 6))"
283 (flet ((check-arg (x)
284 (if (not (or (typep x
'sequence
) (typep x
'matrix
)))
285 (error "bad argument type")))
286 (arg-cols (x) (if (typep x
'sequence
) 1 (num-cols x
)))
287 (arg-rows (x) (if (typep x
'sequence
) (length x
) (num-rows x
))))
288 (dolist (x args
) (check-arg x
)) ;; verify data structure conformance.
289 (let ((m (arg-rows (first args
)))
290 (n (arg-cols (first args
))))
291 (declare (fixnum m n
))
292 (dolist (x (rest args
))
293 (if (/= m
(arg-rows x
)) (error "column lengths do not match"))
294 (incf n
(arg-cols x
)))
295 (do* ((result (make-array (list m n
)))
296 (args args
(rest args
))
298 (x (first args
) (first args
)))
302 (let ((cx (make-next-element x
)))
304 (setf (aref result i firstcol
) (get-next-element cx i
)))))
306 (let ((k (arg-cols x
)))
309 (setf (aref result i
(+ firstcol j
)) (aref x i j
)))))))
310 (incf firstcol
(arg-cols x
))))))
312 (defun bind-rows (&rest args
)
314 The ARGS can be matrices, vectors, or lists. Arguments are bound into a matrix
316 Example: (bind-rows #2a((1 2)(3 4)) #(5 6)) returns #2a((1 2)(3 4)(5 6))"
317 (flet ((check-arg (x)
318 (if (not (or (typep x
'sequence
)
320 (error "bad argument type")))
321 (arg-cols (x) (if (typep x
'sequence
) (length x
) (num-cols x
)))
322 (arg-rows (x) (if (typep x
'sequence
) 1 (num-rows x
))))
323 (dolist (x args
) (check-arg x
))
324 (let ((m (arg-rows (first args
)))
325 (n (arg-cols (first args
))))
326 (declare (fixnum m n
))
327 (dolist (x (rest args
))
328 (if (/= n
(arg-cols x
)) (error "row lengths do not match"))
329 (incf m
(arg-rows x
)))
330 (do* ((result (make-array (list m n
)))
331 (args args
(rest args
))
333 (x (first args
) (first args
)))
337 (let ((cx (make-next-element x
)))
339 (setf (aref result firstrow i
) (get-next-element cx i
)))))
341 (let ((k (arg-rows x
)))
344 (setf (aref result
(+ firstrow j
) i
) (aref x j i
)))))))
345 (incf firstrow
(arg-rows x
))))))
346 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
348 ;;;; Copying Functions
350 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
353 ;;; COPY-VECTOR function
356 (defun copy-vector (x)
358 Returns a copy of the vector X"
362 ;;; COPY-ARRAY function
365 (defun copy-array (a)
367 Returns a copy of the array A"
368 (vector-to-array (copy-seq (array-data-vector a
))
369 (array-dimensions a
)))