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

Contents of /src/code/irrat.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5