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
24 :lisp-stat-compound-data
)
25 (:export matrixp num-rows num-cols matmult identity-matrix diagonal
26 row-list column-list inner-product outer-product
27 cross-product transpose bind-columns bind-rows
28 array-data-vector vector-to-array
30 check-matrix check-square-matrix
32 copy-array copy-vector
35 (in-package :lisp-stat-matrix
)
37 (deftype matrix
() 'array
) ;; temp fix
39 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
41 ;;;; Array to Row-Major Data Vector Conversion Functions
43 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
45 (defun array-data-vector (a)
47 Displaces array A to a vector"
48 (make-array (array-total-size a
)
50 :element-type
(array-element-type a
)))
52 (defun vector-to-array (v dims
)
54 Displaces vector V to array with dimensions DIMS"
57 :element-type
(array-element-type v
)))
61 (defun check-matrix (a)
62 (if (not (and (arrayp a
) (= (array-rank a
) 2)))
63 (error "not a matrix - ~s" a
)
66 (defun check-square-matrix (a)
67 (if (and (check-matrix a
)
68 (/= (array-dimension a
0) (array-dimension a
1))
69 (error "matrix not square - ~s" a
))
74 Returns T if X is a matrix, NIL otherwise."
75 (and (arrayp x
) (= (array-rank x
) 2)))
79 Returns number of rows in X."
80 (array-dimension x
0))
84 Returns number of columns in X."
85 (array-dimension x
1))
87 (defun matmult (a b
&rest args
)
88 "Args: (a b &rest args)
89 Returns the matrix product of matrices a, b, etc. If a is a vector it is
90 treated as a row vector; if b is a vector it is treated as a column vector."
91 ;; fixme: why does SBCL claim this is unreachable?
92 (let ((rtype (cond ((and (matrixp a
) (matrixp b
)) 'matrix
)
93 ((and (sequencep a
) (sequencep b
)) 'number
)
94 ((sequencep a
) (if (consp a
) 'list
'vector
))
95 ((sequencep b
) (if (consp b
) 'list
'vector
)))))
98 (setf a
(vector-to-array (coerce a
'vector
) (list 1 (length a
)))))
100 (setf b
(vector-to-array (coerce b
'vector
) (list (length b
) 1))))
101 (if (not (= (array-dimension a
1) (array-dimension b
0)))
102 (error "dimensions do not match"))
104 (reduce #'matmult args
:initial-value
(matmult a b
))
105 (let* ((n (array-dimension a
0))
106 (m (array-dimension b
1))
107 (p (array-dimension a
1))
108 (c (make-array (list n m
)))
110 (declare (fixnum n m p
))
119 (* (aref a i k
) (aref b k j
)))))
120 (setf (aref c i j
) x
)))
123 (number (aref c
0 0))
124 (t (coerce (compound-data-seq c
) rtype
)))))))
126 (defun identity-matrix (n)
128 Returns the identity matrix of rank N."
129 (let ((result (make-array (list n n
) :initial-element
0)))
130 (dotimes (i n result
)
132 (setf (aref result i i
) 1))))
134 ;; this thing is not very efficient at this point - too much coercing
137 If X is a matrix, returns the diagonal of X. If X is a sequence, returns a
138 diagonal matrix of rank (length X) with diagonal elements eq to the elements
141 (let* ((n (min (num-rows x
) (num-cols x
)))
142 (result (make-array n
)))
143 (dotimes (i n
(coerce result
'list
))
144 (setf (aref result i
) (aref x i i
)))))
146 (let* ((x (coerce x
'vector
))
148 (result (make-array (list n n
) :initial-element
0)))
149 (dotimes (i n result
)
150 (setf (aref result i i
) (aref x i
)))))
151 (t (error "argument must be a matrix or a sequence"))))
155 Returns a list of the rows of M as vectors"
157 (let ((m (num-rows x
))
160 (declare (fixnum m n
))
163 (let ((row (make-array n
)))
166 (setf (aref row i
) (aref x k i
))))))
167 (dotimes (i m result
)
169 (setf result
(cons (get-row (- m i
1)) result
))))))
171 (defun column-list (x)
173 Returns a list of the columns of M as vectors"
175 (let ((m (num-rows x
))
178 (declare (fixnum m n
))
181 (let ((col (make-array m
)))
184 (setf (aref col i
) (aref x i k
))))))
185 (dotimes (i n result
)
187 (setf result
(cons (get-col (- n i
1)) result
))))))
189 (defun inner-product (x y
)
191 Returns inner product of sequences X and Y."
195 (cx (make-next-element x
))
196 (cy (make-next-element y
))
199 (if (/= n
(length y
)) (error "sequence lengths do not match"))
200 (dotimes (i n result
)
203 (+ result
(* (get-next-element cx i
) (get-next-element cy i
)))))))
205 (defun outer-product (x y
&optional
(f #'*))
206 "Args: (x y &optional (fcn #'*))
207 Returns the generalized outer product of x and y, using fcn. Tat is, the result
208 is a matrix of dimension ((length x) (length y)) and the (i j) element of the
209 result is computed as (apply fcn (aref x i) (aref y j))."
210 (let* ((x (coerce x
'vector
))
211 (y (coerce y
'vector
))
214 (a (make-array (list m n
))))
215 (declare (fixnum m n
))
220 (setf (aref a i j
) (funcall f
(aref x i
) (aref y j
)))))))
222 (defun cross-product (x)
224 If X is a matrix returns (matmult (transpose X) X). If X is a vector returns
225 (inner-product X X)."
227 (let* ((n (num-rows x
))
229 (c (make-array (list p p
))))
230 (declare (fixnum n p
))
238 (incf val
(* (aref x k i
) (aref x k j
))))
239 (setf (aref c i j
) val
)
240 (setf (aref c j i
) val
))))))
242 (defun transpose-list (x)
243 (let ((m (length (first x
))))
245 (if (not (consp next
)) (error "not a list - ~a" x
))
246 (if (/= m
(length next
)) (error "sublists not the same length")))
247 (do* ((cx (copy-list x
))
248 (result (make-list m
))
249 (next result
(cdr next
)))
251 (setf (first next
) (mapcar #'first cx
))
252 (do ((next cx
(cdr next
)))
254 (setf (first next
) (rest (first next
)))))))
258 Returns the transpose of the matrix M."
260 ((consp x
) (transpose-list x
))
263 (let* ((m (num-rows x
))
265 (tx (make-array (list n m
))))
266 (declare (fixnum m n
))
271 (setf (aref tx j i
) (aref x i j
))))))))
273 (defun bind-columns (&rest args
)
275 The ARGS can be matrices, vectors, or lists. Arguments are bound into a matrix
277 Example: (bind-columns #2a((1 2)(3 4)) #(5 6)) returns #2a((1 2 5)(3 4 6))"
278 (flet ((check-arg (x)
279 (if (not (or (sequencep x
) (matrixp x
)))
280 (error "bad argument type")))
281 (arg-cols (x) (if (sequencep x
) 1 (num-cols x
)))
282 (arg-rows (x) (if (sequencep x
) (length x
) (num-rows x
))))
283 (dolist (x args
) (check-arg x
))
284 (let ((m (arg-rows (first args
)))
285 (n (arg-cols (first args
))))
286 (declare (fixnum m n
))
287 (dolist (x (rest args
))
288 (if (/= m
(arg-rows x
)) (error "column lengths do not match"))
289 (incf n
(arg-cols x
)))
290 (do* ((result (make-array (list m n
)))
291 (args args
(rest args
))
293 (x (first args
) (first args
)))
297 (let ((cx (make-next-element x
)))
299 (setf (aref result i firstcol
) (get-next-element cx i
)))))
301 (let ((k (arg-cols x
)))
304 (setf (aref result i
(+ firstcol j
)) (aref x i j
)))))))
305 (incf firstcol
(arg-cols x
))))))
307 (defun bind-rows (&rest args
)
309 The ARGS can be matrices, vectors, or lists. Arguments are bound into a matrix
311 Example: (bind-rows #2a((1 2)(3 4)) #(5 6)) returns #2a((1 2)(3 4)(5 6))"
312 (flet ((check-arg (x)
313 (if (not (or (sequencep x
) (matrixp x
)))
314 (error "bad argument type")))
315 (arg-cols (x) (if (sequencep x
) (length x
) (num-cols x
)))
316 (arg-rows (x) (if (sequencep x
) 1 (num-rows x
))))
317 (dolist (x args
) (check-arg x
))
318 (let ((m (arg-rows (first args
)))
319 (n (arg-cols (first args
))))
320 (declare (fixnum m n
))
321 (dolist (x (rest args
))
322 (if (/= n
(arg-cols x
)) (error "row lengths do not match"))
323 (incf m
(arg-rows x
)))
324 (do* ((result (make-array (list m n
)))
325 (args args
(rest args
))
327 (x (first args
) (first args
)))
331 (let ((cx (make-next-element x
)))
333 (setf (aref result firstrow i
) (get-next-element cx i
)))))
335 (let ((k (arg-rows x
)))
338 (setf (aref result
(+ firstrow j
) i
) (aref x j i
)))))))
339 (incf firstrow
(arg-rows x
))))))
340 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
342 ;;;; Copying Functions
344 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
347 ;;; COPY-VECTOR function
350 (defun copy-vector (x)
352 Returns a copy of the vector X"
356 ;;; COPY-ARRAY function
359 (defun copy-array (a)
361 Returns a copy of the array A"
362 (vector-to-array (copy-seq (array-data-vector a
))
363 (array-dimensions a
)))