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

Contents of /src/code/irrat.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.55.8.2 - (show annotations)
Fri Sep 26 20:13:10 2008 UTC (5 years, 6 months ago) by rtoy
Branch: sse2-branch
CVS Tags: sse2-packed-base, sse2-checkpoint-2008-10-01, sse2-merge-with-2008-10
Branch point for: sse2-packed-branch
Changes since 1.55.8.1: +24 -25 lines
Clean up conditionals.
1 ;;; -*- Mode: Lisp; Package: KERNEL; Log: code.log -*-
2 ;;;
3 ;;; **********************************************************************
4 ;;; 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 "$Header: /tiger/var/lib/cvsroots/cmucl/src/code/irrat.lisp,v 1.55.8.2 2008/09/26 20:13:10 rtoy Exp $")
9 ;;;
10 ;;; **********************************************************************
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 ;(defconstant e 2.71828182845904523536028747135266249775724709369996L0)
25
26 ;;; 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 (defmacro def-math-rtn (name num-args)
30 (let ((function (intern (concatenate 'simple-string
31 "%"
32 (string-upcase name)))))
33 `(progn
34 (declaim (inline ,function))
35 (export ',function)
36 (alien:def-alien-routine (,name ,function) double-float
37 ,@(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
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 #+double-double
51 ((double-double-float)
52 (,(symbolicate "DD-" function) ,var))))
53
54 ); eval-when (compile load eval)
55
56
57 ;;;; Stubs for the Unix math library.
58
59 ;;; Please refer to the Unix man pages for details about these routines.
60
61 ;;; Trigonometric.
62 #-(and x86 (not sse2))
63 (progn
64 ;; For x86 (without sse2), we can use x87 instructions to implement
65 ;; these. With sse2, we don't currently support that, so these
66 ;; should be disabled.
67 (def-math-rtn "sin" 1)
68 (def-math-rtn "cos" 1)
69 (def-math-rtn "tan" 1)
70 (def-math-rtn "atan" 1)
71 (def-math-rtn "atan2" 2))
72 (def-math-rtn "asin" 1)
73 (def-math-rtn "acos" 1)
74 (def-math-rtn "sinh" 1)
75 (def-math-rtn "cosh" 1)
76 (def-math-rtn "tanh" 1)
77 (def-math-rtn "asinh" 1)
78 (def-math-rtn "acosh" 1)
79 (def-math-rtn "atanh" 1)
80
81 ;;; Exponential and Logarithmic.
82 #-(and x86 (not sse2))
83 (progn
84 (def-math-rtn "exp" 1)
85 (def-math-rtn "log" 1)
86 (def-math-rtn "log10" 1))
87
88 (def-math-rtn "pow" 2)
89 #-(or x86 sparc-v7 sparc-v8 sparc-v9)
90 (def-math-rtn "sqrt" 1)
91 (def-math-rtn "hypot" 2)
92
93 ;; Don't want log1p to use the x87 instruction.
94 #-(or hpux (and x86 (not sse2)))
95 (def-math-rtn "log1p" 1)
96
97 ;; These are needed for use by byte-compiled files. But don't use
98 ;; these with sse2 since we don't support using the x87 instructions
99 ;; here.
100 #+(and x86 (not sse2))
101 (progn
102 #+nil
103 (defun %sin (x)
104 (declare (double-float x)
105 (values double-float))
106 (%sin x))
107 (defun %sin-quick (x)
108 (declare (double-float x)
109 (values double-float))
110 (%sin-quick x))
111 #+nil
112 (defun %cos (x)
113 (declare (double-float x)
114 (values double-float))
115 (%cos x))
116 (defun %cos-quick (x)
117 (declare (double-float x)
118 (values double-float))
119 (%cos-quick x))
120 #+nil
121 (defun %tan (x)
122 (declare (double-float x)
123 (values double-float))
124 (%tan x))
125 (defun %tan-quick (x)
126 (declare (double-float x)
127 (values double-float))
128 (%tan-quick x))
129 (defun %atan (x)
130 (declare (double-float x)
131 (values double-float))
132 (%atan x))
133 (defun %atan2 (x y)
134 (declare (double-float x y)
135 (values double-float))
136 (%atan2 x y))
137 (defun %exp (x)
138 (declare (double-float x)
139 (values double-float))
140 (%exp x))
141 (defun %log (x)
142 (declare (double-float x)
143 (values double-float))
144 (%log x))
145 (defun %log10 (x)
146 (declare (double-float x)
147 (values double-float))
148 (%log10 x))
149 #+nil ;; notyet
150 (defun %pow (x y)
151 (declare (type (double-float 0d0) x)
152 (double-float y)
153 (values (double-float 0d0)))
154 (%pow x y))
155 (defun %sqrt (x)
156 (declare (double-float x)
157 (values double-float))
158 (%sqrt x))
159 (defun %scalbn (f ex)
160 (declare (double-float f)
161 (type (signed-byte 32) ex)
162 (values double-float))
163 (%scalbn f ex))
164 (defun %scalb (f ex)
165 (declare (double-float f ex)
166 (values double-float))
167 (%scalb f ex))
168 (defun %logb (x)
169 (declare (double-float x)
170 (values double-float))
171 (%logb x))
172 (defun %log1p (x)
173 (declare (double-float x)
174 (values double-float))
175 (%log1p x))
176 ) ; progn
177
178
179 ;; As above for x86. It also seems to be needed to handle
180 ;; constant-folding in the compiler.
181 #+sparc
182 (progn
183 (defun %sqrt (x)
184 (declare (double-float x)
185 (values double-float))
186 (%sqrt x))
187 )
188
189 ;;; The standard libm routines for sin, cos, and tan on x86 (Linux)
190 ;;; and ppc are not very accurate for large arguments when compared to
191 ;;; sparc (and maxima). This is basically caused by the fact that
192 ;;; those libraries do not do an accurate argument reduction. The
193 ;;; following functions use some routines Sun's free fdlibm library to
194 ;;; do accurate reduction. Then we call the standard C functions (or
195 ;;; vops for x86) on the reduced argument. This produces much more
196 ;;; accurate values.
197
198 #+(or ppc x86)
199 (progn
200 (declaim (inline %%ieee754-rem-pi/2))
201 ;; Basic argument reduction routine. It returns two values: n and y
202 ;; such that (n + 8*k)*pi/2+y = x where |y|<pi/4 and n indicates in
203 ;; which octant the arg lies. Y is actually computed in two parts,
204 ;; y[0] and y[1] such that the sum is y, for accuracy.
205
206 (alien:def-alien-routine ("__ieee754_rem_pio2" %%ieee754-rem-pi/2) c-call:int
207 (x double-float)
208 (y (* double-float)))
209
210 ;; Same as above, but instead of needing to pass an array in, the
211 ;; output array is broken up into two output values instead. This is
212 ;; easier for the user, and we don't have to wrap calls with
213 ;; without-gcing.
214 (declaim (inline %ieee754-rem-pi/2))
215 (alien:def-alien-routine ("ieee754_rem_pio2" %ieee754-rem-pi/2) c-call:int
216 (x double-float)
217 (y0 double-float :out)
218 (y1 double-float :out))
219
220 )
221
222 #+(or ppc sse2)
223 (progn
224 (declaim (inline %%sin %%cos %%tan))
225 (macrolet ((frob (alien-name lisp-name)
226 `(alien:def-alien-routine (,alien-name ,lisp-name) double-float
227 (x double-float))))
228 (frob "sin" %%sin)
229 (frob "cos" %%cos)
230 (frob "tan" %%tan))
231 )
232
233 #+(or ppc x86)
234 (macrolet
235 ((frob (sin cos tan)
236 `(progn
237 ;; In all of the routines below, we just compute the sum of
238 ;; y0 and y1 and use that as the (reduced) argument for the
239 ;; trig functions. This is slightly less accurate than what
240 ;; fdlibm does, which calls special functions using y0 and
241 ;; y1 separately, for greater accuracy. This isn't
242 ;; implemented, and some spot checks indicate that what we
243 ;; have here is accurate.
244 ;;
245 ;; For x86 with an fsin/fcos/fptan instruction, the pi/4 is
246 ;; probably too restrictive.
247 (defun %sin (x)
248 (declare (double-float x))
249 (if (< (abs x) (/ pi 4))
250 (,sin x)
251 ;; Argument reduction needed
252 (multiple-value-bind (n y0 y1)
253 (%ieee754-rem-pi/2 x)
254 (let ((reduced (+ y0 y1)))
255 (case (logand n 3)
256 (0 (,sin reduced))
257 (1 (,cos reduced))
258 (2 (- (,sin reduced)))
259 (3 (- (,cos reduced))))))))
260 (defun %cos (x)
261 (declare (double-float x))
262 (if (< (abs x) (/ pi 4))
263 (,cos x)
264 ;; Argument reduction needed
265 (multiple-value-bind (n y0 y1)
266 (%ieee754-rem-pi/2 x)
267 (let ((reduced (+ y0 y1)))
268 (case (logand n 3)
269 (0 (,cos reduced))
270 (1 (- (,sin reduced)))
271 (2 (- (,cos reduced)))
272 (3 (,sin reduced)))))))
273 (defun %tan (x)
274 (declare (double-float x))
275 (if (< (abs x) (/ pi 4))
276 (,tan x)
277 ;; Argument reduction needed
278 (multiple-value-bind (n y0 y1)
279 (%ieee754-rem-pi/2 x)
280 (let ((reduced (+ y0 y1)))
281 (if (evenp n)
282 (,tan reduced)
283 (- (/ (,tan reduced)))))))))))
284 ;; Don't want %sin-quick and friends with sse2.
285 #+(and x86 (not sse2))
286 (frob %sin-quick %cos-quick %tan-quick)
287 #+(or ppc sse2)
288 (frob %%sin %%cos %%tan))
289
290
291
292 ;;;; Power functions.
293
294 (defun exp (number)
295 "Return e raised to the power NUMBER."
296 (number-dispatch ((number number))
297 (handle-reals %exp number)
298 ((complex)
299 (* (exp (realpart number))
300 (cis (imagpart number))))))
301
302 ;;; INTEXP -- Handle the rational base, integer power case.
303
304 (defparameter *intexp-maximum-exponent* 10000)
305
306 ;;; This function precisely calculates base raised to an integral power. It
307 ;;; separates the cases by the sign of power, for efficiency reasons, as powers
308 ;;; can be calculated more efficiently if power is a positive integer. Values
309 ;;; of power are calculated as positive integers, and inverted if negative.
310 ;;;
311 (defun intexp (base power)
312 ;; Handle the special case of 1^power. Maxima sometimes does this,
313 ;; and there's no need to cause a continuable error in this case.
314 ;; Should we also handle (-1)^power?
315 (when (eql base 1)
316 (return-from intexp base))
317
318 (when (> (abs power) *intexp-maximum-exponent*)
319 (cerror "Continue with calculation."
320 "The absolute value of ~S exceeds ~S."
321 power '*intexp-maximum-exponent* base power))
322 (cond ((minusp power)
323 (/ (intexp base (- power))))
324 ((eql base 2)
325 (ash 1 power))
326 (t
327 (do ((nextn (ash power -1) (ash power -1))
328 (total (if (oddp power) base 1)
329 (if (oddp power) (* base total) total)))
330 ((zerop nextn) total)
331 (setq base (* base base))
332 (setq power nextn)))))
333
334
335 ;;; EXPT -- Public
336 ;;;
337 ;;; If an integer power of a rational, use INTEXP above. Otherwise, do
338 ;;; floating point stuff. If both args are real, we try %POW right off,
339 ;;; assuming it will return 0 if the result may be complex. If so, we call
340 ;;; COMPLEX-POW which directly computes the complex result. We also separate
341 ;;; the complex-real and real-complex cases from the general complex case.
342 ;;;
343 (defun expt (base power)
344 "Returns BASE raised to the POWER."
345 (if (zerop power)
346 ;; CLHS says that if the power is 0, the result is 1, subject to
347 ;; numeric contagion. But what happens if base is infinity or
348 ;; NaN? Do we silently return 1? For now, I think we should
349 ;; signal an error if the FP modes say so.
350 (let ((result (1+ (* base power))))
351 ;; If we get an NaN here, that means base*power above didn't
352 ;; produce 0 and FP traps were disabled, so we handle that
353 ;; here. Should this be a continuable restart?
354 (if (and (floatp result) (float-nan-p result))
355 (float 1 result)
356 result))
357 (labels (;; determine if the double float is an integer.
358 ;; 0 - not an integer
359 ;; 1 - an odd int
360 ;; 2 - an even int
361 (isint (ihi lo)
362 (declare (type (unsigned-byte 31) ihi)
363 (type (unsigned-byte 32) lo)
364 (optimize (speed 3) (safety 0)))
365 (let ((isint 0))
366 (declare (type fixnum isint))
367 (cond ((>= ihi #x43400000) ; exponent >= 53
368 (setq isint 2))
369 ((>= ihi #x3ff00000)
370 (let ((k (- (ash ihi -20) #x3ff))) ; exponent
371 (declare (type (mod 53) k))
372 (cond ((> k 20)
373 (let* ((shift (- 52 k))
374 (j (logand (ash lo (- shift))))
375 (j2 (ash j shift)))
376 (declare (type (mod 32) shift)
377 (type (unsigned-byte 32) j j2))
378 (when (= j2 lo)
379 (setq isint (- 2 (logand j 1))))))
380 ((= lo 0)
381 (let* ((shift (- 20 k))
382 (j (ash ihi (- shift)))
383 (j2 (ash j shift)))
384 (declare (type (mod 32) shift)
385 (type (unsigned-byte 31) j j2))
386 (when (= j2 ihi)
387 (setq isint (- 2 (logand j 1))))))))))
388 isint))
389 (real-expt (x y rtype)
390 (let ((x (coerce x 'double-float))
391 (y (coerce y 'double-float)))
392 (declare (double-float x y))
393 (let* ((x-hi (kernel:double-float-high-bits x))
394 (x-lo (kernel:double-float-low-bits x))
395 (x-ihi (logand x-hi #x7fffffff))
396 (y-hi (kernel:double-float-high-bits y))
397 (y-lo (kernel:double-float-low-bits y))
398 (y-ihi (logand y-hi #x7fffffff)))
399 (declare (type (signed-byte 32) x-hi y-hi)
400 (type (unsigned-byte 31) x-ihi y-ihi)
401 (type (unsigned-byte 32) x-lo y-lo))
402 ;; y==zero: x**0 = 1
403 (when (zerop (logior y-ihi y-lo))
404 (return-from real-expt (coerce 1d0 rtype)))
405 ;; +-NaN return x+y
406 (when (or (> x-ihi #x7ff00000)
407 (and (= x-ihi #x7ff00000) (/= x-lo 0))
408 (> y-ihi #x7ff00000)
409 (and (= y-ihi #x7ff00000) (/= y-lo 0)))
410 (return-from real-expt (coerce (+ x y) rtype)))
411 (let ((yisint (if (< x-hi 0) (isint y-ihi y-lo) 0)))
412 (declare (type fixnum yisint))
413 ;; special value of y
414 (when (and (zerop y-lo) (= y-ihi #x7ff00000))
415 ;; y is +-inf
416 (return-from real-expt
417 (cond ((and (= x-ihi #x3ff00000) (zerop x-lo))
418 ;; +-1**inf is NaN
419 (coerce (- y y) rtype))
420 ((>= x-ihi #x3ff00000)
421 ;; (|x|>1)**+-inf = inf,0
422 (if (>= y-hi 0)
423 (coerce y rtype)
424 (coerce 0 rtype)))
425 (t
426 ;; (|x|<1)**-,+inf = inf,0
427 (if (< y-hi 0)
428 (coerce (- y) rtype)
429 (coerce 0 rtype))))))
430
431 (let ((abs-x (abs x)))
432 (declare (double-float abs-x))
433 ;; special value of x
434 (when (and (zerop x-lo)
435 (or (= x-ihi #x7ff00000) (zerop x-ihi)
436 (= x-ihi #x3ff00000)))
437 ;; x is +-0,+-inf,+-1
438 (let ((z (if (< y-hi 0)
439 (/ 1 abs-x) ; z = (1/|x|)
440 abs-x)))
441 (declare (double-float z))
442 (when (< x-hi 0)
443 (cond ((and (= x-ihi #x3ff00000) (zerop yisint))
444 ;; (-1)**non-int
445 (let ((y*pi (* y pi)))
446 (declare (double-float y*pi))
447 (return-from real-expt
448 (complex
449 (coerce (%cos y*pi) rtype)
450 (coerce (%sin y*pi) rtype)))))
451 ((= yisint 1)
452 ;; (x<0)**odd = -(|x|**odd)
453 (setq z (- z)))))
454 (return-from real-expt (coerce z rtype))))
455
456 (if (>= x-hi 0)
457 ;; x>0
458 (coerce (kernel::%pow x y) rtype)
459 ;; x<0
460 (let ((pow (kernel::%pow abs-x y)))
461 (declare (double-float pow))
462 (case yisint
463 (1 ; Odd
464 (coerce (* -1d0 pow) rtype))
465 (2 ; Even
466 (coerce pow rtype))
467 (t ; Non-integer
468 (let ((y*pi (* y pi)))
469 (declare (double-float y*pi))
470 (complex
471 (coerce (* pow (%cos y*pi)) rtype)
472 (coerce (* pow (%sin y*pi)) rtype)))))))))))))
473 (declare (inline real-expt))
474 (number-dispatch ((base number) (power number))
475 (((foreach fixnum (or bignum ratio) (complex rational)) integer)
476 (intexp base power))
477 (((foreach single-float double-float) rational)
478 (real-expt base power '(dispatch-type base)))
479 (((foreach fixnum (or bignum ratio) single-float)
480 (foreach ratio single-float))
481 (real-expt base power 'single-float))
482 (((foreach fixnum (or bignum ratio) single-float double-float)
483 double-float)
484 (real-expt base power 'double-float))
485 ((double-float single-float)
486 (real-expt base power 'double-float))
487 #+double-double
488 (((foreach fixnum (or bignum ratio) single-float double-float double-double-float)
489 double-double-float)
490 (dd-%pow (coerce base 'double-double-float) power))
491 #+double-double
492 ((double-double-float
493 (foreach fixnum (or bignum ratio) single-float double-float))
494 (dd-%pow base (coerce power 'double-double-float)))
495 (((foreach (complex rational) (complex float)) rational)
496 (* (expt (abs base) power)
497 (cis (* power (phase base)))))
498 (((foreach fixnum (or bignum ratio) single-float double-float
499 #+double-double double-double-float)
500 complex)
501 (if (and (zerop base) (plusp (realpart power)))
502 (* base power)
503 (exp (* power (log base)))))
504 (((foreach (complex float) (complex rational))
505 (foreach complex double-float single-float #+double-double double-double-float))
506 (if (and (zerop base) (plusp (realpart power)))
507 (* base power)
508 (exp (* power (log base)))))))))
509
510 ;; Log base 2 of a real number. The result is a either a double-float
511 ;; or double-double-float number (real or complex, as appropriate),
512 ;; depending on the type of FLOAT-TYPE.
513 (defun log2 (x &optional (float-type 1d0))
514 (labels ((log-of-2 (f)
515 ;; log(2), with the precision specified by the type of F
516 (number-dispatch ((f real))
517 ((double-float)
518 #.(log 2d0))
519 #+double-double
520 ((double-double-float)
521 #.(log 2w0))))
522 (log-2-pi (f)
523 ;; log(pi), with the precision specified by the type of F
524 (number-dispatch ((f real))
525 ((double-float)
526 #.(/ pi (log 2d0)))
527 #+double-double
528 ((double-double-float)
529 #.(/ dd-pi (log 2w0)))))
530 (log1p (x)
531 ;; log(1+x), with the precision specified by the type of
532 ;; X
533 (number-dispatch ((x real))
534 (((foreach single-float double-float))
535 (%log1p (float x 1d0)))
536 #+double-double
537 ((double-double-float)
538 (dd-%log1p x))))
539 (log2-bignum (bignum)
540 ;; Write x = 2^n*f where 1/2 < f <= 1. Then log2(x) = n
541 ;; + log2(f).
542 ;;
543 ;; So we grab the top few bits of x and scale that
544 ;; appropriately, take the log of it and add it to n.
545 ;;
546 ;; Return n and log2(f) separately.
547 (if (minusp bignum)
548 (multiple-value-bind (n frac)
549 (log2-bignum (abs bignum))
550 (values n (complex frac (log-2-pi float-type))))
551 (let ((n (integer-length bignum))
552 (float-bits (float-digits float-type)))
553 (if (< n float-bits)
554 (values 0 (log (float bignum float-type)
555 (float 2 float-type)))
556 (let ((exp (min float-bits n))
557 (f (ldb (byte float-bits
558 (max 0 (- n float-bits)))
559 bignum)))
560 (values n (log (scale-float (float f float-type) (- exp))
561 (float 2 float-type)))))))))
562 (etypecase x
563 (float
564 (/ (log (float x float-type)) (log-of-2 float-type)))
565 (ratio
566 (let ((top (numerator x))
567 (bot (denominator x)))
568 ;; If the number of bits in the numerator and
569 ;; denominator are different, just use the fact
570 ;; log(x/y) = log(x) - log(y). But to preserve
571 ;; accuracy, we actually do
572 ;; (log2(x)-log2(y))/log2(e)).
573 ;;
574 ;; However, if the numerator and denominator have the
575 ;; same number of bits, implying the quotient is near
576 ;; one, we use log1p(x) = log(1+x). Since the number is
577 ;; rational, we don't lose precision subtracting 1 from
578 ;; it, and converting it to double-float is accurate.
579 (if (= (integer-length top)
580 (integer-length bot))
581 (/ (log1p (float (- x 1) float-type))
582 (log-of-2 float-type))
583 (multiple-value-bind (top-n top-frac)
584 (log2-bignum top)
585 (multiple-value-bind (bot-n bot-frac)
586 (log2-bignum bot)
587 (+ (- top-n bot-n)
588 (- top-frac bot-frac)))))))
589 (integer
590 (multiple-value-bind (n frac)
591 (log2-bignum x)
592 (+ n frac))))))
593
594 (defun log (number &optional (base nil base-p))
595 "Return the logarithm of NUMBER in the base BASE, which defaults to e."
596 (if base-p
597 (cond ((zerop base)
598 ;; ANSI spec
599 base)
600 ((and (realp number) (realp base))
601 ;; CLHS 12.1.4.1 says
602 ;;
603 ;; When rationals and floats are combined by a
604 ;; numerical function, the rational is first converted
605 ;; to a float of the same format.
606 ;;
607 ;; So assume this applies to floats as well convert all
608 ;; numbers to the largest float format before computing
609 ;; the log.
610 ;;
611 ;; This makes (log 17 10.0) = (log 17.0 10) and so on.
612 (number-dispatch ((number real) (base real))
613 ((double-float
614 (foreach double-float single-float))
615 (/ (log2 number) (log2 base)))
616 (((foreach fixnum bignum ratio)
617 (foreach fixnum bignum ratio single-float))
618 (let* ((result (/ (log2 number) (log2 base))))
619 ;; Figure out the right result type
620 (if (realp result)
621 (coerce result 'single-float)
622 (coerce result '(complex single-float)))))
623 (((foreach fixnum bignum ratio)
624 double-float)
625 (/ (log2 number) (log2 base)))
626 ((single-float
627 (foreach fixnum bignum ratio))
628 (let* ((result (/ (log2 number) (log2 base))))
629 ;; Figure out the right result type
630 (if (realp result)
631 (coerce result 'single-float)
632 (coerce result '(complex single-float)))))
633 ((double-float
634 (foreach fixnum bignum ratio))
635 (/ (log2 number) (log2 base)))
636 ((single-float double-float)
637 (/ (log (coerce number 'double-float)) (log base)))
638 #+double-double
639 ((double-double-float
640 (foreach fixnum bignum ratio))
641 (/ (log2 number 1w0) (log2 base 1w0)))
642 #+double-double
643 ((double-double-float
644 (foreach double-double-float double-float single-float))
645 (/ (log number) (log (coerce base 'double-double-float))))
646 #+double-double
647 (((foreach fixnum bignum ratio)
648 double-double-float)
649 (/ (log2 number 1w0) (log2 base 1w0)))
650 #+double-double
651 (((foreach double-float single-float)
652 double-double-float)
653 (/ (log (coerce number 'double-double-float)) (log base)))
654 (((foreach single-float)
655 (foreach single-float))
656 ;; Converting everything to double-float helps the
657 ;; cases like (log 17 10) = (/ (log 17) (log 10)).
658 ;; This is usually handled above, but if we compute (/
659 ;; (log 17) (log 10)), we get a slightly different
660 ;; answer due to roundoff. This makes it a bit more
661 ;; consistent.
662 ;;
663 ;; FIXME: This probably needs more work.
664 (let ((result (/ (log (float number 1d0))
665 (log (float base 1d0)))))
666 (if (realp result)
667 (coerce result 'single-float)
668 (coerce result '(complex single-float)))))))
669 (t
670 ;; FIXME: This probably needs some work as well.
671 (/ (log number) (log base))))
672 (number-dispatch ((number number))
673 (((foreach fixnum bignum))
674 (if (minusp number)
675 (complex (coerce (log (- number)) 'single-float)
676 (coerce pi 'single-float))
677 (coerce (/ (log2 number) #.(log (exp 1d0) 2d0)) 'single-float)))
678 ((ratio)
679 (if (minusp number)
680 (complex (coerce (log (- number)) 'single-float)
681 (coerce pi 'single-float))
682 ;; What happens when the ratio is close to 1? We need to
683 ;; be careful to preserve accuracy.
684 (let ((top (numerator number))
685 (bot (denominator number)))
686 ;; If the number of bits in the numerator and
687 ;; denominator are different, just use the fact
688 ;; log(x/y) = log(x) - log(y). But to preserve
689 ;; accuracy, we actually do
690 ;; (log2(x)-log2(y))/log2(e)).
691 ;;
692 ;; However, if the numerator and denominator have the
693 ;; same number of bits, implying the quotient is near
694 ;; one, we use log1p(x) = log(1+x). Since the number is
695 ;; rational, we don't lose precision subtracting 1 from
696 ;; it, and converting it to double-float is accurate.
697 (if (= (integer-length top)
698 (integer-length bot))
699 (coerce (%log1p (coerce (- number 1) 'double-float))
700 'single-float)
701 (coerce (/ (- (log2 top) (log2 bot))
702 #.(log (exp 1d0) 2d0))
703 'single-float)))))
704 (((foreach single-float double-float))
705 ;; Is (log -0) -infinity (libm.a) or -infinity + i*pi (Kahan)?
706 ;; Since this doesn't seem to be an implementation issue
707 ;; I (pw) take the Kahan result.
708 (if (< (float-sign number)
709 (coerce 0 '(dispatch-type number)))
710 (complex (log (- number)) (coerce pi '(dispatch-type number)))
711 (coerce (%log (coerce number 'double-float))
712 '(dispatch-type number))))
713 #+double-double
714 ((double-double-float)
715 (let ((hi (kernel:double-double-hi number)))
716 (if (< (float-sign hi) 0d0)
717 (complex (dd-%log (- number)) dd-pi)
718 (dd-%log number))))
719 ((complex)
720 (complex-log number)))))
721
722 (defun sqrt (number)
723 "Return the square root of NUMBER."
724 (number-dispatch ((number number))
725 (((foreach fixnum bignum ratio))
726 (if (minusp number)
727 (complex-sqrt number)
728 (coerce (%sqrt (coerce number 'double-float)) 'single-float)))
729 (((foreach single-float double-float))
730 (if (minusp number)
731 (complex-sqrt number)
732 (coerce (%sqrt (coerce number 'double-float))
733 '(dispatch-type number))))
734 #+double-double
735 ((double-double-float)
736 (if (minusp number)
737 (dd-complex-sqrt number)
738 (multiple-value-bind (hi lo)
739 (c::sqrt-dd (kernel:double-double-hi number) (kernel:double-double-lo number))
740 (kernel:%make-double-double-float hi lo))))
741 ((complex)
742 (complex-sqrt number))))
743
744
745 ;;;; Trigonometic and Related Functions
746
747 (defun abs (number)
748 "Returns the absolute value of the number."
749 (number-dispatch ((number number))
750 (((foreach single-float double-float fixnum rational
751 #+double-double double-double-float))
752 (abs number))
753 ((complex)
754 (let ((rx (realpart number))
755 (ix (imagpart number)))
756 (etypecase rx
757 (rational
758 (sqrt (+ (* rx rx) (* ix ix))))
759 (single-float
760 (coerce (%hypot (coerce rx 'double-float)
761 (coerce ix 'double-float))
762 'single-float))
763 (double-float
764 (%hypot rx ix))
765 #+double-double
766 (double-double-float
767 (multiple-value-bind (abs^2 scale)
768 (dd-cssqs number)
769 (scale-float (sqrt abs^2) scale))))))))
770
771 (defun phase (number)
772 "Returns the angle part of the polar representation of a complex number.
773 For complex numbers, this is (atan (imagpart number) (realpart number)).
774 For non-complex positive numbers, this is 0. For non-complex negative
775 numbers this is PI."
776 (etypecase number
777 (rational
778 (if (minusp number)
779 (coerce pi 'single-float)
780 0.0f0))
781 (single-float
782 (if (minusp (float-sign number))
783 (coerce pi 'single-float)
784 0.0f0))
785 (double-float
786 (if (minusp (float-sign number))
787 (coerce pi 'double-float)
788 0.0d0))
789 #+double-double
790 (double-double-float
791 (if (minusp (float-sign number))
792 dd-pi
793 0w0))
794 (complex
795 (atan (imagpart number) (realpart number)))))
796
797
798 (defun sin (number)
799 "Return the sine of NUMBER."
800 (number-dispatch ((number number))
801 (handle-reals %sin number)
802 ((complex)
803 (let ((x (realpart number))
804 (y (imagpart number)))
805 (complex (* (sin x) (cosh y))
806 (* (cos x) (sinh y)))))))
807
808 (defun cos (number)
809 "Return the cosine of NUMBER."
810 (number-dispatch ((number number))
811 (handle-reals %cos number)
812 ((complex)
813 (let ((x (realpart number))
814 (y (imagpart number)))
815 (complex (* (cos x) (cosh y))
816 (- (* (sin x) (sinh y))))))))
817
818 (defun tan (number)
819 "Return the tangent of NUMBER."
820 (number-dispatch ((number number))
821 (handle-reals %tan number)
822 ((complex)
823 (complex-tan number))))
824
825 (defun cis (theta)
826 "Return cos(Theta) + i sin(Theta), AKA exp(i Theta)."
827 (if (complexp theta)
828 (error "Argument to CIS is complex: ~S" theta)
829 (complex (cos theta) (sin theta))))
830
831 (defun asin (number)
832 "Return the arc sine of NUMBER."
833 (number-dispatch ((number number))
834 ((rational)
835 (if (or (> number 1) (< number -1))
836 (complex-asin number)
837 (coerce (%asin (coerce number 'double-float)) 'single-float)))
838 (((foreach single-float double-float))
839 (if (or (float-nan-p number)
840 (and (<= number (coerce 1 '(dispatch-type number)))
841 (>= number (coerce -1 '(dispatch-type number)))))
842 (coerce (%asin (coerce number 'double-float))
843 '(dispatch-type number))
844 (complex-asin number)))
845 #+double-double
846 ((double-double-float)
847 (if (or (float-nan-p number)
848 (and (<= number 1w0)
849 (>= number -1w0)))
850 (dd-%asin number)
851 (dd-complex-asin number)))
852 ((complex)
853 (complex-asin number))))
854
855 (defun acos (number)
856 "Return the arc cosine of NUMBER."
857 (number-dispatch ((number number))
858 ((rational)
859 (if (or (> number 1) (< number -1))
860 (complex-acos number)
861 (coerce (%acos (coerce number 'double-float)) 'single-float)))
862 (((foreach single-float double-float))
863 (if (or (float-nan-p number)
864 (and (<= number (coerce 1 '(dispatch-type number)))
865 (>= number (coerce -1 '(dispatch-type number)))))
866 (coerce (%acos (coerce number 'double-float))
867 '(dispatch-type number))
868 (complex-acos number)))
869 #+double-double
870 ((double-double-float)
871 (if (or (float-nan-p number)
872 (and (<= number 1w0)
873 (>= number -1w0)))
874 (dd-%acos number)
875 (complex-acos number)))
876 ((complex)
877 (complex-acos number))))
878
879
880 (defun atan (y &optional (x nil xp))
881 "Return the arc tangent of Y if X is omitted or Y/X if X is supplied."
882 (if xp
883 (flet ((atan2 (y x)
884 (declare (type double-float y x)
885 (values double-float))
886 (if (zerop x)
887 (if (zerop y)
888 (if (plusp (float-sign x))
889 y
890 (float-sign y pi))
891 (float-sign y (/ pi 2)))
892 (%atan2 y x))))
893 ;; If X is given, both X and Y must be real numbers.
894 (number-dispatch ((y real) (x real))
895 ((double-float
896 (foreach double-float single-float fixnum bignum ratio))
897 (atan2 y (coerce x 'double-float)))
898 (((foreach single-float fixnum bignum ratio)
899 double-float)
900 (atan2 (coerce y 'double-float) x))
901 (((foreach single-float fixnum bignum ratio)
902 (foreach single-float fixnum bignum ratio))
903 (coerce (atan2 (coerce y 'double-float) (coerce x 'double-float))
904 'single-float))
905 #+double-double
906 ((double-double-float
907 (foreach double-double-float double-float single-float fixnum bignum ratio))
908 (dd-%atan2 y (coerce x 'double-double-float)))
909 #+double-double
910 (((foreach double-float single-float fixnum bignum ratio)
911 double-double-float)
912 (dd-%atan2 (coerce y 'double-double-float) x))))
913 (number-dispatch ((y number))
914 (handle-reals %atan y)
915 ((complex)
916 (complex-atan y)))))
917
918 (defun sinh (number)
919 "Return the hyperbolic sine of NUMBER."
920 (number-dispatch ((number number))
921 (handle-reals %sinh number)
922 ((complex)
923 (let ((x (realpart number))
924 (y (imagpart number)))
925 (complex (* (sinh x) (cos y))
926 (* (cosh x) (sin y)))))))
927
928 (defun cosh (number)
929 "Return the hyperbolic cosine of NUMBER."
930 (number-dispatch ((number number))
931 (handle-reals %cosh number)
932 ((complex)
933 (let ((x (realpart number))
934 (y (imagpart number)))
935 (complex (* (cosh x) (cos y))
936 (* (sinh x) (sin y)))))))
937
938 (defun tanh (number)
939 "Return the hyperbolic tangent of NUMBER."
940 (number-dispatch ((number number))
941 (handle-reals %tanh number)
942 ((complex)
943 (complex-tanh number))))
944
945 (defun asinh (number)
946 "Return the hyperbolic arc sine of NUMBER."
947 (number-dispatch ((number number))
948 (handle-reals %asinh number)
949 ((complex)
950 (complex-asinh number))))
951
952 (defun acosh (number)
953 "Return the hyperbolic arc cosine of NUMBER."
954 (number-dispatch ((number number))
955 ((rational)
956 ;; acosh is complex if number < 1
957 (if (< number 1)
958 (complex-acosh number)
959 (coerce (%acosh (coerce number 'double-float)) 'single-float)))
960 (((foreach single-float double-float))
961 (if (< number (coerce 1 '(dispatch-type number)))
962 (complex-acosh number)
963 (coerce (%acosh (coerce number 'double-float))
964 '(dispatch-type number))))
965 #+double-double
966 ((double-double-float)
967 (if (< number 1w0)
968 (complex-acosh number)
969 (dd-%acosh number)))
970 ((complex)
971 (complex-acosh number))))
972
973 (defun atanh (number)
974 "Return the hyperbolic arc tangent of NUMBER."
975 (number-dispatch ((number number))
976 ((rational)
977 ;; atanh is complex if |number| > 1
978 (if (or (> number 1) (< number -1))
979 (complex-atanh number)
980 (coerce (%atanh (coerce number 'double-float)) 'single-float)))
981 (((foreach single-float double-float))
982 (if (or (> number (coerce 1 '(dispatch-type number)))
983 (< number (coerce -1 '(dispatch-type number))))
984 (complex-atanh number)
985 (coerce (%atanh (coerce number 'double-float))
986 '(dispatch-type number))))
987 #+double-double
988 ((double-double-float)
989 (if (or (> number 1w0)
990 (< number -1w0))
991 (complex-atanh number)
992 (dd-%atanh (coerce number 'double-double-float))))
993 ((complex)
994 (complex-atanh number))))
995
996 ;;; HP-UX does not supply a C version of log1p, so use the definition.
997 ;;; We really need to fix this. The definition really loses big-time
998 ;;; in roundoff as x gets small.
999
1000 #+hpux
1001 (declaim (inline %log1p))
1002 #+hpux
1003 (defun %log1p (number)
1004 (declare (double-float number)
1005 (optimize (speed 3) (safety 0)))
1006 (the double-float (log (the (double-float 0d0) (+ number 1d0)))))
1007
1008
1009 ;;;;
1010 ;;;; This is a set of routines that implement many elementary
1011 ;;;; transcendental functions as specified by ANSI Common Lisp. The
1012 ;;;; implementation is based on Kahan's paper.
1013 ;;;;
1014 ;;;; I believe I have accurately implemented the routines and are
1015 ;;;; correct, but you may want to check for your self.
1016 ;;;;
1017 ;;;; These functions are written for CMU Lisp and take advantage of
1018 ;;;; some of the features available there. It may be possible,
1019 ;;;; however, to port this to other Lisps.
1020 ;;;;
1021 ;;;; Some functions are significantly more accurate than the original
1022 ;;;; definitions in CMU Lisp. In fact, some functions in CMU Lisp
1023 ;;;; give the wrong answer like (acos #c(-2.0 0.0)), where the true
1024 ;;;; answer is pi + i*log(2-sqrt(3)).
1025 ;;;;
1026 ;;;; All of the implemented functions will take any number for an
1027 ;;;; input, but the result will always be a either a complex
1028 ;;;; single-float or a complex double-float.
1029 ;;;;
1030 ;;;; General functions
1031 ;;;; complex-sqrt
1032 ;;;; complex-log
1033 ;;;; complex-atanh
1034 ;;;; complex-tanh
1035 ;;;; complex-acos
1036 ;;;; complex-acosh
1037 ;;;; complex-asin
1038 ;;;; complex-asinh
1039 ;;;; complex-atan
1040 ;;;; complex-tan
1041 ;;;;
1042 ;;;; Utility functions:
1043 ;;;; scalb logb
1044 ;;;;
1045 ;;;; Internal functions:
1046 ;;;; square coerce-to-complex-type cssqs complex-log-scaled
1047 ;;;;
1048 ;;;;
1049 ;;;; Please send any bug reports, comments, or improvements to Raymond
1050 ;;;; Toy at toy@rtp.ericsson.se.
1051 ;;;;
1052 ;;;; References
1053 ;;;;
1054 ;;;; Kahan, W. "Branch Cuts for Complex Elementary Functions, or Much
1055 ;;;; Ado About Nothing's Sign Bit" in Iserles and Powell (eds.) "The
1056 ;;;; State of the Art in Numerical Analysis", pp. 165-211, Clarendon
1057 ;;;; Press, 1987
1058 ;;;;
1059
1060 (declaim (inline square))
1061 (defun square (x)
1062 (declare (float x))
1063 (* x x))
1064
1065 ;; If you have these functions in libm, perhaps they should be used
1066 ;; instead of these Lisp versions. These versions are probably good
1067 ;; enough, especially since they are portable.
1068
1069 (declaim (inline scalb))
1070 (defun scalb (x n)
1071 "Compute 2^N * X without compute 2^N first (use properties of the
1072 underlying floating-point format"
1073 (declare (type float x)
1074 (type double-float-exponent n))
1075 (scale-float x n))
1076
1077 (declaim (inline logb-finite))
1078 (defun logb-finite (x)
1079 "Same as logb but X is not infinity and non-zero and not a NaN, so
1080 that we can always return an integer"
1081 (declare (type float x))
1082 (multiple-value-bind (signif expon sign)
1083 (decode-float x)
1084 (declare (ignore signif sign))
1085 ;; decode-float is almost right, except that the exponent
1086 ;; is off by one
1087 (1- expon)))
1088
1089 (defun logb (x)
1090 "Compute an integer N such that 1 <= |2^(-N) * x| < 2.
1091 For the special cases, the following values are used:
1092
1093 x logb
1094 NaN NaN
1095 +/- infinity +infinity
1096 0 -infinity
1097 "
1098 (declare (type float x))
1099 (cond ((float-nan-p x)
1100 x)
1101 ((float-infinity-p x)
1102 #.ext:double-float-positive-infinity)
1103 ((zerop x)
1104 ;; The answer is negative infinity, but we are supposed to
1105 ;; signal divide-by-zero, so do the actual division
1106 (/ -1 x)
1107 )
1108 (t
1109 (logb-finite x))))
1110
1111
1112
1113 ;; This function is used to create a complex number of the appropriate
1114 ;; type.
1115
1116 (declaim (inline coerce-to-complex-type))
1117 (defun coerce-to-complex-type (x y z)
1118 "Create complex number with real part X and imaginary part Y such that
1119 it has the same type as Z. If Z has type (complex rational), the X
1120 and Y are coerced to single-float."
1121 (declare (double-float x y)
1122 (number z)
1123 (optimize (extensions:inhibit-warnings 3)))
1124 (if (typep (realpart z) 'double-float)
1125 (complex x y)
1126 ;; Convert anything that's not a double-float to a single-float.
1127 (complex (float x 1f0)
1128 (float y 1f0))))
1129
1130 (defun cssqs (z)
1131 ;; Compute |(x+i*y)/2^k|^2 scaled to avoid over/underflow. The
1132 ;; result is r + i*k, where k is an integer.
1133
1134 ;; Save all FP flags
1135 (let ((x (float (realpart z) 1d0))
1136 (y (float (imagpart z) 1d0)))
1137 ;; Would this be better handled using an exception handler to
1138 ;; catch the overflow or underflow signal? For now, we turn all
1139 ;; traps off and look at the accrued exceptions to see if any
1140 ;; signal would have been raised.
1141 (with-float-traps-masked (:underflow :overflow)
1142 (let ((rho (+ (square x) (square y))))
1143 (declare (optimize (speed 3) (space 0)))
1144 (cond ((and (or (float-nan-p rho)
1145 (float-infinity-p rho))
1146 (or (float-infinity-p (abs x))
1147 (float-infinity-p (abs y))))
1148 (values ext:double-float-positive-infinity 0))
1149 ((let ((threshold #.(/ least-positive-double-float
1150 double-float-epsilon))
1151 (traps (ldb vm::float-sticky-bits
1152 (vm:floating-point-modes))))
1153 ;; Overflow raised or (underflow raised and rho <
1154 ;; lambda/eps)
1155 (or (not (zerop (logand vm:float-overflow-trap-bit traps)))
1156 (and (not (zerop (logand vm:float-underflow-trap-bit traps)))
1157 (< rho threshold))))
1158 ;; If we're here, neither x nor y are infinity and at
1159 ;; least one is non-zero.. Thus logb returns a nice
1160 ;; integer.
1161 (let ((k (- (logb-finite (max (abs x) (abs y))))))
1162 (values (+ (square (scalb x k))
1163 (square (scalb y k)))
1164 (- k))))
1165 (t
1166 (values rho 0)))))))
1167
1168 (defun complex-sqrt (z)
1169 "Principle square root of Z
1170
1171 Z may be any number, but the result is always a complex."
1172 (declare (number z))
1173 #+double-double
1174 (when (typep z '(or double-double-float (complex double-double-float)))
1175 (return-from complex-sqrt (dd-complex-sqrt z)))
1176 (multiple-value-bind (rho k)
1177 (cssqs z)
1178 (declare (type (or (member 0d0) (double-float 0d0)) rho)
1179 (type fixnum k))
1180 (let ((x (float (realpart z) 1.0d0))
1181 (y (float (imagpart z) 1.0d0))
1182 (eta 0d0)
1183 (nu 0d0))
1184 (declare (double-float x y eta nu))
1185
1186 (locally
1187 ;; space 0 to get maybe-inline functions inlined.
1188 (declare (optimize (speed 3) (space 0)))
1189
1190 (if (not (locally (declare (optimize (inhibit-warnings 3)))
1191 (float-nan-p x)))
1192 (setf rho (+ (scalb (abs x) (- k)) (sqrt rho))))
1193
1194 (cond ((oddp k)
1195 (setf k (ash k -1)))
1196 (t
1197 (setf k (1- (ash k -1)))
1198 (setf rho (+ rho rho))))
1199
1200 (setf rho (scalb (sqrt rho) k))
1201
1202 (setf eta rho)
1203 (setf nu y)
1204
1205 (when (/= rho 0d0)
1206 (when (not (float-infinity-p (abs nu)))
1207 (setf nu (/ (/ nu rho) 2d0)))
1208 (when (< x 0d0)
1209 (setf eta (abs nu))
1210 (setf nu (float-sign y rho))))
1211 (coerce-to-complex-type eta nu z)))))
1212
1213 (defun complex-log-scaled (z j)
1214 "Compute log(2^j*z).
1215
1216 This is for use with J /= 0 only when |z| is huge."
1217 (declare (number z)
1218 (fixnum j))
1219 ;; The constants t0, t1, t2 should be evaluated to machine
1220 ;; precision. In addition, Kahan says the accuracy of log1p
1221 ;; influences the choices of these constants but doesn't say how to
1222 ;; choose them. We'll just assume his choices matches our
1223 ;; implementation of log1p.
1224 (let ((t0 #.(/ 1 (sqrt 2.0d0)))
1225 (t1 1.2d0)
1226 (t2 3d0)
1227 (ln2 #.(log 2d0))
1228 (x (float (realpart z) 1.0d0))
1229 (y (float (imagpart z) 1.0d0)))
1230 (multiple-value-bind (rho k)
1231 (cssqs z)
1232 (declare (optimize (speed 3)))
1233 (let ((beta (max (abs x) (abs y)))
1234 (theta (min (abs x) (abs y))))
1235 (coerce-to-complex-type (if (and (zerop k)
1236 (< t0 beta)
1237 (or (<= beta t1)
1238 (< rho t2)))
1239 (/ (%log1p (+ (* (- beta 1.0d0)
1240 (+ beta 1.0d0))
1241 (* theta theta)))
1242 2d0)
1243 (+ (/ (log rho) 2d0)
1244 (* (+ k j) ln2)))
1245 (atan y x)
1246 z)))))
1247
1248 (defun complex-log (z)
1249 "Log of Z = log |Z| + i * arg Z
1250
1251 Z may be any number, but the result is always a complex."
1252 (declare (number z))
1253 #+double-double
1254 (when (typep z '(or double-double-float (complex double-double-float)))
1255 (return-from complex-log (dd-complex-log-scaled z 0)))
1256 (complex-log-scaled z 0))
1257
1258 ;; Let us note the following "strange" behavior. atanh 1.0d0 is
1259 ;; +infinity, but the following code returns approx 176 + i*pi/4. The
1260 ;; reason for the imaginary part is caused by the fact that arg i*y is
1261 ;; never 0 since we have positive and negative zeroes.
1262
1263 (defun complex-atanh (z)
1264 "Compute atanh z = (log(1+z) - log(1-z))/2"
1265 (declare (number z))
1266 #+double-double
1267 (when (typep z '(or double-double-float (complex double-double-float)))
1268 (return-from complex-atanh (dd-complex-atanh z)))
1269
1270 (if (and (realp z) (< z -1))
1271 ;; atanh is continuous in quadrant III in this case.
1272 (complex-atanh (complex z -0f0))
1273 (let* ( ;; Constants
1274 (theta (/ (sqrt most-positive-double-float) 4.0d0))
1275 (rho (/ 4.0d0 (sqrt most-positive-double-float)))
1276 (half-pi (/ pi 2.0d0))
1277 (rp (float (realpart z) 1.0d0))
1278 (beta (float-sign rp 1.0d0))
1279 (x (* beta rp))
1280 (y (* beta (- (float (imagpart z) 1.0d0))))
1281 (eta 0.0d0)
1282 (nu 0.0d0))
1283 ;; Shouldn't need this declare.
1284 (declare (double-float x y))
1285 (locally
1286 (declare (optimize (speed 3)))
1287 (cond ((or (> x theta)
1288 (> (abs y) theta))
1289 ;; To avoid overflow...
1290 (setf nu (float-sign y half-pi))
1291 ;; eta is real part of 1/(x + iy). This is x/(x^2+y^2),
1292 ;; which can cause overflow. Arrange this computation so
1293 ;; that it won't overflow.
1294 (setf eta (let* ((x-bigger (> x (abs y)))
1295 (r (if x-bigger (/ y x) (/ x y)))
1296 (d (+ 1.0d0 (* r r))))
1297 (if x-bigger
1298 (/ (/ x) d)
1299 (/ (/ r y) d)))))
1300 ((= x 1.0d0)
1301 ;; Should this be changed so that if y is zero, eta is set
1302 ;; to +infinity instead of approx 176? In any case
1303 ;; tanh(176) is 1.0d0 within working precision.
1304 (let ((t1 (+ 4d0 (square y)))
1305 (t2 (+ (abs y) rho)))
1306 (setf eta (log (/ (sqrt (sqrt t1))
1307 (sqrt t2))))
1308 (setf nu (* 0.5d0
1309 (float-sign y
1310 (+ half-pi (atan (* 0.5d0 t2))))))))
1311 (t
1312 (let ((t1 (+ (abs y) rho)))
1313 ;; Normal case using log1p(x) = log(1 + x)
1314 (setf eta (* 0.25d0
1315 (%log1p (/ (* 4.0d0 x)
1316 (+ (square (- 1.0d0 x))
1317 (square t1))))))
1318 (setf nu (* 0.5d0
1319 (atan (* 2.0d0 y)
1320 (- (* (- 1.0d0 x)
1321 (+ 1.0d0 x))
1322 (square t1))))))))
1323 (coerce-to-complex-type (* beta eta)
1324 (- (* beta nu))
1325 z)))))
1326
1327 (defun complex-tanh (z)
1328 "Compute tanh z = sinh z / cosh z"
1329 (declare (number z))
1330 #+double-double
1331 (when (typep z '(or double-double-float (complex double-double-float)))
1332 (return-from complex-tanh (dd-complex-tanh z)))
1333
1334 (let ((x (float (realpart z) 1.0d0))
1335 (y (float (imagpart z) 1.0d0)))
1336 (locally
1337 ;; space 0 to get maybe-inline functions inlined
1338 (declare (optimize (speed 3) (space 0)))
1339 (cond ((> (abs x)
1340 #-(or linux hpux) #.(/ (%asinh most-positive-double-float) 4d0)
1341 ;; This is more accurate under linux.
1342 #+(or linux hpux) #.(/ (+ (%log 2.0d0)
1343 (%log most-positive-double-float)) 4d0))
1344 (coerce-to-complex-type (float-sign x)
1345 (float-sign y) z))
1346 (t
1347 (let* ((tv (%tan y))
1348 (beta (+ 1.0d0 (* tv tv)))
1349 (s (sinh x))
1350 (rho (sqrt (+ 1.0d0 (* s s)))))
1351 (if (float-infinity-p (abs tv))
1352 (coerce-to-complex-type (/ rho s)
1353 (/ tv)
1354 z)
1355 (let ((den (+ 1.0d0 (* beta s s))))
1356 (coerce-to-complex-type (/ (* beta rho s)
1357 den)
1358 (/ tv den)
1359 z)))))))))
1360
1361 ;; Kahan says we should only compute the parts needed. Thus, the
1362 ;; realpart's below should only compute the real part, not the whole
1363 ;; complex expression. Doing this can be important because we may get
1364 ;; spurious signals that occur in the part that we are not using.
1365 ;;
1366 ;; However, we take a pragmatic approach and just use the whole
1367 ;; expression.
1368
1369 ;; NOTE: The formula given by Kahan is somewhat ambiguous in whether
1370 ;; it's the conjugate of the square root or the square root of the
1371 ;; conjugate. This needs to be checked.
1372
1373 ;; I checked. It doesn't matter because (conjugate (sqrt z)) is the
1374 ;; same as (sqrt (conjugate z)) for all z. This follows because
1375 ;;
1376 ;; (conjugate (sqrt z)) = exp(0.5*log |z|)*exp(-0.5*j*arg z).
1377 ;;
1378 ;; (sqrt (conjugate z)) = exp(0.5*log|z|)*exp(0.5*j*arg conj z)
1379 ;;
1380 ;; and these two expressions are equal if and only if arg conj z =
1381 ;; -arg z, which is clearly true for all z.
1382
1383 ;; NOTE: The rules of Common Lisp says that if you mix a real with a
1384 ;; complex, the real is converted to a complex before performing the
1385 ;; operation. However, Kahan says in this paper (pg 176):
1386 ;;
1387 ;; (iii) Careless handling can turn infinity or the sign of zero into
1388 ;; misinformation that subsequently disappears leaving behind
1389 ;; only a plausible but incorrect result. That is why compilers
1390 ;; must not transform z-1 into z-(1+i*0), as we have seen above,
1391 ;; nor -(-x-x^2) into (x+x^2), as we shall see below, lest a
1392 ;; subsequent logarithm or square root produce a non-zero
1393 ;; imaginary part whose sign is opposite to what was intended.
1394 ;;
1395 ;; The interesting examples are too long and complicated to reproduce
1396 ;; here. We refer the reader to his paper.
1397 ;;
1398 ;; The functions below are intended to handle the cases where a real
1399 ;; is mixed with a complex and we don't want CL complex contagion to
1400 ;; occur..
1401
1402 (declaim (inline 1+z 1-z z-1 z+1))
1403 (defun 1+z (z)
1404 (complex (+ 1 (realpart z)) (imagpart z)))
1405 (defun 1-z (z)
1406 (complex (- 1 (realpart z)) (- (imagpart z))))
1407 (defun z-1 (z)
1408 (complex (- (realpart z) 1) (imagpart z)))
1409 (defun z+1 (z)
1410 (complex (+ (realpart z) 1) (imagpart z)))
1411
1412 (defun complex-acos (z)
1413 "Compute acos z = pi/2 - asin z
1414
1415 Z may be any number, but the result is always a complex."
1416 (declare (number z))
1417 #+double-double
1418 (when (typep z '(or double-double-float (complex double-double-float)))
1419 (return-from complex-acos (dd-complex-acos z)))
1420 (if (and (realp z) (> z 1))
1421 ;; acos is continuous in quadrant IV in this case.
1422 (complex-acos (complex z -0f0))
1423 (let ((sqrt-1+z (complex-sqrt (1+z z)))
1424 (sqrt-1-z (complex-sqrt (1-z z))))
1425 (with-float-traps-masked (:divide-by-zero)
1426 (complex (* 2 (atan (/ (realpart sqrt-1-z)
1427 (realpart sqrt-1+z))))
1428 (asinh (imagpart (* (conjugate sqrt-1+z)
1429 sqrt-1-z))))))))
1430
1431 (defun complex-acosh (z)
1432 "Compute acosh z = 2 * log(sqrt((z+1)/2) + sqrt((z-1)/2))
1433
1434 Z may be any number, but the result is always a complex."
1435 (declare (number z))
1436 (let ((sqrt-z-1 (complex-sqrt (z-1 z)))
1437 (sqrt-z+1 (complex-sqrt (z+1 z))))
1438 (with-float-traps-masked (:divide-by-zero)
1439 (complex (asinh (realpart (* (conjugate sqrt-z-1)
1440 sqrt-z+1)))
1441 (* 2 (atan (/ (imagpart sqrt-z-1)
1442 (realpart sqrt-z+1))))))))
1443
1444
1445 (defun complex-asin (z)
1446 "Compute asin z = asinh(i*z)/i
1447
1448 Z may be any number, but the result is always a complex."
1449 (declare (number z))
1450 #+double-double
1451 (when (typep z '(or double-double-float (complex double-double-float)))
1452 (return-from complex-asin (dd-complex-asin z)))
1453 (if (and (realp z) (> z 1))
1454 ;; asin is continuous in quadrant IV in this case.
1455 (complex-asin (complex z -0f0))
1456 (let ((sqrt-1-z (complex-sqrt (1-z z)))
1457 (sqrt-1+z (complex-sqrt (1+z z))))
1458 (with-float-traps-masked (:divide-by-zero)
1459 (complex (atan (/ (realpart z)
1460 (realpart (* sqrt-1-z sqrt-1+z))))
1461 (asinh (imagpart (* (conjugate sqrt-1-z)
1462 sqrt-1+z))))))))
1463
1464 (defun complex-asinh (z)
1465 "Compute asinh z = log(z + sqrt(1 + z*z))
1466
1467 Z may be any number, but the result is always a complex."
1468 (declare (number z))
1469 ;; asinh z = -i * asin (i*z)
1470 #+double-double
1471 (when (typep z '(or double-double-float (complex double-double-float)))
1472 (return-from complex-asinh (dd-complex-asinh z)))
1473 (let* ((iz (complex (- (imagpart z)) (realpart z)))
1474 (result (complex-asin iz)))
1475 (complex (imagpart result)
1476 (- (realpart result)))))
1477
1478 (defun complex-atan (z)
1479 "Compute atan z = atanh (i*z) / i
1480
1481 Z may be any number, but the result is always a complex."
1482 (declare (number z))
1483 ;; atan z = -i * atanh (i*z)
1484 #+double-double
1485 (when (typep z '(or double-double-float (complex double-double-float)))
1486 (return-from complex-atan (dd-complex-atan z)))
1487 (let* ((iz (complex (- (imagpart z)) (realpart z)))
1488 (result (complex-atanh iz)))
1489 (complex (imagpart result)
1490 (- (realpart result)))))
1491
1492 (defun complex-tan (z)
1493 "Compute tan z = -i * tanh(i * z)
1494
1495 Z may be any number, but the result is always a complex."
1496 (declare (number z))
1497 ;; tan z = -i * tanh(i*z)
1498 #+double-double
1499 (when (typep z '(or double-double-float (complex double-double-float)))
1500 (return-from complex-tan (dd-complex-tan z)))
1501 (let* ((iz (complex (- (imagpart z)) (realpart z)))
1502 (result (complex-tanh iz)))
1503 (complex (imagpart result)
1504 (- (realpart result)))))
1505

  ViewVC Help
Powered by ViewVC 1.1.5