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 (matrixp a
) (matrixp b
)) 'matrix
)
94 ((and (sequencep a
) (sequencep b
)) 'number
)
95 ((sequencep a
) (if (consp a
) 'list
'vector
))
96 ((sequencep b
) (if (consp b
) 'list
'vector
)))))
99 (setf a
(vector-to-array (coerce a
'vector
) (list 1 (length a
)))))
101 (setf b
(vector-to-array (coerce b
'vector
) (list (length b
) 1))))
102 (if (not (= (array-dimension a
1) (array-dimension b
0)))
103 (error "dimensions do not match"))
105 (reduce #'matmult args
:initial-value
(matmult a b
))
106 (let* ((n (array-dimension a
0))
107 (m (array-dimension b
1))
108 (p (array-dimension a
1))
109 (c (make-array (list n m
)))
111 (declare (fixnum n m p
))
120 (* (aref a i k
) (aref b k j
)))))
121 (setf (aref c i j
) x
)))
124 (number (aref c
0 0))
125 (t (coerce (compound-data-seq c
) rtype
)))))))
127 (defun identity-matrix (n)
129 Returns the identity matrix of rank N."
130 (let ((result (make-array (list n n
) :initial-element
0)))
131 (dotimes (i n result
)
133 (setf (aref result i i
) 1))))
135 ;; this thing is not very efficient at this point - too much coercing
138 If X is a matrix, returns the diagonal of X. If X is a sequence, returns a
139 diagonal matrix of rank (length X) with diagonal elements eq to the elements
142 (let* ((n (min (num-rows x
) (num-cols x
)))
143 (result (make-array n
)))
144 (dotimes (i n
(coerce result
'list
))
145 (setf (aref result i
) (aref x i i
)))))
147 (let* ((x (coerce x
'vector
))
149 (result (make-array (list n n
) :initial-element
0)))
150 (dotimes (i n result
)
151 (setf (aref result i i
) (aref x i
)))))
152 (t (error "argument must be a matrix or a sequence"))))
156 Returns a list of the rows of M as vectors"
158 (let ((m (num-rows x
))
161 (declare (fixnum m n
))
164 (let ((row (make-array n
)))
167 (setf (aref row i
) (aref x k i
))))))
168 (dotimes (i m result
)
170 (setf result
(cons (get-row (- m i
1)) result
))))))
172 (defun column-list (x)
174 Returns a list of the columns of M as vectors"
176 (let ((m (num-rows x
))
179 (declare (fixnum m n
))
182 (let ((col (make-array m
)))
185 (setf (aref col i
) (aref x i k
))))))
186 (dotimes (i n result
)
188 (setf result
(cons (get-col (- n i
1)) result
))))))
190 (defun inner-product (x y
)
192 Returns inner product of sequences X and Y."
196 (cx (make-next-element x
))
197 (cy (make-next-element y
))
200 (if (/= n
(length y
)) (error "sequence lengths do not match"))
201 (dotimes (i n result
)
204 (+ result
(* (get-next-element cx i
) (get-next-element cy i
)))))))
206 (defun outer-product (x y
&optional
(f #'*))
207 "Args: (x y &optional (fcn #'*))
208 Returns the generalized outer product of x and y, using fcn. Tat is, the result
209 is a matrix of dimension ((length x) (length y)) and the (i j) element of the
210 result is computed as (apply fcn (aref x i) (aref y j))."
211 (let* ((x (coerce x
'vector
))
212 (y (coerce y
'vector
))
215 (a (make-array (list m n
))))
216 (declare (fixnum m n
))
221 (setf (aref a i j
) (funcall f
(aref x i
) (aref y j
)))))))
223 (defun cross-product (x)
225 If X is a matrix returns (matmult (transpose X) X). If X is a vector returns
226 (inner-product X X)."
228 (let* ((n (num-rows x
))
230 (c (make-array (list p p
))))
231 (declare (fixnum n p
))
239 (incf val
(* (aref x k i
) (aref x k j
))))
240 (setf (aref c i j
) val
)
241 (setf (aref c j i
) val
))))))
243 (defun transpose-list (x)
244 (let ((m (length (first x
))))
246 (if (not (consp next
)) (error "not a list - ~a" x
))
247 (if (/= m
(length next
)) (error "sublists not the same length")))
248 (do* ((cx (copy-list x
))
249 (result (make-list m
))
250 (next result
(cdr next
)))
252 (setf (first next
) (mapcar #'first cx
))
253 (do ((next cx
(cdr next
)))
255 (setf (first next
) (rest (first next
)))))))
259 Returns the transpose of the matrix M."
261 ((consp x
) (transpose-list x
))
264 (let* ((m (num-rows x
))
266 (tx (make-array (list n m
))))
267 (declare (fixnum m n
))
272 (setf (aref tx j i
) (aref x i j
))))))))
274 (defun bind-columns (&rest args
)
276 The ARGS can be matrices, vectors, or lists. Arguments are bound into a matrix
278 Example: (bind-columns #2a((1 2)(3 4)) #(5 6)) returns #2a((1 2 5)(3 4 6))"
279 (flet ((check-arg (x)
280 (if (not (or (sequencep x
) (matrixp x
)))
281 (error "bad argument type")))
282 (arg-cols (x) (if (sequencep x
) 1 (num-cols x
)))
283 (arg-rows (x) (if (sequencep x
) (length x
) (num-rows x
))))
284 (dolist (x args
) (check-arg x
))
285 (let ((m (arg-rows (first args
)))
286 (n (arg-cols (first args
))))
287 (declare (fixnum m n
))
288 (dolist (x (rest args
))
289 (if (/= m
(arg-rows x
)) (error "column lengths do not match"))
290 (incf n
(arg-cols x
)))
291 (do* ((result (make-array (list m n
)))
292 (args args
(rest args
))
294 (x (first args
) (first args
)))
298 (let ((cx (make-next-element x
)))
300 (setf (aref result i firstcol
) (get-next-element cx i
)))))
302 (let ((k (arg-cols x
)))
305 (setf (aref result i
(+ firstcol j
)) (aref x i j
)))))))
306 (incf firstcol
(arg-cols x
))))))
308 (defun bind-rows (&rest args
)
310 The ARGS can be matrices, vectors, or lists. Arguments are bound into a matrix
312 Example: (bind-rows #2a((1 2)(3 4)) #(5 6)) returns #2a((1 2)(3 4)(5 6))"
313 (flet ((check-arg (x)
314 (if (not (or (sequencep x
) (matrixp x
)))
315 (error "bad argument type")))
316 (arg-cols (x) (if (sequencep x
) (length x
) (num-cols x
)))
317 (arg-rows (x) (if (sequencep x
) 1 (num-rows x
))))
318 (dolist (x args
) (check-arg x
))
319 (let ((m (arg-rows (first args
)))
320 (n (arg-cols (first args
))))
321 (declare (fixnum m n
))
322 (dolist (x (rest args
))
323 (if (/= n
(arg-cols x
)) (error "row lengths do not match"))
324 (incf m
(arg-rows x
)))
325 (do* ((result (make-array (list m n
)))
326 (args args
(rest args
))
328 (x (first args
) (first args
)))
332 (let ((cx (make-next-element x
)))
334 (setf (aref result firstrow i
) (get-next-element cx i
)))))
336 (let ((k (arg-rows x
)))
339 (setf (aref result
(+ firstrow j
) i
) (aref x j i
)))))))
340 (incf firstrow
(arg-rows x
))))))
341 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
343 ;;;; Copying Functions
345 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
348 ;;; COPY-VECTOR function
351 (defun copy-vector (x)
353 Returns a copy of the vector X"
357 ;;; COPY-ARRAY function
360 (defun copy-array (a)
362 Returns a copy of the array A"
363 (vector-to-array (copy-seq (array-data-vector a
))
364 (array-dimensions a
)))