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

Contents of /src/code/irrat.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.24 - (hide annotations)
Sat Nov 1 22:58:14 1997 UTC (16 years, 5 months ago) by dtc
Branch: MAIN
Changes since 1.23: +3 -3 lines
Improved support for (complex single-float) and (complex double-float)
types. Adds storage classes to the backend for these so they can be
stored in registers or on the stack without consing; new primitive
types etc. Also adds (simple-array (complex {single,double}-float))
array types to avoid consing and speed vectors operations.  All
these changes are conditional on the :complex-float feature. More work
is needed to exploit these changes: improving the type dispatch in the
various function; maybe compiler transforms or more VOPs to handle
common functions inline.
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 dtc 1.24 "$Header: /tiger/var/lib/cvsroots/cmucl/src/code/irrat.lisp,v 1.24 1997/11/01 22:58:14 dtc 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 ram 1.5 (proclaim '(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     (,function ,var))))
50    
51     ); eval-when (compile load eval)
52    
53    
54     ;;;; Stubs for the Unix math library.
55    
56     ;;; Please refer to the Unix man pages for details about these routines.
57    
58     ;;; Trigonometric.
59 ram 1.17 #-x86 (def-math-rtn "sin" 1)
60     #-x86 (def-math-rtn "cos" 1)
61     #-x86 (def-math-rtn "tan" 1)
62 wlott 1.1 (def-math-rtn "asin" 1)
63     (def-math-rtn "acos" 1)
64 ram 1.17 #-x86 (def-math-rtn "atan" 1)
65     #-x86 (def-math-rtn "atan2" 2)
66 wlott 1.1 (def-math-rtn "sinh" 1)
67     (def-math-rtn "cosh" 1)
68     (def-math-rtn "tanh" 1)
69 pw 1.18 (def-math-rtn "asinh" 1)
70     (def-math-rtn "acosh" 1)
71     (def-math-rtn "atanh" 1)
72 wlott 1.1
73     ;;; Exponential and Logarithmic.
74 dtc 1.19 #-x86 (def-math-rtn "exp" 1)
75     #-x86 (def-math-rtn "log" 1)
76     #-x86 (def-math-rtn "log10" 1)
77 wlott 1.1 (def-math-rtn "pow" 2)
78 ram 1.17 #-x86 (def-math-rtn "sqrt" 1)
79 wlott 1.1 (def-math-rtn "hypot" 2)
80 pw 1.18 #-hpux
81 ram 1.17 (def-math-rtn "log1p" 1)
82    
83     #+x86 ;; These are needed for use by byte-compiled files.
84     (progn
85     (defun %sin (x)
86 dtc 1.19 (declare (double-float x)
87 ram 1.17 (values double-float))
88     (%sin x))
89     (defun %sin-quick (x)
90     (declare (double-float x)
91     (values double-float))
92     (%sin-quick x))
93     (defun %cos (x)
94 dtc 1.19 (declare (double-float x)
95 ram 1.17 (values double-float))
96     (%cos x))
97     (defun %cos-quick (x)
98     (declare (double-float x)
99     (values double-float))
100     (%cos-quick x))
101     (defun %tan (x)
102     (declare (double-float x)
103     (values double-float))
104     (%tan x))
105     (defun %tan-quick (x)
106     (declare (double-float x)
107     (values double-float))
108     (%tan-quick x))
109     (defun %atan (x)
110     (declare (double-float x)
111     (values double-float))
112     (%atan x))
113     (defun %atan2 (x y)
114     (declare (double-float x y)
115     (values double-float))
116     (%atan2 x y))
117     (defun %exp (x)
118 dtc 1.19 (declare (double-float x)
119 ram 1.17 (values double-float))
120     (%exp x))
121     (defun %log (x)
122 dtc 1.19 (declare (double-float x)
123 ram 1.17 (values double-float))
124     (%log x))
125     (defun %log10 (x)
126 dtc 1.19 (declare (double-float x)
127 ram 1.17 (values double-float))
128     (%log10 x))
129     #+nil ;; notyet
130     (defun %pow (x y)
131     (declare (type (double-float 0d0) x)
132 dtc 1.19 (double-float y)
133 ram 1.17 (values (double-float 0d0)))
134     (%pow x y))
135     (defun %sqrt (x)
136 dtc 1.19 (declare (double-float x)
137 ram 1.17 (values double-float))
138     (%sqrt x))
139     (defun %scalbn (f ex)
140 dtc 1.19 (declare (double-float f)
141 ram 1.17 (type (signed-byte 32) ex)
142     (values double-float))
143     (%scalbn f ex))
144     (defun %scalb (f ex)
145     (declare (double-float f ex)
146     (values double-float))
147     (%scalb f ex))
148     (defun %logb (x)
149 dtc 1.19 (declare (double-float x)
150     (values double-float))
151     (%logb x))
152 ram 1.17 ) ; progn
153 wlott 1.1
154    
155     ;;;; Power functions.
156    
157     (defun exp (number)
158     "Return e raised to the power NUMBER."
159     (number-dispatch ((number number))
160     (handle-reals %exp number)
161     ((complex)
162     (* (exp (realpart number))
163     (cis (imagpart number))))))
164    
165     ;;; INTEXP -- Handle the rational base, integer power case.
166    
167     (defparameter *intexp-maximum-exponent* 10000)
168    
169 ram 1.6 ;;; This function precisely calculates base raised to an integral power. It
170     ;;; separates the cases by the sign of power, for efficiency reasons, as powers
171     ;;; can be calculated more efficiently if power is a positive integer. Values
172     ;;; of power are calculated as positive integers, and inverted if negative.
173     ;;;
174 wlott 1.1 (defun intexp (base power)
175     (when (> (abs power) *intexp-maximum-exponent*)
176     (cerror "Continue with calculation."
177     "The absolute value of ~S exceeds ~S."
178     power '*intexp-maximum-exponent* base power))
179     (cond ((minusp power)
180     (/ (intexp base (- power))))
181     ((eql base 2)
182     (ash 1 power))
183     (t
184     (do ((nextn (ash power -1) (ash power -1))
185     (total (if (oddp power) base 1)
186     (if (oddp power) (* base total) total)))
187     ((zerop nextn) total)
188     (setq base (* base base))
189     (setq power nextn)))))
190    
191    
192 ram 1.6 ;;; EXPT -- Public
193     ;;;
194     ;;; If an integer power of a rational, use INTEXP above. Otherwise, do
195     ;;; floating point stuff. If both args are real, we try %POW right off,
196     ;;; assuming it will return 0 if the result may be complex. If so, we call
197     ;;; COMPLEX-POW which directly computes the complex result. We also separate
198     ;;; the complex-real and real-complex cases from the general complex case.
199     ;;;
200 wlott 1.1 (defun expt (base power)
201 wlott 1.3 "Returns BASE raised to the POWER."
202 wlott 1.1 (if (zerop power)
203 ram 1.6 (1+ (* base power))
204 dtc 1.22 (labels (;; determine if the double float is an integer.
205     ;; 0 - not an integer
206     ;; 1 - an odd int
207     ;; 2 - an even int
208     (isint (ihi lo)
209     (declare (type (unsigned-byte 31) ihi)
210     (type (unsigned-byte 32) lo)
211     (optimize (speed 3) (safety 0)))
212     (let ((isint 0))
213     (declare (type fixnum isint))
214     (cond ((>= ihi #x43400000) ; exponent >= 53
215     (setq isint 2))
216     ((>= ihi #x3ff00000)
217     (let ((k (- (ash ihi -20) #x3ff))) ; exponent
218     (declare (type (mod 53) k))
219     (cond ((> k 20)
220     (let* ((shift (- 52 k))
221     (j (logand (ash lo (- shift))))
222     (j2 (ash j shift)))
223     (declare (type (mod 32) shift)
224     (type (unsigned-byte 32) j j2))
225     (when (= j2 lo)
226     (setq isint (- 2 (logand j 1))))))
227     ((= lo 0)
228     (let* ((shift (- 20 k))
229     (j (ash ihi (- shift)))
230     (j2 (ash j shift)))
231     (declare (type (mod 32) shift)
232     (type (unsigned-byte 31) j j2))
233     (when (= j2 ihi)
234     (setq isint (- 2 (logand j 1))))))))))
235     isint))
236     (real-expt (x y rtype)
237     (let ((x (coerce x 'double-float))
238     (y (coerce y 'double-float)))
239     (declare (double-float x y))
240     (let* ((x-hi (kernel:double-float-high-bits x))
241     (x-lo (kernel:double-float-low-bits x))
242     (x-ihi (logand x-hi #x7fffffff))
243     (y-hi (kernel:double-float-high-bits y))
244     (y-lo (kernel:double-float-low-bits y))
245     (y-ihi (logand y-hi #x7fffffff)))
246     (declare (type (signed-byte 32) x-hi y-hi)
247     (type (unsigned-byte 31) x-ihi y-ihi)
248     (type (unsigned-byte 32) x-lo y-lo))
249     ;; y==zero: x**0 = 1
250     (when (zerop (logior y-ihi y-lo))
251     (return-from real-expt (coerce 1d0 rtype)))
252     ;; +-NaN return x+y
253     (when (or (> x-ihi #x7ff00000)
254     (and (= x-ihi #x7ff00000) (/= x-lo 0))
255     (> y-ihi #x7ff00000)
256     (and (= y-ihi #x7ff00000) (/= y-lo 0)))
257     (return-from real-expt (coerce (+ x y) rtype)))
258     (let ((yisint (if (< x-hi 0) (isint y-ihi y-lo) 0)))
259     (declare (type fixnum yisint))
260     ;; special value of y
261     (when (and (zerop y-lo) (= y-ihi #x7ff00000))
262     ;; y is +-inf
263     (return-from real-expt
264     (cond ((and (= x-ihi #x3ff00000) (zerop x-lo))
265     ;; +-1**inf is NaN
266     (coerce (- y y) rtype))
267     ((>= x-ihi #x3ff00000)
268     ;; (|x|>1)**+-inf = inf,0
269     (if (>= y-hi 0)
270     (coerce y rtype)
271     (coerce 0 rtype)))
272     (t
273     ;; (|x|<1)**-,+inf = inf,0
274     (if (< y-hi 0)
275     (coerce (- y) rtype)
276     (coerce 0 rtype))))))
277    
278     (let ((abs-x (abs x)))
279     (declare (double-float abs-x))
280     ;; special value of x
281     (when (and (zerop x-lo)
282     (or (= x-ihi #x7ff00000) (zerop x-ihi)
283     (= x-ihi #x3ff00000)))
284     ;; x is +-0,+-inf,+-1
285     (let ((z (if (< y-hi 0)
286     (/ 1 abs-x) ; z = (1/|x|)
287     abs-x)))
288     (declare (double-float z))
289     (when (< x-hi 0)
290     (cond ((and (= x-ihi #x3ff00000) (zerop yisint))
291     ;; (-1)**non-int
292     (let ((y*pi (* y pi)))
293     (declare (double-float y*pi))
294     (return-from real-expt
295 dtc 1.24 (complex
296 dtc 1.22 (coerce (%cos y*pi) rtype)
297     (coerce (%sin y*pi) rtype)))))
298     ((= yisint 1)
299     ;; (x<0)**odd = -(|x|**odd)
300     (setq z (- z)))))
301     (return-from real-expt (coerce z rtype))))
302    
303     (if (>= x-hi 0)
304     ;; x>0
305     (coerce (kernel::%pow x y) rtype)
306     ;; x<0
307     (let ((pow (kernel::%pow abs-x y)))
308     (declare (double-float pow))
309     (case yisint
310     (1 ; Odd
311     (coerce (* -1d0 pow) rtype))
312     (2 ; Even
313     (coerce pow rtype))
314     (t ; Non-integer
315     (let ((y*pi (* y pi)))
316     (declare (double-float y*pi))
317 dtc 1.24 (complex
318 dtc 1.22 (coerce (* pow (%cos y*pi)) rtype)
319     (coerce (* pow (%sin y*pi)) rtype)))))))))))))
320     (declare (inline real-expt))
321     (number-dispatch ((base number) (power number))
322     (((foreach fixnum (or bignum ratio) (complex rational)) integer)
323     (intexp base power))
324     (((foreach single-float double-float) rational)
325     (real-expt base power '(dispatch-type base)))
326     (((foreach fixnum (or bignum ratio) single-float)
327     (foreach ratio single-float))
328     (real-expt base power 'single-float))
329     (((foreach fixnum (or bignum ratio) single-float double-float)
330     double-float)
331     (real-expt base power 'double-float))
332     ((double-float single-float)
333     (real-expt base power 'double-float))
334     (((foreach (complex rational) (complex float)) rational)
335     (* (expt (abs base) power)
336     (cis (* power (phase base)))))
337     (((foreach fixnum (or bignum ratio) single-float double-float)
338     complex)
339     (if (and (zerop base) (plusp (realpart power)))
340     (* base power)
341     (exp (* power (log base)))))
342     (((foreach (complex float) (complex rational))
343     (foreach complex double-float single-float))
344     (if (and (zerop base) (plusp (realpart power)))
345     (* base power)
346     (exp (* power (log base)))))))))
347 wlott 1.1
348     (defun log (number &optional (base nil base-p))
349 wlott 1.3 "Return the logarithm of NUMBER in the base BASE, which defaults to e."
350 wlott 1.1 (if base-p
351 pw 1.18 (if (zerop base)
352     base ; ANSI spec
353     (/ (log number) (log base)))
354 wlott 1.1 (number-dispatch ((number number))
355 ram 1.17 (((foreach fixnum bignum ratio))
356 wlott 1.3 (if (minusp number)
357     (complex (log (- number)) (coerce pi 'single-float))
358     (coerce (%log (coerce number 'double-float)) 'single-float)))
359 ram 1.17 (((foreach single-float double-float))
360     ;; Is (log -0) -infinity (libm.a) or -infinity + i*pi (Kahan)?
361     ;; Since this doesn't seem to be an implementation issue
362     ;; I (pw) take the Kahan result.
363     (if (< (float-sign number)
364     (coerce 0 '(dispatch-type number)))
365     (complex (log (- number)) (coerce pi '(dispatch-type number)))
366     (coerce (%log (coerce number 'double-float))
367     '(dispatch-type number))))
368     ((complex)
369     (complex-log number)))))
370 wlott 1.1
371     (defun sqrt (number)
372     "Return the square root of NUMBER."
373     (number-dispatch ((number number))
374 ram 1.17 (((foreach fixnum bignum ratio))
375 wlott 1.3 (if (minusp number)
376 ram 1.17 (complex-sqrt number)
377 wlott 1.3 (coerce (%sqrt (coerce number 'double-float)) 'single-float)))
378 ram 1.17 (((foreach single-float double-float))
379     ;; NOTE there is a problem with (at least x86 NPX) of what result
380     ;; should be returned for (sqrt -0.0). The x86 hardware FSQRT
381     ;; instruction returns -0d0. The result is that Python will perhaps
382     ;; note the following test in generic sqrt, non-negatively constrained
383     ;; float types will be passed to FSQRT (or libm on other boxes).
384     ;; So, in the interest of consistency of compiled and interpreted
385     ;; codes, the following test is disabled for now. Maybe the float-sign
386     ;; test could be moved to the optimization codes.
387     (if (< (#+nil float-sign #-nil identity number)
388     (coerce 0 '(dispatch-type number)))
389     (complex-sqrt number)
390     (coerce (%sqrt (coerce number 'double-float))
391     '(dispatch-type number))))
392     ((complex)
393     (complex-sqrt number))))
394 wlott 1.1
395    
396     ;;;; Trigonometic and Related Functions
397    
398 wlott 1.2 (defun abs (number)
399     "Returns the absolute value of the number."
400     (number-dispatch ((number number))
401     (((foreach single-float double-float fixnum rational))
402     (abs number))
403     ((complex)
404     (let ((rx (realpart number))
405     (ix (imagpart number)))
406     (etypecase rx
407     (rational
408     (sqrt (+ (* rx rx) (* ix ix))))
409     (single-float
410     (coerce (%hypot (coerce rx 'double-float)
411     (coerce ix 'double-float))
412     'single-float))
413     (double-float
414     (%hypot rx ix)))))))
415 wlott 1.1
416     (defun phase (number)
417     "Returns the angle part of the polar representation of a complex number.
418 wlott 1.3 For complex numbers, this is (atan (imagpart number) (realpart number)).
419 wlott 1.1 For non-complex positive numbers, this is 0. For non-complex negative
420     numbers this is PI."
421 wlott 1.3 (etypecase number
422 ram 1.17 (rational
423 wlott 1.3 (if (minusp number)
424     (coerce pi 'single-float)
425     0.0f0))
426 ram 1.17 (single-float
427     (if (minusp (float-sign number))
428     (coerce pi 'single-float)
429     0.0f0))
430 wlott 1.3 (double-float
431 ram 1.17 (if (minusp (float-sign number))
432 wlott 1.3 (coerce pi 'double-float)
433     0.0d0))
434     (complex
435     (atan (imagpart number) (realpart number)))))
436 wlott 1.1
437    
438     (defun sin (number)
439 wlott 1.3 "Return the sine of NUMBER."
440 wlott 1.1 (number-dispatch ((number number))
441     (handle-reals %sin number)
442     ((complex)
443     (let ((x (realpart number))
444     (y (imagpart number)))
445 ram 1.17 (complex (* (sin x) (cosh y))
446     (* (cos x) (sinh y)))))))
447 wlott 1.1
448     (defun cos (number)
449 wlott 1.3 "Return the cosine of NUMBER."
450 wlott 1.1 (number-dispatch ((number number))
451     (handle-reals %cos number)
452     ((complex)
453     (let ((x (realpart number))
454     (y (imagpart number)))
455 ram 1.17 (complex (* (cos x) (cosh y))
456     (- (* (sin x) (sinh y))))))))
457 wlott 1.1
458     (defun tan (number)
459 wlott 1.3 "Return the tangent of NUMBER."
460 wlott 1.1 (number-dispatch ((number number))
461     (handle-reals %tan number)
462     ((complex)
463 ram 1.17 (complex-tan number))))
464 wlott 1.1
465 wlott 1.2 (defun cis (theta)
466 wlott 1.3 "Return cos(Theta) + i sin(Theta), AKA exp(i Theta)."
467 wlott 1.2 (if (complexp theta)
468     (error "Argument to CIS is complex: ~S" theta)
469     (complex (cos theta) (sin theta))))
470 wlott 1.1
471     (defun asin (number)
472 wlott 1.3 "Return the arc sine of NUMBER."
473 wlott 1.1 (number-dispatch ((number number))
474 wlott 1.3 ((rational)
475     (if (or (> number 1) (< number -1))
476     (complex-asin number)
477     (coerce (%asin (coerce number 'double-float)) 'single-float)))
478     (((foreach single-float double-float))
479     (if (or (> number (coerce 1 '(dispatch-type number)))
480     (< number (coerce -1 '(dispatch-type number))))
481     (complex-asin number)
482     (coerce (%asin (coerce number 'double-float))
483     '(dispatch-type number))))
484 wlott 1.1 ((complex)
485 wlott 1.3 (complex-asin number))))
486 wlott 1.1
487     (defun acos (number)
488 wlott 1.3 "Return the arc cosine of NUMBER."
489 wlott 1.1 (number-dispatch ((number number))
490 wlott 1.3 ((rational)
491     (if (or (> number 1) (< number -1))
492     (complex-acos number)
493     (coerce (%acos (coerce number 'double-float)) 'single-float)))
494     (((foreach single-float double-float))
495     (if (or (> number (coerce 1 '(dispatch-type number)))
496     (< number (coerce -1 '(dispatch-type number))))
497     (complex-acos number)
498     (coerce (%acos (coerce number 'double-float))
499     '(dispatch-type number))))
500 wlott 1.1 ((complex)
501 wlott 1.3 (complex-acos number))))
502 wlott 1.1
503    
504     (defun atan (y &optional (x nil xp))
505 wlott 1.3 "Return the arc tangent of Y if X is omitted or Y/X if X is supplied."
506 wlott 1.1 (if xp
507 wlott 1.12 (flet ((atan2 (y x)
508 wlott 1.13 (declare (type double-float y x)
509     (values double-float))
510     (if (zerop x)
511     (if (zerop y)
512     (if (plusp (float-sign x))
513     y
514     (float-sign y pi))
515     (float-sign y (/ pi 2)))
516     (%atan2 y x))))
517     (number-dispatch ((y number) (x number))
518     ((double-float
519     (foreach double-float single-float fixnum bignum ratio))
520     (atan2 y (coerce x 'double-float)))
521     (((foreach single-float fixnum bignum ratio)
522     double-float)
523     (atan2 (coerce y 'double-float) x))
524     (((foreach single-float fixnum bignum ratio)
525     (foreach single-float fixnum bignum ratio))
526     (coerce (atan2 (coerce y 'double-float) (coerce x 'double-float))
527     'single-float))))
528 wlott 1.1 (number-dispatch ((y number))
529     (handle-reals %atan y)
530     ((complex)
531 ram 1.17 (complex-atan y)))))
532 wlott 1.3
533 ram 1.17 ;; It seems that everyone has a C version of sinh, cosh, and
534     ;; tanh. Let's use these for reals because the original
535     ;; implementations based on the definitions lose big in round-off
536     ;; error. These bad definitions also mean that sin and cos for
537     ;; complex numbers can also lose big.
538 wlott 1.3
539 ram 1.17 #+nil
540 wlott 1.3 (defun sinh (number)
541     "Return the hyperbolic sine of NUMBER."
542     (/ (- (exp number) (exp (- number))) 2))
543    
544 ram 1.17 (defun sinh (number)
545     "Return the hyperbolic sine of NUMBER."
546     (number-dispatch ((number number))
547     (handle-reals %sinh number)
548     ((complex)
549     (let ((x (realpart number))
550     (y (imagpart number)))
551     (complex (* (sinh x) (cos y))
552     (* (cosh x) (sin y)))))))
553    
554     #+nil
555 wlott 1.3 (defun cosh (number)
556     "Return the hyperbolic cosine of NUMBER."
557     (/ (+ (exp number) (exp (- number))) 2))
558    
559 ram 1.17 (defun cosh (number)
560     "Return the hyperbolic cosine of NUMBER."
561     (number-dispatch ((number number))
562     (handle-reals %cosh number)
563     ((complex)
564     (let ((x (realpart number))
565     (y (imagpart number)))
566     (complex (* (cosh x) (cos y))
567     (* (sinh x) (sin y)))))))
568    
569 wlott 1.3 (defun tanh (number)
570     "Return the hyperbolic tangent of NUMBER."
571 ram 1.17 (number-dispatch ((number number))
572     (handle-reals %tanh number)
573     ((complex)
574     (complex-tanh number))))
575 wlott 1.3
576     (defun asinh (number)
577     "Return the hyperbolic arc sine of NUMBER."
578 ram 1.17 (number-dispatch ((number number))
579     (handle-reals %asinh number)
580     ((complex)
581     (complex-asinh number))))
582 wlott 1.3
583     (defun acosh (number)
584     "Return the hyperbolic arc cosine of NUMBER."
585 ram 1.17 (number-dispatch ((number number))
586     ((rational)
587     ;; acosh is complex if number < 1
588     (if (< number 1)
589     (complex-acosh number)
590     (coerce (%acosh (coerce number 'double-float)) 'single-float)))
591     (((foreach single-float double-float))
592     (if (< number (coerce 1 '(dispatch-type number)))
593     (complex-acosh number)
594     (coerce (%acosh (coerce number 'double-float))
595     '(dispatch-type number))))
596     ((complex)
597     (complex-acosh number))))
598 wlott 1.3
599     (defun atanh (number)
600     "Return the hyperbolic arc tangent of NUMBER."
601 ram 1.17 (number-dispatch ((number number))
602     ((rational)
603     ;; atanh is complex if |number| > 1
604     (if (or (> number 1) (< number -1))
605     (complex-atanh number)
606     (coerce (%atanh (coerce number 'double-float)) 'single-float)))
607     (((foreach single-float double-float))
608     (if (or (> number (coerce 1 '(dispatch-type number)))
609     (< number (coerce -1 '(dispatch-type number))))
610     (complex-atanh number)
611     (coerce (%atanh (coerce number 'double-float))
612     '(dispatch-type number))))
613     ((complex)
614     (complex-atanh number))))
615 wlott 1.14
616 pw 1.18 ;;; HP-UX does not supply a C version of log1p, so
617     ;;; use the definition.
618 wlott 1.14
619     #+hpux
620 pw 1.18 (declaim (inline %log1p))
621 wlott 1.14 #+hpux
622 pw 1.18 (defun %log1p (number)
623     (declare (double-float number)
624     (optimize (speed 3) (safety 0)))
625     (the double-float (log (the (double-float 0d0) (+ number 1d0)))))
626 ram 1.17
627    
628     #+old-elfun
629     (progn
630     ;;; Here are the old definitions of the special functions, for
631     ;;; complex-valued arguments. Some of these functions suffer from
632     ;;; severe round-off error or unnecessary overflow.
633    
634     (proclaim '(inline mult-by-i))
635     (defun mult-by-i (number)
636     (complex (- (imagpart number))
637     (realpart number)))
638    
639     (defun complex-sqrt (x)
640     (exp (/ (log x) 2)))
641    
642     (defun complex-log (x)
643     (complex (log (abs x))
644     (phase x)))
645    
646     (defun complex-atanh (number)
647 ram 1.16 (/ (- (log (1+ number)) (log (- 1 number))) 2))
648 ram 1.17
649     (defun complex-tanh (number)
650     (/ (- (exp number) (exp (- number)))
651     (+ (exp number) (exp (- number)))))
652    
653     (defun complex-acos (number)
654     (* -2 (mult-by-i (log (+ (sqrt (/ (1+ number) 2))
655     (mult-by-i (sqrt (/ (- 1 number) 2))))))))
656    
657     (defun complex-acosh (number)
658     (* 2 (log (+ (sqrt (/ (1+ number) 2)) (sqrt (/ (1- number) 2))))))
659    
660     (defun complex-asin (number)
661     (- (mult-by-i (log (+ (mult-by-i number) (sqrt (- 1 (* number number))))))))
662    
663     (defun complex-asinh (number)
664     (log (+ number (sqrt (1+ (* number number))))))
665    
666     (defun complex-atan (y)
667     (let ((im (imagpart y))
668     (re (realpart y)))
669     (/ (- (log (complex (- 1 im) re))
670     (log (complex (+ 1 im) (- re))))
671     (complex 0 2))))
672    
673     (defun complex-tan (number)
674     (let* ((num (sin number))
675     (denom (cos number)))
676     (if (zerop denom) (error "~S undefined tangent." number)
677     (/ num denom))))
678     )
679    
680     #-old-specfun
681     (progn
682     ;;;;
683     ;;;; This is a set of routines that implement many elementary
684     ;;;; transcendental functions as specified by ANSI Common Lisp. The
685     ;;;; implementation is based on Kahan's paper.
686     ;;;;
687     ;;;; I believe I have accurately implemented the routines and are
688     ;;;; correct, but you may want to check for your self.
689     ;;;;
690     ;;;; These functions are written for CMU Lisp and take advantage of
691     ;;;; some of the features available there. It may be possible,
692     ;;;; however, to port this to other Lisps.
693     ;;;;
694     ;;;; Some functions are significantly more accurate than the original
695     ;;;; definitions in CMU Lisp. In fact, some functions in CMU Lisp
696     ;;;; give the wrong answer like (acos #c(-2.0 0.0)), where the true
697     ;;;; answer is pi + i*log(2-sqrt(3)).
698     ;;;;
699     ;;;; All of the implemented functions will take any number for an
700     ;;;; input, but the result will always be a either a complex
701     ;;;; single-float or a complex double-float.
702     ;;;;
703     ;;;; General functions
704     ;;;; complex-sqrt
705     ;;;; complex-log
706     ;;;; complex-atanh
707     ;;;; complex-tanh
708     ;;;; complex-acos
709     ;;;; complex-acosh
710     ;;;; complex-asin
711     ;;;; complex-asinh
712     ;;;; complex-atan
713     ;;;; complex-tan
714     ;;;;
715     ;;;; Utility functions:
716     ;;;; scalb logb
717     ;;;;
718     ;;;; Internal functions:
719     ;;;; square coerce-to-complex-type cssqs complex-log-scaled
720     ;;;;
721     ;;;;
722     ;;;; Please send any bug reports, comments, or improvements to Raymond
723     ;;;; Toy at toy@rtp.ericsson.se.
724     ;;;;
725     ;;;; References
726     ;;;;
727     ;;;; Kahan, W. "Branch Cuts for Complex Elementary Functions, or Much
728     ;;;; Ado About Nothing's Sign Bit" in Iserles and Powell (eds.) "The
729     ;;;; State of the Art in Numerical Analysis", pp. 165-211, Clarendon
730     ;;;; Press, 1987
731     ;;;;
732     (declaim (inline square))
733     (declaim (ftype (function (double-float) (double-float 0d0)) square))
734     (defun square (x)
735     (declare (double-float x)
736     (values (double-float 0d0)))
737     (* x x))
738    
739     ;; If you have these functions in libm, perhaps they should be used
740     ;; instead of these Lisp versions. These versions are probably good
741     ;; enough, especially since they are portable.
742    
743 dtc 1.19 (declaim (inline scalb))
744 ram 1.17 (defun scalb (x n)
745     "Compute 2^N * X without compute 2^N first (use properties of the
746     underlying floating-point format"
747     (declare (type double-float x)
748 dtc 1.19 (type double-float-exponent n))
749 ram 1.17 (scale-float x n))
750    
751     (defun logb (x)
752     "Compute an integer N such that 1 <= |2^N * x| < 2.
753     For the special cases, the following values are used:
754    
755     x logb
756     NaN NaN
757     +/- infinity +infinity
758     0 -infinity
759     "
760     (declare (type double-float x))
761     (cond ((float-trapping-nan-p x)
762     x)
763     ((float-infinity-p x)
764     #.ext:double-float-positive-infinity)
765     ((zerop x)
766     ;; The answer is negative infinity, but we are supposed to
767     ;; signal divide-by-zero.
768     ;; (error 'division-by-zero :operation 'logb :operands (list x))
769     (/ -1.0d0 x)
770     )
771     (t
772     (multiple-value-bind (signif expon sign)
773     (decode-float x)
774     (declare (ignore signif sign))
775     ;; decode-float is almost right, except that the exponent
776     ;; is off by one
777     (1- expon)))))
778    
779     ;; This function is used to create a complex number of the appropriate
780     ;; type.
781    
782     (declaim (inline coerce-to-complex-type))
783     (defun coerce-to-complex-type (x y z)
784     "Create complex number with real part X and imaginary part Y such that
785     it has the same type as Z. If Z has type (complex rational), the X
786     and Y are coerced to single-float."
787     (declare (double-float x y)
788     (number z))
789     (if (subtypep (type-of (realpart z)) 'double-float)
790     (complex x y)
791     ;; Convert anything that's not a double-float to a single-float.
792     (complex (float x 1.0)
793     (float y 1.0))))
794    
795     (defun cssqs (z)
796 dtc 1.21 ;; Compute |(x+i*y)/2^k|^2 scaled to avoid over/underflow. The
797     ;; result is r + i*k, where k is an integer.
798 ram 1.17
799     ;; Save all FP flags
800 dtc 1.21 (let ((x (float (realpart z) 1d0))
801     (y (float (imagpart z) 1d0))
802     (k 0)
803     (rho 0d0))
804 ram 1.17 (declare (double-float x y)
805     (type (double-float 0d0) rho)
806     (fixnum k))
807 dtc 1.21 ;; Would this be better handled using an exception handler to
808     ;; catch the overflow or underflow signal? For now, we turn all
809     ;; traps off and look at the accrued exceptions to see if any
810     ;; signal would have been raised.
811     (with-float-traps-masked (:underflow :overflow)
812     (setf rho (+ (square x) (square y)))
813     (cond ((and (or (float-trapping-nan-p rho)
814     (float-infinity-p rho))
815     (or (float-infinity-p (abs x))
816     (float-infinity-p (abs y))))
817     (setf rho #.ext:double-float-positive-infinity))
818     ((let ((threshold #.(/ least-positive-double-float
819     double-float-epsilon))
820 dtc 1.23 (traps (ldb vm::float-sticky-bits
821 dtc 1.21 (vm:floating-point-modes))))
822     ;; Overflow raised or (underflow raised and rho <
823     ;; lambda/eps)
824     (or (not (zerop (logand vm:float-overflow-trap-bit traps)))
825     (and (not (zerop (logand vm:float-underflow-trap-bit traps)))
826     (< rho threshold))))
827     (setf k (logb (max (abs x) (abs y))))
828     (setf rho (+ (square (scalb x (- k)))
829     (square (scalb y (- k))))))))
830     (values rho k)))
831 ram 1.17
832     (defun complex-sqrt (z)
833     "Principle square root of Z
834    
835     Z may be any number, but the result is always a complex."
836     (declare (number z))
837     (multiple-value-bind (rho k)
838     (cssqs z)
839     (declare (type (double-float 0d0) rho)
840     (fixnum k))
841     (let ((x (float (realpart z) 1.0d0))
842     (y (float (imagpart z) 1.0d0))
843     (eta 0d0)
844     (nu 0d0))
845     (declare (double-float x y eta nu))
846    
847     (if (not (float-trapping-nan-p x))
848     (setf rho (+ (scalb (abs x) (- k)) (sqrt rho))))
849    
850     (cond ((oddp k)
851     (setf k (ash k -1)))
852     (t
853     (setf k (1- (ash k -1)))
854     (setf rho (+ rho rho))))
855    
856     (setf rho (scalb (sqrt rho) k))
857    
858     (setf eta rho)
859     (setf nu y)
860    
861     (when (/= rho 0d0)
862     (when (not (float-infinity-p (abs nu)))
863     (setf nu (/ (/ nu rho) 2d0)))
864     (when (< x 0d0)
865     (setf eta (abs nu))
866     (setf nu (float-sign y rho))))
867     (coerce-to-complex-type eta nu z))))
868    
869     (defun complex-log-scaled (z j)
870     "Compute log(2^j*z).
871    
872     This is for use with J /= 0 only when |z| is huge."
873     (declare (number z)
874     (fixnum j))
875     ;; The constants t0, t1, t2 should be evaluated to machine
876     ;; precision. In addition, Kahan says the accuracy of log1p
877     ;; influences the choices of these constants but doesn't say how to
878     ;; choose them. We'll just assume his choices matches our
879     ;; implementation of log1p.
880     (let ((t0 #.(/ 1 (sqrt 2.0d0)))
881     (t1 1.2d0)
882     (t2 3d0)
883     (ln2 #.(log 2d0))
884     (x (float (realpart z) 1.0d0))
885     (y (float (imagpart z) 1.0d0)))
886     (multiple-value-bind (rho k)
887     (cssqs z)
888     (declare (type (double-float 0d0) rho)
889     (fixnum k))
890     (let ((beta (max (abs x) (abs y)))
891     (theta (min (abs x) (abs y))))
892     (declare (type (double-float 0d0) beta theta))
893     (if (and (zerop k)
894     (< t0 beta)
895     (or (<= beta t1)
896     (< rho t2)))
897     (setf rho (/ (%log1p (+ (* (- beta 1.0d0)
898     (+ beta 1.0d0))
899     (* theta theta)))
900     2d0))
901     (setf rho (+ (/ (log rho) 2d0)
902     (* (+ k j) ln2))))
903     (setf theta (atan y x))
904     (coerce-to-complex-type rho theta z)))))
905    
906     (defun complex-log (z)
907     "Log of Z = log |Z| + i * arg Z
908    
909     Z may be any number, but the result is always a complex."
910     (declare (number z))
911     (complex-log-scaled z 0))
912    
913     ;; Let us note the following "strange" behavior. atanh 1.0d0 is
914     ;; +infinity, but the following code returns approx 176 + i*pi/4. The
915     ;; reason for the imaginary part is caused by the fact that arg i*y is
916     ;; never 0 since we have positive and negative zeroes.
917    
918     (defun complex-atanh (z)
919     "Compute atanh z = (log(1+z) - log(1-z))/2"
920     (declare (number z))
921     (let* (;; Constants
922     (theta #.(/ (sqrt most-positive-double-float) 4.0d0))
923     (rho #.(/ 4.0d0 (sqrt most-positive-double-float)))
924     (half-pi #.(/ pi 2.0d0))
925     (rp (float (realpart z) 1.0d0))
926     (beta (float-sign rp 1.0d0))
927     (x (* beta rp))
928     (y (* beta (- (float (imagpart z) 1.0d0))))
929     (eta 0.0d0)
930     (nu 0.0d0))
931     (declare (double-float theta rho half-pi rp beta y eta nu)
932     (type (double-float 0d0) x))
933     (cond ((or (> x theta)
934     (> (abs y) theta))
935     ;; To avoid overflow...
936     (setf eta (float-sign y half-pi))
937     ;; nu is real part of 1/(x + iy). This is x/(x^2+y^2),
938     ;; which can cause overflow. Arrange this computation so
939     ;; that it won't overflow.
940     (setf nu (let* ((x-bigger (> x (abs y)))
941     (r (if x-bigger (/ y x) (/ x y)))
942     (d (+ 1.0d0 (* r r))))
943     (declare (double-float r d))
944     (if x-bigger
945     (/ (/ x) d)
946     (/ (/ r y) d)))))
947     ((= x 1.0d0)
948     ;; Should this be changed so that if y is zero, eta is set
949     ;; to +infinity instead of approx 176? In any case
950     ;; tanh(176) is 1.0d0 within working precision.
951     (let ((t1 (+ 4d0 (square y)))
952     (t2 (+ (abs y) rho)))
953     (declare (type (double-float 0d0) t1 t2))
954     #+nil
955     (setf eta (log (/ (sqrt (sqrt t1)))
956     (sqrt t2)))
957     (setf eta (* 0.5d0 (log (the (double-float 0.0d0)
958     (/ (sqrt t1) t2)))))
959     (setf nu (* 0.5d0
960     (float-sign y
961     (+ half-pi (atan (* 0.5d0 t2))))))))
962     (t
963     (let ((t1 (+ (abs y) rho)))
964     (declare (double-float t1))
965     ;; Normal case using log1p(x) = log(1 + x)
966     (setf eta (* 0.25d0
967     (%log1p (/ (* 4.0d0 x)
968     (+ (square (- 1.0d0 x))
969     (square t1))))))
970     (setf nu (* 0.5d0
971     (atan (* 2.0d0 y)
972     (- (* (- 1.0d0 x)
973     (+ 1.0d0 x))
974     (square t1))))))))
975     (coerce-to-complex-type (* beta eta)
976     (- (* beta nu))
977     z)))
978    
979     (defun complex-tanh (z)
980     "Compute tanh z = sinh z / cosh z"
981     (declare (number z))
982     (let ((x (float (realpart z) 1.0d0))
983     (y (float (imagpart z) 1.0d0)))
984     (declare (double-float x y))
985     (cond ((> (abs x)
986 pw 1.18 #-(or linux hpux) #.(/ (%asinh most-positive-double-float) 4d0)
987 ram 1.17 ;; This is more accurate under linux.
988 pw 1.18 #+(or linux hpux) #.(/ (+ (%log 2.0d0)
989     (%log most-positive-double-float)) 4d0))
990 ram 1.17 (complex (float-sign x)
991     (float-sign y 0.0d0)))
992     (t
993 dtc 1.20 (let* ((tv (%tan y))
994 ram 1.17 (beta (+ 1.0d0 (* tv tv)))
995     (s (sinh x))
996     (rho (sqrt (+ 1.0d0 (* s s)))))
997     (declare (double-float tv s)
998     (type (double-float 0.0d0) beta rho))
999     (if (float-infinity-p (abs tv))
1000     (coerce-to-complex-type (/ rho s)
1001     (/ tv)
1002     z)
1003     (let ((den (+ 1.0d0 (* beta s s))))
1004     (coerce-to-complex-type (/ (* beta rho s)
1005     den)
1006     (/ tv den)
1007     z))))))))
1008    
1009     ;; Kahan says we should only compute the parts needed. Thus, the
1010     ;; realpart's below should only compute the real part, not the whole
1011     ;; complex expression. Doing this can be important because we may get
1012     ;; spurious signals that occur in the part that we are not using.
1013     ;;
1014     ;; However, we take a pragmatic approach and just use the whole
1015     ;; expression.
1016    
1017     ;; NOTE: The formula given by Kahan is somewhat ambiguous in whether
1018     ;; it's the conjugate of the square root or the square root of the
1019     ;; conjugate. This needs to be checked.
1020    
1021     ;; I checked. It doesn't matter because (conjugate (sqrt z)) is the
1022     ;; same as (sqrt (conjugate z)) for all z. This follows because
1023     ;;
1024     ;; (conjugate (sqrt z)) = exp(0.5*log |z|)*exp(-0.5*j*arg z).
1025     ;;
1026     ;; (sqrt (conjugate z)) = exp(0.5*log|z|)*exp(0.5*j*arg conj z)
1027     ;;
1028     ;;.and these two expressions are equal if and only if arg conj z =
1029     ;;-arg z, which is clearly true for all z.
1030    
1031     (defun complex-acos (z)
1032     "Compute acos z = pi/2 - asin z
1033    
1034     Z may be any number, but the result is always a complex."
1035     (declare (number z))
1036     (let ((sqrt-1+z (complex-sqrt (+ 1 z)))
1037     (sqrt-1-z (complex-sqrt (- 1 z))))
1038 dtc 1.21 (with-float-traps-masked (:divide-by-zero)
1039     (complex (* 2 (atan (/ (realpart sqrt-1-z)
1040     (realpart sqrt-1+z))))
1041     (asinh (imagpart (* (conjugate sqrt-1+z)
1042     sqrt-1-z)))))))
1043 ram 1.17
1044     (defun complex-acosh (z)
1045     "Compute acosh z = 2 * log(sqrt((z+1)/2) + sqrt((z-1)/2))
1046    
1047     Z may be any number, but the result is always a complex."
1048     (declare (number z))
1049     (let ((sqrt-z-1 (complex-sqrt (- z 1)))
1050     (sqrt-z+1 (complex-sqrt (+ z 1))))
1051 dtc 1.21 (with-float-traps-masked (:divide-by-zero)
1052     (complex (asinh (realpart (* (conjugate sqrt-z-1)
1053     sqrt-z+1)))
1054     (* 2 (atan (/ (imagpart sqrt-z-1)
1055     (realpart sqrt-z+1))))))))
1056 ram 1.17
1057    
1058     (defun complex-asin (z)
1059     "Compute asin z = asinh(i*z)/i
1060    
1061     Z may be any number, but the result is always a complex."
1062     (declare (number z))
1063     (let ((sqrt-1-z (complex-sqrt (- 1 z)))
1064     (sqrt-1+z (complex-sqrt (+ 1 z))))
1065 dtc 1.21 (with-float-traps-masked (:divide-by-zero)
1066     (complex (atan (/ (realpart z)
1067     (realpart (* sqrt-1-z sqrt-1+z))))
1068     (asinh (imagpart (* (conjugate sqrt-1-z)
1069     sqrt-1+z)))))))
1070 ram 1.17
1071     (defun complex-asinh (z)
1072     "Compute asinh z = log(z + sqrt(1 + z*z))
1073    
1074     Z may be any number, but the result is always a complex."
1075     (declare (number z))
1076     ;; asinh z = -i * asin (i*z)
1077     (let* ((iz (complex (- (imagpart z)) (realpart z)))
1078     (result (complex-asin iz)))
1079     (complex (imagpart result)
1080     (- (realpart result)))))
1081    
1082     (defun complex-atan (z)
1083     "Compute atan z = atanh (i*z) / i
1084    
1085     Z may be any number, but the result is always a complex."
1086     (declare (number z))
1087     ;; atan z = -i * atanh (i*z)
1088     (let* ((iz (complex (- (imagpart z)) (realpart z)))
1089     (result (complex-atanh iz)))
1090     (complex (imagpart result)
1091     (- (realpart result)))))
1092    
1093     (defun complex-tan (z)
1094     "Compute tan z = -i * tanh(i * z)
1095    
1096     Z may be any number, but the result is always a complex."
1097     (declare (number z))
1098     ;; tan z = -i * tanh(i*z)
1099     (let* ((iz (complex (- (imagpart z)) (realpart z)))
1100     (result (complex-tanh iz)))
1101     (complex (imagpart result)
1102     (- (realpart result)))))
1103     )

  ViewVC Help
Powered by ViewVC 1.1.5