Commit 4119ce8a authored by Nikodemus Siivola's avatar Nikodemus Siivola

Revert "generic MEAN and MEDIAN, new function DISPLACE-ARRAY"

  This reverts commit 485544d4.

  MEAN and MEDIAN are again non-generic, and DISPLACE-ARRAY is removed.

  Motivation on the mailing list.
parent 95f8d221
...@@ -16,14 +16,3 @@ arguments." ...@@ -16,14 +16,3 @@ arguments."
(setf (row-major-aref new-array i) (setf (row-major-aref new-array i)
(row-major-aref array i))) (row-major-aref array i)))
new-array)) new-array))
(declaim (inline displace-array))
(defun displace-array (array &key (offset 0)
(dimensions (- (array-total-size array)
offset)))
"Return an array displaced to ARRAY with the given OFFSET and DIMENSIONS.
Default arguments displace to a vector."
(make-array dimensions
:displaced-to array
:displaced-index-offset offset
:element-type (array-element-type array)))
...@@ -84,58 +84,21 @@ Examples: ...@@ -84,58 +84,21 @@ Examples:
interpolation coefficient V." interpolation coefficient V."
(+ a (* v (- b a)))) (+ a (* v (- b a))))
(defgeneric mean (object) (declaim (inline mean))
(:documentation "Returns the mean of OBJECT. (defun mean (sample)
Predefined methods work on sequences and arrays of numbers. Users can "Returns the mean of SAMPLE. SAMPLE must be a sequence of numbers."
define new methods.") (/ (reduce #'+ sample) (length sample)))
(:method ((object list))
(let ((sum 0) (declaim (inline median))
(count 0)) (defun median (sample)
(declare (fixnum count)) "Returns median of SAMPLE. SAMPLE must be a sequence of real numbers."
(dolist (elt object) (let* ((vector (sort (copy-sequence 'vector sample) #'<))
(incf sum elt)
(incf count))
(/ sum count)))
(:method ((object vector))
;; Need a separate method for vectors, since
;; they could have fill-pointers which we need to respect.
(let ((n (length object)))
(/ (loop for index below n
summing (aref object index))
n)))
(:method ((object array))
(let ((n (array-total-size object)))
(/ (loop for index below n
summing (row-major-aref object index))
n)))
(:method ((object sequence))
;; For implementations supporting custom sequence types.
(/ (reduce #'+ object) (length object))))
(defun median-in-place (vector)
(declare (vector vector))
(let* ((vector (sort vector #'<))
(length (length vector)) (length (length vector))
(middle (truncate length 2))) (middle (truncate length 2)))
(if (oddp length) (if (oddp length)
(aref vector middle) (aref vector middle)
(/ (+ (aref vector middle) (aref vector (1- middle))) 2)))) (/ (+ (aref vector middle) (aref vector (1- middle))) 2))))
(defgeneric median (object)
(:documentation
"Returns median of OBJECT.
Predefined methods work on sequences and arrays of numbers. Users can
define new methods.")
(:method ((object list))
(median-in-place (copy-sequence 'vector object)))
(:method ((object array))
(median-in-place (copy-sequence 'vector (if (vectorp object)
object
(displace-array object)))))
(:method ((object sequence))
;; For implementations supporting custom sequence types.
(median-in-place (copy-sequence 'vector object))))
(declaim (inline variance)) (declaim (inline variance))
(defun variance (sample &key (biased t)) (defun variance (sample &key (biased t))
"Variance of SAMPLE. Returns the biased variance if BIASED is true (the default), "Variance of SAMPLE. Returns the biased variance if BIASED is true (the default),
......
...@@ -101,7 +101,6 @@ ...@@ -101,7 +101,6 @@
#:array-index #:array-index
#:array-length #:array-length
#:copy-array #:copy-array
#:displace-array
;; Sequences ;; Sequences
#:copy-sequence #:copy-sequence
#:deletef #:deletef
......
...@@ -47,24 +47,6 @@ ...@@ -47,24 +47,6 @@
(typep copy 'simple-array))) (typep copy 'simple-array)))
t) t)
(deftest displace-array.1
(displace-array #2A((1 2)
(3 4)))
#(1 2 3 4))
(deftest displace-array.2
(displace-array #2A((1 2)
(3 4))
:offset 1)
#(2 3 4))
(deftest displace-array.3
(displace-array #2A((1 2)
(3 4))
:offset 1
:dimensions '(3 1))
#2A((2) (3) (4)))
(deftest array-index.1 (deftest array-index.1
(typep 0 'array-index) (typep 0 'array-index)
t) t)
...@@ -976,12 +958,6 @@ ...@@ -976,12 +958,6 @@
(mean '(1 2 10)) (mean '(1 2 10))
13/3) 13/3)
(deftest mean.4
(mean #2A((1 2 3)
(4 5 6)
(7 8 9)))
5)
(deftest median.1 (deftest median.1
(median '(100 0 99 1 98 2 97)) (median '(100 0 99 1 98 2 97))
97) 97)
...@@ -990,11 +966,6 @@ ...@@ -990,11 +966,6 @@
(median '(100 0 99 1 98 2 97 96)) (median '(100 0 99 1 98 2 97 96))
193/2) 193/2)
(deftest median.3
(median #2A((100 0 99 1)
(98 2 97 96)))
193/2)
(deftest variance.1 (deftest variance.1
(variance (list 1 2 3)) (variance (list 1 2 3))
2/3) 2/3)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment