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 (:shadowing-import-from
:lisp-stat-object-system
23 slot-value call-method call-next-method
)
24 (:export permute-array sum prod count-elements mean
27 (in-package :lisp-stat-basics
)
29 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
31 ;;;; Array Permutation Functions
33 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
35 (defun permute-indices (x y perm check
)
36 "Args: (x y perm check).
37 permute x into y using perm; all should be vectors; If check is TRUE
38 the routine will check to make sure no indices are reused, but x
40 (let ((rank (length x
)))
41 (declare (fixnum rank
))
44 (let ((k (aref perm i
)))
45 (if (not (fixnump k
)) (error "bad permutation sequence - ~a" perm
))
46 (if (or (< k
0) (>= k rank
))
47 (error "bad permutation sequence - ~a" perm
))
48 (setf (aref y i
) (aref x k
))
49 ;; to insure dimensions are not re-used
50 (if check
(setf (aref x k
) NIL
))))))
52 (defun indices-from-rowmajor (a k result
)
54 Compute indices in a from rowmajor index k, put in vector result."
57 (if (not (arrayp a
)) (error "not an array - ~a" a
))
58 (if (or (> 0 k
) (>= k
(array-total-size a
))) (error "index out of range"))
62 (dim (array-dimensions a
)))
63 (declare (fixnum face rank
))
64 (let ((cdim (make-next-element dim
)))
67 (setf face
(* face
(get-next-element cdim i
)))))
68 (let ((cdim (make-next-element dim
)))
70 (setf face
(/ face
(get-next-element cdim i
)))
71 (setf (aref result i
) (floor (/ k face
)))
72 (setf k
(rem k face
))))))
74 (defun translate-index (i result x perm indices oldindices ilist
)
75 "Args: (i result x perm indices oldindices ilist).
76 Translate row major index in original array to row major index in new
77 array. Use indices vectors and ilist for temporary storage."
79 (let ((rank (array-rank x
)))
80 (declare (fixnum rank
))
81 (indices-from-rowmajor x i oldindices
)
82 (permute-indices oldindices indices perm nil
)
83 (do ((next ilist
(rest next
))
85 ((not (and (< k rank
) (consp next
))))
86 (setf (first next
) (aref indices k
)))
87 (apply #'array-row-major-index result ilist
)))
89 (defun permute-array (x perm
)
91 Returns a copy of the array A permuted according to the permutation P."
92 (if (not (arrayp x
)) (error "not an array - ~a" x
))
94 (if (/= (length perm
) (array-rank x
))
95 (error "bad permutation sequence - ~a" perm
))
96 (let* ((perm (coerce perm
'vector
))
98 (dim (make-array rank
))
99 (olddim (coerce (array-dimensions x
) 'vector
)))
100 (declare (fixnum rank
))
101 ;; construct new dimension vector
102 (permute-indices olddim dim perm t
)
103 ;; make result array and the index vectors and lists */
104 (let* ((result (make-array (coerce dim
'list
)))
105 (indices (make-array rank
))
106 (oldindices (make-array rank
))
107 (ilist (make-list rank
))
108 (data (compound-data-seq x
))
109 (result_data (compound-data-seq result
))
114 (setf (aref oldindices i
) (list nil
)))
115 ;; fill in the result
116 (if (/= n
(length result_data
)) (error "bad data"))
117 (dotimes (i n result
)
119 (let ((k (translate-index i result x perm indices oldindices ilist
)))
121 (setf (aref result_data k
) (aref data i
)))))))
123 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
125 ;;;; SUM, PROD, COUNT-ELEMENTS, and MEAN Functions
127 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
132 (let ((seq (compound-data-seq x
))
136 (setf sum
(+ sum
(if (numberp x
) x
(sum-1 x
)))))
137 (let ((n (length seq
)))
141 (let ((x (aref seq i
)))
142 (setf sum
(+ sum
(if (numberp x
) x
(sum-1 x
)))))))))))
144 (defun sum (&rest args
)
145 "Args: (&rest number-data)
146 Returns the sum of all the elements of its arguments. Returns 0 if there
147 are no arguments. Vector reducing."
149 (sum-1 (if (rest args
) args
(first args
)))
155 (let ((seq (compound-data-seq x
))
159 (setf prod
(* prod
(if (numberp x
) x
(prod-1 x
)))))
160 (let ((n (length seq
)))
164 (let ((x (aref seq i
)))
165 (setf prod
(* prod
(if (numberp x
) x
(prod-1 x
)))))))))))
167 (defun prod (&rest args
)
168 "Args: (&rest number-data)
169 Returns the product of all the elements of its arguments. Returns 1 if there
170 are no arguments. Vector reducing."
172 (prod-1 (if (rest args
) args
(first args
)))
175 (defun count-elements (x)
176 "Args: (number &rest more-numbers)
177 Returns the number of its arguments. Vector reducing"
178 (if (compound-data-p x
)
179 (let ((seq (compound-data-seq x
))
182 (dolist (x seq count
)
183 (incf count
(if (compound-data-p x
) (count-elements x
) 1)))
184 (let ((n (length seq
)))
188 (let ((x (aref seq i
)))
189 (incf count
(if (compound-data-p x
) (count-elements x
) 1)))))))
193 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
195 ;;;; IF-ELSE Functions
197 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
199 (defun if-else (a x y
)
201 Takes simple or compound data items FIRST, X and Y and returns result of
202 elementswise selecting from X if FIRST is not NIL and from Y otherwise."
203 (flet ((base-if-else (a x y
) (if a x y
)))
204 (recursive-map-elements #'base-if-else
#'if-else a x y
)))