Export matrix stuff properly (?).
[CommonLispStat.git] / lsfloat.lsp
blobdcd1c10053356a5a073b6b54be80f0ba8ec72d7f
1 ;;; -*- mode: lisp -*-
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 ;;;; lsfloat -- Floating point specs and transcendental functions
7 ;;;;
8 ;;;; Copyright (c) 1991, by Luke Tierney. Permission is granted for
9 ;;;; unrestricted use.
10 ;;;;
11 ;;;; Common Lisp allows for four different floating point types that need
12 ;;;; not be distinct. For statistical work, the type I prefer to use is
13 ;;;; the one that is closest to a C double. This type is named stat-float.
14 ;;;; By setting the variable *read-default-float-format* to this type, you
15 ;;;; insure that data entered as floating point data is read in with this
16 ;;;; type. The problem arises with data read as integers that is passed to
17 ;;;; a transcendental, like sqrt. Floating point contagion rules say these
18 ;;;; integers are to be converted to type single-float. Unless single-float
19 ;;;; is equivalent to C double, as it is in Mac CL and KCL, this is not
20 ;;;; what I want. Hence this file redefines the transcendentals to first
21 ;;;; coerce their arguments to stat-float before applying the built-in
22 ;;;; functions.
23 ;;;;
24 ;;;; No actual modifications to the transcendentals are needed if
25 ;;;; single-float is the same as stat-float. The fearure
26 ;;;; :stat-float-is-double-float is used to indicate this.
27 ;;;;
28 ;;;; KCL NOTE:
29 ;;;; In (A)KCL the type short-float corresponds to C float and the types
30 ;;;; single-float, double-float and long-float correspond to C double.
31 ;;;; But in the implementation of the transcendentals (A)KCL coerces
32 ;;;; rationals to short-float, not single-float. CLtL1 is a little vague
33 ;;;; on this (it talks about "single precision") but CLtL2 clarifies that
34 ;;;; rationals should produce single-float results. So (A)KCL is wrong, at
35 ;;;; least relative to the clarification in CLtL2. I therefore decided
36 ;;;; to fix (A)KCL in files c/num_sfun.c and lsp/numlib.lsp. If these
37 ;;;; fixes are applied, the feature :stat-float-is-double-float should be
38 ;;;; defined.
41 ;;; ======== From the CLHS ==========
43 ;; Type SHORT-FLOAT, SINGLE-FLOAT, DOUBLE-FLOAT, LONG-FLOAT
45 ;; Supertypes:
47 ;; short-float: short-float, float, real, number, t
50 ;; single-float: single-float, float, real, number, t
52 ;; double-float: double-float, float, real, number, t
54 ;; long-float: long-float, float, real, number, t
56 ;; Description: For the four defined subtypes of type float, it is true
57 ;; that intermediate between the type short-float and the type long-float
58 ;; are the type single-float and the type double-float. The precise
59 ;; definition of these categories is implementation-defined. The
60 ;; precision (measured in ``bits'', computed as p log 2b) and the
61 ;; exponent size (also measured in ``bits,'' computed as log 2(n+1),
62 ;; where n is the maximum exponent value) is recommended to be at least
63 ;; as great as the values in the next figure. Each of the defined
64 ;; subtypes of type float might or might not have a minus zero.
67 ;; Format Minimum Precision Minimum Exponent Size
68 ;; ----------
69 ;; Short 13 bits 5 bits
70 ;; Single 24 bits 8 bits
71 ;; Double 50 bits 8 bits
72 ;; Long 50 bits 8 bits
74 ;; Figure 12-12. Recommended Minimum Floating-Point Precision and Exponent Size
76 ;; There can be fewer than four internal representations for floats. If
77 ;; there are fewer distinct representations, the following rules apply:
79 ;; If there is only one, it is the type single-float. In this
80 ;; representation, an object is simultaneously of types single-float,
81 ;; double-float, short-float, and long-float.
83 ;; Two internal representations can be arranged in either of the
84 ;; following ways:
86 ;; Two types are provided: single-float and short-float. An object is
87 ;; simultaneously of types single-float, double-float, and long-float.
89 ;; Two types are provided: single-float and double-float. An object is
90 ;; simultaneously of types single-float and short-float, or double-float
91 ;; and long-float.
93 ;; Three internal representations can be arranged in either of the
94 ;; following ways:
96 ;; Three types are provided: short-float, single-float, and
97 ;; double-float. An object can simultaneously be of type double-float and
98 ;; long-float.
100 ;; Three types are provided: single-float, double-float, and
101 ;; long-float. An object can simultaneously be of types single-float and
102 ;; short-float.
105 ;;; Package Setup
108 (defpackage :lisp-stat-float
109 (:use :common-lisp)
110 (:export +stat-float-typing+ +stat-cfloat-typing+ +stat-float-template+
111 machine-epsilon
113 base-float
115 make-base-trans-fun-2 make-base-trans-fun
117 BASE-LOG BASE-EXP BASE-EXPT BASE-SQRT BASE-SIN BASE-COS
118 BASE-TAN BASE-ASIN BASE-ACOS BASE-ATAN BASE-SINH
119 BASE-COSH BASE-TANH BASE-ASINH BASE-ACOSH BASE-ATANH
120 BASE-ABS BASE-PHASE BASE-FFLOOR BASE-FCEILING BASE-FTRUNCATE
121 BASE-FROUND BASE-SIGNUM BASE-CIS))
123 (in-package #:lisp-stat-float)
125 ;; This should technically be conditionalized to the Lisp
126 ;; implementation, i.e.
127 #+sbcl(pushnew :stat-float-is-double-float *features*)
128 #+cmu(pushnew :stat-float-is-double-float *features*)
129 #+clisp(pushnew :stat-float-is-double-float *features*)
130 ;; etc... the above need to be verified!
132 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
133 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
134 ;;;;
135 ;;;; Constants determining default floating point type for statistical
136 ;;;; operations. This type generally corresponds to a C double.
137 ;;;;
138 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
139 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
141 ;; Why do we do this instead of defparameter? mostly to allow for doc
142 ;; defs as well as to minimize dereferencing mistakes.
143 (defmacro define-constant (name value &optional doc)
144 `(defparameter ,name (if (boundp ',name) (symbol-value ',name) ,value)
145 ,@(when doc (list doc))))
147 (define-constant +stat-float-typing+ 'long-float)
148 (define-constant +stat-cfloat-typing+ '(complex long-float))
149 (define-constant +stat-float-template+ 0.d0)
151 ;(defparameter +stat-float-typing+ 'long-float)
152 ;(defparameter +stat-cfloat-typing+ '(complex long-float))
153 ;(defparameter +stat-float-template+ 0.d0)
155 (deftype stat-float () +stat-float-typing+)
156 (deftype stat-cfloat () +stat-cfloat-typing+)
158 (defparameter machine-epsilon
159 (do ((epsilon (float 1.0 +stat-float-template+)
160 (/ epsilon 2.0)))
161 ((= (+ 1.0 (/ epsilon 2.0)) 1.0) epsilon)))
163 (defmacro declare-double (&rest vars) `(declare (long-float ,@vars)))
165 (setf *read-default-float-format* +stat-float-typing+)
167 ;;; FIX-BASE-DOC adds note about modification to the end of the
168 ;;; documentation string argument.
169 (defmacro fix-base-doc (doc)
170 `(format nil
171 "~a~%Modified to coerce arguments(s) to stat-float or stat-cfloat."
172 ,doc))
174 ;;; FIX-BASE-FUN-DOC fixes documentation of SYM and installs in BASE-SYM
175 (defun fix-base-fun-doc (sym base-sym)
176 (let ((doc (documentation sym 'function)))
177 (if doc (setf (documentation base-sym 'function) (fix-base-doc doc)))))
179 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
180 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
181 ;;;;
182 ;;;; Functions and Macros for modifying functions to coerce to standard
183 ;;;; floating point type.
184 ;;;;
185 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
186 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
188 ;;; FFIX - coerces its arguments to standard real or complex floating
189 ;;; point number if needed.
190 #-stat-float-is-double-float
191 (defmacro ffix (x)
192 `(if (complexp ,x)
193 (coerce ,x +stat-cfloat-typing+)
194 (float ,x +stat-float-template+)))
196 #+stat-float-is-double-float
197 (defmacro ffix (x) x)
199 ;;; MAKEDOUBLE coerces its argument to the standard floating point
200 ;;; type specified by +stat-float-template+
202 (defun makedouble (x) (float x +stat-float-template+))
204 ;; Why doesn't this optimization work?
205 ;; #+stat-float-is-double-float
206 ;; (eval-when (compile)
207 ;; (proclaim '(function makedouble (t) long-float)))
210 ;;; MAKE-BASE-TRANS-FUN Macro for re-defining one argument transcendental
211 ;;; functions
213 #-:stat-float-is-double-float
214 (defmacro make-base-trans-fun (sym)
215 (let* ((base-sym (intern (string-upcase (format nil "BASE-~s" sym))))
216 (doc (documentation sym 'function))
217 (doc-list (if doc (list (fix-base-doc doc)))))
218 `(defun ,base-sym (x)
219 ,@doc-list
220 (declare (inline ,sym coerce float))
221 (,sym (ffix x)))))
223 #+:stat-float-is-double-float
224 (defmacro make-base-trans-fun (sym)
225 (let* ((base-sym (intern (string-upcase (format nil "BASE-~s" sym))))
226 (doc (documentation sym 'function)))
227 `(progn (setf (symbol-function ',base-sym) (symbol-function ',sym))
228 (if ,doc (setf (documentation ',base-sym 'function) ,doc)))))
231 ;;; MAKE-BASE-TRANS-FUN-2 Macro for re-defining transcendental functions
232 ;;; with an optional second argument
234 #-:stat-float-is-double-float
235 (defmacro make-base-trans-fun-2 (sym)
236 (let* ((base-sym (intern (string-upcase (format nil "BASE-~s" sym))))
237 (doc (documentation sym 'function))
238 (doc-list (if doc (list (fix-base-doc doc)))))
239 `(defun ,base-sym (x &optional y)
240 ,@doc-list
241 (declare (inline ,sym coerce float))
242 (if y (,sym (ffix x) (ffix y)) (,sym (ffix x))))))
244 #+:stat-float-is-double-float
245 (defmacro make-base-trans-fun-2 (sym)
246 (let* ((base-sym (intern (string-upcase (format nil "BASE-~s" sym))))
247 (doc (documentation sym 'function)))
248 `(progn (setf (symbol-function ',base-sym) (symbol-function ',sym))
249 (if ,doc (setf (documentation ',base-sym 'function) ,doc)))))
251 ;;; Modified base functions to coerce to standard floating point type
255 ;;; BASE-FLOAT
256 #-:stat-float-is-double-float
257 (progn
258 (defun base-float (x &optional (y +stat-float-template+)) (float x y))
259 (fix-base-fun-doc 'float 'base-float))
261 #+:stat-float-is-double-float
262 (make-base-trans-fun float)
264 ;;; BASE-EXPT
265 #-:stat-float-is-double-float
266 (progn
267 (defun base-expt (x y)
268 (declare (inline expt coerce float integerp))
269 (if (integerp y) (expt x y) (expt (ffix x) (ffix y))))
271 (fix-base-fun-doc 'expt 'base-expt))
273 #+:stat-float-is-double-float
274 (make-base-trans-fun expt)
276 ;;; Others
277 (make-base-trans-fun-2 log)
278 (make-base-trans-fun exp)
279 (make-base-trans-fun sqrt)
280 (make-base-trans-fun sin)
281 (make-base-trans-fun cos)
282 (make-base-trans-fun tan)
283 (make-base-trans-fun asin)
284 (make-base-trans-fun acos)
285 (make-base-trans-fun-2 atan)
286 (make-base-trans-fun sinh)
287 (make-base-trans-fun cosh)
288 (make-base-trans-fun tanh)
289 (make-base-trans-fun asinh)
290 (make-base-trans-fun acosh)
291 (make-base-trans-fun atanh)
292 (make-base-trans-fun abs)
293 (make-base-trans-fun phase)
294 (make-base-trans-fun-2 ffloor)
295 (make-base-trans-fun-2 fceiling)
296 (make-base-trans-fun-2 ftruncate)
297 (make-base-trans-fun-2 fround)
298 (make-base-trans-fun signum)
299 (make-base-trans-fun cis)