X-Git-Url: http://git.kpe.io/?a=blobdiff_plain;f=math.lisp;h=e9118443fdec24c716cf21898b42615ce5973692;hb=27a9a1fba69e1f861a0dbbb1ad321d2e60ccdb8f;hp=9a6862bad3f6168196b2838b69ef8eed1505f092;hpb=d1e9c5969a0eecb7923dc350754ea9ab50417a1e;p=kmrcl.git diff --git a/math.lisp b/math.lisp index 9a6862b..e911844 100644 --- a/math.lisp +++ b/math.lisp @@ -7,7 +7,7 @@ ;;;; Programmer: Kevin M. Rosenberg ;;;; Date Started: Nov 2002 ;;;; -;;;; $Id: math.lisp,v 1.2 2003/04/28 16:07:42 kevin Exp $ +;;;; $Id$ ;;;; ;;;; This file, part of KMRCL, is Copyright (c) 2002 by Kevin M. Rosenberg ;;;; @@ -17,7 +17,7 @@ ;;;; ************************************************************************* -(in-package :kmrcl) +(in-package #:kmrcl) (defun deriv (f dx) #'(lambda (x) @@ -28,3 +28,48 @@ (funcall (deriv #'sin 1d-8) x)) ;;; (sin^ pi) + +(defmacro ensure-integer (obj) + "Ensure object is an integer. If it is a string, then parse it" + `(if (stringp ,obj) + (parse-integer ,obj) + ,obj)) + +(defun histogram (v n-bins &key min max) + (declare (fixnum n-bins)) + (when (listp v) + (setq v (coerce v 'vector))) + (when (zerop (length v)) + (return-from histogram (values nil nil nil)) ) + (let ((n (length v)) + (bins (make-array n-bins :element-type 'integer :initial-element 0)) + found-min found-max) + (declare (fixnum n)) + (unless (and min max) + (setq found-min (aref v 0) + found-max (aref v 0)) + (loop for i fixnum from 1 to (1- n) + do + (let ((x (aref v i))) + (cond + ((> x found-max) + (setq found-max x)) + ((< x found-min) + (setq found-min x))))) + (unless min + (setq min found-min)) + (unless max + (setq max found-max))) + (let ((width (/ (- max min) n-bins))) + (setq width (+ width (* double-float-epsilon width))) + (dotimes (i n) + (let ((bin (nth-value 0 (truncate (- (aref v i) min) width)))) + (declare (fixnum bin)) + (when (and (not (minusp bin)) + (< bin n-bins)) + (incf (aref bins bin)))))) + (values bins min max))) + + +(defun fixnum-width () + (nth-value 0 (truncate (+ (/ (log (1+ most-positive-fixnum)) (log 2)) .5))))