1 ;;;-*- Mode: Lisp; Syntax: ANSI-Common-Lisp -*-
4 ;;;; [NumLinAlg] James W. Demmel "Applied Numerical Linear Algebra",
5 ;;;; Society for Industrial and Applied Mathematics, 1997
6 ;;;; ISBN: 0-89871-389-7
8 (common-lisp:in-package
:lisp-unit
)
10 (defparameter *epsilon
* nil
11 "A place for the user to set the error epsilon if the defaults are
14 ;;; (ROUNDOFF-ERROR x y) => number
15 ;;; Return the error delta between the exact and approximate floating
17 ;;; [NumLinAlg] : Equation 1.1, pg. 12
18 (defun roundoff-error (exact approximate
)
19 "Returned the error delta between the exact and approximate floating
21 (abs (if (or (zerop exact
) (zerop approximate
))
23 (- (/ approximate exact
) 1.0))))
25 ;;; (FLOAT-EQUAL float1 float2 &optional epsilon) => true or false
26 ;;; Return true if the absolute difference between float1 and float2
27 ;;; is less than epsilon. If an epsilon is not specified and either
28 ;;; float1 or float2 is single precision, the single-float-epsilon is
30 (defun float-equal (float1 float2
&optional
(epsilon *epsilon
*))
31 "Return true if the absolute difference between float1 and float2 is
32 less than some epsilon."
37 ((and (zerop float1
) (zerop float2
)))
39 (> epsilon
(roundoff-error float1 float2
)))
40 ((and (typep float1
'double-float
) (typep float2
'double-float
))
41 (> (* 2.0 double-float-epsilon
) (roundoff-error float1 float2
)))
42 ((or (typep float1
'single-float
) (typep float2
'single-float
))
43 (> (* 2.0 single-float-epsilon
) (roundoff-error float1 float2
)))
46 (defmacro assert-float-equal
(expected form
&rest extras
)
47 (expand-assert :equal form form expected extras
:test
#'float-equal
))
49 ;;; (COMPLEX-EQUAL complex1 complex2 &optional epsilon) => true or false
50 ;;; Return true if the absolute difference of the real components and
51 ;;; the absolute difference of the imaginary components is less then
52 ;;; epsilon. If an epsilon is not specified and either complex1 or
53 ;;; complex2 is (complex single-float), the single-float-epsilon is
55 (defun complex-equal (complex1 complex2
&optional
(epsilon *epsilon
*))
56 "Return true if the absolute difference between Re(complex1),
57 Re(complex2) and the absolute difference between Im(complex1),
58 Im(complex2) is less than epsilon."
60 (typep complex1
'(complex float
))
61 (typep complex2
'(complex float
))
62 (float-equal (realpart complex1
) (realpart complex2
) epsilon
)
63 (float-equal (imagpart complex1
) (imagpart complex2
) epsilon
)))
65 (defmacro assert-complex-equal
(expected form
&rest extras
)
66 (expand-assert :equal form form expected extras
:test
#'complex-equal
))
68 ;;; (NUMBER-EQUAL number1 number2) => true or false
69 ;;; Return true if the numbers are equal using the appropriate
71 (defun number-equal (number1 number2
&optional
(epsilon *epsilon
*))
72 "Return true if the numbers are equal using the appropriate
75 ((and (floatp number1
) (floatp number2
))
76 (float-equal number1 number2 epsilon
))
77 ((and (typep number1
'(complex float
)) (typep number2
'(complex float
)))
78 (complex-equal number1 number2 epsilon
))
79 ((and (numberp number1
) (numberp number2
))
81 (t (error "~A and ~A are not numbers." number1 number2
))))
83 (defmacro assert-number-equal
(expected form
&rest extras
)
84 (expand-assert :equal form form expected extras
:test
#'number-equal
))
86 ;;; (NORMALIZE-FLOAT significand &optional exponent) => significand,exponent
87 (defun normalize-float (significand &optional
(exponent 0))
88 "Return the normalized floating point number and exponent."
91 (values significand
0))
92 ((>= (abs significand
) 10)
93 (normalize-float (/ significand
10.0) (1+ exponent
)))
94 ((< (abs significand
) 1)
95 (normalize-float (* significand
10.0) (1- exponent
)))
96 (t (values significand exponent
))))
98 ;;; (SIGFIG-EQUAL float1 float2 significant-figures) => true or false
99 (defun sigfig-equal (float1 float2 significant-figures
)
100 "Return true if the floating point numbers have equal significant
102 ;; Convert 5 to precision of FLOAT1 and 10 to precision of
103 ;; FLOAT2. Then, rely on Rule of Float and Rational Contagion, CLHS
104 ;; 12.1.4.1, to obtain a DELTA of the proper precision.
105 (let ((delta (* (float 5 float1
) (expt (float 10 float2
) (- significant-figures
)))))
106 (if (or (zerop float1
) (zerop float2
))
107 (< (abs (+ float1 float2
)) delta
)
108 (multiple-value-bind (sig1 exp1
) (normalize-float float1
)
109 (multiple-value-bind (sig2 exp2
) (normalize-float float2
)
111 (< (abs (- sig1 sig2
)) delta
)))))))
113 (defmacro assert-sigfig-equal
(significant-figures expected form
&rest extras
)
114 (expand-assert :equal form form expected extras
115 :test
(lambda (f1 f2
) (sigfig-equal f1 f2 significant-figures
))))
117 ;;; (ELEMENT-EQUAL array1 array2 indice dimensions) => true or false
118 ;;; A utility function for ARRAY-EQUAL.
119 (defun element-equal (array1 array2 indices dimensions
&key
(test #'number-equal
))
120 "Return true if the index of array1 equals array2."
121 (let* ((rank (first dimensions
))
122 (remaining (rest dimensions
))
126 (element-equal array1 array2
127 (cons index indices
) remaining
:test test
))
130 (apply #'aref array1 index
(reverse indices
))
131 (apply #'aref array2 index
(reverse indices
)))))))
132 (do ((index 0 (1+ index
))
133 (result t
(funcall update-result index
)))
134 ((or (not result
) (>= index rank
)) result
))))
136 ;;; (ARRAY-EQUAL array1 array2) => true or false
137 ;;; Return true of the elements of the array are equal.
138 (defun array-equal (array1 array2
&key
(test #'number-equal
))
139 "Return true if the elements of the array are equal."
140 (when (equal (array-dimensions array1
) (array-dimensions array2
))
141 (element-equal array1 array2 nil
(array-dimensions array1
) :test test
)))