1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
6 ;;;; Purpose: General purpose math functions
7 ;;;; Programmer: Kevin M. Rosenberg
8 ;;;; Date Started: Nov 2002
12 ;;;; This file, part of KMRCL, is Copyright (c) 2002 by Kevin M. Rosenberg
14 ;;;; KMRCL users are granted the rights to distribute and use this software
15 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
16 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
17 ;;;; *************************************************************************
24 (/ (- (funcall f (+ x dx)) (funcall f x))
28 (funcall (deriv #'sin 1d-8) x))
32 (defmacro ensure-integer (obj)
33 "Ensure object is an integer. If it is a string, then parse it"
38 (defun histogram (v n-bins &key min max)
39 (declare (fixnum n-bins))
41 (setq v (coerce v 'vector)))
42 (when (zerop (length v))
43 (return-from histogram (values nil nil nil)) )
45 (bins (make-array n-bins :element-type 'integer :initial-element 0))
49 (setq found-min (aref v 0)
51 (loop for i fixnum from 1 to (1- n)
58 (setq found-min x)))))
62 (setq max found-max)))
63 (let ((width (/ (- max min) n-bins)))
64 (setq width (+ width (* double-float-epsilon width)))
66 (let ((bin (nth-value 0 (truncate (- (aref v i) min) width))))
67 (declare (fixnum bin))
68 (when (and (not (minusp bin))
70 (incf (aref bins bin))))))
71 (values bins min max)))
74 (defun fixnum-width ()
75 (nth-value 0 (truncate (+ (/ (log (1+ most-positive-fixnum)) (log 2)) .5))))
77 (defun scaled-epsilon (float &optional (operation '+))
78 "Return the smallest number that would return a value different from
79 FLOAT if OPERATION were applied to FLOAT and this number. OPERATION
80 should be either + or -, and defauls to +."
81 (multiple-value-bind (significand exponent)
83 (multiple-value-bind (1.0-significand 1.0-exponent)
84 (decode-float (float 1.0 float))
85 (if (and (eq operation '-)
86 (= significand 1.0-significand))
87 (scale-float (typecase float
88 (short-float short-float-negative-epsilon)
89 (single-float single-float-negative-epsilon)
90 (double-float double-float-negative-epsilon)
91 (long-float long-float-negative-epsilon))
92 (- exponent 1.0-exponent))
93 (scale-float (typecase float
94 (short-float short-float-epsilon)
95 (single-float single-float-epsilon)
96 (double-float double-float-epsilon)
97 (long-float long-float-epsilon))
98 (- exponent 1.0-exponent))))))