2 ;;; Copyright (c) 2005--2007, by A.J. Rossini <blindglobe@gmail.com>
3 ;;; See COPYRIGHT file for any additional restrictions (BSD license).
4 ;;; Since 1991, ANSI was finally finished. Edited for ANSI Common Lisp.
6 ;;;; lsbasics -- Low level Lisp-Stat functions
8 ;;;; Copyright (c) 1991, by Luke Tierney. Permission is granted for
15 (defpackage :lisp-stat-basics
17 :lisp-stat-object-system
22 :lisp-stat-compound-data
26 (:shadowing-import-from
:lisp-stat-object-system
27 slot-value call-method call-next-method
)
28 (:shadowing-import-from
:lisp-stat-types
32 copy-vector copy-array which repeat
33 permute-array sum prod count-elements mean if-else sample
37 ;; matrixp num-rows num-cols matmult identity-matrix diagonal row-list
38 ;; column-list inner-product outer-product cross-product transpose
39 ;; bind-columns bind-rows
42 ;; chol-decomp lu-decomp lu-solve determinant inverse sv-decomp
43 ;; qr-decomp rcondest make-rotation
44 ;; fft make-sweep-matrix sweep-operator ax+y numgrad numhess
47 ;; in linalg.lisp, possibly not supported by matlisp
48 spline kernel-dens kernel-smooth
50 make-rv-function make-rv-function-1
52 log-gamma uniform-rand normal-cdf normal-quant normal-dens
53 normal-rand bivnorm-cdf cauchy-cdf cauchy-quant cauchy-dens
54 cauchy-rand gamma-cdf gamma-quant gamma-dens gamma-rand
55 chisq-cdf chisq-quant chisq-dens chisq-rand beta-cdf beta-quant
56 beta-dens beta-rand t-cdf t-quant t-dens t-rand f-cdf f-quant
57 f-dens f-rand poisson-cdf poisson-quant poisson-pmf poisson-rand
58 binomial-cdf binomial-quant binomial-pmf binomial-rand
63 (in-package #:lisp-stat-basics
)
66 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
68 ;;;; Copying Functions
70 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
73 ;;; COPY-VECTOR function
76 (defun copy-vector (x)
78 Returns a copy of the vector X"
82 ;;; COPY-ARRAY function
87 Returns a copy of the array A"
88 (vector-to-array (copy-seq (array-data-vector a
))
89 (array-dimensions a
)))
91 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
93 ;;;; Sequence Functions
95 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
104 Returns a list of the indices where elements of sequence X are not NIL."
105 (let ((x (list (compound-data-seq x
)))
108 (flet ((add-result (x)
109 (if result
(setf (rest tail
) (list x
)) (setf result
(list x
)))
110 (setf tail
(if tail
(rest tail
) result
)))
111 (get-next-element (seq-list i
)
112 (cond ((consp (first seq-list
))
113 (let ((elem (first (first seq-list
))))
114 (setf (first seq-list
) (rest (first seq-list
)))
116 (t (aref (first seq-list
) i
)))))
117 (let ((n (length (first x
))))
118 (dotimes (i n result
)
119 (if (get-next-element x i
) (add-result i
)))))))
127 Repeats VALS. If TIMES is a number and VALS is a non-null, non-array atom,
128 a list of length TIMES with all elements eq to VALS is returned. If VALS
129 is a list and TIMES is a number then VALS is appended TIMES times. If
130 TIMES is a list of numbers then VALS must be a list of equal length and
131 the simpler version of repeat is mapped down the two lists.
132 Examples: (repeat 2 5) returns (2 2 2 2 2)
133 (repeat '(1 2) 3) returns (1 2 1 2 1 2)
134 (repeat '(4 5 6) '(1 2 3)) returns (4 5 5 6 6 6)
135 (repeat '((4) (5 6)) '(2 3)) returns (4 4 5 6 5 6 5 6)"
136 (cond ((compound-data-p b
)
137 (let* ((reps (coerce (compound-data-seq (map-elements #'repeat a b
))
139 (result (first reps
))
140 (tail (last (first reps
))))
141 (dolist (next (rest reps
) result
)
143 (setf (rest tail
) next
)
144 (setf tail
(last next
))))))
145 (t (let* ((a (if (compound-data-p a
)
146 (coerce (compound-data-seq a
) 'list
)
149 (dotimes (i b result
)
150 (let ((next (copy-list a
)))
151 (if result
(setf (rest (last next
)) result
))
152 (setf result next
)))))))
154 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
156 ;;;; Subset Selection and Mutation Functions
158 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
160 ;;;; is x an ordered sequence of nonnegative positive integers?
161 (defun ordered-nneg-seq(x)
164 (cx (make-next-element x
))
167 (let ((elem (check-nonneg-fixnum (get-next-element cx i
))))
168 (if (> m elem
) (return nil
) (setf m elem
)))))))
170 ;;;; select or set the subsequence corresponding to the specified indices
171 (defun sequence-select(x indices
&optional
(values nil set-values
))
177 (declare (fixnum rlen dlen vlen
))
179 ;; Check the input data
181 (check-sequence indices
)
182 (if set-values
(check-sequence values
))
184 ;; Find the data sizes
185 (setf data
(if (ordered-nneg-seq indices
) x
(coerce x
'vector
)))
186 (setf dlen
(length data
))
187 (setf rlen
(length indices
))
189 (setf vlen
(length values
))
190 (if (/= vlen rlen
) (error "value and index sequences do not match")))
192 ;; set up the result/value sequence
196 (make-sequence (if (listp x
) 'list
'vector
) rlen
)))
198 ;; get or set the sequence elements
201 (cr (make-next-element result
))
202 (ci (make-next-element indices
))
207 (declare (fixnum i j index
))
208 (setf index
(get-next-element ci i
))
209 (if (<= dlen index
) (error "index out of range - ~a" index
))
210 (let ((elem (get-next-element cr i
)))
217 ((not (and (< j index
) (consp nextx
))))
219 (setf nextx
(rest nextx
)))
220 (setf (first nextx
) elem
))
221 (t (setf (aref x index
) elem
)))))
223 (cr (make-next-element result
))
224 (ci (make-next-element indices
))
230 (declare (fixnum i j index
))
231 (setf index
(get-next-element ci i
))
232 (if (<= dlen index
) (error "index out of range - ~a" index
))
234 ((listp data
) ;; indices must be ordered
236 ((not (and (< j index
) (consp nextx
))))
238 (setf nextx
(rest nextx
)))
239 (setf elem
(first nextx
)))
240 (t (setf elem
(aref data index
))))
241 (set-next-element cr i elem
)))
245 (defun old-rowmajor-index (index indices dim olddim
)
246 "translate row major index in resulting subarray to row major index
247 in the original array."
248 (declare (fixnum index
))
249 (let ((rank (length dim
))
253 (declare (fixnum rank face oldface
))
257 (setf face
(* face
(aref dim i
)))
258 (setf oldface
(* oldface
(aref olddim i
))))
262 (setf face
(/ face
(aref dim i
)))
263 (setf oldface
(/ oldface
(aref olddim i
)))
265 (* oldface
(aref (aref indices i
) (floor (/ index face
))))) ;;*** is this floor really needed???
266 (setf index
(rem index face
)))
269 (defun subarray-select (a indexlist
&optional
(values nil set_values
))
270 "extract or set subarray for the indices from a displaced array."
282 (declare (fixnum rank n
))
284 (if (or (sequencep a
) (not (arrayp a
))) (error "not an array - ~a" a
))
285 (if (not (listp indexlist
)) (error "bad index list - ~a" indices
))
286 (if (/= (length indexlist
) (array-rank a
))
287 (error "wrong number of indices"))
289 (setf indices
(coerce indexlist
'vector
))
291 (setf olddim
(coerce (array-dimensions a
) 'vector
))
293 ;; compute the result dimension vector and fix up the indices
294 (setf rank
(array-rank a
))
295 (setf dim
(make-array rank
))
298 (setf index
(aref indices i
))
299 (setf n
(aref olddim i
))
300 (setf index
(if (fixnump index
) (vector index
) (coerce index
'vector
)))
301 (setf k
(length index
))
304 (if (<= n
(check-nonneg-fixnum (aref index j
)))
305 (error "index out of bounds - ~a" (aref index j
)))
306 (setf (aref indices i
) index
))
307 (setf (aref dim i
) (length index
)))
309 ;; set up the result or check the values
310 (let ((dim-list (coerce dim
'list
)))
314 ((compound-data-p values
)
315 (if (or (not (arrayp values
)) (/= rank
(array-rank values
)))
316 (error "bad values array - ~a" values
))
317 (setf vdim
(coerce (array-dimensions values
) 'vector
))
320 (if (/= (aref vdim i
) (aref dim i
))
321 (error "bad value array dimensions - ~a" values
)))
322 (setf result values
))
323 (t (setf result
(make-array dim-list
:initial-element values
)))))
324 (t (setf result
(make-array dim-list
)))))
326 ;; compute the result or set the values
327 (setf data
(compound-data-seq a
))
328 (setf result_data
(compound-data-seq result
))
329 (setf n
(length result_data
))
332 (setf k
(old-rowmajor-index i indices dim olddim
))
333 (if (or (> 0 k
) (>= k
(length data
))) (error "index out of range"))
335 (setf (aref data k
) (aref result_data i
))
336 (setf (aref result_data i
) (aref data k
))))
344 (defun select (x &rest args
)
345 "Args: (a &rest indices)
346 A can be a list or an array. If A is a list and INDICES is a single number
347 then the appropriate element of A is returned. If is a list and INDICES is
348 a list of numbers then the sublist of the corresponding elements is returned.
349 If A in an array then the number of INDICES must match the ARRAY-RANK of A.
350 If each index is a number then the appropriate array element is returned.
351 Otherwise the INDICES must all be lists of numbers and the corresponding
352 submatrix of A is returned. SELECT can be used in setf."
354 ((every #'fixnump args
)
355 (if (listp x
) (nth (first args
) x
) (apply #'aref x args
)))
356 ((sequencep x
) (sequence-select x
(first args
)))
357 (t (subarray-select x args
))))
360 ;; Built in SET-SELECT (SETF method for SELECT)
361 (defun set-select (x &rest args
)
362 (let ((indices (butlast args
))
363 (values (first (last args
))))
366 (if (not (consp indices
)) (error "bad indices - ~a" indices
))
367 (let* ((indices (first indices
))
368 (i-list (if (fixnump indices
) (list indices
) indices
))
369 (v-list (if (fixnump indices
) (list values
) values
)))
370 (sequence-select x i-list v-list
)))
372 (subarray-select x indices values
))
373 (t (error "bad argument type - ~a" x
)))
376 (defsetf select set-select
)
379 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
381 ;;;; Array Permutation Functions
383 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
385 (defun permute-indices (x y perm check
)
386 "Args: (x y perm check).
387 permute x into y using perm; all should be vectors; If check is TRUE
388 the routine will check to make sure no indices are reused, but x
390 (let ((rank (length x
)))
391 (declare (fixnum rank
))
394 (let ((k (aref perm i
)))
395 (if (not (fixnump k
)) (error "bad permutation sequence - ~a" perm
))
396 (if (or (< k
0) (>= k rank
))
397 (error "bad permutation sequence - ~a" perm
))
398 (setf (aref y i
) (aref x k
))
399 ;; to insure dimensions are not re-used
400 (if check
(setf (aref x k
) NIL
))))))
402 (defun indices-from-rowmajor (a k result
)
404 Compute indices in a from rowmajor index k, put in vector result."
407 (if (not (arrayp a
)) (error "not an array - ~a" a
))
408 (if (or (> 0 k
) (>= k
(array-total-size a
))) (error "index out of range"))
411 (rank (array-rank a
))
412 (dim (array-dimensions a
)))
413 (declare (fixnum face rank
))
414 (let ((cdim (make-next-element dim
)))
417 (setf face
(* face
(get-next-element cdim i
)))))
418 (let ((cdim (make-next-element dim
)))
420 (setf face
(/ face
(get-next-element cdim i
)))
421 (setf (aref result i
) (floor (/ k face
)))
422 (setf k
(rem k face
))))))
424 (defun translate-index (i result x perm indices oldindices ilist
)
425 "Args: (i result x perm indices oldindices ilist).
426 Translate row major index in original array to row major index in new
427 array. Use indices vectors and ilist for temporary storage."
429 (let ((rank (array-rank x
)))
430 (declare (fixnum rank
))
431 (indices-from-rowmajor x i oldindices
)
432 (permute-indices oldindices indices perm nil
)
433 (do ((next ilist
(rest next
))
435 ((not (and (< k rank
) (consp next
))))
436 (setf (first next
) (aref indices k
)))
437 (apply #'array-row-major-index result ilist
)))
439 (defun permute-array (x perm
)
441 Returns a copy of the array A permuted according to the permutation P."
442 (if (not (arrayp x
)) (error "not an array - ~a" x
))
443 (check-sequence perm
)
444 (if (/= (length perm
) (array-rank x
))
445 (error "bad permutation sequence - ~a" perm
))
446 (let* ((perm (coerce perm
'vector
))
447 (rank (array-rank x
))
448 (dim (make-array rank
))
449 (olddim (coerce (array-dimensions x
) 'vector
)))
450 (declare (fixnum rank
))
451 ;; construct new dimension vector
452 (permute-indices olddim dim perm t
)
453 ;; make result array and the index vectors and lists */
454 (let* ((result (make-array (coerce dim
'list
)))
455 (indices (make-array rank
))
456 (oldindices (make-array rank
))
457 (ilist (make-list rank
))
458 (data (compound-data-seq x
))
459 (result_data (compound-data-seq result
))
464 (setf (aref oldindices i
) (list nil
)))
465 ;; fill in the result
466 (if (/= n
(length result_data
)) (error "bad data"))
467 (dotimes (i n result
)
469 (let ((k (translate-index i result x perm indices oldindices ilist
)))
471 (setf (aref result_data k
) (aref data i
)))))))
473 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
475 ;;;; SUM, PROD, COUNT-ELEMENTS, and MEAN Functions
477 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
482 (let ((seq (compound-data-seq x
))
486 (setf sum
(+ sum
(if (numberp x
) x
(sum-1 x
)))))
487 (let ((n (length seq
)))
491 (let ((x (aref seq i
)))
492 (setf sum
(+ sum
(if (numberp x
) x
(sum-1 x
)))))))))))
494 (defun sum (&rest args
)
495 "Args: (&rest number-data)
496 Returns the sum of all the elements of its arguments. Returns 0 if there
497 are no arguments. Vector reducing."
499 (sum-1 (if (rest args
) args
(first args
)))
505 (let ((seq (compound-data-seq x
))
509 (setf prod
(* prod
(if (numberp x
) x
(prod-1 x
)))))
510 (let ((n (length seq
)))
514 (let ((x (aref seq i
)))
515 (setf prod
(* prod
(if (numberp x
) x
(prod-1 x
)))))))))))
517 (defun prod (&rest args
)
518 "Args: (&rest number-data)
519 Returns the product of all the elements of its arguments. Returns 1 if there
520 are no arguments. Vector reducing."
522 (prod-1 (if (rest args
) args
(first args
)))
525 (defun count-elements (x)
526 "Args: (number &rest more-numbers)
527 Returns the number of its arguments. Vector reducing"
528 (if (compound-data-p x
)
529 (let ((seq (compound-data-seq x
))
532 (dolist (x seq count
)
533 (incf count
(if (compound-data-p x
) (count-elements x
) 1)))
534 (let ((n (length seq
)))
538 (let ((x (aref seq i
)))
539 (incf count
(if (compound-data-p x
) (count-elements x
) 1)))))))
543 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
545 ;;;; IF-ELSE Functions
547 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
549 (defun if-else (a x y
)
551 Takes simple or compound data items FIRST, X and Y and returns result of
552 elementswise selecting from X if FIRST is not NIL and from Y otherwise."
553 (flet ((base-if-else (a x y
) (if a x y
)))
554 (recursive-map-elements #'base-if-else
#'if-else a x y
)))