/[cmucl]/src/code/irrat.lisp
ViewVC logotype

Contents of /src/code/irrat.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.45.2.1.2.1 - (hide annotations)
Sun Jun 11 20:11:45 2006 UTC (7 years, 10 months ago) by rtoy
Branch: double-double-reader-branch
CVS Tags: double-double-array-base, double-double-reader-checkpoint-1
Branch point for: double-double-array-branch
Changes since 1.45.2.1: +4 -3 lines
Add basic support for reading and printing double-double-floats.  I
think this needs to be built from a binary that already has basic
double-double support.  That is, you need to compile these changes
with a binary built from the double-double-branch.  (It may work
to cross-compile this from the 2006-06 snapshot, but I did not try
it.)

Reading double-double-floats appears to work.  PRINT appears to work.
However, formatted output of double-double-floats may not be accurate
because SCALE-EXPONENT still only supports double-floats.

code/bignum.lisp:
o Add support for coercing a BIGNUM to a DOUBLE-DOUBLE-FLOAT.

code/float.lisp:
o Add support for converting a RATIO to a DOUBLE-DOUBLE-FLOAT.  This
  is needed to be able to read a double-double-float correctly.
o INTEGER-DECODE-DOUBLE-DOUBLE-FLOAT seems to be buggy.  Use a new
  version that works but probably conses more than is necessary and is
  probably slower than necessary.  This is needed to print out
  DOUBLE-DOUBLE-FLOATs correctly.
o Add DECODE-DOUBLE-DOUBLE-FLOAT while we're at it.
o Add SCALE-DOUBLE-DOUBLE-FLOAT.  This is also needed for printing.
o Disable %DOUBLE-DOUBLE-FLOAT.  There's already an implementation in
  float-tran.  (We really should move the float-tran version to here.)


code/format.lisp:
o Tell printer to use #\w when printing double-double-floats.

code/irrat.lisp:
o The deftransform for ABS is working, so use that instead of the
  hack.

code/numbers.lisp:
o Update FLOAT-CONTAGION to support mixing rationals with
  double-double-floats.

code/print.lisp:
o Recognize double-double-floats for printing.  Use "w" as the
  exponent marker for double-double floats.
o Very minor change to FLONUM-TO-DIGITS to support
  double-double-floats.

code/reader.lisp:
o Tell the reader that #\w and #\W are exponent markers for
  double-double-float numbers by giving them the secondary attribute
  constituent-expt.
o Tell the reader to creat double-double-floats when reading such
  numbers.
1 wlott 1.1 ;;; -*- Mode: Lisp; Package: KERNEL; Log: code.log -*-
2     ;;;
3     ;;; **********************************************************************
4 ram 1.7 ;;; This code was written as part of the CMU Common Lisp project at
5     ;;; Carnegie Mellon University, and has been placed in the public domain.
6     ;;;
7     (ext:file-comment
8 rtoy 1.45.2.1.2.1 "$Header: /tiger/var/lib/cvsroots/cmucl/src/code/irrat.lisp,v 1.45.2.1.2.1 2006/06/11 20:11:45 rtoy Exp $")
9 ram 1.7 ;;;
10 wlott 1.1 ;;; **********************************************************************
11     ;;;
12     ;;; This file contains all the irrational functions. Actually, most of the
13     ;;; work is done by calling out to C...
14     ;;;
15     ;;; Author: William Lott.
16     ;;;
17    
18     (in-package "KERNEL")
19    
20    
21     ;;;; Random constants, utility functions, and macros.
22    
23     (defconstant pi 3.14159265358979323846264338327950288419716939937511L0)
24 wlott 1.2 ;(defconstant e 2.71828182845904523536028747135266249775724709369996L0)
25 wlott 1.1
26 ram 1.5 ;;; Make these INLINE, since the call to C is at least as compact as a Lisp
27     ;;; call, and saves number consing to boot.
28     ;;;
29 wlott 1.1 (defmacro def-math-rtn (name num-args)
30     (let ((function (intern (concatenate 'simple-string
31     "%"
32     (string-upcase name)))))
33 ram 1.4 `(progn
34 pw 1.31 (declaim (inline ,function))
35 ram 1.4 (export ',function)
36 wlott 1.10 (alien:def-alien-routine (,name ,function) double-float
37 ram 1.4 ,@(let ((results nil))
38     (dotimes (i num-args (nreverse results))
39     (push (list (intern (format nil "ARG-~D" i))
40     'double-float)
41     results)))))))
42 wlott 1.1
43     (eval-when (compile load eval)
44    
45     (defun handle-reals (function var)
46     `((((foreach fixnum single-float bignum ratio))
47     (coerce (,function (coerce ,var 'double-float)) 'single-float))
48     ((double-float)
49 rtoy 1.45.2.1 (,function ,var))
50     #+double-double
51     ((double-double-float)
52     ;; A hack until we write double-double-float versions of these
53     ;; special functions.
54     (kernel:make-double-double-float (,function (kernel:double-double-hi ,var))
55     0d0))))
56 wlott 1.1
57     ); eval-when (compile load eval)
58    
59    
60     ;;;; Stubs for the Unix math library.
61    
62     ;;; Please refer to the Unix man pages for details about these routines.
63    
64     ;;; Trigonometric.
65 ram 1.17 #-x86 (def-math-rtn "sin" 1)
66     #-x86 (def-math-rtn "cos" 1)
67     #-x86 (def-math-rtn "tan" 1)
68 wlott 1.1 (def-math-rtn "asin" 1)
69     (def-math-rtn "acos" 1)
70 ram 1.17 #-x86 (def-math-rtn "atan" 1)
71     #-x86 (def-math-rtn "atan2" 2)
72 wlott 1.1 (def-math-rtn "sinh" 1)
73     (def-math-rtn "cosh" 1)
74     (def-math-rtn "tanh" 1)
75 pw 1.18 (def-math-rtn "asinh" 1)
76     (def-math-rtn "acosh" 1)
77     (def-math-rtn "atanh" 1)
78 wlott 1.1
79     ;;; Exponential and Logarithmic.
80 dtc 1.19 #-x86 (def-math-rtn "exp" 1)
81     #-x86 (def-math-rtn "log" 1)
82     #-x86 (def-math-rtn "log10" 1)
83 wlott 1.1 (def-math-rtn "pow" 2)
84 dtc 1.30 #-(or x86 sparc-v7 sparc-v8 sparc-v9) (def-math-rtn "sqrt" 1)
85 wlott 1.1 (def-math-rtn "hypot" 2)
86 dtc 1.27 #-(or hpux x86) (def-math-rtn "log1p" 1)
87 ram 1.17
88     #+x86 ;; These are needed for use by byte-compiled files.
89     (progn
90     (defun %sin (x)
91 dtc 1.19 (declare (double-float x)
92 ram 1.17 (values double-float))
93     (%sin x))
94     (defun %sin-quick (x)
95     (declare (double-float x)
96     (values double-float))
97     (%sin-quick x))
98     (defun %cos (x)
99 dtc 1.19 (declare (double-float x)
100 ram 1.17 (values double-float))
101     (%cos x))
102     (defun %cos-quick (x)
103     (declare (double-float x)
104     (values double-float))
105     (%cos-quick x))
106     (defun %tan (x)
107     (declare (double-float x)
108     (values double-float))
109     (%tan x))
110     (defun %tan-quick (x)
111     (declare (double-float x)
112     (values double-float))
113     (%tan-quick x))
114     (defun %atan (x)
115     (declare (double-float x)
116     (values double-float))
117     (%atan x))
118     (defun %atan2 (x y)
119     (declare (double-float x y)
120     (values double-float))
121     (%atan2 x y))
122     (defun %exp (x)
123 dtc 1.19 (declare (double-float x)
124 ram 1.17 (values double-float))
125     (%exp x))
126     (defun %log (x)
127 dtc 1.19 (declare (double-float x)
128 ram 1.17 (values double-float))
129     (%log x))
130     (defun %log10 (x)
131 dtc 1.19 (declare (double-float x)
132 ram 1.17 (values double-float))
133     (%log10 x))
134     #+nil ;; notyet
135     (defun %pow (x y)
136     (declare (type (double-float 0d0) x)
137 dtc 1.19 (double-float y)
138 ram 1.17 (values (double-float 0d0)))
139     (%pow x y))
140     (defun %sqrt (x)
141 dtc 1.19 (declare (double-float x)
142 ram 1.17 (values double-float))
143     (%sqrt x))
144     (defun %scalbn (f ex)
145 dtc 1.19 (declare (double-float f)
146 ram 1.17 (type (signed-byte 32) ex)
147     (values double-float))
148     (%scalbn f ex))
149     (defun %scalb (f ex)
150     (declare (double-float f ex)
151     (values double-float))
152     (%scalb f ex))
153     (defun %logb (x)
154 dtc 1.19 (declare (double-float x)
155     (values double-float))
156     (%logb x))
157 dtc 1.27 (defun %log1p (x)
158     (declare (double-float x)
159     (values double-float))
160     (%log1p x))
161 ram 1.17 ) ; progn
162 wlott 1.1
163 rtoy 1.44
164     ;; As above for x86. It also seems to be needed to handle
165     ;; constant-folding in the compiler.
166     #+sparc
167     (progn
168     (defun %sqrt (x)
169     (declare (double-float x)
170     (values double-float))
171     (%sqrt x))
172     )
173    
174 wlott 1.1
175     ;;;; Power functions.
176    
177     (defun exp (number)
178     "Return e raised to the power NUMBER."
179     (number-dispatch ((number number))
180     (handle-reals %exp number)
181     ((complex)
182     (* (exp (realpart number))
183     (cis (imagpart number))))))
184    
185     ;;; INTEXP -- Handle the rational base, integer power case.
186    
187     (defparameter *intexp-maximum-exponent* 10000)
188    
189 ram 1.6 ;;; This function precisely calculates base raised to an integral power. It
190     ;;; separates the cases by the sign of power, for efficiency reasons, as powers
191     ;;; can be calculated more efficiently if power is a positive integer. Values
192     ;;; of power are calculated as positive integers, and inverted if negative.
193     ;;;
194 wlott 1.1 (defun intexp (base power)
195 rtoy 1.45 ;; Handle the special case of 1^power. Maxima sometimes does this,
196     ;; and there's no need to cause a continuable error in this case.
197     ;; Should we also handle (-1)^power?
198     (when (eql base 1)
199     (return-from intexp base))
200    
201 wlott 1.1 (when (> (abs power) *intexp-maximum-exponent*)
202     (cerror "Continue with calculation."
203     "The absolute value of ~S exceeds ~S."
204     power '*intexp-maximum-exponent* base power))
205     (cond ((minusp power)
206     (/ (intexp base (- power))))
207     ((eql base 2)
208     (ash 1 power))
209     (t
210     (do ((nextn (ash power -1) (ash power -1))
211     (total (if (oddp power) base 1)
212     (if (oddp power) (* base total) total)))
213     ((zerop nextn) total)
214     (setq base (* base base))
215     (setq power nextn)))))
216    
217    
218 ram 1.6 ;;; EXPT -- Public
219     ;;;
220     ;;; If an integer power of a rational, use INTEXP above. Otherwise, do
221     ;;; floating point stuff. If both args are real, we try %POW right off,
222     ;;; assuming it will return 0 if the result may be complex. If so, we call
223     ;;; COMPLEX-POW which directly computes the complex result. We also separate
224     ;;; the complex-real and real-complex cases from the general complex case.
225     ;;;
226 wlott 1.1 (defun expt (base power)
227 wlott 1.3 "Returns BASE raised to the POWER."
228 wlott 1.1 (if (zerop power)
229 rtoy 1.40 ;; CLHS says that if the power is 0, the result is 1, subject to
230     ;; numeric contagion. But what happens if base is infinity or
231     ;; NaN? Do we silently return 1? For now, I think we should
232     ;; signal an error if the FP modes say so.
233     (let ((result (1+ (* base power))))
234     ;; If we get an NaN here, that means base*power above didn't
235     ;; produce 0 and FP traps were disabled, so we handle that
236     ;; here. Should this be a continuable restart?
237     (if (and (floatp result) (float-nan-p result))
238     (float 1 result)
239     result))
240 dtc 1.22 (labels (;; determine if the double float is an integer.
241     ;; 0 - not an integer
242     ;; 1 - an odd int
243     ;; 2 - an even int
244     (isint (ihi lo)
245     (declare (type (unsigned-byte 31) ihi)
246     (type (unsigned-byte 32) lo)
247     (optimize (speed 3) (safety 0)))
248     (let ((isint 0))
249     (declare (type fixnum isint))
250     (cond ((>= ihi #x43400000) ; exponent >= 53
251     (setq isint 2))
252     ((>= ihi #x3ff00000)
253     (let ((k (- (ash ihi -20) #x3ff))) ; exponent
254     (declare (type (mod 53) k))
255     (cond ((> k 20)
256     (let* ((shift (- 52 k))
257     (j (logand (ash lo (- shift))))
258     (j2 (ash j shift)))
259     (declare (type (mod 32) shift)
260     (type (unsigned-byte 32) j j2))
261     (when (= j2 lo)
262     (setq isint (- 2 (logand j 1))))))
263     ((= lo 0)
264     (let* ((shift (- 20 k))
265     (j (ash ihi (- shift)))
266     (j2 (ash j shift)))
267     (declare (type (mod 32) shift)
268     (type (unsigned-byte 31) j j2))
269     (when (= j2 ihi)
270     (setq isint (- 2 (logand j 1))))))))))
271     isint))
272     (real-expt (x y rtype)
273     (let ((x (coerce x 'double-float))
274     (y (coerce y 'double-float)))
275     (declare (double-float x y))
276     (let* ((x-hi (kernel:double-float-high-bits x))
277     (x-lo (kernel:double-float-low-bits x))
278     (x-ihi (logand x-hi #x7fffffff))
279     (y-hi (kernel:double-float-high-bits y))
280     (y-lo (kernel:double-float-low-bits y))
281     (y-ihi (logand y-hi #x7fffffff)))
282     (declare (type (signed-byte 32) x-hi y-hi)
283     (type (unsigned-byte 31) x-ihi y-ihi)
284     (type (unsigned-byte 32) x-lo y-lo))
285     ;; y==zero: x**0 = 1
286     (when (zerop (logior y-ihi y-lo))
287     (return-from real-expt (coerce 1d0 rtype)))
288     ;; +-NaN return x+y
289     (when (or (> x-ihi #x7ff00000)
290     (and (= x-ihi #x7ff00000) (/= x-lo 0))
291     (> y-ihi #x7ff00000)
292     (and (= y-ihi #x7ff00000) (/= y-lo 0)))
293     (return-from real-expt (coerce (+ x y) rtype)))
294     (let ((yisint (if (< x-hi 0) (isint y-ihi y-lo) 0)))
295     (declare (type fixnum yisint))
296     ;; special value of y
297     (when (and (zerop y-lo) (= y-ihi #x7ff00000))
298     ;; y is +-inf
299     (return-from real-expt
300     (cond ((and (= x-ihi #x3ff00000) (zerop x-lo))
301     ;; +-1**inf is NaN
302     (coerce (- y y) rtype))
303     ((>= x-ihi #x3ff00000)
304     ;; (|x|>1)**+-inf = inf,0
305     (if (>= y-hi 0)
306     (coerce y rtype)
307     (coerce 0 rtype)))
308     (t
309     ;; (|x|<1)**-,+inf = inf,0
310     (if (< y-hi 0)
311     (coerce (- y) rtype)
312     (coerce 0 rtype))))))
313    
314     (let ((abs-x (abs x)))
315     (declare (double-float abs-x))
316     ;; special value of x
317     (when (and (zerop x-lo)
318     (or (= x-ihi #x7ff00000) (zerop x-ihi)
319     (= x-ihi #x3ff00000)))
320     ;; x is +-0,+-inf,+-1
321     (let ((z (if (< y-hi 0)
322     (/ 1 abs-x) ; z = (1/|x|)
323     abs-x)))
324     (declare (double-float z))
325     (when (< x-hi 0)
326     (cond ((and (= x-ihi #x3ff00000) (zerop yisint))
327     ;; (-1)**non-int
328     (let ((y*pi (* y pi)))
329     (declare (double-float y*pi))
330     (return-from real-expt
331 dtc 1.24 (complex
332 dtc 1.22 (coerce (%cos y*pi) rtype)
333     (coerce (%sin y*pi) rtype)))))
334     ((= yisint 1)
335     ;; (x<0)**odd = -(|x|**odd)
336     (setq z (- z)))))
337     (return-from real-expt (coerce z rtype))))
338    
339     (if (>= x-hi 0)
340     ;; x>0
341     (coerce (kernel::%pow x y) rtype)
342     ;; x<0
343     (let ((pow (kernel::%pow abs-x y)))
344     (declare (double-float pow))
345     (case yisint
346     (1 ; Odd
347     (coerce (* -1d0 pow) rtype))
348     (2 ; Even
349     (coerce pow rtype))
350     (t ; Non-integer
351     (let ((y*pi (* y pi)))
352     (declare (double-float y*pi))
353 dtc 1.24 (complex
354 dtc 1.22 (coerce (* pow (%cos y*pi)) rtype)
355     (coerce (* pow (%sin y*pi)) rtype)))))))))))))
356     (declare (inline real-expt))
357     (number-dispatch ((base number) (power number))
358     (((foreach fixnum (or bignum ratio) (complex rational)) integer)
359     (intexp base power))
360     (((foreach single-float double-float) rational)
361     (real-expt base power '(dispatch-type base)))
362     (((foreach fixnum (or bignum ratio) single-float)
363     (foreach ratio single-float))
364     (real-expt base power 'single-float))
365     (((foreach fixnum (or bignum ratio) single-float double-float)
366     double-float)
367     (real-expt base power 'double-float))
368     ((double-float single-float)
369     (real-expt base power 'double-float))
370     (((foreach (complex rational) (complex float)) rational)
371     (* (expt (abs base) power)
372     (cis (* power (phase base)))))
373     (((foreach fixnum (or bignum ratio) single-float double-float)
374     complex)
375     (if (and (zerop base) (plusp (realpart power)))
376     (* base power)
377     (exp (* power (log base)))))
378     (((foreach (complex float) (complex rational))
379     (foreach complex double-float single-float))
380     (if (and (zerop base) (plusp (realpart power)))
381     (* base power)
382     (exp (* power (log base)))))))))
383 wlott 1.1
384 toy 1.34 ;; Compute the base 2 log of an integer
385     (defun log2 (x)
386     ;; Write x = 2^n*f where 1/2 < f <= 1. Then log2(x) = n + log2(f).
387     ;;
388     ;; So we grab the top few bits of x and scale that appropriately,
389     ;; take the log of it and add it to n.
390     (let ((n (integer-length x)))
391     (if (< n vm:double-float-digits)
392     (log (coerce x 'double-float) 2d0)
393     (let ((exp (min vm:double-float-digits n))
394     (f (ldb (byte vm:double-float-digits
395     (max 0 (- n vm:double-float-digits)))
396     x)))
397     (+ n (log (scale-float (float f 1d0) (- exp))
398     2d0))))))
399 rtoy 1.43
400 wlott 1.1 (defun log (number &optional (base nil base-p))
401 wlott 1.3 "Return the logarithm of NUMBER in the base BASE, which defaults to e."
402 wlott 1.1 (if base-p
403 toy 1.34 (cond ((zerop base)
404     ;; ANSI spec
405     base)
406     ((and (integerp number) (integerp base)
407     (plusp number) (plusp base))
408     ;; Let's try to do something nice when both the number
409     ;; and the base are positive integers. Use the rule that
410     ;; log_b(x) = log_2(x)/log_2(b)
411     (coerce (/ (log2 number) (log2 base)) 'single-float))
412 rtoy 1.43 ((and (realp number) (realp base))
413     ;; CLHS 12.1.4.1 says
414     ;;
415     ;; When rationals and floats are combined by a
416     ;; numerical function, the rational is first converted
417     ;; to a float of the same format.
418     ;;
419     ;; So assume this applies to floats as well convert all
420     ;; numbers to the largest float format before computing
421     ;; the log.
422     ;;
423     ;; This makes (log 17 10.0) = (log 17.0 10) and so on.
424     (number-dispatch ((number real) (base real))
425     ((double-float
426     (foreach double-float single-float fixnum bignum ratio))
427     (/ (log number) (log (coerce base 'double-float))))
428     (((foreach single-float fixnum bignum ratio)
429     double-float)
430     (/ (log (coerce number 'double-float)) (log base)))
431     (((foreach single-float fixnum bignum ratio)
432     (foreach single-float fixnum bignum ratio))
433     ;; Converting everything to double-float helps the
434     ;; cases like (log 17 10) = (/ (log 17) (log 10)).
435     ;; This is usually handled above, but if we compute (/
436     ;; (log 17) (log 10)), we get a slightly different
437     ;; answer due to roundoff. This makes it a bit more
438     ;; consistent.
439     ;;
440     ;; FIXME: This probably needs more work.
441     (let ((result (/ (log (float number 1d0))
442     (log (float base 1d0)))))
443     (if (realp result)
444     (coerce result 'single-float)
445     (coerce result '(complex single-float)))))))
446 toy 1.34 (t
447 rtoy 1.43 ;; FIXME: This probably needs some work as well.
448 toy 1.34 (/ (log number) (log base))))
449 wlott 1.1 (number-dispatch ((number number))
450 toy 1.34 (((foreach fixnum bignum))
451 wlott 1.3 (if (minusp number)
452 toy 1.34 (complex (coerce (log (- number)) 'single-float)
453     (coerce pi 'single-float))
454 toy 1.37 (coerce (/ (log2 number) #.(log (exp 1d0) 2d0)) 'single-float)))
455 toy 1.34 ((ratio)
456     (if (minusp number)
457     (complex (coerce (log (- number)) 'single-float)
458     (coerce pi 'single-float))
459 toy 1.35 ;; What happens when the ratio is close to 1? We need to
460     ;; be careful to preserve accuracy.
461     (let ((top (numerator number))
462     (bot (denominator number)))
463     ;; If the number of bits in the numerator and
464     ;; denominator are different, just use the fact
465 toy 1.37 ;; log(x/y) = log(x) - log(y). But to preserve
466     ;; accuracy, we actually do
467     ;; (log2(x)-log2(y))/log2(e)).
468     ;;
469     ;; However, if the numerator and denominator have the
470     ;; same number of bits, implying the quotient is near
471     ;; one, we use log1p(x) = log(1+x). Since the number is
472     ;; rational, we don't lose precision subtracting 1 from
473     ;; it, and converting it to double-float is accurate.
474 toy 1.35 (if (= (integer-length top)
475 toy 1.36 (integer-length bot))
476 toy 1.35 (coerce (%log1p (coerce (- number 1) 'double-float))
477     'single-float)
478 toy 1.37 (coerce (/ (- (log2 top) (log2 bot))
479     #.(log (exp 1d0) 2d0))
480     'single-float)))))
481 ram 1.17 (((foreach single-float double-float))
482     ;; Is (log -0) -infinity (libm.a) or -infinity + i*pi (Kahan)?
483     ;; Since this doesn't seem to be an implementation issue
484     ;; I (pw) take the Kahan result.
485     (if (< (float-sign number)
486     (coerce 0 '(dispatch-type number)))
487     (complex (log (- number)) (coerce pi '(dispatch-type number)))
488     (coerce (%log (coerce number 'double-float))
489     '(dispatch-type number))))
490 rtoy 1.45.2.1 #+double-double
491     ((double-double-float)
492     ;; Hack!
493     (let ((hi (kernel:double-double-hi number)))
494     (if (< (float-sign hi)
495     (coerce 0 '(dispatch-type number)))
496     (complex (coerce (log (- hi)) 'kernel:double-double-float)
497     (coerce pi '(dispatch-type number)))
498     (coerce (%log hi) '(dispatch-type number)))))
499 ram 1.17 ((complex)
500     (complex-log number)))))
501 wlott 1.1
502     (defun sqrt (number)
503     "Return the square root of NUMBER."
504     (number-dispatch ((number number))
505 ram 1.17 (((foreach fixnum bignum ratio))
506 wlott 1.3 (if (minusp number)
507 ram 1.17 (complex-sqrt number)
508 wlott 1.3 (coerce (%sqrt (coerce number 'double-float)) 'single-float)))
509 ram 1.17 (((foreach single-float double-float))
510 dtc 1.28 (if (minusp number)
511 ram 1.17 (complex-sqrt number)
512     (coerce (%sqrt (coerce number 'double-float))
513     '(dispatch-type number))))
514 rtoy 1.45.2.1 #+double-double
515     ((double-double-float)
516     (multiple-value-bind (hi lo)
517     (c::sqrt-dd (kernel:double-double-hi number) (kernel:double-double-lo number))
518     (kernel:make-double-double-float hi lo)))
519     ((complex)
520     (complex-sqrt number))))
521 wlott 1.1
522    
523     ;;;; Trigonometic and Related Functions
524    
525 wlott 1.2 (defun abs (number)
526     "Returns the absolute value of the number."
527     (number-dispatch ((number number))
528 rtoy 1.45.2.1.2.1 (((foreach single-float double-float fixnum rational
529     #+double-double double-double-float))
530 wlott 1.2 (abs number))
531 rtoy 1.45.2.1 #+(and nil double-double)
532     ((double-double-float)
533     ;; This is a hack until abs deftransform is working
534     (multiple-value-bind (hi lo)
535     (c::abs-dd (kernel:double-double-hi number) (kernel:double-double-lo number))
536     (kernel:make-double-double-float hi lo)))
537 rtoy 1.45.2.1.2.1 #+(and nil double-double)
538 rtoy 1.45.2.1 ((double-double-float)
539     ;; This is a hack until abs deftransform is working
540     (let ((hi (kernel:double-double-hi number))
541     (lo (kernel:double-double-lo number)))
542     (declare (double-float hi lo))
543     (when (minusp hi)
544     (setf hi (- hi))
545     (setf lo (- lo)))
546     (kernel:make-double-double-float hi lo)))
547 wlott 1.2 ((complex)
548     (let ((rx (realpart number))
549     (ix (imagpart number)))
550     (etypecase rx
551     (rational
552     (sqrt (+ (* rx rx) (* ix ix))))
553     (single-float
554     (coerce (%hypot (coerce rx 'double-float)
555     (coerce ix 'double-float))
556     'single-float))
557     (double-float
558 rtoy 1.45.2.1 (%hypot rx ix))
559     #+double-double
560     (double-double-float
561     (error "abs complex double-double-float not implemented!")))))))
562 wlott 1.1
563     (defun phase (number)
564     "Returns the angle part of the polar representation of a complex number.
565 wlott 1.3 For complex numbers, this is (atan (imagpart number) (realpart number)).
566 wlott 1.1 For non-complex positive numbers, this is 0. For non-complex negative
567     numbers this is PI."
568 wlott 1.3 (etypecase number
569 ram 1.17 (rational
570 wlott 1.3 (if (minusp number)
571     (coerce pi 'single-float)
572     0.0f0))
573 ram 1.17 (single-float
574     (if (minusp (float-sign number))
575     (coerce pi 'single-float)
576     0.0f0))
577 wlott 1.3 (double-float
578 ram 1.17 (if (minusp (float-sign number))
579 wlott 1.3 (coerce pi 'double-float)
580     0.0d0))
581 rtoy 1.45.2.1 #+double-double
582     (double-double-float
583     (if (minusp (float-sign number))
584     (coerce pi 'double-double-float)
585     (kernel:make-double-double-float 0d0 0d0)))
586 wlott 1.3 (complex
587     (atan (imagpart number) (realpart number)))))
588 wlott 1.1
589    
590     (defun sin (number)
591 wlott 1.3 "Return the sine of NUMBER."
592 wlott 1.1 (number-dispatch ((number number))
593     (handle-reals %sin number)
594     ((complex)
595     (let ((x (realpart number))
596     (y (imagpart number)))
597 ram 1.17 (complex (* (sin x) (cosh y))
598     (* (cos x) (sinh y)))))))
599 wlott 1.1
600     (defun cos (number)
601 wlott 1.3 "Return the cosine of NUMBER."
602 wlott 1.1 (number-dispatch ((number number))
603     (handle-reals %cos number)
604     ((complex)
605     (let ((x (realpart number))
606     (y (imagpart number)))
607 ram 1.17 (complex (* (cos x) (cosh y))
608     (- (* (sin x) (sinh y))))))))
609 wlott 1.1
610     (defun tan (number)
611 wlott 1.3 "Return the tangent of NUMBER."
612 wlott 1.1 (number-dispatch ((number number))
613     (handle-reals %tan number)
614     ((complex)
615 ram 1.17 (complex-tan number))))
616 wlott 1.1
617 wlott 1.2 (defun cis (theta)
618 wlott 1.3 "Return cos(Theta) + i sin(Theta), AKA exp(i Theta)."
619 wlott 1.2 (if (complexp theta)
620     (error "Argument to CIS is complex: ~S" theta)
621     (complex (cos theta) (sin theta))))
622 wlott 1.1
623     (defun asin (number)
624 wlott 1.3 "Return the arc sine of NUMBER."
625 wlott 1.1 (number-dispatch ((number number))
626 wlott 1.3 ((rational)
627     (if (or (> number 1) (< number -1))
628     (complex-asin number)
629     (coerce (%asin (coerce number 'double-float)) 'single-float)))
630     (((foreach single-float double-float))
631 rtoy 1.42 (if (or (float-nan-p number)
632     (and (<= number (coerce 1 '(dispatch-type number)))
633     (>= number (coerce -1 '(dispatch-type number)))))
634 wlott 1.3 (coerce (%asin (coerce number 'double-float))
635 rtoy 1.42 '(dispatch-type number))
636     (complex-asin number)))
637 wlott 1.1 ((complex)
638 wlott 1.3 (complex-asin number))))
639 wlott 1.1
640     (defun acos (number)
641 wlott 1.3 "Return the arc cosine of NUMBER."
642 wlott 1.1 (number-dispatch ((number number))
643 wlott 1.3 ((rational)
644     (if (or (> number 1) (< number -1))
645     (complex-acos number)
646     (coerce (%acos (coerce number 'double-float)) 'single-float)))
647     (((foreach single-float double-float))
648 rtoy 1.42 (if (or (float-nan-p number)
649     (and (<= number (coerce 1 '(dispatch-type number)))
650     (>= number (coerce -1 '(dispatch-type number)))))
651 wlott 1.3 (coerce (%acos (coerce number 'double-float))
652 rtoy 1.42 '(dispatch-type number))
653     (complex-acos number)))
654 wlott 1.1 ((complex)
655 wlott 1.3 (complex-acos number))))
656 wlott 1.1
657    
658     (defun atan (y &optional (x nil xp))
659 wlott 1.3 "Return the arc tangent of Y if X is omitted or Y/X if X is supplied."
660 wlott 1.1 (if xp
661 wlott 1.12 (flet ((atan2 (y x)
662 wlott 1.13 (declare (type double-float y x)
663     (values double-float))
664     (if (zerop x)
665     (if (zerop y)
666     (if (plusp (float-sign x))
667     y
668     (float-sign y pi))
669     (float-sign y (/ pi 2)))
670     (%atan2 y x))))
671 toy 1.33 ;; If X is given, both X and Y must be real numbers.
672     (number-dispatch ((y real) (x real))
673 wlott 1.13 ((double-float
674     (foreach double-float single-float fixnum bignum ratio))
675     (atan2 y (coerce x 'double-float)))
676     (((foreach single-float fixnum bignum ratio)
677     double-float)
678     (atan2 (coerce y 'double-float) x))
679     (((foreach single-float fixnum bignum ratio)
680     (foreach single-float fixnum bignum ratio))
681     (coerce (atan2 (coerce y 'double-float) (coerce x 'double-float))
682     'single-float))))
683 wlott 1.1 (number-dispatch ((y number))
684     (handle-reals %atan y)
685     ((complex)
686 ram 1.17 (complex-atan y)))))
687 wlott 1.3
688 ram 1.17 (defun sinh (number)
689     "Return the hyperbolic sine of NUMBER."
690     (number-dispatch ((number number))
691     (handle-reals %sinh number)
692     ((complex)
693     (let ((x (realpart number))
694     (y (imagpart number)))
695     (complex (* (sinh x) (cos y))
696     (* (cosh x) (sin y)))))))
697    
698     (defun cosh (number)
699     "Return the hyperbolic cosine of NUMBER."
700     (number-dispatch ((number number))
701     (handle-reals %cosh number)
702     ((complex)
703     (let ((x (realpart number))
704     (y (imagpart number)))
705     (complex (* (cosh x) (cos y))
706     (* (sinh x) (sin y)))))))
707    
708 wlott 1.3 (defun tanh (number)
709     "Return the hyperbolic tangent of NUMBER."
710 ram 1.17 (number-dispatch ((number number))
711     (handle-reals %tanh number)
712     ((complex)
713     (complex-tanh number))))
714 wlott 1.3
715     (defun asinh (number)
716     "Return the hyperbolic arc sine of NUMBER."
717 ram 1.17 (number-dispatch ((number number))
718     (handle-reals %asinh number)
719     ((complex)
720     (complex-asinh number))))
721 wlott 1.3
722     (defun acosh (number)
723     "Return the hyperbolic arc cosine of NUMBER."
724 ram 1.17 (number-dispatch ((number number))
725     ((rational)
726     ;; acosh is complex if number < 1
727     (if (< number 1)
728     (complex-acosh number)
729     (coerce (%acosh (coerce number 'double-float)) 'single-float)))
730     (((foreach single-float double-float))
731     (if (< number (coerce 1 '(dispatch-type number)))
732     (complex-acosh number)
733     (coerce (%acosh (coerce number 'double-float))
734     '(dispatch-type number))))
735     ((complex)
736     (complex-acosh number))))
737 wlott 1.3
738     (defun atanh (number)
739     "Return the hyperbolic arc tangent of NUMBER."
740 ram 1.17 (number-dispatch ((number number))
741     ((rational)
742     ;; atanh is complex if |number| > 1
743     (if (or (> number 1) (< number -1))
744     (complex-atanh number)
745     (coerce (%atanh (coerce number 'double-float)) 'single-float)))
746     (((foreach single-float double-float))
747     (if (or (> number (coerce 1 '(dispatch-type number)))
748     (< number (coerce -1 '(dispatch-type number))))
749     (complex-atanh number)
750     (coerce (%atanh (coerce number 'double-float))
751     '(dispatch-type number))))
752     ((complex)
753     (complex-atanh number))))
754 wlott 1.14
755 toy 1.32 ;;; HP-UX does not supply a C version of log1p, so use the definition.
756     ;;; We really need to fix this. The definition really loses big-time
757     ;;; in roundoff as x gets small.
758 wlott 1.14
759     #+hpux
760 pw 1.18 (declaim (inline %log1p))
761 wlott 1.14 #+hpux
762 pw 1.18 (defun %log1p (number)
763     (declare (double-float number)
764     (optimize (speed 3) (safety 0)))
765     (the double-float (log (the (double-float 0d0) (+ number 1d0)))))
766 ram 1.17
767    
768     ;;;;
769     ;;;; This is a set of routines that implement many elementary
770     ;;;; transcendental functions as specified by ANSI Common Lisp. The
771     ;;;; implementation is based on Kahan's paper.
772     ;;;;
773     ;;;; I believe I have accurately implemented the routines and are
774     ;;;; correct, but you may want to check for your self.
775     ;;;;
776     ;;;; These functions are written for CMU Lisp and take advantage of
777     ;;;; some of the features available there. It may be possible,
778     ;;;; however, to port this to other Lisps.
779     ;;;;
780     ;;;; Some functions are significantly more accurate than the original
781     ;;;; definitions in CMU Lisp. In fact, some functions in CMU Lisp
782     ;;;; give the wrong answer like (acos #c(-2.0 0.0)), where the true
783     ;;;; answer is pi + i*log(2-sqrt(3)).
784     ;;;;
785     ;;;; All of the implemented functions will take any number for an
786     ;;;; input, but the result will always be a either a complex
787     ;;;; single-float or a complex double-float.
788     ;;;;
789     ;;;; General functions
790     ;;;; complex-sqrt
791     ;;;; complex-log
792     ;;;; complex-atanh
793     ;;;; complex-tanh
794     ;;;; complex-acos
795     ;;;; complex-acosh
796     ;;;; complex-asin
797     ;;;; complex-asinh
798     ;;;; complex-atan
799     ;;;; complex-tan
800     ;;;;
801     ;;;; Utility functions:
802     ;;;; scalb logb
803     ;;;;
804     ;;;; Internal functions:
805     ;;;; square coerce-to-complex-type cssqs complex-log-scaled
806     ;;;;
807     ;;;;
808     ;;;; Please send any bug reports, comments, or improvements to Raymond
809     ;;;; Toy at toy@rtp.ericsson.se.
810     ;;;;
811     ;;;; References
812     ;;;;
813     ;;;; Kahan, W. "Branch Cuts for Complex Elementary Functions, or Much
814     ;;;; Ado About Nothing's Sign Bit" in Iserles and Powell (eds.) "The
815     ;;;; State of the Art in Numerical Analysis", pp. 165-211, Clarendon
816     ;;;; Press, 1987
817     ;;;;
818 toy 1.32
819 ram 1.17 (declaim (inline square))
820     (defun square (x)
821 toy 1.32 (declare (double-float x))
822 ram 1.17 (* x x))
823    
824     ;; If you have these functions in libm, perhaps they should be used
825     ;; instead of these Lisp versions. These versions are probably good
826     ;; enough, especially since they are portable.
827    
828 dtc 1.19 (declaim (inline scalb))
829 ram 1.17 (defun scalb (x n)
830     "Compute 2^N * X without compute 2^N first (use properties of the
831     underlying floating-point format"
832     (declare (type double-float x)
833 dtc 1.19 (type double-float-exponent n))
834 ram 1.17 (scale-float x n))
835    
836 toy 1.32 (declaim (inline logb-finite))
837     (defun logb-finite (x)
838     "Same as logb but X is not infinity and non-zero and not a NaN, so
839     that we can always return an integer"
840     (declare (type double-float x))
841     (multiple-value-bind (signif expon sign)
842     (decode-float x)
843     (declare (ignore signif sign))
844     ;; decode-float is almost right, except that the exponent
845     ;; is off by one
846     (1- expon)))
847    
848 ram 1.17 (defun logb (x)
849 toy 1.32 "Compute an integer N such that 1 <= |2^(-N) * x| < 2.
850 ram 1.17 For the special cases, the following values are used:
851    
852     x logb
853     NaN NaN
854     +/- infinity +infinity
855     0 -infinity
856     "
857     (declare (type double-float x))
858 dtc 1.26 (cond ((float-nan-p x)
859 ram 1.17 x)
860     ((float-infinity-p x)
861     #.ext:double-float-positive-infinity)
862     ((zerop x)
863     ;; The answer is negative infinity, but we are supposed to
864 toy 1.32 ;; signal divide-by-zero, so do the actual division
865 ram 1.17 (/ -1.0d0 x)
866     )
867     (t
868 toy 1.32 (logb-finite x))))
869    
870    
871 ram 1.17
872     ;; This function is used to create a complex number of the appropriate
873     ;; type.
874    
875     (declaim (inline coerce-to-complex-type))
876     (defun coerce-to-complex-type (x y z)
877     "Create complex number with real part X and imaginary part Y such that
878     it has the same type as Z. If Z has type (complex rational), the X
879     and Y are coerced to single-float."
880     (declare (double-float x y)
881 toy 1.32 (number z)
882     (optimize (extensions:inhibit-warnings 3)))
883     (if (typep (realpart z) 'double-float)
884 ram 1.17 (complex x y)
885     ;; Convert anything that's not a double-float to a single-float.
886 toy 1.32 (complex (float x 1f0)
887     (float y 1f0))))
888 ram 1.17
889     (defun cssqs (z)
890 dtc 1.21 ;; Compute |(x+i*y)/2^k|^2 scaled to avoid over/underflow. The
891     ;; result is r + i*k, where k is an integer.
892 ram 1.17
893     ;; Save all FP flags
894 dtc 1.21 (let ((x (float (realpart z) 1d0))
895 toy 1.32 (y (float (imagpart z) 1d0)))
896 dtc 1.21 ;; Would this be better handled using an exception handler to
897     ;; catch the overflow or underflow signal? For now, we turn all
898     ;; traps off and look at the accrued exceptions to see if any
899     ;; signal would have been raised.
900     (with-float-traps-masked (:underflow :overflow)
901 toy 1.32 (let ((rho (+ (square x) (square y))))
902     (declare (optimize (speed 3) (space 0)))
903     (cond ((and (or (float-nan-p rho)
904     (float-infinity-p rho))
905     (or (float-infinity-p (abs x))
906     (float-infinity-p (abs y))))
907     (values ext:double-float-positive-infinity 0))
908     ((let ((threshold #.(/ least-positive-double-float
909     double-float-epsilon))
910     (traps (ldb vm::float-sticky-bits
911     (vm:floating-point-modes))))
912     ;; Overflow raised or (underflow raised and rho <
913     ;; lambda/eps)
914     (or (not (zerop (logand vm:float-overflow-trap-bit traps)))
915     (and (not (zerop (logand vm:float-underflow-trap-bit traps)))
916     (< rho threshold))))
917     ;; If we're here, neither x nor y are infinity and at
918     ;; least one is non-zero.. Thus logb returns a nice
919     ;; integer.
920     (let ((k (- (logb-finite (max (abs x) (abs y))))))
921     (values (+ (square (scalb x k))
922     (square (scalb y k)))
923     (- k))))
924     (t
925     (values rho 0)))))))
926 ram 1.17
927     (defun complex-sqrt (z)
928     "Principle square root of Z
929    
930     Z may be any number, but the result is always a complex."
931     (declare (number z))
932     (multiple-value-bind (rho k)
933     (cssqs z)
934 toy 1.32 (declare (type (or (member 0d0) (double-float 0d0)) rho)
935     (type fixnum k))
936 ram 1.17 (let ((x (float (realpart z) 1.0d0))
937     (y (float (imagpart z) 1.0d0))
938     (eta 0d0)
939     (nu 0d0))
940     (declare (double-float x y eta nu))
941    
942 toy 1.32 (locally
943     ;; space 0 to get maybe-inline functions inlined.
944     (declare (optimize (speed 3) (space 0)))
945    
946     (if (not (float-nan-p x))
947     (setf rho (+ (scalb (abs x) (- k)) (sqrt rho))))
948    
949     (cond ((oddp k)
950     (setf k (ash k -1)))
951     (t
952     (setf k (1- (ash k -1)))
953     (setf rho (+ rho rho))))
954    
955     (setf rho (scalb (sqrt rho) k))
956    
957     (setf eta rho)
958     (setf nu y)
959    
960     (when (/= rho 0d0)
961     (when (not (float-infinity-p (abs nu)))
962     (setf nu (/ (/ nu rho) 2d0)))
963     (when (< x 0d0)
964     (setf eta (abs nu))
965     (setf nu (float-sign y rho))))
966     (coerce-to-complex-type eta nu z)))))
967 ram 1.17
968     (defun complex-log-scaled (z j)
969     "Compute log(2^j*z).
970    
971     This is for use with J /= 0 only when |z| is huge."
972     (declare (number z)
973     (fixnum j))
974     ;; The constants t0, t1, t2 should be evaluated to machine
975     ;; precision. In addition, Kahan says the accuracy of log1p
976     ;; influences the choices of these constants but doesn't say how to
977     ;; choose them. We'll just assume his choices matches our
978     ;; implementation of log1p.
979     (let ((t0 #.(/ 1 (sqrt 2.0d0)))
980     (t1 1.2d0)
981     (t2 3d0)
982     (ln2 #.(log 2d0))
983     (x (float (realpart z) 1.0d0))
984     (y (float (imagpart z) 1.0d0)))
985     (multiple-value-bind (rho k)
986     (cssqs z)
987 toy 1.32 (declare (optimize (speed 3)))
988 ram 1.17 (let ((beta (max (abs x) (abs y)))
989     (theta (min (abs x) (abs y))))
990 toy 1.32 (coerce-to-complex-type (if (and (zerop k)
991     (< t0 beta)
992     (or (<= beta t1)
993     (< rho t2)))
994     (/ (%log1p (+ (* (- beta 1.0d0)
995     (+ beta 1.0d0))
996     (* theta theta)))
997     2d0)
998     (+ (/ (log rho) 2d0)
999     (* (+ k j) ln2)))
1000     (atan y x)
1001     z)))))
1002 ram 1.17
1003     (defun complex-log (z)
1004     "Log of Z = log |Z| + i * arg Z
1005    
1006     Z may be any number, but the result is always a complex."
1007     (declare (number z))
1008     (complex-log-scaled z 0))
1009    
1010     ;; Let us note the following "strange" behavior. atanh 1.0d0 is
1011     ;; +infinity, but the following code returns approx 176 + i*pi/4. The
1012     ;; reason for the imaginary part is caused by the fact that arg i*y is
1013     ;; never 0 since we have positive and negative zeroes.
1014    
1015     (defun complex-atanh (z)
1016     "Compute atanh z = (log(1+z) - log(1-z))/2"
1017     (declare (number z))
1018 rtoy 1.41 (if (and (realp z) (< z -1))
1019     ;; atanh is continuous in quadrant III in this case.
1020     (complex-atanh (complex z -0f0))
1021     (let* ( ;; Constants
1022     (theta (/ (sqrt most-positive-double-float) 4.0d0))
1023     (rho (/ 4.0d0 (sqrt most-positive-double-float)))
1024     (half-pi (/ pi 2.0d0))
1025     (rp (float (realpart z) 1.0d0))
1026     (beta (float-sign rp 1.0d0))
1027     (x (* beta rp))
1028     (y (* beta (- (float (imagpart z) 1.0d0))))
1029     (eta 0.0d0)
1030     (nu 0.0d0))
1031     ;; Shouldn't need this declare.
1032     (declare (double-float x y))
1033     (locally
1034     (declare (optimize (speed 3)))
1035     (cond ((or (> x theta)
1036     (> (abs y) theta))
1037     ;; To avoid overflow...
1038     (setf nu (float-sign y half-pi))
1039     ;; eta is real part of 1/(x + iy). This is x/(x^2+y^2),
1040     ;; which can cause overflow. Arrange this computation so
1041     ;; that it won't overflow.
1042     (setf eta (let* ((x-bigger (> x (abs y)))
1043     (r (if x-bigger (/ y x) (/ x y)))
1044     (d (+ 1.0d0 (* r r))))
1045     (if x-bigger
1046     (/ (/ x) d)
1047     (/ (/ r y) d)))))
1048     ((= x 1.0d0)
1049     ;; Should this be changed so that if y is zero, eta is set
1050     ;; to +infinity instead of approx 176? In any case
1051     ;; tanh(176) is 1.0d0 within working precision.
1052     (let ((t1 (+ 4d0 (square y)))
1053     (t2 (+ (abs y) rho)))
1054     (setf eta (log (/ (sqrt (sqrt t1))
1055     (sqrt t2))))
1056     (setf nu (* 0.5d0
1057     (float-sign y
1058     (+ half-pi (atan (* 0.5d0 t2))))))))
1059     (t
1060     (let ((t1 (+ (abs y) rho)))
1061     ;; Normal case using log1p(x) = log(1 + x)
1062     (setf eta (* 0.25d0
1063     (%log1p (/ (* 4.0d0 x)
1064     (+ (square (- 1.0d0 x))
1065     (square t1))))))
1066     (setf nu (* 0.5d0
1067     (atan (* 2.0d0 y)
1068     (- (* (- 1.0d0 x)
1069     (+ 1.0d0 x))
1070     (square t1))))))))
1071     (coerce-to-complex-type (* beta eta)
1072     (- (* beta nu))
1073     z)))))
1074 ram 1.17
1075     (defun complex-tanh (z)
1076     "Compute tanh z = sinh z / cosh z"
1077     (declare (number z))
1078     (let ((x (float (realpart z) 1.0d0))
1079     (y (float (imagpart z) 1.0d0)))
1080 toy 1.32 (locally
1081     ;; space 0 to get maybe-inline functions inlined
1082     (declare (optimize (speed 3) (space 0)))
1083     (cond ((> (abs x)
1084     #-(or linux hpux) #.(/ (%asinh most-positive-double-float) 4d0)
1085     ;; This is more accurate under linux.
1086     #+(or linux hpux) #.(/ (+ (%log 2.0d0)
1087     (%log most-positive-double-float)) 4d0))
1088     (coerce-to-complex-type (float-sign x)
1089     (float-sign y) z))
1090     (t
1091     (let* ((tv (%tan y))
1092     (beta (+ 1.0d0 (* tv tv)))
1093     (s (sinh x))
1094     (rho (sqrt (+ 1.0d0 (* s s)))))
1095     (if (float-infinity-p (abs tv))
1096     (coerce-to-complex-type (/ rho s)
1097     (/ tv)
1098     z)
1099     (let ((den (+ 1.0d0 (* beta s s))))
1100     (coerce-to-complex-type (/ (* beta rho s)
1101     den)
1102     (/ tv den)
1103     z)))))))))
1104 ram 1.17
1105     ;; Kahan says we should only compute the parts needed. Thus, the
1106     ;; realpart's below should only compute the real part, not the whole
1107     ;; complex expression. Doing this can be important because we may get
1108     ;; spurious signals that occur in the part that we are not using.
1109     ;;
1110     ;; However, we take a pragmatic approach and just use the whole
1111     ;; expression.
1112    
1113     ;; NOTE: The formula given by Kahan is somewhat ambiguous in whether
1114     ;; it's the conjugate of the square root or the square root of the
1115     ;; conjugate. This needs to be checked.
1116    
1117     ;; I checked. It doesn't matter because (conjugate (sqrt z)) is the
1118     ;; same as (sqrt (conjugate z)) for all z. This follows because
1119     ;;
1120     ;; (conjugate (sqrt z)) = exp(0.5*log |z|)*exp(-0.5*j*arg z).
1121     ;;
1122     ;; (sqrt (conjugate z)) = exp(0.5*log|z|)*exp(0.5*j*arg conj z)
1123     ;;
1124 toy 1.35 ;; and these two expressions are equal if and only if arg conj z =
1125     ;; -arg z, which is clearly true for all z.
1126 ram 1.17
1127 rtoy 1.38 ;; NOTE: The rules of Common Lisp says that if you mix a real with a
1128     ;; complex, the real is converted to a complex before performing the
1129     ;; operation. However, Kahan says in this paper (pg 176):
1130     ;;
1131     ;; (iii) Careless handling can turn infinity or the sign of zero into
1132     ;; misinformation that subsequently disappears leaving behind
1133     ;; only a plausible but incorrect result. That is why compilers
1134     ;; must not transform z-1 into z-(1+i*0), as we have seen above,
1135     ;; nor -(-x-x^2) into (x+x^2), as we shall see below, lest a
1136     ;; subsequent logarithm or square root produce a non-zero
1137     ;; imaginary part whose sign is opposite to what was intended.
1138     ;;
1139     ;; The interesting examples are too long and complicated to reproduce
1140     ;; here. We refer the reader to his paper.
1141     ;;
1142     ;; The functions below are intended to handle the cases where a real
1143     ;; is mixed with a complex and we don't want CL complex contagion to
1144     ;; occur..
1145    
1146     (declaim (inline 1+z 1-z z-1 z+1))
1147     (defun 1+z (z)
1148     (complex (+ 1 (realpart z)) (imagpart z)))
1149     (defun 1-z (z)
1150     (complex (- 1 (realpart z)) (- (imagpart z))))
1151     (defun z-1 (z)
1152     (complex (- (realpart z) 1) (imagpart z)))
1153     (defun z+1 (z)
1154     (complex (+ (realpart z) 1) (imagpart z)))
1155    
1156 ram 1.17 (defun complex-acos (z)
1157     "Compute acos z = pi/2 - asin z
1158    
1159     Z may be any number, but the result is always a complex."
1160     (declare (number z))
1161 rtoy 1.41 (if (and (realp z) (> z 1))
1162     ;; acos is continuous in quadrant IV in this case.
1163     (complex-acos (complex z -0f0))
1164     (let ((sqrt-1+z (complex-sqrt (1+z z)))
1165     (sqrt-1-z (complex-sqrt (1-z z))))
1166     (with-float-traps-masked (:divide-by-zero)
1167     (complex (* 2 (atan (/ (realpart sqrt-1-z)
1168     (realpart sqrt-1+z))))
1169     (asinh (imagpart (* (conjugate sqrt-1+z)
1170     sqrt-1-z))))))))
1171 ram 1.17
1172     (defun complex-acosh (z)
1173     "Compute acosh z = 2 * log(sqrt((z+1)/2) + sqrt((z-1)/2))
1174    
1175     Z may be any number, but the result is always a complex."
1176     (declare (number z))
1177 rtoy 1.38 (let ((sqrt-z-1 (complex-sqrt (z-1 z)))
1178     (sqrt-z+1 (complex-sqrt (z+1 z))))
1179 dtc 1.21 (with-float-traps-masked (:divide-by-zero)
1180     (complex (asinh (realpart (* (conjugate sqrt-z-1)
1181     sqrt-z+1)))
1182     (* 2 (atan (/ (imagpart sqrt-z-1)
1183     (realpart sqrt-z+1))))))))
1184 ram 1.17
1185    
1186     (defun complex-asin (z)
1187     "Compute asin z = asinh(i*z)/i
1188    
1189     Z may be any number, but the result is always a complex."
1190     (declare (number z))
1191 rtoy 1.41 (if (and (realp z) (> z 1))
1192     ;; asin is continuous in quadrant IV in this case.
1193     (complex-asin (complex z -0f0))
1194     (let ((sqrt-1-z (complex-sqrt (1-z z)))
1195     (sqrt-1+z (complex-sqrt (1+z z))))
1196     (with-float-traps-masked (:divide-by-zero)
1197     (complex (atan (/ (realpart z)
1198     (realpart (* sqrt-1-z sqrt-1+z))))
1199     (asinh (imagpart (* (conjugate sqrt-1-z)
1200     sqrt-1+z))))))))
1201 ram 1.17
1202     (defun complex-asinh (z)
1203     "Compute asinh z = log(z + sqrt(1 + z*z))
1204    
1205     Z may be any number, but the result is always a complex."
1206     (declare (number z))
1207     ;; asinh z = -i * asin (i*z)
1208     (let* ((iz (complex (- (imagpart z)) (realpart z)))
1209     (result (complex-asin iz)))
1210     (complex (imagpart result)
1211     (- (realpart result)))))
1212    
1213     (defun complex-atan (z)
1214     "Compute atan z = atanh (i*z) / i
1215    
1216     Z may be any number, but the result is always a complex."
1217     (declare (number z))
1218     ;; atan z = -i * atanh (i*z)
1219     (let* ((iz (complex (- (imagpart z)) (realpart z)))
1220     (result (complex-atanh iz)))
1221     (complex (imagpart result)
1222     (- (realpart result)))))
1223    
1224     (defun complex-tan (z)
1225     "Compute tan z = -i * tanh(i * z)
1226    
1227     Z may be any number, but the result is always a complex."
1228     (declare (number z))
1229     ;; tan z = -i * tanh(i*z)
1230     (let* ((iz (complex (- (imagpart z)) (realpart z)))
1231     (result (complex-tanh iz)))
1232     (complex (imagpart result)
1233     (- (realpart result)))))
1234 toy 1.32

  ViewVC Help
Powered by ViewVC 1.1.5