2 ;;; Copyright (c) 2005--2008, 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
11 (in-package :lisp-stat-basics
)
13 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
15 ;;;; Array Permutation Functions
17 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
19 (defun permute-indices (x y perm check
)
20 "Args: (x y perm check).
21 permute x into y using perm; all should be vectors; If check is TRUE
22 the routine will check to make sure no indices are reused, but x
24 (let ((rank (length x
)))
25 (declare (fixnum rank
))
28 (let ((k (aref perm i
)))
29 (if (not (fixnump k
)) (error "bad permutation sequence - ~a" perm
))
30 (if (or (< k
0) (>= k rank
))
31 (error "bad permutation sequence - ~a" perm
))
32 (setf (aref y i
) (aref x k
))
33 ;; to insure dimensions are not re-used
34 (if check
(setf (aref x k
) NIL
))))))
36 (defun indices-from-rowmajor (a k result
)
38 Compute indices in a from rowmajor index k, put in vector result."
41 (if (not (arrayp a
)) (error "not an array - ~a" a
))
42 (if (or (> 0 k
) (>= k
(array-total-size a
))) (error "index out of range"))
46 (dim (array-dimensions a
)))
47 (declare (fixnum face rank
))
48 (let ((cdim (make-next-element dim
)))
51 (setf face
(* face
(get-next-element cdim i
)))))
52 (let ((cdim (make-next-element dim
)))
54 (setf face
(/ face
(get-next-element cdim i
)))
55 (setf (aref result i
) (floor (/ k face
)))
56 (setf k
(rem k face
))))))
58 (defun translate-index (i result x perm indices oldindices ilist
)
59 "Args: (i result x perm indices oldindices ilist).
60 Translate row major index in original array to row major index in new
61 array. Use indices vectors and ilist for temporary storage."
63 (let ((rank (array-rank x
)))
64 (declare (fixnum rank
))
65 (indices-from-rowmajor x i oldindices
)
66 (permute-indices oldindices indices perm nil
)
67 (do ((next ilist
(rest next
))
69 ((not (and (< k rank
) (consp next
))))
70 (setf (first next
) (aref indices k
)))
71 (apply #'array-row-major-index result ilist
)))
73 (defun permute-array (x perm
)
75 Returns a copy of the array A permuted according to the permutation P."
76 (if (not (arrayp x
)) (error "not an array - ~a" x
))
78 (if (/= (length perm
) (array-rank x
))
79 (error "bad permutation sequence - ~a" perm
))
80 (let* ((perm (coerce perm
'vector
))
82 (dim (make-array rank
))
83 (olddim (coerce (array-dimensions x
) 'vector
)))
84 (declare (fixnum rank
))
85 ;; construct new dimension vector
86 (permute-indices olddim dim perm t
)
87 ;; make result array and the index vectors and lists */
88 (let* ((result (make-array (coerce dim
'list
)))
89 (indices (make-array rank
))
90 (oldindices (make-array rank
))
91 (ilist (make-list rank
))
92 (data (compound-data-seq x
))
93 (result_data (compound-data-seq result
))
98 (setf (aref oldindices i
) (list nil
)))
100 (if (/= n
(length result_data
)) (error "bad data"))
101 (dotimes (i n result
)
103 (let ((k (translate-index i result x perm indices oldindices ilist
)))
105 (setf (aref result_data k
) (aref data i
)))))))
107 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
109 ;;;; SUM, PROD, COUNT-ELEMENTS, and MEAN Functions
111 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
116 (let ((seq (compound-data-seq x
))
120 (incf sum
(+ sum
(if (numberp x
) x
(sum-1 x
))))) ;; was setf
121 (let ((n (length seq
)))
125 (let ((x (aref seq i
)))
126 (setf sum
(+ sum
(if (numberp x
) x
(sum-1 x
)))))))))))
130 (defun sum (&rest args
)
131 "Args: (&rest number-data)
132 Returns the sum of all the elements of its arguments. Returns 0 if there
133 are no arguments. Vector reducing."
135 (sum-1 (if (rest args
) args
(first args
)))
143 (let ((seq (compound-data-seq x
))
147 (setf prod
(* prod
(if (numberp x
) x
(prod-1 x
)))))
148 (let ((n (length seq
)))
152 (let ((x (aref seq i
)))
153 (setf prod
(* prod
(if (numberp x
) x
(prod-1 x
)))))))))))
155 (defun prod (&rest args
)
156 "Args: (&rest number-data)
157 Returns the product of all the elements of its arguments. Returns 1 if there
158 are no arguments. Vector reducing."
160 (prod-1 (if (rest args
) args
(first args
)))
163 (defun count-elements (x)
164 "Args: (number &rest more-numbers)
165 Returns the number of its arguments. Vector reducing"
166 (if (compound-data-p x
)
167 (let ((seq (compound-data-seq x
))
170 (dolist (x seq count
)
171 (incf count
(if (compound-data-p x
) (count-elements x
) 1)))
172 (let ((n (length seq
)))
176 (let ((x (aref seq i
)))
177 (incf count
(if (compound-data-p x
) (count-elements x
) 1)))))))
181 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
183 ;;;; IF-ELSE Functions
185 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
187 (defun if-else (a x y
)
189 Takes simple or compound data items A, X and Y and returns result
190 of elementswise selection, from X if A is not NIL, otherwise from Y."
191 (flet ((base-if-else (a x y
) (if a x y
)))
192 (recursive-map-elements #'base-if-else
#'if-else a x y
)))