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
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 (in-package :lisp-stat-matrix
)
33 (deftype matrix
() 'array
) ;; temp fix
35 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
37 ;;;; Array to Row-Major Data Vector Conversion Functions
39 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
41 (defun array-data-vector (a)
43 Displaces array A to a vector"
44 (make-array (array-total-size a
)
46 :element-type
(array-element-type a
)))
48 (defun vector-to-array (v dims
)
50 Displaces vector V to array with dimensions DIMS"
53 :element-type
(array-element-type v
)))
57 (defun check-matrix (a)
58 (if (not (and (arrayp a
) (= (array-rank a
) 2)))
59 (error "not a matrix - ~s" a
)
62 (defun check-square-matrix (a)
63 (if (and (check-matrix a
)
64 (/= (array-dimension a
0) (array-dimension a
1))
65 (error "matrix not square - ~s" a
))
70 Returns T if X is a matrix, NIL otherwise."
71 (and (arrayp x
) (= (array-rank x
) 2)))
75 Returns number of rows in X."
76 (array-dimension x
0))
80 Returns number of columns in X."
81 (array-dimension x
1))
83 (defun matmult (a b
&rest args
)
84 "Args: (a b &rest args)
85 Returns the matrix product of matrices a, b, etc. If a is a vector it is
86 treated as a row vector; if b is a vector it is treated as a column vector."
87 (let ((rtype (cond ((and (matrixp a
) (matrixp b
)) 'matrix
)
88 ((and (sequencep a
) (sequencep b
)) 'number
)
89 ((sequencep a
) (if (consp a
) 'list
'vector
))
90 ((sequencep b
) (if (consp b
) 'list
'vector
)))))
93 (setf a
(vector-to-array (coerce a
'vector
) (list 1 (length a
)))))
95 (setf b
(vector-to-array (coerce b
'vector
) (list (length b
) 1))))
96 (if (not (= (array-dimension a
1) (array-dimension b
0)))
97 (error "dimensions do not match"))
99 (reduce #'matmult args
:initial-value
(matmult a b
))
100 (let* ((n (array-dimension a
0))
101 (m (array-dimension b
1))
102 (p (array-dimension a
1))
103 (c (make-array (list n m
)))
105 (declare (fixnum n m p
))
114 (* (aref a i k
) (aref b k j
)))))
115 (setf (aref c i j
) x
)))
118 (number (aref c
0 0))
119 (t (coerce (compound-data-seq c
) rtype
)))))))
121 (defun identity-matrix (n)
123 Returns the identity matrix of rank N."
124 (let ((result (make-array (list n n
) :initial-element
0)))
125 (dotimes (i n result
)
127 (setf (aref result i i
) 1))))
129 ;; this thing is not very efficient at this point - too much coercing
132 If X is a matrix, returns the diagonal of X. If X is a sequence, returns a
133 diagonal matrix of rank (length X) with diagonal elements eq to the elements
136 (let* ((n (min (num-rows x
) (num-cols x
)))
137 (result (make-array n
)))
138 (dotimes (i n
(coerce result
'list
))
139 (setf (aref result i
) (aref x i i
)))))
141 (let* ((x (coerce x
'vector
))
143 (result (make-array (list n n
) :initial-element
0)))
144 (dotimes (i n result
)
145 (setf (aref result i i
) (aref x i
)))))
146 (t (error "argument must be a matrix or a sequence"))))
150 Returns a list of the rows of M as vectors"
152 (let ((m (num-rows x
))
155 (declare (fixnum m n
))
158 (let ((row (make-array n
)))
161 (setf (aref row i
) (aref x k i
))))))
162 (dotimes (i m result
)
164 (setf result
(cons (get-row (- m i
1)) result
))))))
166 (defun column-list (x)
168 Returns a list of the columns of M as vectors"
170 (let ((m (num-rows x
))
173 (declare (fixnum m n
))
176 (let ((col (make-array m
)))
179 (setf (aref col i
) (aref x i k
))))))
180 (dotimes (i n result
)
182 (setf result
(cons (get-col (- n i
1)) result
))))))
184 (defun inner-product (x y
)
186 Returns inner product of sequences X and Y."
190 (cx (make-next-element x
))
191 (cy (make-next-element y
))
194 (if (/= n
(length y
)) (error "sequence lengths do not match"))
195 (dotimes (i n result
)
198 (+ result
(* (get-next-element cx i
) (get-next-element cy i
)))))))
200 (defun outer-product (x y
&optional
(f #'*))
201 "Args: (x y &optional (fcn #'*))
202 Returns the generalized outer product of x and y, using fcn. Tat is, the result
203 is a matrix of dimension ((length x) (length y)) and the (i j) element of the
204 result is computed as (apply fcn (aref x i) (aref y j))."
205 (let* ((x (coerce x
'vector
))
206 (y (coerce y
'vector
))
209 (a (make-array (list m n
))))
210 (declare (fixnum m n
))
215 (setf (aref a i j
) (funcall f
(aref x i
) (aref y j
)))))))
217 (defun cross-product (x)
219 If X is a matrix returns (matmult (transpose X) X). If X is a vector returns
220 (inner-product X X)."
222 (let* ((n (num-rows x
))
224 (c (make-array (list p p
))))
225 (declare (fixnum n p
))
233 (incf val
(* (aref x k i
) (aref x k j
))))
234 (setf (aref c i j
) val
)
235 (setf (aref c j i
) val
))))))
237 (defun transpose-list (x)
238 (let ((m (length (first x
))))
240 (if (not (consp next
)) (error "not a list - ~a" x
))
241 (if (/= m
(length next
)) (error "sublists not the same length")))
242 (do* ((cx (copy-list x
))
243 (result (make-list m
))
244 (next result
(cdr next
)))
246 (setf (first next
) (mapcar #'first cx
))
247 (do ((next cx
(cdr next
)))
249 (setf (first next
) (rest (first next
)))))))
253 Returns the transpose of the matrix M."
255 ((consp x
) (transpose-list x
))
258 (let* ((m (num-rows x
))
260 (tx (make-array (list n m
))))
261 (declare (fixnum m n
))
266 (setf (aref tx j i
) (aref x i j
))))))))
268 (defun bind-columns (&rest args
)
270 The ARGS can be matrices, vectors, or lists. Arguments are bound into a matrix
272 Example: (bind-columns #2a((1 2)(3 4)) #(5 6)) returns #2a((1 2 5)(3 4 6))"
273 (flet ((check-arg (x)
274 (if (not (or (sequencep x
) (matrixp x
)))
275 (error "bad argument type")))
276 (arg-cols (x) (if (sequencep x
) 1 (num-cols x
)))
277 (arg-rows (x) (if (sequencep x
) (length x
) (num-rows x
))))
278 (dolist (x args
) (check-arg x
))
279 (let ((m (arg-rows (first args
)))
280 (n (arg-cols (first args
))))
281 (declare (fixnum m n
))
282 (dolist (x (rest args
))
283 (if (/= m
(arg-rows x
)) (error "column lengths do not match"))
284 (incf n
(arg-cols x
)))
285 (do* ((result (make-array (list m n
)))
286 (args args
(rest args
))
288 (x (first args
) (first args
)))
292 (let ((cx (make-next-element x
)))
294 (setf (aref result i firstcol
) (get-next-element cx i
)))))
296 (let ((k (arg-cols x
)))
299 (setf (aref result i
(+ firstcol j
)) (aref x i j
)))))))
300 (incf firstcol
(arg-cols x
))))))
302 (defun bind-rows (&rest args
)
304 The ARGS can be matrices, vectors, or lists. Arguments are bound into a matrix
306 Example: (bind-rows #2a((1 2)(3 4)) #(5 6)) returns #2a((1 2)(3 4)(5 6))"
307 (flet ((check-arg (x)
308 (if (not (or (sequencep x
) (matrixp x
)))
309 (error "bad argument type")))
310 (arg-cols (x) (if (sequencep x
) (length x
) (num-cols x
)))
311 (arg-rows (x) (if (sequencep x
) 1 (num-rows x
))))
312 (dolist (x args
) (check-arg x
))
313 (let ((m (arg-rows (first args
)))
314 (n (arg-cols (first args
))))
315 (declare (fixnum m n
))
316 (dolist (x (rest args
))
317 (if (/= n
(arg-cols x
)) (error "row lengths do not match"))
318 (incf m
(arg-rows x
)))
319 (do* ((result (make-array (list m n
)))
320 (args args
(rest args
))
322 (x (first args
) (first args
)))
326 (let ((cx (make-next-element x
)))
328 (setf (aref result firstrow i
) (get-next-element cx i
)))))
330 (let ((k (arg-rows x
)))
333 (setf (aref result
(+ firstrow j
) i
) (aref x j i
)))))))
334 (incf firstrow
(arg-rows x
))))))