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
23 (:shadowing-import-from
:lisp-stat-object-system
24 slot-value call-method call-next-method
)
27 copy-vector copy-array which repeat
28 permute-array sum prod count-elements mean if-else sample sort-data
32 matrixp num-rows num-cols matmult identity-matrix diagonal row-list
33 column-list inner-product outer-product cross-product transpose
34 bind-columns bind-rows
36 chol-decomp lu-decomp lu-solve determinant inverse sv-decomp
37 qr-decomp rcondest make-rotation
38 fft make-sweep-matrix sweep-operator ax
+y numgrad numhess
40 ;; in linalg.lisp, possibly not supported by matlisp
41 spline kernel-dens kernel-smooth
43 make-rv-function make-rv-function-1
45 log-gamma uniform-rand normal-cdf normal-quant normal-dens
46 normal-rand bivnorm-cdf cauchy-cdf cauchy-quant cauchy-dens
47 cauchy-rand gamma-cdf gamma-quant gamma-dens gamma-rand
48 chisq-cdf chisq-quant chisq-dens chisq-rand beta-cdf beta-quant
49 beta-dens beta-rand t-cdf t-quant t-dens t-rand f-cdf f-quant
50 f-dens f-rand poisson-cdf poisson-quant poisson-pmf poisson-rand
51 binomial-cdf binomial-quant binomial-pmf binomial-rand
56 ;; (defpackage :lisp-stat-basics
57 ;; (:nicknames :ls-basics)
58 ;; (:use ;; :common-lisp
59 ;; :lisp-stat-object-system
62 ;; ;;(:shadowing-import-from (package-shadowing-symbols #:lisp-stat-object-system))
66 ;; sequencep copy-vector copy-array iseq which repeat select
67 ;; permute-array sum prod count-elements mean if-else
68 ;; sample sort-data order rank
71 ;; ;; #+ kcl (export '(function-lambda-expression realp fixnump))
75 ;; compound-data-p map-elements compound-data-seq
76 ;; compound-data-length element-seq compound-data-proto
79 ;; log-gamma uniform-rand normal-cdf normal-quant normal-dens
80 ;; normal-rand bivnorm-cdf cauchy-cdf cauchy-quant cauchy-dens
81 ;; cauchy-rand gamma-cdf gamma-quant gamma-dens gamma-rand
82 ;; chisq-cdf chisq-quant chisq-dens chisq-rand beta-cdf beta-quant
83 ;; beta-dens beta-rand t-cdf t-quant t-dens t-rand f-cdf f-quant
84 ;; f-dens f-rand poisson-cdf poisson-quant poisson-pmf poisson-rand
85 ;; binomial-cdf binomial-quant binomial-pmf binomial-rand
89 ;; chol-decomp lu-decomp lu-solve determinant inverse sv-decomp
90 ;; qr-decomp rcondest make-rotation spline kernel-dens kernel-smooth
91 ;; fft make-sweep-matrix sweep-operator ax+y numgrad numhess
95 ;; matrixp num-rows num-cols matmult identity-matrix diagonal
96 ;; row-list column-list inner-product outer-product cross-product
97 ;; transpose bind-columns bind-rows
101 ;; +stat-float-typing+ +stat-cfloat-typing+ +stat-float-template+
106 ;; ;; (import '(ccl:def-logical-directory ccl:ff-load ccl:deffcfun ccl:defccallable))
110 (in-package #:lisp-stat-basics
)
112 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
114 ;;;; Type Checking Functions
116 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
120 Returns T if X is a fixnum; NIL otherwise."
121 (declare (inline typep
))
124 (defun check-nonneg-fixnum (x)
125 (if (and (fixnump x
) (<= 0 x
)) x
(error "not a non-negative fixnum")))
127 (defun check-one-fixnum (x)
128 (if (not (fixnump x
)) (error "not a fixnum - ~a" x
)))
130 (defun check-one-real (a)
131 (if (not (or (rationalp a
) (floatp a
)))
132 (error "not a real number ~s" a
)
135 (defun check-one-number (a)
136 (if (not (numberp a
))
137 (error "not a number ~s" a
)
140 (defun check-sequence (a)
141 (if (not (or (vectorp a
) (consp a
))) (error "not a sequence - ~s" a
)))
143 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
145 ;;;; Sequence Element Access
147 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
149 (defun get-next-element (x i
)
150 "Get element i from seq x. FIXME: not really??"
151 (let ((myseq (first x
)))
153 (let ((elem (first myseq
)))
154 (setf (first x
) (rest myseq
))
158 (defun set-next-element (x i v
)
159 (let ((seq (first x
)))
162 (setf (first x
) (rest seq
)))
163 (t (setf (aref seq i
) v
)))))
165 (defun make-next-element (x) (list x
))
167 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
169 ;;;; Array to Row-Major Data Vector Conversion Functions
171 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
173 (defun array-data-vector (a)
175 Displaces array A to a vector"
176 (make-array (array-total-size a
)
178 :element-type
(array-element-type a
)))
180 (defun vector-to-array (v dims
)
182 Displaces vector V to array with dimensions DIMS"
185 :element-type
(array-element-type v
)))
187 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
189 ;;;; Copying Functions
191 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
194 ;;; COPY-VECTOR function
197 (defun copy-vector (x)
199 Returns a copy of the vector X"
203 ;;; COPY-ARRAY function
206 (defun copy-array (a)
208 Returns a copy of the array A"
209 (vector-to-array (copy-seq (array-data-vector a
))
210 (array-dimensions a
)))
212 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
214 ;;;; Sequence Functions
216 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
225 Returns a list of the indices where elements of sequence X are not NIL."
226 (let ((x (list (compound-data-seq x
)))
229 (flet ((add-result (x)
230 (if result
(setf (rest tail
) (list x
)) (setf result
(list x
)))
231 (setf tail
(if tail
(rest tail
) result
)))
232 (get-next-element (seq-list i
)
233 (cond ((consp (first seq-list
))
234 (let ((elem (first (first seq-list
))))
235 (setf (first seq-list
) (rest (first seq-list
)))
237 (t (aref (first seq-list
) i
)))))
238 (let ((n (length (first x
))))
239 (dotimes (i n result
)
240 (if (get-next-element x i
) (add-result i
)))))))
248 Repeats VALS. If TIMES is a number and VALS is a non-null, non-array atom,
249 a list of length TIMES with all elements eq to VALS is returned. If VALS
250 is a list and TIMES is a number then VALS is appended TIMES times. If
251 TIMES is a list of numbers then VALS must be a list of equal length and
252 the simpler version of repeat is mapped down the two lists.
253 Examples: (repeat 2 5) returns (2 2 2 2 2)
254 (repeat '(1 2) 3) returns (1 2 1 2 1 2)
255 (repeat '(4 5 6) '(1 2 3)) returns (4 5 5 6 6 6)
256 (repeat '((4) (5 6)) '(2 3)) returns (4 4 5 6 5 6 5 6)"
257 (cond ((compound-data-p b
)
258 (let* ((reps (coerce (compound-data-seq (map-elements #'repeat a b
))
260 (result (first reps
))
261 (tail (last (first reps
))))
262 (dolist (next (rest reps
) result
)
264 (setf (rest tail
) next
)
265 (setf tail
(last next
))))))
266 (t (let* ((a (if (compound-data-p a
)
267 (coerce (compound-data-seq a
) 'list
)
270 (dotimes (i b result
)
271 (let ((next (copy-list a
)))
272 (if result
(setf (rest (last next
)) result
))
273 (setf result next
)))))))
275 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
277 ;;;; Subset Selection and Mutation Functions
279 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
281 ;;;; is x an ordered sequence of nonnegative positive integers?
282 (defun ordered-nneg-seq(x)
285 (cx (make-next-element x
))
288 (let ((elem (check-nonneg-fixnum (get-next-element cx i
))))
289 (if (> m elem
) (return nil
) (setf m elem
)))))))
291 ;;;; select or set the subsequence corresponding to the specified indices
292 (defun sequence-select(x indices
&optional
(values nil set-values
))
298 (declare (fixnum rlen dlen vlen
))
300 ;; Check the input data
302 (check-sequence indices
)
303 (if set-values
(check-sequence values
))
305 ;; Find the data sizes
306 (setf data
(if (ordered-nneg-seq indices
) x
(coerce x
'vector
)))
307 (setf dlen
(length data
))
308 (setf rlen
(length indices
))
310 (setf vlen
(length values
))
311 (if (/= vlen rlen
) (error "value and index sequences do not match")))
313 ;; set up the result/value sequence
317 (make-sequence (if (listp x
) 'list
'vector
) rlen
)))
319 ;; get or set the sequence elements
322 (cr (make-next-element result
))
323 (ci (make-next-element indices
))
328 (declare (fixnum i j index
))
329 (setf index
(get-next-element ci i
))
330 (if (<= dlen index
) (error "index out of range - ~a" index
))
331 (let ((elem (get-next-element cr i
)))
338 ((not (and (< j index
) (consp nextx
))))
340 (setf nextx
(rest nextx
)))
341 (setf (first nextx
) elem
))
342 (t (setf (aref x index
) elem
)))))
344 (cr (make-next-element result
))
345 (ci (make-next-element indices
))
351 (declare (fixnum i j index
))
352 (setf index
(get-next-element ci i
))
353 (if (<= dlen index
) (error "index out of range - ~a" index
))
355 ((listp data
) ;; indices must be ordered
357 ((not (and (< j index
) (consp nextx
))))
359 (setf nextx
(rest nextx
)))
360 (setf elem
(first nextx
)))
361 (t (setf elem
(aref data index
))))
362 (set-next-element cr i elem
)))
366 (defun old-rowmajor-index (index indices dim olddim
)
367 "translate row major index in resulting subarray to row major index
368 in the original array."
369 (declare (fixnum index
))
370 (let ((rank (length dim
))
374 (declare (fixnum rank face oldface
))
378 (setf face
(* face
(aref dim i
)))
379 (setf oldface
(* oldface
(aref olddim i
))))
383 (setf face
(/ face
(aref dim i
)))
384 (setf oldface
(/ oldface
(aref olddim i
)))
386 (* oldface
(aref (aref indices i
) (floor (/ index face
))))) ;;*** is this floor really needed???
387 (setf index
(rem index face
)))
390 (defun subarray-select (a indexlist
&optional
(values nil set_values
))
391 "extract or set subarray for the indices from a displaced array."
403 (declare (fixnum rank n
))
405 (if (or (sequencep a
) (not (arrayp a
))) (error "not an array - ~a" a
))
406 (if (not (listp indexlist
)) (error "bad index list - ~a" indices
))
407 (if (/= (length indexlist
) (array-rank a
))
408 (error "wrong number of indices"))
410 (setf indices
(coerce indexlist
'vector
))
412 (setf olddim
(coerce (array-dimensions a
) 'vector
))
414 ;; compute the result dimension vector and fix up the indices
415 (setf rank
(array-rank a
))
416 (setf dim
(make-array rank
))
419 (setf index
(aref indices i
))
420 (setf n
(aref olddim i
))
421 (setf index
(if (fixnump index
) (vector index
) (coerce index
'vector
)))
422 (setf k
(length index
))
425 (if (<= n
(check-nonneg-fixnum (aref index j
)))
426 (error "index out of bounds - ~a" (aref index j
)))
427 (setf (aref indices i
) index
))
428 (setf (aref dim i
) (length index
)))
430 ;; set up the result or check the values
431 (let ((dim-list (coerce dim
'list
)))
435 ((compound-data-p values
)
436 (if (or (not (arrayp values
)) (/= rank
(array-rank values
)))
437 (error "bad values array - ~a" values
))
438 (setf vdim
(coerce (array-dimensions values
) 'vector
))
441 (if (/= (aref vdim i
) (aref dim i
))
442 (error "bad value array dimensions - ~a" values
)))
443 (setf result values
))
444 (t (setf result
(make-array dim-list
:initial-element values
)))))
445 (t (setf result
(make-array dim-list
)))))
447 ;; compute the result or set the values
448 (setf data
(compound-data-seq a
))
449 (setf result_data
(compound-data-seq result
))
450 (setf n
(length result_data
))
453 (setf k
(old-rowmajor-index i indices dim olddim
))
454 (if (or (> 0 k
) (>= k
(length data
))) (error "index out of range"))
456 (setf (aref data k
) (aref result_data i
))
457 (setf (aref result_data i
) (aref data k
))))
465 (defun select (x &rest args
)
466 "Args: (a &rest indices)
467 A can be a list or an array. If A is a list and INDICES is a single number
468 then the appropriate element of A is returned. If is a list and INDICES is
469 a list of numbers then the sublist of the corresponding elements is returned.
470 If A in an array then the number of INDICES must match the ARRAY-RANK of A.
471 If each index is a number then the appropriate array element is returned.
472 Otherwise the INDICES must all be lists of numbers and the corresponding
473 submatrix of A is returned. SELECT can be used in setf."
475 ((every #'fixnump args
)
476 (if (listp x
) (nth (first args
) x
) (apply #'aref x args
)))
477 ((sequencep x
) (sequence-select x
(first args
)))
478 (t (subarray-select x args
))))
481 ;; Built in SET-SELECT (SETF method for SELECT)
482 (defun set-select (x &rest args
)
483 (let ((indices (butlast args
))
484 (values (first (last args
))))
487 (if (not (consp indices
)) (error "bad indices - ~a" indices
))
488 (let* ((indices (first indices
))
489 (i-list (if (fixnump indices
) (list indices
) indices
))
490 (v-list (if (fixnump indices
) (list values
) values
)))
491 (sequence-select x i-list v-list
)))
493 (subarray-select x indices values
))
494 (t (error "bad argument type - ~a" x
)))
497 (defsetf select set-select
)
500 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
502 ;;;; Array Permutation Functions
504 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
506 (defun permute-indices (x y perm check
)
507 "Args: (x y perm check).
508 permute x into y using perm; all should be vectors; If check is TRUE
509 the routine will check to make sure no indices are reused, but x
511 (let ((rank (length x
)))
512 (declare (fixnum rank
))
515 (let ((k (aref perm i
)))
516 (if (not (fixnump k
)) (error "bad permutation sequence - ~a" perm
))
517 (if (or (< k
0) (>= k rank
))
518 (error "bad permutation sequence - ~a" perm
))
519 (setf (aref y i
) (aref x k
))
520 ;; to insure dimensions are not re-used
521 (if check
(setf (aref x k
) NIL
))))))
523 (defun indices-from-rowmajor (a k result
)
525 Compute indices in a from rowmajor index k, put in vector result."
528 (if (not (arrayp a
)) (error "not an array - ~a" a
))
529 (if (or (> 0 k
) (>= k
(array-total-size a
))) (error "index out of range"))
532 (rank (array-rank a
))
533 (dim (array-dimensions a
)))
534 (declare (fixnum face rank
))
535 (let ((cdim (make-next-element dim
)))
538 (setf face
(* face
(get-next-element cdim i
)))))
539 (let ((cdim (make-next-element dim
)))
541 (setf face
(/ face
(get-next-element cdim i
)))
542 (setf (aref result i
) (floor (/ k face
)))
543 (setf k
(rem k face
))))))
545 (defun translate-index (i result x perm indices oldindices ilist
)
546 "Args: (i result x perm indices oldindices ilist).
547 Translate row major index in original array to row major index in new
548 array. Use indices vectors and ilist for temporary storage."
550 (let ((rank (array-rank x
)))
551 (declare (fixnum rank
))
552 (indices-from-rowmajor x i oldindices
)
553 (permute-indices oldindices indices perm nil
)
554 (do ((next ilist
(rest next
))
556 ((not (and (< k rank
) (consp next
))))
557 (setf (first next
) (aref indices k
)))
558 (apply #'array-row-major-index result ilist
)))
560 (defun permute-array (x perm
)
562 Returns a copy of the array A permuted according to the permutation P."
563 (if (not (arrayp x
)) (error "not an array - ~a" x
))
564 (check-sequence perm
)
565 (if (/= (length perm
) (array-rank x
))
566 (error "bad permutation sequence - ~a" perm
))
567 (let* ((perm (coerce perm
'vector
))
568 (rank (array-rank x
))
569 (dim (make-array rank
))
570 (olddim (coerce (array-dimensions x
) 'vector
)))
571 (declare (fixnum rank
))
572 ;; construct new dimension vector
573 (permute-indices olddim dim perm t
)
574 ;; make result array and the index vectors and lists */
575 (let* ((result (make-array (coerce dim
'list
)))
576 (indices (make-array rank
))
577 (oldindices (make-array rank
))
578 (ilist (make-list rank
))
579 (data (compound-data-seq x
))
580 (result_data (compound-data-seq result
))
585 (setf (aref oldindices i
) (list nil
)))
586 ;; fill in the result
587 (if (/= n
(length result_data
)) (error "bad data"))
588 (dotimes (i n result
)
590 (let ((k (translate-index i result x perm indices oldindices ilist
)))
592 (setf (aref result_data k
) (aref data i
)))))))
594 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
596 ;;;; SUM, PROD, COUNT-ELEMENTS, and MEAN Functions
598 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
603 (let ((seq (compound-data-seq x
))
607 (setf sum
(+ sum
(if (numberp x
) x
(sum-1 x
)))))
608 (let ((n (length seq
)))
612 (let ((x (aref seq i
)))
613 (setf sum
(+ sum
(if (numberp x
) x
(sum-1 x
)))))))))))
615 (defun sum (&rest args
)
616 "Args: (&rest number-data)
617 Returns the sum of all the elements of its arguments. Returns 0 if there
618 are no arguments. Vector reducing."
620 (sum-1 (if (rest args
) args
(first args
)))
626 (let ((seq (compound-data-seq x
))
630 (setf prod
(* prod
(if (numberp x
) x
(prod-1 x
)))))
631 (let ((n (length seq
)))
635 (let ((x (aref seq i
)))
636 (setf prod
(* prod
(if (numberp x
) x
(prod-1 x
)))))))))))
638 (defun prod (&rest args
)
639 "Args: (&rest number-data)
640 Returns the product of all the elements of its arguments. Returns 1 if there
641 are no arguments. Vector reducing."
643 (prod-1 (if (rest args
) args
(first args
)))
646 (defun count-elements (x)
647 "Args: (number &rest more-numbers)
648 Returns the number of its arguments. Vector reducing"
649 (if (compound-data-p x
)
650 (let ((seq (compound-data-seq x
))
653 (dolist (x seq count
)
654 (incf count
(if (compound-data-p x
) (count-elements x
) 1)))
655 (let ((n (length seq
)))
659 (let ((x (aref seq i
)))
660 (incf count
(if (compound-data-p x
) (count-elements x
) 1)))))))
665 Returns the mean of the elements x. Vector reducing."
668 (labels ((add-to-mean (x)
669 (let ((count+1 (+ count
1.0)))
670 (setf mean
(+ (* (/ count count
+1) mean
) (* (/ count
+1) x
)))
671 (setf count count
+1)))
675 (let ((seq (compound-data-seq x
)))
678 (if (numberp x
) (add-to-mean x
) (find-mean x
)))
679 (let ((n (length seq
)))
682 (let ((x (aref seq i
)))
685 (find-mean x
))))))))))
689 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
691 ;;;; Sorting Functions
693 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
697 Returns a sequence with the numbers or strings in the sequence X in order."
698 (flet ((less (x y
) (if (numberp x
) (< x y
) (string-lessp x y
))))
699 (stable-sort (copy-seq (compound-data-seq x
)) #'less
)))
703 Returns a sequence of the indices of elements in the sequence of numbers
704 or strings X in order."
705 (let* ((seq (compound-data-seq x
))
706 (type (if (consp seq
) 'list
'vector
))
708 (flet ((entry (x) (setf i
(+ i
1)) (list x i
))
712 (if (numberp x
) (< x y
) (string-lessp x y
)))))
713 (let ((sorted-seq (stable-sort (map type
#'entry seq
) #'less
)))
714 (map type
#'second sorted-seq
)))))
716 ;; this isn't destructive -- do we document destructive only, or any
720 Returns a sequence with the elements of the list or array of numbers or
721 strings X replaced by their ranks."
722 (let ((ranked-seq (order (order x
))))
723 (make-compound-data (compound-data-shape x
) ranked-seq
)))
725 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
727 ;;;; IF-ELSE and SAMPLE Functions
729 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
731 (defun if-else (a x y
)
733 Takes simple or compound data items FIRST, X and Y and returns result of
734 elementswise selecting from X if FIRST is not NIL and from Y otherwise."
735 (flet ((base-if-else (a x y
) (if a x y
)))
736 (recursive-map-elements #'base-if-else
#'if-else a x y
)))
738 (defun sample (x ssize
&optional replace
)
739 "Args: (x n &optional (replace nil))
740 Returns a list of a random sample of size N from sequence X drawn with or
741 without replacement."
744 (x (if (consp x
) (coerce x
'vector
) (copy-vector x
)))
747 (dotimes (i ssize result
)
748 (let ((j (if replace
(random n
) (+ i
(random (- n i
))))))
749 (setf result
(cons (aref x j
) result
))
750 (unless replace
;; swap elements i and j
751 (let ((temp (aref x i
)))
752 (setf (aref x i
) (aref x j
))
753 (setf (aref x j
) temp
))))))))