document some tasks in dataframe.lisp that need resolution.
[CommonLispStat.git] / src / basics / lsbasics.lsp
blob311d5252f5a43db1e57647b262908be3310c8757
1 ;;; -*- mode: lisp -*-
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
7 ;;;;
8 ;;;; Copyright (c) 1991, by Luke Tierney. Permission is granted for
9 ;;;; unrestricted use.
11 (in-package :lisp-stat-basics)
13 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
14 ;;;;
15 ;;;; Array Permutation Functions
16 ;;;;
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
23 will be destroyed."
24 (let ((rank (length x)))
25 (declare (fixnum rank))
26 (dotimes (i rank)
27 (declare (fixnum i))
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)
37 "Args: (a k result).
38 Compute indices in a from rowmajor index k, put in vector result."
39 (declare (fixnum k))
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"))
44 (let ((face 1)
45 (rank (array-rank a))
46 (dim (array-dimensions a)))
47 (declare (fixnum face rank))
48 (let ((cdim (make-next-element dim)))
49 (dotimes (i rank)
50 (declare (fixnum i))
51 (setf face (* face (get-next-element cdim i)))))
52 (let ((cdim (make-next-element dim)))
53 (dotimes (i rank)
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."
62 (declare (fixnum i))
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))
68 (k 0 (+ k 1)))
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)
74 "Args: (a p)
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))
77 (check-sequence perm)
78 (if (/= (length perm) (array-rank x))
79 (error "bad permutation sequence - ~a" perm))
80 (let* ((perm (coerce perm 'vector))
81 (rank (array-rank x))
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))
94 (n (length data)))
95 (declare (fixnum n))
96 (dotimes (i rank)
97 (declare (fixnum i))
98 (setf (aref oldindices i) (list nil)))
99 ;; fill in the result
100 (if (/= n (length result_data)) (error "bad data"))
101 (dotimes (i n result)
102 (declare (fixnum i))
103 (let ((k (translate-index i result x perm indices oldindices ilist)))
104 (declare (fixnum k))
105 (setf (aref result_data k) (aref data i)))))))
107 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
108 ;;;;
109 ;;;; SUM, PROD, COUNT-ELEMENTS, and MEAN Functions
110 ;;;;
111 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
113 (defun sum-1 (x)
114 (if (numberp x)
116 (let ((seq (compound-data-seq x))
117 (sum 0))
118 (if (consp seq)
119 (dolist (x seq sum)
120 (incf sum (+ sum (if (numberp x) x (sum-1 x))))) ;; was setf
121 (let ((n (length seq)))
122 (declare (fixnum n))
123 (dotimes (i n sum)
124 (declare (fixnum i))
125 (let ((x (aref seq i)))
126 (setf sum (+ sum (if (numberp x) x (sum-1 x)))))))))))
128 ;; incr
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."
134 (if args
135 (sum-1 (if (rest args) args (first args)))
140 (defun prod-1 (x)
141 (if (numberp x)
143 (let ((seq (compound-data-seq x))
144 (prod 1))
145 (if (consp seq)
146 (dolist (x seq prod)
147 (setf prod (* prod (if (numberp x) x (prod-1 x)))))
148 (let ((n (length seq)))
149 (declare (fixnum n))
150 (dotimes (i n prod)
151 (declare (fixnum i))
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."
159 (if args
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))
168 (count 0))
169 (if (consp seq)
170 (dolist (x seq count)
171 (incf count (if (compound-data-p x) (count-elements x) 1)))
172 (let ((n (length seq)))
173 (declare (fixnum n))
174 (dotimes (i n count)
175 (declare (fixnum i))
176 (let ((x (aref seq i)))
177 (incf count (if (compound-data-p x) (count-elements x) 1)))))))
181 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
182 ;;;;
183 ;;;; IF-ELSE Functions
184 ;;;;
185 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
187 (defun if-else (a x y)
188 "Args: (first 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)))