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
21 :lisp-stat-compound-data
22 :lisp-stat-matrix
;; ??
24 (:shadowing-import-from
:lisp-stat-object-system
25 slot-value call-method call-next-method
)
28 copy-vector copy-array which repeat
29 permute-array sum prod count-elements mean if-else sample sort-data
33 ;; matrixp num-rows num-cols matmult identity-matrix diagonal row-list
34 ;; column-list inner-product outer-product cross-product transpose
35 ;; bind-columns bind-rows
38 chol-decomp lu-decomp lu-solve determinant inverse sv-decomp
39 qr-decomp rcondest make-rotation
40 fft make-sweep-matrix sweep-operator ax
+y numgrad numhess
42 ;; in linalg.lisp, possibly not supported by matlisp
43 spline kernel-dens kernel-smooth
45 make-rv-function make-rv-function-1
47 log-gamma uniform-rand normal-cdf normal-quant normal-dens
48 normal-rand bivnorm-cdf cauchy-cdf cauchy-quant cauchy-dens
49 cauchy-rand gamma-cdf gamma-quant gamma-dens gamma-rand
50 chisq-cdf chisq-quant chisq-dens chisq-rand beta-cdf beta-quant
51 beta-dens beta-rand t-cdf t-quant t-dens t-rand f-cdf f-quant
52 f-dens f-rand poisson-cdf poisson-quant poisson-pmf poisson-rand
53 binomial-cdf binomial-quant binomial-pmf binomial-rand
58 ;; (defpackage :lisp-stat-basics
59 ;; (:nicknames :ls-basics)
60 ;; (:use ;; :common-lisp
61 ;; :lisp-stat-object-system
64 ;; ;;(:shadowing-import-from (package-shadowing-symbols #:lisp-stat-object-system))
68 ;; sequencep copy-vector copy-array iseq which repeat select
69 ;; permute-array sum prod count-elements mean if-else
70 ;; sample sort-data order rank
73 ;; ;; #+ kcl (export '(function-lambda-expression realp fixnump))
77 ;; compound-data-p map-elements compound-data-seq
78 ;; compound-data-length element-seq compound-data-proto
81 ;; log-gamma uniform-rand normal-cdf normal-quant normal-dens
82 ;; normal-rand bivnorm-cdf cauchy-cdf cauchy-quant cauchy-dens
83 ;; cauchy-rand gamma-cdf gamma-quant gamma-dens gamma-rand
84 ;; chisq-cdf chisq-quant chisq-dens chisq-rand beta-cdf beta-quant
85 ;; beta-dens beta-rand t-cdf t-quant t-dens t-rand f-cdf f-quant
86 ;; f-dens f-rand poisson-cdf poisson-quant poisson-pmf poisson-rand
87 ;; binomial-cdf binomial-quant binomial-pmf binomial-rand
91 ;; chol-decomp lu-decomp lu-solve determinant inverse sv-decomp
92 ;; qr-decomp rcondest make-rotation spline kernel-dens kernel-smooth
93 ;; fft make-sweep-matrix sweep-operator ax+y numgrad numhess
97 ;; matrixp num-rows num-cols matmult identity-matrix diagonal
98 ;; row-list column-list inner-product outer-product cross-product
99 ;; transpose bind-columns bind-rows
103 ;; +stat-float-typing+ +stat-cfloat-typing+ +stat-float-template+
108 ;; ;; (import '(ccl:def-logical-directory ccl:ff-load ccl:deffcfun ccl:defccallable))
112 (in-package #:lisp-stat-basics
)
114 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
116 ;;;; Type Checking Functions
118 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
122 Returns T if X is a fixnum; NIL otherwise."
123 (declare (inline typep
))
126 (defun check-nonneg-fixnum (x)
127 (if (and (fixnump x
) (<= 0 x
)) x
(error "not a non-negative fixnum")))
129 (defun check-one-fixnum (x)
130 (if (not (fixnump x
)) (error "not a fixnum - ~a" x
)))
132 (defun check-one-real (a)
133 (if (not (or (rationalp a
) (floatp a
)))
134 (error "not a real number ~s" a
)
137 (defun check-one-number (a)
138 (if (not (numberp a
))
139 (error "not a number ~s" a
)
142 (defun check-sequence (a)
143 (if (not (or (vectorp a
) (consp a
))) (error "not a sequence - ~s" a
)))
145 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
147 ;;;; Sequence Element Access
149 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
151 (defun get-next-element (x i
)
152 "Get element i from seq x. FIXME: not really??"
153 (let ((myseq (first x
)))
155 (let ((elem (first myseq
)))
156 (setf (first x
) (rest myseq
))
160 (defun set-next-element (x i v
)
161 (let ((seq (first x
)))
164 (setf (first x
) (rest seq
)))
165 (t (setf (aref seq i
) v
)))))
167 (defun make-next-element (x) (list x
))
169 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
171 ;;;; Array to Row-Major Data Vector Conversion Functions
173 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
175 (defun array-data-vector (a)
177 Displaces array A to a vector"
178 (make-array (array-total-size a
)
180 :element-type
(array-element-type a
)))
182 (defun vector-to-array (v dims
)
184 Displaces vector V to array with dimensions DIMS"
187 :element-type
(array-element-type v
)))
189 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
191 ;;;; Copying Functions
193 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
196 ;;; COPY-VECTOR function
199 (defun copy-vector (x)
201 Returns a copy of the vector X"
205 ;;; COPY-ARRAY function
208 (defun copy-array (a)
210 Returns a copy of the array A"
211 (vector-to-array (copy-seq (array-data-vector a
))
212 (array-dimensions a
)))
214 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
216 ;;;; Sequence Functions
218 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
227 Returns a list of the indices where elements of sequence X are not NIL."
228 (let ((x (list (compound-data-seq x
)))
231 (flet ((add-result (x)
232 (if result
(setf (rest tail
) (list x
)) (setf result
(list x
)))
233 (setf tail
(if tail
(rest tail
) result
)))
234 (get-next-element (seq-list i
)
235 (cond ((consp (first seq-list
))
236 (let ((elem (first (first seq-list
))))
237 (setf (first seq-list
) (rest (first seq-list
)))
239 (t (aref (first seq-list
) i
)))))
240 (let ((n (length (first x
))))
241 (dotimes (i n result
)
242 (if (get-next-element x i
) (add-result i
)))))))
250 Repeats VALS. If TIMES is a number and VALS is a non-null, non-array atom,
251 a list of length TIMES with all elements eq to VALS is returned. If VALS
252 is a list and TIMES is a number then VALS is appended TIMES times. If
253 TIMES is a list of numbers then VALS must be a list of equal length and
254 the simpler version of repeat is mapped down the two lists.
255 Examples: (repeat 2 5) returns (2 2 2 2 2)
256 (repeat '(1 2) 3) returns (1 2 1 2 1 2)
257 (repeat '(4 5 6) '(1 2 3)) returns (4 5 5 6 6 6)
258 (repeat '((4) (5 6)) '(2 3)) returns (4 4 5 6 5 6 5 6)"
259 (cond ((compound-data-p b
)
260 (let* ((reps (coerce (compound-data-seq (map-elements #'repeat a b
))
262 (result (first reps
))
263 (tail (last (first reps
))))
264 (dolist (next (rest reps
) result
)
266 (setf (rest tail
) next
)
267 (setf tail
(last next
))))))
268 (t (let* ((a (if (compound-data-p a
)
269 (coerce (compound-data-seq a
) 'list
)
272 (dotimes (i b result
)
273 (let ((next (copy-list a
)))
274 (if result
(setf (rest (last next
)) result
))
275 (setf result next
)))))))
277 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
279 ;;;; Subset Selection and Mutation Functions
281 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
283 ;;;; is x an ordered sequence of nonnegative positive integers?
284 (defun ordered-nneg-seq(x)
287 (cx (make-next-element x
))
290 (let ((elem (check-nonneg-fixnum (get-next-element cx i
))))
291 (if (> m elem
) (return nil
) (setf m elem
)))))))
293 ;;;; select or set the subsequence corresponding to the specified indices
294 (defun sequence-select(x indices
&optional
(values nil set-values
))
300 (declare (fixnum rlen dlen vlen
))
302 ;; Check the input data
304 (check-sequence indices
)
305 (if set-values
(check-sequence values
))
307 ;; Find the data sizes
308 (setf data
(if (ordered-nneg-seq indices
) x
(coerce x
'vector
)))
309 (setf dlen
(length data
))
310 (setf rlen
(length indices
))
312 (setf vlen
(length values
))
313 (if (/= vlen rlen
) (error "value and index sequences do not match")))
315 ;; set up the result/value sequence
319 (make-sequence (if (listp x
) 'list
'vector
) rlen
)))
321 ;; get or set the sequence elements
324 (cr (make-next-element result
))
325 (ci (make-next-element indices
))
330 (declare (fixnum i j index
))
331 (setf index
(get-next-element ci i
))
332 (if (<= dlen index
) (error "index out of range - ~a" index
))
333 (let ((elem (get-next-element cr i
)))
340 ((not (and (< j index
) (consp nextx
))))
342 (setf nextx
(rest nextx
)))
343 (setf (first nextx
) elem
))
344 (t (setf (aref x index
) elem
)))))
346 (cr (make-next-element result
))
347 (ci (make-next-element indices
))
353 (declare (fixnum i j index
))
354 (setf index
(get-next-element ci i
))
355 (if (<= dlen index
) (error "index out of range - ~a" index
))
357 ((listp data
) ;; indices must be ordered
359 ((not (and (< j index
) (consp nextx
))))
361 (setf nextx
(rest nextx
)))
362 (setf elem
(first nextx
)))
363 (t (setf elem
(aref data index
))))
364 (set-next-element cr i elem
)))
368 (defun old-rowmajor-index (index indices dim olddim
)
369 "translate row major index in resulting subarray to row major index
370 in the original array."
371 (declare (fixnum index
))
372 (let ((rank (length dim
))
376 (declare (fixnum rank face oldface
))
380 (setf face
(* face
(aref dim i
)))
381 (setf oldface
(* oldface
(aref olddim i
))))
385 (setf face
(/ face
(aref dim i
)))
386 (setf oldface
(/ oldface
(aref olddim i
)))
388 (* oldface
(aref (aref indices i
) (floor (/ index face
))))) ;;*** is this floor really needed???
389 (setf index
(rem index face
)))
392 (defun subarray-select (a indexlist
&optional
(values nil set_values
))
393 "extract or set subarray for the indices from a displaced array."
405 (declare (fixnum rank n
))
407 (if (or (sequencep a
) (not (arrayp a
))) (error "not an array - ~a" a
))
408 (if (not (listp indexlist
)) (error "bad index list - ~a" indices
))
409 (if (/= (length indexlist
) (array-rank a
))
410 (error "wrong number of indices"))
412 (setf indices
(coerce indexlist
'vector
))
414 (setf olddim
(coerce (array-dimensions a
) 'vector
))
416 ;; compute the result dimension vector and fix up the indices
417 (setf rank
(array-rank a
))
418 (setf dim
(make-array rank
))
421 (setf index
(aref indices i
))
422 (setf n
(aref olddim i
))
423 (setf index
(if (fixnump index
) (vector index
) (coerce index
'vector
)))
424 (setf k
(length index
))
427 (if (<= n
(check-nonneg-fixnum (aref index j
)))
428 (error "index out of bounds - ~a" (aref index j
)))
429 (setf (aref indices i
) index
))
430 (setf (aref dim i
) (length index
)))
432 ;; set up the result or check the values
433 (let ((dim-list (coerce dim
'list
)))
437 ((compound-data-p values
)
438 (if (or (not (arrayp values
)) (/= rank
(array-rank values
)))
439 (error "bad values array - ~a" values
))
440 (setf vdim
(coerce (array-dimensions values
) 'vector
))
443 (if (/= (aref vdim i
) (aref dim i
))
444 (error "bad value array dimensions - ~a" values
)))
445 (setf result values
))
446 (t (setf result
(make-array dim-list
:initial-element values
)))))
447 (t (setf result
(make-array dim-list
)))))
449 ;; compute the result or set the values
450 (setf data
(compound-data-seq a
))
451 (setf result_data
(compound-data-seq result
))
452 (setf n
(length result_data
))
455 (setf k
(old-rowmajor-index i indices dim olddim
))
456 (if (or (> 0 k
) (>= k
(length data
))) (error "index out of range"))
458 (setf (aref data k
) (aref result_data i
))
459 (setf (aref result_data i
) (aref data k
))))
467 (defun select (x &rest args
)
468 "Args: (a &rest indices)
469 A can be a list or an array. If A is a list and INDICES is a single number
470 then the appropriate element of A is returned. If is a list and INDICES is
471 a list of numbers then the sublist of the corresponding elements is returned.
472 If A in an array then the number of INDICES must match the ARRAY-RANK of A.
473 If each index is a number then the appropriate array element is returned.
474 Otherwise the INDICES must all be lists of numbers and the corresponding
475 submatrix of A is returned. SELECT can be used in setf."
477 ((every #'fixnump args
)
478 (if (listp x
) (nth (first args
) x
) (apply #'aref x args
)))
479 ((sequencep x
) (sequence-select x
(first args
)))
480 (t (subarray-select x args
))))
483 ;; Built in SET-SELECT (SETF method for SELECT)
484 (defun set-select (x &rest args
)
485 (let ((indices (butlast args
))
486 (values (first (last args
))))
489 (if (not (consp indices
)) (error "bad indices - ~a" indices
))
490 (let* ((indices (first indices
))
491 (i-list (if (fixnump indices
) (list indices
) indices
))
492 (v-list (if (fixnump indices
) (list values
) values
)))
493 (sequence-select x i-list v-list
)))
495 (subarray-select x indices values
))
496 (t (error "bad argument type - ~a" x
)))
499 (defsetf select set-select
)
502 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
504 ;;;; Array Permutation Functions
506 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
508 (defun permute-indices (x y perm check
)
509 "Args: (x y perm check).
510 permute x into y using perm; all should be vectors; If check is TRUE
511 the routine will check to make sure no indices are reused, but x
513 (let ((rank (length x
)))
514 (declare (fixnum rank
))
517 (let ((k (aref perm i
)))
518 (if (not (fixnump k
)) (error "bad permutation sequence - ~a" perm
))
519 (if (or (< k
0) (>= k rank
))
520 (error "bad permutation sequence - ~a" perm
))
521 (setf (aref y i
) (aref x k
))
522 ;; to insure dimensions are not re-used
523 (if check
(setf (aref x k
) NIL
))))))
525 (defun indices-from-rowmajor (a k result
)
527 Compute indices in a from rowmajor index k, put in vector result."
530 (if (not (arrayp a
)) (error "not an array - ~a" a
))
531 (if (or (> 0 k
) (>= k
(array-total-size a
))) (error "index out of range"))
534 (rank (array-rank a
))
535 (dim (array-dimensions a
)))
536 (declare (fixnum face rank
))
537 (let ((cdim (make-next-element dim
)))
540 (setf face
(* face
(get-next-element cdim i
)))))
541 (let ((cdim (make-next-element dim
)))
543 (setf face
(/ face
(get-next-element cdim i
)))
544 (setf (aref result i
) (floor (/ k face
)))
545 (setf k
(rem k face
))))))
547 (defun translate-index (i result x perm indices oldindices ilist
)
548 "Args: (i result x perm indices oldindices ilist).
549 Translate row major index in original array to row major index in new
550 array. Use indices vectors and ilist for temporary storage."
552 (let ((rank (array-rank x
)))
553 (declare (fixnum rank
))
554 (indices-from-rowmajor x i oldindices
)
555 (permute-indices oldindices indices perm nil
)
556 (do ((next ilist
(rest next
))
558 ((not (and (< k rank
) (consp next
))))
559 (setf (first next
) (aref indices k
)))
560 (apply #'array-row-major-index result ilist
)))
562 (defun permute-array (x perm
)
564 Returns a copy of the array A permuted according to the permutation P."
565 (if (not (arrayp x
)) (error "not an array - ~a" x
))
566 (check-sequence perm
)
567 (if (/= (length perm
) (array-rank x
))
568 (error "bad permutation sequence - ~a" perm
))
569 (let* ((perm (coerce perm
'vector
))
570 (rank (array-rank x
))
571 (dim (make-array rank
))
572 (olddim (coerce (array-dimensions x
) 'vector
)))
573 (declare (fixnum rank
))
574 ;; construct new dimension vector
575 (permute-indices olddim dim perm t
)
576 ;; make result array and the index vectors and lists */
577 (let* ((result (make-array (coerce dim
'list
)))
578 (indices (make-array rank
))
579 (oldindices (make-array rank
))
580 (ilist (make-list rank
))
581 (data (compound-data-seq x
))
582 (result_data (compound-data-seq result
))
587 (setf (aref oldindices i
) (list nil
)))
588 ;; fill in the result
589 (if (/= n
(length result_data
)) (error "bad data"))
590 (dotimes (i n result
)
592 (let ((k (translate-index i result x perm indices oldindices ilist
)))
594 (setf (aref result_data k
) (aref data i
)))))))
596 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
598 ;;;; SUM, PROD, COUNT-ELEMENTS, and MEAN Functions
600 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
605 (let ((seq (compound-data-seq x
))
609 (setf sum
(+ sum
(if (numberp x
) x
(sum-1 x
)))))
610 (let ((n (length seq
)))
614 (let ((x (aref seq i
)))
615 (setf sum
(+ sum
(if (numberp x
) x
(sum-1 x
)))))))))))
617 (defun sum (&rest args
)
618 "Args: (&rest number-data)
619 Returns the sum of all the elements of its arguments. Returns 0 if there
620 are no arguments. Vector reducing."
622 (sum-1 (if (rest args
) args
(first args
)))
628 (let ((seq (compound-data-seq x
))
632 (setf prod
(* prod
(if (numberp x
) x
(prod-1 x
)))))
633 (let ((n (length seq
)))
637 (let ((x (aref seq i
)))
638 (setf prod
(* prod
(if (numberp x
) x
(prod-1 x
)))))))))))
640 (defun prod (&rest args
)
641 "Args: (&rest number-data)
642 Returns the product of all the elements of its arguments. Returns 1 if there
643 are no arguments. Vector reducing."
645 (prod-1 (if (rest args
) args
(first args
)))
648 (defun count-elements (x)
649 "Args: (number &rest more-numbers)
650 Returns the number of its arguments. Vector reducing"
651 (if (compound-data-p x
)
652 (let ((seq (compound-data-seq x
))
655 (dolist (x seq count
)
656 (incf count
(if (compound-data-p x
) (count-elements x
) 1)))
657 (let ((n (length seq
)))
661 (let ((x (aref seq i
)))
662 (incf count
(if (compound-data-p x
) (count-elements x
) 1)))))))
667 Returns the mean of the elements x. Vector reducing."
670 (labels ((add-to-mean (x)
671 (let ((count+1 (+ count
1.0)))
672 (setf mean
(+ (* (/ count count
+1) mean
) (* (/ count
+1) x
)))
673 (setf count count
+1)))
677 (let ((seq (compound-data-seq x
)))
680 (if (numberp x
) (add-to-mean x
) (find-mean x
)))
681 (let ((n (length seq
)))
684 (let ((x (aref seq i
)))
687 (find-mean x
))))))))))
691 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
693 ;;;; Sorting Functions
695 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
699 Returns a sequence with the numbers or strings in the sequence X in order."
700 (flet ((less (x y
) (if (numberp x
) (< x y
) (string-lessp x y
))))
701 (stable-sort (copy-seq (compound-data-seq x
)) #'less
)))
705 Returns a sequence of the indices of elements in the sequence of numbers
706 or strings X in order."
707 (let* ((seq (compound-data-seq x
))
708 (type (if (consp seq
) 'list
'vector
))
710 (flet ((entry (x) (setf i
(+ i
1)) (list x i
))
714 (if (numberp x
) (< x y
) (string-lessp x y
)))))
715 (let ((sorted-seq (stable-sort (map type
#'entry seq
) #'less
)))
716 (map type
#'second sorted-seq
)))))
718 ;; this isn't destructive -- do we document destructive only, or any
722 Returns a sequence with the elements of the list or array of numbers or
723 strings X replaced by their ranks."
724 (let ((ranked-seq (order (order x
))))
725 (make-compound-data (compound-data-shape x
) ranked-seq
)))
727 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
729 ;;;; IF-ELSE and SAMPLE Functions
731 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
733 (defun if-else (a x y
)
735 Takes simple or compound data items FIRST, X and Y and returns result of
736 elementswise selecting from X if FIRST is not NIL and from Y otherwise."
737 (flet ((base-if-else (a x y
) (if a x y
)))
738 (recursive-map-elements #'base-if-else
#'if-else a x y
)))
740 (defun sample (x ssize
&optional replace
)
741 "Args: (x n &optional (replace nil))
742 Returns a list of a random sample of size N from sequence X drawn with or
743 without replacement."
746 (x (if (consp x
) (coerce x
'vector
) (copy-vector x
)))
749 (dotimes (i ssize result
)
750 (let ((j (if replace
(random n
) (+ i
(random (- n i
))))))
751 (setf result
(cons (aref x j
) result
))
752 (unless replace
;; swap elements i and j
753 (let ((temp (aref x i
)))
754 (setf (aref x i
) (aref x j
))
755 (setf (aref x j
) temp
))))))))