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

Contents of /src/code/irrat.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.45.2.1.2.1.2.2 - (show annotations)
Thu Jun 29 01:28:02 2006 UTC (7 years, 9 months ago) by rtoy
Branch: double-double-array-branch
Changes since 1.45.2.1.2.1.2.1: +89 -22 lines
Add implementation of special functions for double-double-float.  More
testing required, but basic functionality works.

code/irrat-dd.lisp:
o New file which implements all the required special functions for
  double-double.  Modify existing COMPLEX-<foo> functions to handle
  double-double numbers.

code/irrat.lisp:
o Update HANDLE-REALS to handle double-double float case.
o Update EXPT for double-double float.  (But negative number to
  non-integer power not working yet.)
o LOG handles double-double, but not 2-arg log yet.
o SQRT handles double-double, including complex result.
o ASIN handles double-double.
o ACOS handles double-double.
o ATAN handles double-double.
o ACOSH handles double-double.
o ATANH handles double-double.
o Adjust declaration for SQUARE, SCALB, LOGB-FINITE, and LOGB to allow
  any float type.
o COMPLEX-SQRT handles double-doubles.
o COMPLEX-LOG handles double-doubles.
o COMPLEX-ATANH handles double-doubles.
o COMPLEX-TANH handles double-doubles.
o COMPLEX-ACOS handles double-doubles.
o COMPLEX-ASIN handles double-doubles.
o COMPLEX-ASINH handles double-doubles.
o COMPLEX-ATAN handles double-doubles.
o COMPLEX-TAN handles double-doubles.

tools/worldbuild.lisp:
o Load irrat-dd.

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

  ViewVC Help
Powered by ViewVC 1.1.5