Issue kill(all) in rtest6b so that value assigned to d in a preceding test does not...
[maxima/cygwin.git] / src / numeric.lisp
blob4596df90e798b74655f130beef7fb785cd73b71b
1 ;;; -*- Mode: Lisp; Package: Maxima; Syntax: Common-Lisp; Base: 10-*- ;;;;
3 ;;; This package contains a numeric class for use with Maxima. The
4 ;;; purpose is to allow users to write numerical algorithms that
5 ;;; support double-float, (complex double-float) and Maxima bfloat and
6 ;;; complex bfloat arithmetic, without having to write separate
7 ;;; versions for each. Of course, specially written versions for
8 ;;; double-float and (complex double-float) will probably be much
9 ;;; faster, but this allows users to write just one routine that can
10 ;;; handle all of the data types in a more "natural" Lisp style.
12 #+cmu
13 (eval-when (:compile-toplevel :load-toplevel :execute)
14 (setf lisp::*enable-package-locked-errors* nil))
16 (in-package #:bigfloat)
18 (defun intofp (re)
19 ;; Kind of like Maxima's INTOFP, but we only handle numeric types.
20 ;; We should return a Maxima bigfloat object (list of bigfloat
21 ;; marker, mantissa, and exponent).
22 (cond ((floatp re)
23 (maxima::bcons (maxima::floattofp re)))
24 ((eql re 0)
25 (maxima::bcons '(0 0)))
26 ((integerp re)
27 (maxima::bcons (list (maxima::fpround re) (cl:+ maxima::*m maxima::fpprec))))
28 ((typep re 'ratio)
29 ;; Should we do something better than converting the
30 ;; numerator and denominator to floats and dividing?
31 (maxima::bcons (maxima::fpquotient (cdr (intofp (numerator re)))
32 (cdr (intofp (denominator re))))))
33 ((maxima::$bfloatp re)
34 ;; Call bigfloatp to make sure we round/scale the bigfloat to
35 ;; the correct precision!
36 (maxima::bigfloatp re))
38 (error "Don't know how to convert ~S to a BIGFLOAT" re))))
40 (defclass numeric ()
42 (:documentation "Basic class, like CL's NUMBER type"))
44 (defclass bigfloat (numeric)
45 ;; We store the Maxima internal bigfloat format because we also need
46 ;; the precision in case we have mixed size bigfloat operations.
47 ;; (We could recompute it from the size of the mantissa part, but
48 ;; why bother?
49 ((real :initform (intofp 0)
50 :initarg :real
51 :documentation "A Maxima bigfloat. This contains the full
52 Maxima bigfloat object including the mantissa, the exponent
53 and the bigfloat marker and precision." ))
54 (:documentation "Big float, equivalent to a Maxima bfloat object"))
56 ;; Extract the internal representation of a bigfloat, and adjust the
57 ;; precision to the current value of fpprec.
58 (defmethod real-value ((b bigfloat))
59 (maxima::bigfloatp (slot-value b 'real)))
61 (defclass complex-bigfloat (numeric)
62 ;; Currently, the real and imaginary parts contain a Maxima bigfloat
63 ;; including the bigfloat marker and the mantissa and exponent.
64 ;; Should they be BIGFLOAT objects instead?
65 ((real :initform (intofp 0)
66 :initarg :real
67 :documentation "Real part of a complex bigfloat")
68 (imag :initform (intofp 0)
69 :initarg :imag
70 :documentation "Imaginary part of a complex bigfloat"))
71 (:documentation "Complex bigfloat"))
73 ;; Extract the internal representation of the real part of a
74 ;; complex-bigfloat, and adjust the precision to the current value of
75 ;; fpprec.
76 (defmethod real-value ((b complex-bigfloat))
77 (maxima::bigfloatp (slot-value b 'real)))
79 ;; Extract the internal representation of the imaginary part of a
80 ;; complex-bigfloat, and adjust the precision to the current value of
81 ;; fpprec.
82 (defmethod imag-value ((b complex-bigfloat))
83 (maxima::bigfloatp (slot-value b 'imag)))
85 (defmethod make-load-form ((x bigfloat) &optional environment)
86 (declare (ignore environment))
87 `(make-instance ',(class-of x)
88 :real ',(real-value x)))
90 ;;; BIGFLOAT - External
91 ;;;
92 ;;; BIGFLOAT converts a number to a BIGFLOAT or COMPLEX-BIGFLOAT.
93 ;;; This is intended to convert CL numbers or Maxima (internal)
94 ;;; numbers to a bigfloat object.
95 (defun bigfloat (re &optional im)
96 "Convert RE to a BIGFLOAT. If IM is given, return a COMPLEX-BIGFLOAT"
97 (cond (im
98 (make-instance 'complex-bigfloat
99 :real (intofp re)
100 :imag (intofp im)))
101 ((cl:realp re)
102 (make-instance 'bigfloat :real (intofp re)))
103 ((cl:complexp re)
104 (make-instance 'complex-bigfloat
105 :real (intofp (cl:realpart re))
106 :imag (intofp (cl:imagpart re))))
107 ((maxima::$bfloatp re)
108 (make-instance 'bigfloat :real (intofp re)))
109 ((maxima::complex-number-p re 'maxima::bigfloat-or-number-p)
110 (make-instance 'complex-bigfloat
111 :real (intofp (maxima::$realpart re))
112 :imag (intofp (maxima::$imagpart re))))
113 ((typep re 'bigfloat)
114 ;; Done this way so the new bigfloat updates the precision of
115 ;; the given bigfloat, if necessary.
116 (make-instance 'bigfloat :real (real-value re)))
117 ((typep re 'complex-bigfloat)
118 ;; Done this way so the new bigfloat updates the precision of
119 ;; the given bigfloat, if necessary.
120 (make-instance 'complex-bigfloat
121 :real (real-value re)
122 :imag (imag-value re)))
124 (make-instance 'bigfloat :real (intofp re)))))
127 ;;; MAXIMA::TO - External
129 ;;; Convert a CL number, a BIGFLOAT, or a COMPLEX-BIGFLOAT to
130 ;;; Maxima's internal representation of the number.
131 (defmethod maxima::to ((z cl:float))
134 (defmethod maxima::to ((z cl:rational))
135 (if (typep z 'ratio)
136 (list '(maxima::rat maxima::simp) (numerator z) (denominator z))
139 (defmethod maxima::to ((z cl:complex))
140 (maxima::add (maxima::to (cl:realpart z))
141 (maxima::mul 'maxima::$%i
142 (maxima::to (cl:imagpart z)))))
144 (defmethod maxima::to ((z bigfloat))
145 "Convert BIGFLOAT object to a maxima number"
146 (real-value z))
148 (defmethod maxima::to ((z complex-bigfloat))
149 "Convert COMPLEX-BIGFLOAT object to a maxima number"
150 (maxima::add (real-value z)
151 (maxima::mul 'maxima::$%i
152 (imag-value z))))
154 (defmethod maxima::to ((z t))
157 ;; MAX-EXPONENT roughly computes the log2(|x|). If x is real and x =
158 ;; 2^n*f, with |f| < 1, MAX-EXPONENT returns |n|. For complex
159 ;; numbers, we return one more than the max of the exponent of the
160 ;; real and imaginary parts.
161 (defmethod max-exponent ((x bigfloat))
162 ;; The third element is the exponent of a bigfloat.
163 (cl:abs (third (slot-value x 'real))))
165 (defmethod max-exponent ((x complex-bigfloat))
166 (cl:1+ (cl:max (cl:abs (third (slot-value x 'real)))
167 (cl:abs (third (slot-value x 'imag))))))
169 (defmethod max-exponent ((x cl:float))
170 (if (zerop x)
172 (cl:abs (nth-value 1 (cl:decode-float x)))))
174 (defmethod max-exponent ((x cl:rational))
175 (if (zerop x)
177 (cl:ceiling (cl:log (cl:abs x) 2))))
179 (defmethod max-exponent ((x cl:complex))
180 (cl:1+ (cl:max (max-exponent (cl:realpart x))
181 (max-exponent (cl:imagpart x)))))
183 ;; When computing x^a using exp(a*log(x)), we need extra bits because
184 ;; the integer part of a*log(x) doesn't contribute to the accuracy of
185 ;; the result. The number of extra bits needed is basically the
186 ;; "size" of a plus the number of bits for ceiling(log(x)). We need
187 ;; ceiling(log(x)) extra bits because that's how many bits are taken
188 ;; up by the log(x). The "size" of a is, basically, the exponent of
189 ;; a. If a = 2^n*f where |f| < 1, then the size is abs(n) because
190 ;; that's how many extra bits are added to the integer part of
191 ;; a*log(x). If either |x| or |a| < 1, the size is 0, since no
192 ;; additional bits are taken up.
193 (defun expt-extra-bits (x a)
194 (max 1 (+ (integer-length (max-exponent x))
195 (max-exponent a))))
197 ;;; WITH-EXTRA-PRECISION - Internal
199 ;;; Executes the body BODY with extra precision. The precision is
200 ;;; increased by EXTRA, and the list of variables given in VARLIST have
201 ;;; the precision increased. The precision of the first value of the
202 ;;; body is then reduced back to the normal precision.
203 (defmacro with-extra-precision ((extra (&rest varlist)) &body body)
204 (let ((result (gensym))
205 (old-fpprec (gensym)))
206 `(let ((,result
207 (let ((,old-fpprec maxima::fpprec))
208 (unwind-protect
209 (let ((maxima::fpprec (cl:+ maxima::fpprec ,extra)))
210 (let ,(mapcar #'(lambda (v)
211 ;; Could probably do this in a faster
212 ;; way, but conversion to a maxima
213 ;; form automatically increases the
214 ;; precision of the bigfloat to the
215 ;; new precision. Conversion of that
216 ;; to a bigfloat object preserves the
217 ;; precision.
218 `(,v (bigfloat:to (maxima::to ,v))))
219 varlist)
220 ,@body))
221 (setf maxima::fpprec ,old-fpprec)))))
222 ;; Conversion of the result to a maxima number adjusts the
223 ;; precision appropriately.
224 (bigfloat:to (maxima::to ,result)))))
226 ;;; REALP
228 (defmethod realp ((x cl:real))
231 (defmethod realp ((x bigfloat))
234 (defmethod realp ((x t))
235 nil)
237 ;;; COMPLEXP
238 (defmethod complexp ((x cl:complex))
241 (defmethod complexp ((x complex-bigfloat))
244 (defmethod complexp ((x t))
245 nil)
247 ;;; NUMBERP
248 (defmethod numberp ((x cl:number))
251 (defmethod numberp ((x bigfloat))
254 (defmethod numberp ((x complex-bigfloat))
257 (defmethod numberp ((x t))
258 nil)
260 (defmethod make-load-form ((x complex-bigfloat) &optional environment)
261 (declare (ignore environment))
262 `(make-instance ',(class-of x)
263 :real ',(real-value x)
264 :imag ',(imag-value x)))
266 ;; The print-object and describe-object methods are mostly for
267 ;; debugging purposes. Maxima itself shouldn't ever see such objects.
268 (defmethod print-object ((x bigfloat) stream)
269 (let ((r (cdr (real-value x))))
270 (multiple-value-bind (sign output-list)
271 (if (cl:minusp (first r))
272 (values "-" (maxima::fpformat (maxima::bcons (list (cl:- (first r)) (second r)))))
273 (values "+" (maxima::fpformat (maxima::bcons r))))
274 (format stream "~A~{~D~}" sign output-list))))
276 (defmethod print-object ((x complex-bigfloat) stream)
277 (format stream "~A~A*%i" (realpart x) (imagpart x)))
279 (defmethod describe-object ((x bigfloat) stream)
280 (let ((r (slot-value x 'real)))
281 (format stream "~&~S is a ~D-bit BIGFLOAT with mantissa ~D and exponent ~D~%"
282 x (third (first r)) (second r) (third r))))
284 (defmethod describe-object ((x complex-bigfloat) stream)
285 (format stream "~S is a COMPLEX-BIGFLOAT~%" x)
286 (describe-object (make-instance 'bigfloat :real (slot-value x 'real)) stream)
287 (describe-object (make-instance 'bigfloat :real (slot-value x 'imag)) stream))
290 (defgeneric add1 (a)
291 (:documentation "Add 1"))
293 (defgeneric sub1 (a)
294 (:documentation "Subtract 1"))
297 (defgeneric two-arg-+ (a b)
298 (:documentation "A + B"))
300 (defgeneric two-arg-- (a b)
301 (:documentation "A - B"))
303 (defgeneric two-arg-* (a b)
304 (:documentation "A * B"))
306 (defgeneric two-arg-/ (a b)
307 (:documentation "A / B"))
309 (defgeneric two-arg-< (a b)
310 (:documentation "A < B"))
312 (defgeneric two-arg-> (a b)
313 (:documentation "A > B"))
315 (defgeneric two-arg-<= (a b)
316 (:documentation "A <= B"))
318 (defgeneric two-arg->= (a b)
319 (:documentation "A >= B"))
321 (defgeneric two-arg-= (a b)
322 (:documentation "A = B?"))
325 (defgeneric unary-minus (a)
326 (:documentation "-A"))
328 (defgeneric unary-divide (a)
329 (:documentation "1 / A"))
332 ;;; Basic arithmetic operations
334 ;;; 1+ and 1-
336 (defmethod add1 ((a number))
337 (cl::1+ a))
339 (defmethod add1 ((a bigfloat))
340 (make-instance 'bigfloat
341 :real (maxima::bcons
342 (maxima::fpplus (cdr (real-value a))
343 (maxima::fpone)))))
345 (defmethod add1 ((a complex-bigfloat))
346 (make-instance 'complex-bigfloat
347 :real (maxima::bcons
348 (maxima::fpplus (cdr (real-value a))
349 (maxima::fpone)))
350 :imag (imag-value a)))
352 (defmethod sub1 ((a number))
353 (cl::1- a))
355 (defmethod sub1 ((a bigfloat))
356 (make-instance 'bigfloat
357 :real (maxima::bcons
358 (maxima::fpdifference (cdr (real-value a))
359 (maxima::fpone)))))
361 (defmethod sub1 ((a complex-bigfloat))
362 (make-instance 'complex-bigfloat
363 :real (maxima::bcons
364 (maxima::fpdifference (cdr (real-value a))
365 (maxima::fpone)))
366 :imag (imag-value a)))
368 (declaim (inline 1+ 1-))
370 (defun 1+ (x)
371 (add1 x))
373 (defun 1- (x)
374 (sub1 x))
376 ;; Add two numbers
377 (defmethod two-arg-+ ((a cl:number) (b cl:number))
378 (cl:+ a b))
380 (defmethod two-arg-+ ((a bigfloat) (b bigfloat))
381 (make-instance 'bigfloat
382 :real (maxima::bcons
383 (maxima::fpplus (cdr (real-value a))
384 (cdr (real-value b))))))
386 (defmethod two-arg-+ ((a complex-bigfloat) (b complex-bigfloat))
387 (make-instance 'complex-bigfloat
388 :real (maxima::bcons
389 (maxima::fpplus (cdr (real-value a))
390 (cdr (real-value b))))
391 :imag (maxima::bcons
392 (maxima::fpplus (cdr (imag-value a))
393 (cdr (imag-value b))))))
395 ;; Handle contagion for two-arg-+
396 (defmethod two-arg-+ ((a bigfloat) (b cl:float))
397 (make-instance 'bigfloat
398 :real (maxima::bcons
399 (maxima::fpplus (cdr (real-value a))
400 (cdr (intofp b))))))
402 (defmethod two-arg-+ ((a bigfloat) (b cl:rational))
403 (make-instance 'bigfloat
404 :real (maxima::bcons
405 (maxima::fpplus (cdr (real-value a))
406 (cdr (intofp b))))))
408 (defmethod two-arg-+ ((a bigfloat) (b cl:complex))
409 (make-instance 'complex-bigfloat
410 :real (maxima::bcons
411 (maxima::fpplus (cdr (real-value a))
412 (cdr (intofp (realpart b)))))
413 :imag (intofp (imagpart b))))
415 (defmethod two-arg-+ ((a cl:number) (b bigfloat))
416 (two-arg-+ b a))
418 (defmethod two-arg-+ ((a complex-bigfloat) (b bigfloat))
419 (make-instance 'complex-bigfloat
420 :real (maxima::bcons
421 (maxima::fpplus (cdr (real-value a))
422 (cdr (real-value b))))
423 :imag (imag-value a)))
425 (defmethod two-arg-+ ((a complex-bigfloat) (b number))
426 (make-instance 'complex-bigfloat
427 :real (maxima::bcons
428 (maxima::fpplus (cdr (real-value a))
429 (cdr (intofp (cl:realpart b)))))
430 :imag (maxima::bcons
431 (maxima::fpplus (cdr (imag-value a))
432 (cdr (intofp (cl:imagpart b)))))))
434 (defmethod two-arg-+ ((a bigfloat) (b complex-bigfloat))
435 (two-arg-+ b a))
437 (defmethod two-arg-+ ((a number) (b complex-bigfloat))
438 (two-arg-+ b a))
440 (defun + (&rest args)
441 (if (null args)
443 (do ((args (cdr args) (cdr args))
444 (res (car args)
445 (two-arg-+ res (car args))))
446 ((null args) res))))
448 ;; Negate a number
449 (defmethod unary-minus ((a number))
450 (cl:- a))
452 (defmethod unary-minus ((a bigfloat))
453 (make-instance 'bigfloat
454 :real (maxima::bcons
455 (maxima::fpminus (cdr (real-value a))))))
457 (defmethod unary-minus ((a complex-bigfloat))
458 (make-instance 'complex-bigfloat
459 :real (maxima::bcons
460 (maxima::fpminus (cdr (real-value a))))
461 :imag (maxima::bcons
462 (maxima::fpminus (cdr (imag-value a))))))
464 ;;; Subtract two numbers
465 (defmethod two-arg-- ((a number) (b number))
466 (cl:- a b))
468 (defmethod two-arg-- ((a bigfloat) (b bigfloat))
469 (make-instance 'bigfloat
470 :real (maxima::bcons
471 (maxima::fpdifference (cdr (real-value a))
472 (cdr (real-value b))))))
474 (defmethod two-arg-- ((a complex-bigfloat) (b complex-bigfloat))
475 (make-instance 'complex-bigfloat
476 :real (maxima::bcons
477 (maxima::fpdifference (cdr (real-value a))
478 (cdr (real-value b))))
479 :imag (maxima::bcons
480 (maxima::fpdifference (cdr (imag-value a))
481 (cdr (imag-value b))))))
483 ;; Handle contagion for two-arg--
484 (defmethod two-arg-- ((a bigfloat) (b cl:float))
485 (make-instance 'bigfloat
486 :real (maxima::bcons
487 (maxima::fpdifference (cdr (real-value a))
488 (cdr (intofp b))))))
490 (defmethod two-arg-- ((a bigfloat) (b cl:rational))
491 (make-instance 'bigfloat
492 :real (maxima::bcons
493 (maxima::fpdifference (cdr (real-value a))
494 (cdr (intofp b))))))
496 (defmethod two-arg-- ((a bigfloat) (b cl:complex))
497 (make-instance 'complex-bigfloat
498 :real (maxima::bcons
499 (maxima::fpdifference (cdr (real-value a))
500 (cdr (intofp (realpart b)))))
501 :imag (maxima::bcons (maxima::fpminus (cdr (intofp (imagpart b)))))))
503 (defmethod two-arg-- ((a cl:float) (b bigfloat))
504 (make-instance 'bigfloat
505 :real (maxima::bcons
506 (maxima::fpdifference (cdr (intofp a))
507 (cdr (real-value b))))))
509 (defmethod two-arg-- ((a cl:rational) (b bigfloat))
510 (make-instance 'bigfloat
511 :real (maxima::bcons
512 (maxima::fpdifference (cdr (intofp a))
513 (cdr (real-value b))))))
515 (defmethod two-arg-- ((a cl:complex) (b bigfloat))
516 (two-arg-- (bigfloat (cl:realpart a) (cl:imagpart a)) b))
518 (defmethod two-arg-- ((a complex-bigfloat) (b bigfloat))
519 (make-instance 'complex-bigfloat
520 :real (maxima::bcons
521 (maxima::fpdifference (cdr (real-value a))
522 (cdr (real-value b))))
523 :imag (imag-value a)))
525 (defmethod two-arg-- ((a complex-bigfloat) (b number))
526 (if (cl:complexp b)
527 (two-arg-- a (bigfloat (cl:realpart b) (cl:imagpart b)))
528 (two-arg-- a (bigfloat b))))
530 (defmethod two-arg-- ((a bigfloat) (b complex-bigfloat))
531 (make-instance 'complex-bigfloat
532 :real (maxima::bcons
533 (maxima::fpdifference (cdr (real-value a))
534 (cdr (real-value b))))
535 :imag (maxima::bcons (maxima::fpminus (cdr (imag-value b))))))
537 (defmethod two-arg-- ((a number) (b complex-bigfloat))
538 (if (cl:complexp a)
539 (two-arg-- (bigfloat (cl:realpart a) (cl:imagpart a))
541 (two-arg-- (bigfloat a) b)))
543 (defun - (number &rest more-numbers)
544 (if more-numbers
545 (do ((nlist more-numbers (cdr nlist))
546 (result number))
547 ((atom nlist) result)
548 (declare (list nlist))
549 (setq result (two-arg-- result (car nlist))))
550 (unary-minus number)))
552 ;;; Multiply two numbers
553 (defmethod two-arg-* ((a number) (b number))
554 (cl:* a b))
556 (defmethod two-arg-* ((a bigfloat) (b bigfloat))
557 (make-instance 'bigfloat
558 :real (maxima::bcons
559 (maxima::fptimes* (cdr (real-value a))
560 (cdr (real-value b))))))
562 (defmethod two-arg-* ((a complex-bigfloat) (b complex-bigfloat))
563 (let ((a-re (cdr (real-value a)))
564 (a-im (cdr (imag-value a)))
565 (b-re (cdr (real-value b)))
566 (b-im (cdr (imag-value b))))
567 (make-instance 'complex-bigfloat
568 :real (maxima::bcons
569 (maxima::fpdifference (maxima::fptimes* a-re b-re)
570 (maxima::fptimes* a-im b-im)))
571 :imag (maxima::bcons
572 (maxima::fpplus (maxima::fptimes* a-re b-im)
573 (maxima::fptimes* a-im b-re))))))
575 ;; Handle contagion for two-arg-*
576 (defmethod two-arg-* ((a bigfloat) (b cl:float))
577 (make-instance 'bigfloat
578 :real (maxima::bcons
579 (maxima::fptimes* (cdr (real-value a))
580 (cdr (intofp b))))))
582 (defmethod two-arg-* ((a bigfloat) (b cl:rational))
583 (make-instance 'bigfloat
584 :real (maxima::bcons
585 (maxima::fptimes* (cdr (real-value a))
586 (cdr (intofp b))))))
588 (defmethod two-arg-* ((a bigfloat) (b cl:complex))
589 (make-instance 'complex-bigfloat
590 :real (maxima::bcons
591 (maxima::fptimes* (cdr (real-value a))
592 (cdr (intofp (realpart b)))))
593 :imag (maxima::bcons
594 (maxima::fptimes* (cdr (real-value a))
595 (cdr (intofp (imagpart b)))))))
597 (defmethod two-arg-* ((a cl:number) (b bigfloat))
598 (two-arg-* b a))
600 (defmethod two-arg-* ((a complex-bigfloat) (b bigfloat))
601 (make-instance 'complex-bigfloat
602 :real (maxima::bcons
603 (maxima::fptimes* (cdr (real-value a))
604 (cdr (real-value b))))
605 :imag (maxima::bcons
606 (maxima::fptimes* (cdr (imag-value a))
607 (cdr (real-value b))))))
609 (defmethod two-arg-* ((a complex-bigfloat) (b number))
610 (if (cl:complexp b)
611 (two-arg-* a (bigfloat (cl:realpart b) (cl:imagpart b)))
612 (two-arg-* a (bigfloat b))))
614 (defmethod two-arg-* ((a bigfloat) (b complex-bigfloat))
615 (two-arg-* b a))
617 (defmethod two-arg-* ((a number) (b complex-bigfloat))
618 (two-arg-* b a))
620 (defun * (&rest args)
621 (if (null args)
623 (do ((args (cdr args) (cdr args))
624 (res (car args)
625 (two-arg-* res (car args))))
626 ((null args) res))))
628 ;;; Reciprocal of a number
629 (defmethod unary-divide ((a number))
630 (cl:/ a))
632 (defmethod unary-divide ((a bigfloat))
633 (make-instance 'bigfloat
634 :real (maxima::bcons
635 (maxima::fpquotient (maxima::fpone)
636 (cdr (real-value a))))))
638 (defmethod unary-divide ((b complex-bigfloat))
639 ;; Could just call two-arg-/, but let's optimize this a little
640 (let ((a-re (maxima::fpone))
641 (b-re (cdr (real-value b)))
642 (b-im (cdr (imag-value b))))
643 (if (maxima::fpgreaterp (maxima::fpabs b-re) (maxima::fpabs b-im))
644 (let* ((r (maxima::fpquotient b-im b-re))
645 (dn (maxima::fpplus b-re (maxima::fptimes* r b-im))))
646 (make-instance 'complex-bigfloat
647 :real (maxima::bcons (maxima::fpquotient a-re dn))
648 :imag (maxima::bcons
649 (maxima::fpquotient (maxima::fpminus r)
650 dn))))
651 (let* ((r (maxima::fpquotient b-re b-im))
652 (dn (maxima::fpplus b-im (maxima::fptimes* r b-re))))
653 (make-instance 'complex-bigfloat
654 :real (maxima::bcons (maxima::fpquotient r dn))
655 :imag (maxima::bcons
656 (maxima::fpquotient (maxima::fpminus a-re)
657 dn)))))))
658 ;;; Divide two numbers
659 (defmethod two-arg-/ ((a number) (b number))
660 (cl:/ a b))
662 (defmethod two-arg-/ ((a bigfloat) (b bigfloat))
663 (make-instance 'bigfloat
664 :real (maxima::bcons
665 (maxima::fpquotient (cdr (real-value a))
666 (cdr (real-value b))))))
668 (defmethod two-arg-/ ((a complex-bigfloat) (b complex-bigfloat))
669 (let ((a-re (cdr (real-value a)))
670 (a-im (cdr (imag-value a)))
671 (b-re (cdr (real-value b)))
672 (b-im (cdr (imag-value b))))
673 (if (maxima::fpgreaterp (maxima::fpabs b-re) (maxima::fpabs b-im))
674 (let* ((r (maxima::fpquotient b-im b-re))
675 (dn (maxima::fpplus b-re (maxima::fptimes* r b-im))))
676 (make-instance 'complex-bigfloat
677 :real (maxima::bcons
678 (maxima::fpquotient
679 (maxima::fpplus a-re
680 (maxima::fptimes* a-im r))
681 dn))
682 :imag (maxima::bcons
683 (maxima::fpquotient
684 (maxima::fpdifference a-im
685 (maxima::fptimes* a-re r))
686 dn))))
687 (let* ((r (maxima::fpquotient b-re b-im))
688 (dn (maxima::fpplus b-im (maxima::fptimes* r b-re))))
689 (make-instance 'complex-bigfloat
690 :real (maxima::bcons
691 (maxima::fpquotient
692 (maxima::fpplus (maxima::fptimes* a-re r)
693 a-im)
694 dn))
695 :imag (maxima::bcons
696 (maxima::fpquotient (maxima::fpdifference
697 (maxima::fptimes* a-im r)
698 a-re)
699 dn)))))))
700 ;; Handle contagion for two-arg-/
701 (defmethod two-arg-/ ((a bigfloat) (b cl:float))
702 (make-instance 'bigfloat
703 :real (maxima::bcons
704 (maxima::fpquotient (cdr (real-value a))
705 (cdr (intofp b))))))
707 (defmethod two-arg-/ ((a bigfloat) (b cl:rational))
708 (make-instance 'bigfloat
709 :real (maxima::bcons
710 (maxima::fpquotient (cdr (real-value a))
711 (cdr (intofp b))))))
713 (defmethod two-arg-/ ((a bigfloat) (b cl:complex))
714 (two-arg-/ (complex a) b))
716 (defmethod two-arg-/ ((a cl:float) (b bigfloat))
717 (make-instance 'bigfloat
718 :real (maxima::bcons
719 (maxima::fpquotient (cdr (intofp a))
720 (cdr (real-value b))))))
722 (defmethod two-arg-/ ((a cl:rational) (b bigfloat))
723 (make-instance 'bigfloat
724 :real (maxima::bcons
725 (maxima::fpquotient (cdr (intofp a))
726 (cdr (real-value b))))))
728 (defmethod two-arg-/ ((a cl:complex) (b bigfloat))
729 (two-arg-/ (bigfloat a) b))
732 (defmethod two-arg-/ ((a complex-bigfloat) (b bigfloat))
733 (make-instance 'complex-bigfloat
734 :real (maxima::bcons
735 (maxima::fpquotient (cdr (real-value a))
736 (cdr (real-value b))))
737 :imag (maxima::bcons
738 (maxima::fpquotient (cdr (imag-value a))
739 (cdr (real-value b))))))
741 (defmethod two-arg-/ ((a complex-bigfloat) (b number))
742 (if (cl:complexp b)
743 (two-arg-/ a (bigfloat (cl:realpart b) (cl:imagpart b)))
744 (two-arg-/ a (bigfloat b))))
746 (defmethod two-arg-/ ((a bigfloat) (b complex-bigfloat))
747 (two-arg-/ (make-instance 'complex-bigfloat :real (real-value a))
750 (defmethod two-arg-/ ((a number) (b complex-bigfloat))
751 (if (cl:complexp a)
752 (two-arg-/ (bigfloat (cl:realpart a) (cl:imagpart a))
754 (two-arg-/ (bigfloat a) b)))
757 (defun / (number &rest more-numbers)
758 (if more-numbers
759 (do ((nlist more-numbers (cdr nlist))
760 (result number))
761 ((atom nlist) result)
762 (declare (list nlist))
763 (setq result (two-arg-/ result (car nlist))))
764 (unary-divide number)))
766 ;;; Compare against zero (zerop, minusp, plusp)
767 (macrolet
768 ((frob (name)
769 (let ((cl-name (intern (symbol-name name) :cl)))
770 `(progn
771 (defmethod ,name ((x cl:float))
772 (,cl-name x))
773 (defmethod ,name ((x cl:rational))
774 (,cl-name x))))))
775 (frob plusp)
776 (frob minusp))
778 (defmethod zerop ((x number))
779 (cl:zerop x))
781 (defmethod zerop ((x bigfloat))
782 (let ((r (cdr (real-value x))))
783 (and (zerop (first r))
784 (zerop (second r)))))
786 (defmethod zerop ((a complex-bigfloat))
787 (and (equal (cdr (real-value a)) '(0 0))
788 (equal (cdr (imag-value a)) '(0 0))))
790 (defmethod plusp ((x bigfloat))
791 (cl:plusp (first (cdr (real-value x)))))
793 (defmethod minusp ((x bigfloat))
794 (cl:minusp (first (cdr (real-value x)))))
798 ;;; Equality
799 (defmethod two-arg-= ((a number) (b number))
800 (cl:= a b))
802 (defmethod two-arg-= ((a bigfloat) (b bigfloat))
803 (zerop (two-arg-- a b)))
805 (defmethod two-arg-= ((a complex-bigfloat) (b complex-bigfloat))
806 (zerop (two-arg-- a b)))
808 ;; Handle contagion for two-arg-=. This needs some work. CL says
809 ;; floats and rationals are compared by converting the float to a
810 ;; rational before converting.
811 (defmethod two-arg-= ((a bigfloat) (b number))
812 (zerop (two-arg-- a b)))
814 (defmethod two-arg-= ((a number) (b bigfloat))
815 (two-arg-= b a))
817 (defmethod two-arg-= ((a complex-bigfloat) (b number))
818 (zerop (two-arg-- a b)))
820 (defmethod two-arg-= ((a number) (b complex-bigfloat))
821 (two-arg-= b a))
823 (defun = (number &rest more-numbers)
824 "Returns T if all of its arguments are numerically equal, NIL otherwise."
825 (declare (optimize (safety 2))
826 (dynamic-extent more-numbers))
827 (do ((nlist more-numbers (cdr nlist)))
828 ((atom nlist) t)
829 (declare (list nlist))
830 (if (not (two-arg-= (car nlist) number))
831 (return nil))))
833 (defun /= (number &rest more-numbers)
834 "Returns T if no two of its arguments are numerically equal, NIL otherwise."
835 (declare (optimize (safety 2))
836 (dynamic-extent more-numbers))
837 (do* ((head number (car nlist))
838 (nlist more-numbers (cdr nlist)))
839 ((atom nlist) t)
840 (declare (list nlist))
841 (unless (do* ((nl nlist (cdr nl)))
842 ((atom nl) t)
843 (declare (list nl))
844 (if (two-arg-= head (car nl))
845 (return nil)))
846 (return nil))))
848 ;;; Comparison operations
849 (macrolet
850 ((frob (op)
851 (let ((method (intern (concatenate 'string
852 (string '#:two-arg-)
853 (symbol-name op))))
854 (cl-fun (find-symbol (symbol-name op) :cl)))
855 `(progn
856 (defmethod ,method ((a cl:float) (b cl:float))
857 (,cl-fun a b))
858 (defmethod ,method ((a cl:float) (b cl:rational))
859 (,cl-fun a b))
860 (defmethod ,method ((a cl:rational) (b cl:float))
861 (,cl-fun a b))
862 (defmethod ,method ((a cl:rational) (b cl:rational))
863 (,cl-fun a b))
864 (defun ,op (number &rest more-numbers)
865 "Returns T if its arguments are in strictly increasing order, NIL otherwise."
866 (declare (optimize (safety 2))
867 (dynamic-extent more-numbers))
868 (do* ((n number (car nlist))
869 (nlist more-numbers (cdr nlist)))
870 ((atom nlist) t)
871 (declare (list nlist))
872 (if (not (,method n (car nlist))) (return nil))))))))
873 (frob <)
874 (frob >)
875 (frob <=)
876 (frob >=))
878 (defmethod two-arg-< ((x bigfloat) (y bigfloat))
879 (maxima::fplessp (cdr (real-value x)) (cdr (real-value y))))
881 (defmethod two-arg-< ((x bigfloat) (y cl:float))
882 (maxima::fplessp (cdr (real-value x)) (cdr (intofp y))))
884 (defmethod two-arg-< ((x bigfloat) (y cl:rational))
885 (maxima::fplessp (cdr (real-value x)) (cdr (intofp y))))
887 (defmethod two-arg-< ((x cl:float) (y bigfloat))
888 (maxima::fplessp (cdr (intofp x)) (cdr (real-value y))))
890 (defmethod two-arg-< ((x cl:rational) (y bigfloat))
891 (maxima::fplessp (cdr (intofp x)) (cdr (real-value y))))
893 (defmethod two-arg-> ((x bigfloat) (y bigfloat))
894 (maxima::fpgreaterp (cdr (real-value x)) (cdr (real-value y))))
896 (defmethod two-arg-> ((x bigfloat) (y cl:float))
897 (maxima::fpgreaterp (cdr (real-value x)) (cdr (intofp y))))
899 (defmethod two-arg-> ((x bigfloat) (y cl:rational))
900 (maxima::fpgreaterp (cdr (real-value x)) (cdr (intofp y))))
902 (defmethod two-arg-> ((x cl:float) (y bigfloat))
903 (maxima::fpgreaterp (cdr (intofp x)) (cdr (real-value y))))
905 (defmethod two-arg-> ((x cl:rational) (y bigfloat))
906 (maxima::fpgreaterp (cdr (intofp x)) (cdr (real-value y))))
908 (defmethod two-arg-<= ((x bigfloat) (y bigfloat))
909 (or (zerop (two-arg-- x y))
910 (maxima::fplessp (cdr (real-value x)) (cdr (real-value y)))))
912 (defmethod two-arg-<= ((x bigfloat) (y cl:float))
913 (or (zerop (two-arg-- x y))
914 (maxima::fplessp (cdr (real-value x)) (cdr (intofp y)))))
916 (defmethod two-arg-<= ((x bigfloat) (y cl:rational))
917 (or (zerop (two-arg-- x y))
918 (maxima::fplessp (cdr (real-value x)) (cdr (intofp y)))))
920 (defmethod two-arg-<= ((x cl:float) (y bigfloat))
921 (or (zerop (two-arg-- x y))
922 (maxima::fplessp (cdr (intofp x)) (cdr (real-value y)))))
924 (defmethod two-arg-<= ((x cl:rational) (y bigfloat))
925 (or (zerop (two-arg-- x y))
926 (maxima::fplessp (cdr (intofp x)) (cdr (real-value y)))))
928 (defmethod two-arg->= ((x bigfloat) (y bigfloat))
929 (or (zerop (two-arg-- x y))
930 (maxima::fpgreaterp (cdr (real-value x)) (cdr (real-value y)))))
932 (defmethod two-arg->= ((x bigfloat) (y cl:float))
933 (or (zerop (two-arg-- x y))
934 (maxima::fpgreaterp (cdr (real-value x)) (cdr (intofp y)))))
936 (defmethod two-arg->= ((x bigfloat) (y cl:rational))
937 (or (zerop (two-arg-- x y))
938 (maxima::fpgreaterp (cdr (real-value x)) (cdr (intofp y)))))
940 (defmethod two-arg->= ((x cl:float) (y bigfloat))
941 (or (zerop (two-arg-- x y))
942 (maxima::fpgreaterp (cdr (intofp x)) (cdr (real-value y)))))
944 (defmethod two-arg->= ((x cl:rational) (y bigfloat))
945 (or (zerop (two-arg-- x y))
946 (maxima::fpgreaterp (cdr (intofp x)) (cdr (real-value y)))))
948 ;; Need to define incf and decf to call our generic +/- methods.
949 (defmacro incf (place &optional (delta 1) &environment env)
950 "The first argument is some location holding a number. This number is
951 incremented by the second argument, DELTA, which defaults to 1."
952 (multiple-value-bind (dummies vals newval setter getter)
953 (get-setf-expansion place env)
954 (let ((d (gensym)))
955 `(let* (,@(mapcar #'list dummies vals)
956 (,d ,delta)
957 (,(car newval) (+ ,getter ,d)))
958 ,setter))))
960 (defmacro decf (place &optional (delta 1) &environment env)
961 "The first argument is some location holding a number. This number is
962 decremented by the second argument, DELTA, which defaults to 1."
963 (multiple-value-bind (dummies vals newval setter getter)
964 (get-setf-expansion place env)
965 (let ((d (gensym)))
966 `(let* (,@(mapcar #'list dummies vals)
967 (,d ,delta)
968 (,(car newval) (- ,getter ,d)))
969 ,setter))))
973 ;;; Special functions for real-valued arguments
974 (macrolet
975 ((frob (name)
976 (let ((cl-name (intern (symbol-name name) :cl)))
977 `(progn
978 (defmethod ,name ((x number))
979 (,cl-name x))))))
980 (frob sqrt)
981 (frob abs)
982 (frob exp)
983 (frob sin)
984 (frob cos)
985 (frob tan)
986 (frob asin)
987 (frob acos)
988 (frob sinh)
989 (frob cosh)
990 (frob tanh)
991 (frob asinh)
992 (frob acosh)
993 (frob atanh)
996 (defmethod abs ((x bigfloat))
997 (make-instance 'bigfloat
998 :real (maxima::bcons (maxima::fpabs (cdr (real-value x))))))
1000 (defmethod abs ((z complex-bigfloat))
1001 (let ((x (make-instance 'bigfloat :real (real-value z)))
1002 (y (make-instance 'bigfloat :real (imag-value z))))
1003 ;; Bigfloats don't overflow, so we don't need anything special.
1004 (sqrt (+ (* x x) (* y y)))))
1006 (defmethod exp ((x bigfloat))
1007 (make-instance 'bigfloat
1008 :real (maxima::bcons (maxima::fpexp (cdr (real-value x))))))
1010 (defmethod sin ((x bigfloat))
1011 (make-instance 'bigfloat
1012 :real (maxima::bcons (maxima::fpsin (cdr (real-value x)) t))))
1014 (defmethod cos ((x bigfloat))
1015 (make-instance 'bigfloat
1016 :real (maxima::bcons (maxima::fpsin (cdr (real-value x)) nil))))
1018 (defmethod tan ((x bigfloat))
1019 (let ((r (cdr (real-value x))))
1020 (make-instance 'bigfloat
1021 :real (maxima::bcons
1022 (maxima::fpquotient (maxima::fpsin r t)
1023 (maxima::fpsin r nil))))))
1025 (defmethod asin ((x bigfloat))
1026 (bigfloat (maxima::fpasin (real-value x))))
1028 (defmethod acos ((x bigfloat))
1029 (bigfloat (maxima::fpacos (real-value x))))
1032 (defmethod sqrt ((x bigfloat))
1033 (if (minusp x)
1034 (make-instance 'complex-bigfloat
1035 :real (intofp 0)
1036 :imag (maxima::bcons
1037 (maxima::fproot (maxima::bcons (maxima::fpabs (cdr (real-value x)))) 2)))
1038 (make-instance 'bigfloat
1039 :real (maxima::bcons
1040 (maxima::fproot (real-value x) 2)))))
1042 (defmethod sqrt ((z complex-bigfloat))
1043 (multiple-value-bind (rx ry)
1044 (maxima::complex-sqrt (real-value z)
1045 (imag-value z))
1046 (make-instance 'complex-bigfloat
1047 :real (maxima::bcons rx)
1048 :imag (maxima::bcons ry))))
1050 (defmethod one-arg-log ((a number))
1051 (cl:log a))
1053 (defmethod one-arg-log ((a bigfloat))
1054 (if (minusp a)
1055 (make-instance 'complex-bigfloat
1056 :real (maxima::bcons
1057 (maxima::fplog (maxima::fpabs (cdr (real-value a)))))
1058 :imag (maxima::bcons (maxima::fppi)))
1059 (make-instance 'bigfloat
1060 :real (maxima::bcons (maxima::fplog (cdr (real-value a)))))))
1062 (defmethod one-arg-log ((a complex-bigfloat))
1063 (let* ((res (maxima::big-float-log (real-value a)
1064 (imag-value a))))
1065 (bigfloat res)))
1067 (defmethod two-arg-log ((a number) (b number))
1068 (cl:log a b))
1070 (defmethod two-arg-log ((a numeric) (b numeric))
1071 (two-arg-/ (one-arg-log a) (one-arg-log b)))
1073 (defmethod two-arg-log ((a numeric) (b cl:number))
1074 (two-arg-/ (one-arg-log a) (one-arg-log (bigfloat b))))
1076 (defmethod two-arg-log ((a cl:number) (b numeric))
1077 (two-arg-/ (one-arg-log (bigfloat a)) (one-arg-log b)))
1079 (defun log (a &optional b)
1080 (if b
1081 (two-arg-log a b)
1082 (one-arg-log a)))
1084 (defmethod sinh ((x bigfloat))
1085 (let ((r (real-value x)))
1086 (make-instance 'bigfloat :real (maxima::fpsinh r))))
1088 (defmethod cosh ((x bigfloat))
1089 (let ((r (real-value x)))
1090 (make-instance 'bigfloat :real (maxima::$bfloat `((maxima::%cosh) ,r)))))
1092 (defmethod tanh ((x bigfloat))
1093 (let ((r (real-value x)))
1094 (make-instance 'bigfloat :real (maxima::fptanh r))))
1096 (defmethod asinh ((x bigfloat))
1097 (let ((r (real-value x)))
1098 (make-instance 'bigfloat :real (maxima::fpasinh r))))
1100 (defmethod atanh ((x bigfloat))
1101 (let ((r (maxima::big-float-atanh (real-value x))))
1102 (if (maxima::bigfloatp r)
1103 (make-instance 'bigfloat :real r)
1104 (make-instance 'complex-bigfloat
1105 :real (maxima::$realpart r)
1106 :imag (maxima::$imagpart r)))))
1108 (defmethod acosh ((x bigfloat))
1109 (let* ((r (real-value x))
1110 (value (maxima::meval `((maxima::%acosh maxima::simp) ,r))))
1111 (if (maxima::bigfloatp value)
1112 (make-instance 'bigfloat :real value)
1113 (make-instance 'complex-bigfloat
1114 :real (maxima::$realpart value)
1115 :imag (maxima::$imagpart value)))))
1117 ;;; Complex arguments
1119 ;;; Special functions for complex args
1120 (macrolet
1121 ((frob (name &optional big-float-op-p)
1122 (if big-float-op-p
1123 (let ((big-op (intern (concatenate 'string
1124 (string '#:big-float-)
1125 (string name))
1126 '#:maxima)))
1127 `(defmethod ,name ((a complex-bigfloat))
1128 (let ((res (,big-op (real-value a)
1129 (imag-value a))))
1130 (to res))))
1131 (let ((max-op (intern (concatenate 'string "$" (string name)) '#:maxima)))
1132 `(defmethod ,name ((a complex-bigfloat))
1133 ;; We should do something better than calling meval
1134 (let* ((arg (maxima::add (real-value a)
1135 (maxima::mul 'maxima::$%i (imag-value a))))
1136 (result (maxima::meval `((,',max-op maxima::simp) ,arg))))
1137 (to result)))))))
1138 (frob exp)
1139 (frob sin)
1140 (frob cos)
1141 (frob tan)
1142 (frob asin t)
1143 (frob acos t)
1144 (frob sinh)
1145 (frob cosh)
1146 (frob tanh t)
1147 (frob asinh t)
1148 (frob acosh)
1149 (frob atanh t))
1151 (defmethod one-arg-atan ((a number))
1152 (cl:atan a))
1154 (defmethod one-arg-atan ((a bigfloat))
1155 (make-instance 'bigfloat
1156 :real (maxima::bcons (maxima::fpatan (cdr (real-value a))))))
1158 (defmethod one-arg-atan ((a complex-bigfloat))
1159 ;; We should do something better than calling meval
1160 (let* ((arg (maxima::add (real-value a)
1161 (maxima::mul 'maxima::$%i (imag-value a))))
1162 (result (maxima::meval `((maxima::%atan maxima::simp) ,arg))))
1163 (make-instance 'complex-bigfloat
1164 :real (maxima::$realpart result)
1165 :imag (maxima::$imagpart result))))
1167 (defmethod two-arg-atan ((a real) (b real))
1168 (cl:atan a b))
1170 (defmethod two-arg-atan ((a bigfloat) (b bigfloat))
1171 (make-instance 'bigfloat
1172 :real (maxima::bcons
1173 (maxima::fpatan2 (cdr (real-value a))
1174 (cdr (real-value b))))))
1176 (defmethod two-arg-atan ((a bigfloat) (b cl:float))
1177 (make-instance 'bigfloat
1178 :real (maxima::bcons (maxima::fpatan2 (cdr (real-value a))
1179 (cdr (intofp b))))))
1181 (defmethod two-arg-atan ((a bigfloat) (b cl:rational))
1182 (make-instance 'bigfloat
1183 :real (maxima::bcons (maxima::fpatan2 (cdr (real-value a))
1184 (cdr (intofp b))))))
1186 (defmethod two-arg-atan ((a cl:float) (b bigfloat))
1187 (make-instance 'bigfloat
1188 :real (maxima::bcons (maxima::fpatan2 (cdr (intofp a))
1189 (cdr (real-value b))))))
1191 (defmethod two-arg-atan ((a cl:rational) (b bigfloat))
1192 (make-instance 'bigfloat
1193 :real (maxima::bcons (maxima::fpatan2 (cdr (intofp a))
1194 (cdr (real-value b))))))
1196 (defun atan (a &optional b)
1197 (if b
1198 (two-arg-atan a b)
1199 (one-arg-atan a)))
1201 (defmethod scale-float ((a cl:float) (n integer))
1202 (cl:scale-float a n))
1204 (defmethod scale-float ((a bigfloat) (n integer))
1205 (if (cl:zerop (second (real-value a)))
1206 (make-instance 'bigfloat :real (maxima::bcons (list 0 0)))
1207 (destructuring-bind (marker mantissa exp)
1208 (real-value a)
1209 (declare (ignore marker))
1210 (make-instance 'bigfloat :real (maxima::bcons (list mantissa (+ exp n)))))))
1212 (macrolet
1213 ((frob (name)
1214 (let ((cl-name (intern (string name) '#:cl)))
1215 `(defmethod ,name ((a number))
1216 (,cl-name a)))))
1217 (frob realpart)
1218 (frob imagpart)
1219 (frob conjugate)
1220 (frob phase))
1222 (macrolet
1223 ((frob (name)
1224 (let ((cl-name (intern (string name) '#:cl)))
1225 `(defmethod ,name ((a number) &optional (divisor 1))
1226 (,cl-name a divisor)))))
1227 (frob floor)
1228 (frob ffloor)
1229 (frob ceiling)
1230 (frob fceiling)
1231 (frob truncate)
1232 (frob ftruncate)
1233 (frob round)
1234 (frob fround))
1237 (defmethod realpart ((a bigfloat))
1238 (make-instance 'bigfloat :real (real-value a)))
1240 (defmethod realpart ((a complex-bigfloat))
1241 (make-instance 'bigfloat :real (real-value a)))
1243 (defmethod imagpart ((a bigfloat))
1244 (make-instance 'bigfloat :real (intofp 0)))
1246 (defmethod imagpart ((a complex-bigfloat))
1247 (make-instance 'bigfloat :real (imag-value a)))
1249 (defmethod conjugate ((a bigfloat))
1250 (make-instance 'bigfloat :real (real-value a)))
1252 (defmethod conjugate ((a complex-bigfloat))
1253 (make-instance 'complex-bigfloat
1254 :real (real-value a)
1255 :imag (maxima::bcons (maxima::fpminus (cdr (imag-value a))))))
1257 (defmethod cis ((a cl:float))
1258 (cl:cis a))
1260 (defmethod cis ((a cl:rational))
1261 (cl:cis a))
1263 (defmethod cis ((a bigfloat))
1264 (make-instance 'complex-bigfloat
1265 :real (maxima::bcons (maxima::fpsin (cdr (real-value a)) nil))
1266 :imag (maxima::bcons (maxima::fpsin (cdr (real-value a)) t))))
1268 (defmethod phase ((a bigfloat))
1269 (let ((r (cdr (real-value a))))
1270 (if (cl:>= (car r) 0)
1271 (make-instance 'bigfloat :real (maxima::bcons (list 0 0)))
1272 (make-instance 'bigfloat :real (maxima::bcons (maxima::fppi))))))
1274 (defmethod phase ((a complex-bigfloat))
1275 (make-instance 'bigfloat
1276 :real (maxima::bcons (maxima::fpatan2 (cdr (imag-value a))
1277 (cdr (real-value a))))))
1279 (defun max (number &rest more-numbers)
1280 "Returns the greatest of its arguments."
1281 (declare (optimize (safety 2)) (type (or real bigfloat) number)
1282 (dynamic-extent more-numbers))
1283 (dolist (real more-numbers)
1284 (when (> real number)
1285 (setq number real)))
1286 number)
1288 (defun min (number &rest more-numbers)
1289 "Returns the least of its arguments."
1290 (declare (optimize (safety 2)) (type (or real bigfloat) number)
1291 (dynamic-extent more-numbers))
1292 (do ((nlist more-numbers (cdr nlist))
1293 (result (the (or real bigfloat) number)))
1294 ((null nlist) (return result))
1295 (declare (list nlist))
1296 (if (< (car nlist) result)
1297 (setq result (car nlist)))))
1299 (defmethod one-arg-complex ((a real))
1300 (cl:complex a))
1302 (defmethod one-arg-complex ((a bigfloat))
1303 (make-instance 'complex-bigfloat
1304 :real (real-value a)
1305 :imag (intofp 0)))
1307 (defmethod two-arg-complex ((a real) (b real))
1308 (cl:complex a b))
1310 (defmethod two-arg-complex ((a bigfloat) (b bigfloat))
1311 (make-instance 'complex-bigfloat
1312 :real (real-value a)
1313 :imag (real-value b)))
1315 (defmethod two-arg-complex ((a cl:float) (b bigfloat))
1316 (make-instance 'complex-bigfloat
1317 :real (intofp a)
1318 :imag (real-value b)))
1320 (defmethod two-arg-complex ((a cl:rational) (b bigfloat))
1321 (make-instance 'complex-bigfloat
1322 :real (intofp a)
1323 :imag (real-value b)))
1325 (defmethod two-arg-complex ((a bigfloat) (b cl:float))
1326 (make-instance 'complex-bigfloat
1327 :real (real-value a)
1328 :imag (intofp b)))
1330 (defmethod two-arg-complex ((a bigfloat) (b cl:rational))
1331 (make-instance 'complex-bigfloat
1332 :real (real-value a)
1333 :imag (intofp b)))
1335 (defun complex (a &optional b)
1336 (if b
1337 (two-arg-complex a b)
1338 (one-arg-complex a)))
1340 (defmethod unary-floor ((a bigfloat))
1341 ;; fpentier truncates to zero, so adjust for negative numbers
1342 (let ((trunc (maxima::fpentier (real-value a))))
1343 (cond ((minusp a)
1344 ;; If the truncated value is the same as the original,
1345 ;; there's nothing to do because A was an integer.
1346 ;; Otherwise, we need to subtract 1 to make it the floor.
1347 (if (= trunc a)
1348 trunc
1349 (1- trunc)))
1351 trunc))))
1353 (defmethod unary-ffloor ((a bigfloat))
1354 ;; We can probably do better than converting to an integer and
1355 ;; converting back to a float.
1356 (make-instance 'bigfloat :real (intofp (unary-floor a))))
1358 (defmethod floor ((a bigfloat) &optional (divisor 1))
1359 (if (= divisor 1)
1360 (let ((int (unary-floor a)))
1361 (values int (- a int)))
1362 (let ((q (unary-floor (/ a divisor))))
1363 (values q (- a (* q divisor))))))
1365 (defmethod ffloor ((a bigfloat) &optional (divisor 1))
1366 (if (= divisor 1)
1367 (let ((int (unary-ffloor a)))
1368 (values int (- a int)))
1369 (let ((q (unary-ffloor (/ a divisor))))
1370 (values q (- a (* q divisor))))))
1372 (defmethod unary-truncate ((a bigfloat))
1373 (maxima::fpentier (real-value a)))
1375 (defmethod unary-ftruncate ((a bigfloat))
1376 ;; We can probably do better than converting to an integer and
1377 ;; converting back to a float.
1378 (make-instance 'bigfloat :real (intofp (unary-truncate a))))
1380 (defmethod truncate ((a bigfloat) &optional (divisor 1))
1381 (if (eql divisor 1)
1382 (let ((int (unary-truncate a)))
1383 (values int (- a int)))
1384 (let ((q (unary-truncate (/ a divisor))))
1385 (values q (- a (* q divisor))))))
1387 (defmethod ftruncate ((a bigfloat) &optional (divisor 1))
1388 (if (eql divisor 1)
1389 (let ((int (unary-ftruncate a)))
1390 (values int (- a int)))
1391 (let ((q (unary-ftruncate (/ a divisor))))
1392 (values q (- a (* q divisor))))))
1394 (defmethod unary-ceiling ((a bigfloat))
1395 ;; fpentier truncates to zero, so adjust for positive numbers.
1396 (if (minusp a)
1397 (maxima::fpentier (real-value a))
1398 (maxima::fpentier (real-value (+ a 1)))))
1400 (defmethod unary-fceiling ((a bigfloat))
1401 ;; We can probably do better than converting to an integer and
1402 ;; converting back to a float.
1403 (make-instance 'bigfloat :real (intofp (unary-ceiling a))))
1405 (defmethod ceiling ((a bigfloat) &optional (divisor 1))
1406 (if (eql divisor 1)
1407 (let ((int (unary-ceiling a)))
1408 (values int (- a int)))
1409 (let ((q (unary-ceiling (/ a divisor))))
1410 (values q (- a (* q divisor))))))
1412 (defmethod fceiling ((a bigfloat) &optional (divisor 1))
1413 (if (eql divisor 1)
1414 (let ((int (unary-fceiling a)))
1415 (values int (- a int)))
1416 (let ((q (unary-fceiling (/ a divisor))))
1417 (values q (- a (* q divisor))))))
1419 ;; Stolen from CMUCL.
1420 (defmethod round ((a bigfloat) &optional (divisor 1))
1421 (multiple-value-bind (tru rem)
1422 (truncate a divisor)
1423 (if (zerop rem)
1424 (values tru rem)
1425 (let ((thresh (/ (abs divisor) 2)))
1426 (cond ((or (> rem thresh)
1427 (and (= rem thresh) (oddp tru)))
1428 (if (minusp divisor)
1429 (values (- tru 1) (+ rem divisor))
1430 (values (+ tru 1) (- rem divisor))))
1431 ((let ((-thresh (- thresh)))
1432 (or (< rem -thresh)
1433 (and (= rem -thresh) (oddp tru))))
1434 (if (minusp divisor)
1435 (values (+ tru 1) (- rem divisor))
1436 (values (- tru 1) (+ rem divisor))))
1437 (t (values tru rem)))))))
1439 (defmethod fround ((number bigfloat) &optional (divisor 1))
1440 "Same as ROUND, but returns first value as a float."
1441 (multiple-value-bind (res rem)
1442 (round number divisor)
1443 (values (bigfloat res) rem)))
1445 (defmethod expt ((a number) (b number))
1446 (cl:expt a b))
1448 ;; This needs more work
1449 (defmethod expt ((a numeric) (b numeric))
1450 (if (zerop b)
1451 ;; CLHS says if the power is 0, the answer is 1 of the appropriate type.
1452 (if (or (typep a 'complex-bigfloat)
1453 (typep b 'complex-bigfloat))
1454 (complex (bigfloat 1))
1455 (bigfloat 1))
1456 (cond ((and (zerop a) (plusp (realpart b)))
1457 (* a b))
1458 ((and (realp b) (= b (truncate b)))
1459 ;; Use the numeric^number method because it can be much
1460 ;; more accurate when b is an integer.
1461 (expt a (truncate b)))
1463 (with-extra-precision ((expt-extra-bits a b)
1464 (a b))
1465 (exp (* b (log a))))))))
1467 (defmethod expt ((a cl:number) (b numeric))
1468 (if (zerop b)
1469 ;; CLHS says if the power is 0, the answer is 1 of the appropriate type.
1470 (if (or (typep a 'cl:complex)
1471 (typep b 'complex-bigfloat))
1472 (complex (bigfloat 1))
1473 (bigfloat 1))
1474 (cond ((and (zerop a) (plusp (realpart b)))
1475 (* a b))
1476 ((and (realp b) (= b (truncate b)))
1477 (with-extra-precision ((expt-extra-bits a b)
1478 (a b))
1479 (intofp (expt a (truncate b)))))
1481 (with-extra-precision ((expt-extra-bits a b)
1482 (a b))
1483 (exp (* b (log (bigfloat a)))))))))
1485 (defmethod expt ((a numeric) (b cl:number))
1486 (if (zerop b)
1487 ;; CLHS says if the power is 0, the answer is 1 of the appropriate type.
1488 (if (or (typep a 'complex-bigfloat)
1489 (typep b 'cl:complex))
1490 (complex (bigfloat 1))
1491 (bigfloat 1))
1492 (if (and (zerop a) (plusp (realpart b)))
1493 (* a b)
1494 ;; Handle a few special cases using multiplication.
1495 (cond ((= b 1)
1497 ((= b -1)
1498 (/ a))
1499 ((= b 2)
1500 (* a a))
1501 ((= b -2)
1502 (/ (* a a)))
1503 ((= b 3) (* a a a))
1504 ((= b -3) (/ (* a a a)))
1505 ((= b 4)
1506 (let ((a2 (* a a)))
1507 (* a2 a2)))
1508 ((= b -4)
1509 (let ((a2 (* a a)))
1510 (/ (* a2 a2))))
1512 (with-extra-precision ((expt-extra-bits a b)
1513 (a b))
1514 (exp (* (bigfloat b) (log a)))))))))
1516 ;; Handle a^b a little more carefully because the result is known to
1517 ;; be real when a is real and b is an integer.
1518 (defmethod expt ((a bigfloat) (b integer))
1519 (cond ((zerop b)
1520 (bigfloat 1))
1521 ((and (zerop a) (plusp b))
1522 ;; 0^b, for positive b
1523 (* a b))
1524 ;; Handle a few special cases using multiplication.
1525 ((eql b 1) a)
1526 ((eql b -1) (/ a))
1527 ((eql b 2) (* a a))
1528 ((eql b -2) (/ (* a a)))
1529 ((eql b 3) (* a a a))
1530 ((eql b -3) (/ (* a a a)))
1531 ((eql b 4)
1532 (let ((a2 (* a a)))
1533 (* a2 a2)))
1534 ((eql b -4)
1535 (let ((a2 (* a a)))
1536 (/ (* a2 a2))))
1537 ((minusp a)
1538 ;; a^b = exp(b*log(|a|) + %i*%pi*b)
1539 ;; = exp(b*log(|a|))*exp(%i*%pi*b)
1540 ;; = (-1)^b*exp(b*log(|a|))
1541 (with-extra-precision ((expt-extra-bits a b)
1542 (a b))
1543 (* (exp (* b (log (abs a))))
1544 (if (oddp b) -1 1))))
1546 (with-extra-precision ((expt-extra-bits a b)
1547 (a b))
1548 (exp (* b (log a)))))))
1550 ;;; TO - External
1552 ;;; TO takes a maxima number and converts it. Floats remain
1553 ;;; floats, maxima cl:rationals are converted to CL cl:rationals. Maxima
1554 ;;; bigfloats are convert to BIGFLOATS. Maxima complex numbers are
1555 ;;; converted to CL complex numbers or to COMPLEX-BIGFLOAT's.
1556 (defun to (maxima-num &optional imag)
1557 (let ((result (ignore-errors (%to maxima-num imag))))
1558 (or result
1559 (maxima::merror (intl:gettext "BIGFLOAT: unable to convert ~M to a CL or BIGFLOAT number.")
1560 (if imag
1561 (maxima::add maxima-num (maxima::mul imag 'maxima::$%i))
1562 maxima-num)))))
1564 ;;; MAYBE-TO - External
1566 ;;; Like TO, but if the maxima number can't be converted to a CL
1567 ;;; number or BIGFLOAT, just return the maxima number.
1568 (defun maybe-to (maxima-num &optional imag)
1569 (let ((result (ignore-errors (%to maxima-num imag))))
1570 (or result
1571 (if imag
1572 (maxima::add maxima-num imag)
1573 maxima-num))))
1575 (defun %to (maxima-num &optional imag)
1576 (cond (imag
1577 ;; Clisp has a "feature" that (complex rat float) does not
1578 ;; make the both components of the complex number a float.
1579 ;; Sometimes this is nice, but other times it's annoying
1580 ;; because it is non-ANSI behavior. For our code, we really
1581 ;; want both components to be a float.
1582 #-clisp
1583 (complex (to maxima-num) (to imag))
1584 #+clisp
1585 (let ((re (to maxima-num))
1586 (im (to imag)))
1587 (cond ((and (rationalp re) (floatp im))
1588 (setf re (float re im)))
1589 ((and (rational im) (floatp re))
1590 (setf im (float im re))))
1591 (complex re im)))
1593 (cond ((cl:numberp maxima-num)
1594 maxima-num)
1595 ((eq maxima-num 'maxima::$%i)
1596 ;; Convert %i to an exact complex cl:rational.
1597 #c(0 1))
1598 ((consp maxima-num)
1599 ;; Some kind of maxima number
1600 (cond ((maxima::ratnump maxima-num)
1601 ;; Maxima cl:rational ((mrat ...) num den)
1602 (/ (second maxima-num) (third maxima-num)))
1603 ((maxima::$bfloatp maxima-num)
1604 (bigfloat maxima-num))
1605 ((maxima::complex-number-p maxima-num #'(lambda (x)
1606 (or (cl:realp x)
1607 (maxima::$bfloatp x)
1608 (and (consp x)
1609 (eq (caar x) 'maxima::rat)))))
1610 ;; We have some kind of complex number whose
1611 ;; parts are a cl:real, a bfloat, or a Maxima
1612 ;; cl:rational.
1613 (let ((re (maxima::$realpart maxima-num))
1614 (im (maxima::$imagpart maxima-num)))
1615 (to re im)))))
1616 ((or (typep maxima-num 'bigfloat)
1617 (typep maxima-num 'complex-bigfloat))
1618 maxima-num)
1620 (error "BIGFLOAT: unable to convert to a CL or BIGFLOAT number."))))))
1622 ;;; EPSILON - External
1624 ;;; Return the float epsilon value for the given float type.
1625 (defmethod epsilon ((x cl:float))
1626 (etypecase x
1627 (short-float short-float-epsilon)
1628 (single-float single-float-epsilon)
1629 (double-float double-float-epsilon)
1630 (long-float long-float-epsilon)))
1632 (defmethod epsilon ((x cl:complex))
1633 (epsilon (cl:realpart x)))
1635 (defmethod epsilon ((x bigfloat))
1636 ;; epsilon is just above 2^(-fpprec).
1637 (make-instance 'bigfloat
1638 :real (maxima::bcons (list (1+ (ash 1 (1- maxima::fpprec)))
1639 (- (1- maxima::fpprec))))))
1641 (defmethod epsilon ((x complex-bigfloat))
1642 (epsilon (realpart x)))
1646 ;; Compiler macros to convert + to multiple calls to two-arg-+. Same
1647 ;; for -, *, and /.
1648 (define-compiler-macro + (&whole form &rest args)
1649 (declare (ignore form))
1650 (if (null args)
1652 (do ((args (cdr args) (cdr args))
1653 (res (car args)
1654 `(two-arg-+ ,res ,(car args))))
1655 ((null args) res))))
1657 (define-compiler-macro - (&whole form number &rest more-numbers)
1658 (declare (ignore form))
1659 (if more-numbers
1660 (do ((nlist more-numbers (cdr nlist))
1661 (result number))
1662 ((atom nlist) result)
1663 (declare (list nlist))
1664 (setq result `(two-arg-- ,result ,(car nlist))))
1665 `(unary-minus ,number)))
1667 (define-compiler-macro * (&whole form &rest args)
1668 (declare (ignore form))
1669 (if (null args)
1671 (do ((args (cdr args) (cdr args))
1672 (res (car args)
1673 `(two-arg-* ,res ,(car args))))
1674 ((null args) res))))
1676 (define-compiler-macro / (number &rest more-numbers)
1677 (if more-numbers
1678 (do ((nlist more-numbers (cdr nlist))
1679 (result number))
1680 ((atom nlist) result)
1681 (declare (list nlist))
1682 (setq result `(two-arg-/ ,result ,(car nlist))))
1683 `(unary-divide ,number)))
1685 (define-compiler-macro /= (&whole form number &rest more-numbers)
1686 ;; Convert (/= x y) to (not (two-arg-= x y)). Should we try to
1687 ;; handle a few more cases?
1688 (if (cdr more-numbers)
1689 form
1690 `(not (two-arg-= ,number ,(car more-numbers)))))
1692 ;; Compiler macros to convert <, >, <=, and >= into multiple calls of
1693 ;; the corresponding two-arg-<foo> function.
1694 (macrolet
1695 ((frob (op)
1696 (let ((method (intern (concatenate 'string
1697 (string '#:two-arg-)
1698 (symbol-name op)))))
1699 `(define-compiler-macro ,op (number &rest more-numbers)
1700 (do* ((n number (car nlist))
1701 (nlist more-numbers (cdr nlist))
1702 (res nil))
1703 ((atom nlist)
1704 `(and ,@(nreverse res)))
1705 (push `(,',method ,n ,(car nlist)) res))))))
1706 (frob <)
1707 (frob >)
1708 (frob <=)
1709 (frob >=))
1711 (defmethod integer-decode-float ((x cl:float))
1712 (cl:integer-decode-float x))
1714 (defmethod integer-decode-float ((x bigfloat))
1715 (let ((r (real-value x)))
1716 (values (abs (second r))
1717 (- (third r) (third (first r)))
1718 (signum (second r)))))
1720 (defmethod decode-float ((x cl:float))
1721 (cl:decode-float x))
1723 (defmethod decode-float ((x bigfloat))
1724 (let ((r (real-value x)))
1725 (values (make-instance 'bigfloat
1726 :real (maxima::bcons (list (abs (second r)) 0)))
1727 (third r)
1728 (bigfloat (signum (second r))))))
1730 (progn
1731 (defmethod float ((x real) (y cl:float))
1732 (cl:float x y))
1734 (defmethod float ((x real) (y bigfloat))
1735 (bigfloat x))
1738 ;; Like Maxima's fp2flo, but for single-float numbers.
1739 (defun fp2single (l)
1740 (let ((precision (caddar l))
1741 (mantissa (cadr l))
1742 (exponent (caddr l))
1743 (fpprec (float-digits 1f0))
1744 (maxima::*m 0))
1745 ;; Round the mantissa to the number of bits of precision of the
1746 ;; machine, and then convert it to a floating point fraction. We
1747 ;; have 0.5 <= mantissa < 1
1748 (setq mantissa (maxima::quotient (maxima::fpround mantissa)
1749 (expt 2f0 fpprec)))
1750 ;; Multiply the mantissa by the exponent portion. I'm not sure
1751 ;; why the exponent computation is so complicated.
1753 ;; GCL doesn't signal overflow from scale-float if the number
1754 ;; would overflow. We have to do it this way. 0.5 <= mantissa <
1755 ;; 1. The largest double-float is .999999 * 2^128. So if the
1756 ;; exponent is 128 or higher, we have an overflow.
1757 (let ((e (+ exponent (- precision) maxima::*m fpprec)))
1758 (if (>= (abs e) 129)
1759 (maxima::merror (intl:gettext "FP2SINGLE: floating point overflow converting ~:M to float.") l)
1760 (cl:scale-float mantissa e)))))
1763 (defmethod float ((x bigfloat) (y cl:float))
1764 (if (typep y 'maxima::flonum)
1765 (maxima::fp2flo (real-value x))
1766 (fp2single (real-value x))))
1768 (defmethod random ((x cl:float) &optional (state cl:*random-state*))
1769 (cl:random x state))
1770 (defmethod random ((x integer) &optional (state cl:*random-state*))
1771 (cl:random x state))
1773 (defmethod random ((x bigfloat) &optional (state cl:*random-state*))
1774 ;; Generate an integer with fpprec bits, and convert to a bigfloat
1775 ;; by making the exponent 0. Then multiply by the arg to get the
1776 ;; correct range.
1777 (if (plusp x)
1778 (let ((int (cl:random (ash 1 maxima::fpprec) state)))
1779 (* x (bigfloat (maxima::bcons (list int 0)))))
1780 (error "Argument is not a positive bigfloat: ~A~%" x)))
1782 (defmethod signum ((x number))
1783 (cl:signum x))
1785 (defmethod signum ((x bigfloat))
1786 (cond ((minusp x)
1787 (bigfloat -1))
1788 ((plusp x)
1789 (bigfloat 1))
1791 x)))
1793 (defmethod signum ((x complex-bigfloat))
1794 (/ x (abs x)))
1796 (defmethod float-sign ((x cl:float))
1797 (cl:float-sign x))
1799 (defmethod float-sign ((x bigfloat))
1800 (if (minusp x)
1801 (bigfloat -1)
1802 (bigfloat 1)))
1804 (defmethod float-digits ((x cl:float))
1805 (cl:float-digits x))
1807 (defmethod float-digits ((x bigfloat))
1808 ;; Should we just return fpprec or should we get the actual number
1809 ;; of bits in the bigfloat number? We choose the latter in case the
1810 ;; number and fpprec don't match.
1811 (let ((r (slot-value x 'real)))
1812 (third (first r))))
1814 (defmethod rational ((x real))
1815 (cl:rational x))
1817 (defmethod rational ((x bigfloat))
1818 (destructuring-bind ((marker simp prec) mantissa exp)
1819 (real-value x)
1820 (declare (ignore marker simp))
1821 (* mantissa (expt 2 (- exp prec)))))
1823 (defmethod rationalize ((x real))
1824 (cl:rationalize x))
1826 ;;; This routine taken from CMUCL, which, in turn is a routine from
1827 ;;; CLISP, which is GPL.
1829 ;;; I (rtoy) have modified it from CMUCL so that it only handles bigfloats.
1831 ;;; RATIONALIZE -- Public
1833 ;;; The algorithm here is the method described in CLISP. Bruno Haible has
1834 ;;; graciously given permission to use this algorithm. He says, "You can use
1835 ;;; it, if you present the following explanation of the algorithm."
1837 ;;; Algorithm (recursively presented):
1838 ;;; If x is a rational number, return x.
1839 ;;; If x = 0.0, return 0.
1840 ;;; If x < 0.0, return (- (rationalize (- x))).
1841 ;;; If x > 0.0:
1842 ;;; Call (integer-decode-float x). It returns a m,e,s=1 (mantissa,
1843 ;;; exponent, sign).
1844 ;;; If m = 0 or e >= 0: return x = m*2^e.
1845 ;;; Search a rational number between a = (m-1/2)*2^e and b = (m+1/2)*2^e
1846 ;;; with smallest possible numerator and denominator.
1847 ;;; Note 1: If m is a power of 2, we ought to take a = (m-1/4)*2^e.
1848 ;;; But in this case the result will be x itself anyway, regardless of
1849 ;;; the choice of a. Therefore we can simply ignore this case.
1850 ;;; Note 2: At first, we need to consider the closed interval [a,b].
1851 ;;; but since a and b have the denominator 2^(|e|+1) whereas x itself
1852 ;;; has a denominator <= 2^|e|, we can restrict the search to the open
1853 ;;; interval (a,b).
1854 ;;; So, for given a and b (0 < a < b) we are searching a rational number
1855 ;;; y with a <= y <= b.
1856 ;;; Recursive algorithm fraction_between(a,b):
1857 ;;; c := (ceiling a)
1858 ;;; if c < b
1859 ;;; then return c ; because a <= c < b, c integer
1860 ;;; else
1861 ;;; ; a is not integer (otherwise we would have had c = a < b)
1862 ;;; k := c-1 ; k = floor(a), k < a < b <= k+1
1863 ;;; return y = k + 1/fraction_between(1/(b-k), 1/(a-k))
1864 ;;; ; note 1 <= 1/(b-k) < 1/(a-k)
1866 ;;; You can see that we are actually computing a continued fraction expansion.
1868 ;;; Algorithm (iterative):
1869 ;;; If x is rational, return x.
1870 ;;; Call (integer-decode-float x). It returns a m,e,s (mantissa,
1871 ;;; exponent, sign).
1872 ;;; If m = 0 or e >= 0, return m*2^e*s. (This includes the case x = 0.0.)
1873 ;;; Create rational numbers a := (2*m-1)*2^(e-1) and b := (2*m+1)*2^(e-1)
1874 ;;; (positive and already in lowest terms because the denominator is a
1875 ;;; power of two and the numerator is odd).
1876 ;;; Start a continued fraction expansion
1877 ;;; p[-1] := 0, p[0] := 1, q[-1] := 1, q[0] := 0, i := 0.
1878 ;;; Loop
1879 ;;; c := (ceiling a)
1880 ;;; if c >= b
1881 ;;; then k := c-1, partial_quotient(k), (a,b) := (1/(b-k),1/(a-k)),
1882 ;;; goto Loop
1883 ;;; finally partial_quotient(c).
1884 ;;; Here partial_quotient(c) denotes the iteration
1885 ;;; i := i+1, p[i] := c*p[i-1]+p[i-2], q[i] := c*q[i-1]+q[i-2].
1886 ;;; At the end, return s * (p[i]/q[i]).
1887 ;;; This rational number is already in lowest terms because
1888 ;;; p[i]*q[i-1]-p[i-1]*q[i] = (-1)^i.
1890 (defmethod rationalize ((x bigfloat))
1891 (multiple-value-bind (frac expo sign)
1892 (integer-decode-float x)
1893 (cond ((or (zerop frac) (>= expo 0))
1894 (if (minusp sign)
1895 (- (ash frac expo))
1896 (ash frac expo)))
1898 ;; expo < 0 and (2*m-1) and (2*m+1) are coprime to 2^(1-e),
1899 ;; so build the fraction up immediately, without having to do
1900 ;; a gcd.
1901 (let ((a (/ (- (* 2 frac) 1) (ash 1 (- 1 expo))))
1902 (b (/ (+ (* 2 frac) 1) (ash 1 (- 1 expo))))
1903 (p0 0)
1904 (q0 1)
1905 (p1 1)
1906 (q1 0))
1907 (do ((c (ceiling a) (ceiling a)))
1908 ((< c b)
1909 (let ((top (+ (* c p1) p0))
1910 (bot (+ (* c q1) q0)))
1911 (/ (if (minusp sign)
1912 (- top)
1913 top)
1914 bot)))
1915 (let* ((k (- c 1))
1916 (p2 (+ (* k p1) p0))
1917 (q2 (+ (* k q1) q0)))
1918 (psetf a (/ (- b k))
1919 b (/ (- a k)))
1920 (setf p0 p1
1921 q0 q1
1922 p1 p2
1923 q1 q2))))))))
1925 (defun coerce (obj type)
1926 (flet ((coerce-error ()
1927 (error "Cannot coerce ~A to type ~S" obj type)))
1928 (cond ((typep obj type)
1929 obj)
1930 ((subtypep type 'bigfloat)
1931 ;; (coerce foo 'bigfloat). Foo has to be a real
1932 (cond ((typep obj 'real)
1933 (bigfloat obj))
1935 (coerce-error))))
1936 ((subtypep type 'complex-bigfloat)
1937 ;; (coerce foo 'complex-bigfloat). Foo has to be a real or complex
1938 (cond ((typep obj 'real)
1939 (bigfloat obj 0))
1940 ((typep obj 'cl:complex)
1941 (bigfloat obj))
1942 ((typep obj 'bigfloat)
1943 (bigfloat obj 0))
1945 (coerce-error))))
1946 ((typep obj 'bigfloat)
1947 ;; (coerce bigfloat foo)
1948 (cond ((subtypep type 'cl:float)
1949 (float obj (cl:coerce 0 type)))
1950 ((subtypep type '(cl:complex long-float))
1951 (cl:complex (float (realpart obj) 1l0)
1952 (float (imagpart obj) 1l0)))
1953 ((subtypep type '(cl:complex double-float))
1954 (cl:complex (float (realpart obj) 1d0)
1955 (float (imagpart obj) 1d0)))
1956 ((subtypep type '(cl:complex single-float))
1957 (cl:complex (float (realpart obj) 1f0)
1958 (float (imagpart obj) 1f0)))
1959 ((subtypep type 'cl:complex)
1960 ;; What should we do here? Return a
1961 ;; complex-bigfloat? A complex double-float?
1962 ;; complex single-float? I arbitrarily select
1963 ;; complex maxima:flonum for now.
1964 (cl:complex (float (realpart obj) 1.0)
1965 (float (imagpart obj) 1.0)))
1967 (coerce-error))))
1968 ((typep obj 'complex-bigfloat)
1969 ;; (coerce complex-bigfloat foo)
1970 (cond ((subtypep type 'complex-bigfloat)
1971 obj)
1972 ((subtypep type '(cl:complex long-float))
1973 (cl:complex (float (realpart obj) 1l0)
1974 (float (imagpart obj) 1l0)))
1975 ((subtypep type '(cl:complex double-float))
1976 (cl:complex (float (realpart obj) 1d0)
1977 (float (imagpart obj) 1d0)))
1978 ((subtypep type '(cl:complex single-float))
1979 (cl:complex (float (realpart obj) 1f0)
1980 (float (imagpart obj) 1f0)))
1982 (coerce-error))))
1984 (cl:coerce obj type)))))
1986 ;;; %PI - External
1988 ;;; Return a value of pi with the same precision as the argument.
1989 ;;; For rationals, we return a single-float approximation.
1990 (defmethod %pi ((x cl:rational))
1991 (cl:coerce cl:pi 'single-float))
1993 (defmethod %pi ((x cl:float))
1994 (cl:float cl:pi x))
1996 (defmethod %pi ((x bigfloat))
1997 (to (maxima::bcons (maxima::fppi))))
1999 (defmethod %pi ((x cl:complex))
2000 (cl:float cl:pi (realpart x)))
2002 (defmethod %pi ((x complex-bigfloat))
2003 (to (maxima::bcons (maxima::fppi))))
2005 ;;; %e - External
2007 ;;; Return a value of e with the same precision as the argument.
2008 ;;; For rationals, we return a single-float approximation.
2009 (defmethod %e ((x cl:rational))
2010 (cl:coerce maxima::%e-val 'single-float))
2012 (defmethod %e ((x cl:float))
2013 (cl:float maxima::%e-val x))
2015 (defmethod %e ((x bigfloat))
2016 (to (maxima::bcons (maxima::fpe))))
2018 (defmethod %e ((x cl:complex))
2019 (cl:float maxima::%e-val (realpart x)))
2021 (defmethod %e ((x complex-bigfloat))
2022 (to (maxima::bcons (maxima::fpe))))
2024 ;;;; Useful routines
2026 ;;; Evaluation of continued fractions
2028 (defvar *debug-cf-eval*
2030 "When true, enable some debugging prints when evaluating a
2031 continued fraction.")
2033 ;; Max number of iterations allowed when evaluating the continued
2034 ;; fraction. When this is reached, we assume that the continued
2035 ;; fraction did not converge.
2036 (defvar *max-cf-iterations*
2037 10000
2038 "Max number of iterations allowed when evaluating the continued
2039 fraction. When this is reached, we assume that the continued
2040 fraction did not converge.")
2042 ;;; LENTZ - External
2044 ;;; Lentz's algorithm for evaluating continued fractions.
2046 ;;; Let the continued fraction be:
2048 ;;; a1 a2 a3
2049 ;;; b0 + ---- ---- ----
2050 ;;; b1 + b2 + b3 +
2053 ;;; Then LENTZ expects two functions, each taking a single fixnum
2054 ;;; index. The first returns the b term and the second returns the a
2055 ;;; terms as above for a give n.
2056 (defun lentz (bf af)
2057 (let ((tiny-value-count 0))
2058 (flet ((value-or-tiny (v)
2059 ;; If v is zero, return a "tiny" number.
2060 (if (zerop v)
2061 (progn
2062 (incf tiny-value-count)
2063 (etypecase v
2064 ((or double-float cl:complex)
2065 (sqrt least-positive-normalized-double-float))
2066 ((or bigfloat complex-bigfloat)
2067 ;; What is a "tiny" bigfloat? Bigfloats have
2068 ;; unbounded exponents, so we need something
2069 ;; small, but not zero. Arbitrarily choose an
2070 ;; exponent of 50 times the precision.
2071 (expt 10 (- (* 50 maxima::$fpprec))))))
2072 v)))
2073 (let* ((f (value-or-tiny (funcall bf 0)))
2074 (c f)
2075 (d 0)
2076 (eps (epsilon f)))
2077 (loop
2078 for j from 1 upto *max-cf-iterations*
2079 for an = (funcall af j)
2080 for bn = (funcall bf j)
2081 do (progn
2082 (setf d (value-or-tiny (+ bn (* an d))))
2083 (setf c (value-or-tiny (+ bn (/ an c))))
2084 (when *debug-cf-eval*
2085 (format t "~&j = ~d~%" j)
2086 (format t " an = ~s~%" an)
2087 (format t " bn = ~s~%" bn)
2088 (format t " c = ~s~%" c)
2089 (format t " d = ~s~%" d))
2090 (let ((delta (/ c d)))
2091 (setf d (/ d))
2092 (setf f (* f delta))
2093 (when *debug-cf-eval*
2094 (format t " dl= ~S (|dl - 1| = ~S)~%" delta (abs (1- delta)))
2095 (format t " f = ~S~%" f))
2096 (when (<= (abs (- delta 1)) eps)
2097 (return-from lentz (values f j tiny-value-count)))))
2098 finally
2099 (error 'simple-error
2100 :format-control "~<Continued fraction failed to converge after ~D iterations.~% Delta = ~S~>"
2101 :format-arguments (list *max-cf-iterations* (/ c d))))))))
2103 ;;; SUM-POWER-SERIES - External
2105 ;;; SUM-POWER-SERIES sums the given power series, adding terms until
2106 ;;; the next term would not change the sum.
2108 ;;; The series to be summed is
2110 ;;; S = 1 + sum(c[k]*x^k, k, 1, inf)
2111 ;;; = 1 + sum(prod(f[n]*x, n, 1, k), k, 1, inf)
2113 ;;; where f[n] = c[n]/c[n-1].
2115 (defun sum-power-series (x f)
2116 (let ((eps (epsilon x)))
2117 (do* ((k 1 (+ 1 k))
2118 (sum 1 (+ sum term))
2119 (term (* x (funcall f 1))
2120 (* term x (funcall f k))))
2121 ((< (abs term) (* eps (abs sum)))
2122 sum)
2123 #+nil
2124 (format t "~4d: ~S ~S ~S~%" k sum term (funcall f k)))))
2126 ;; Format bigfloats using ~E format. This is suitable as a ~// format.
2128 ;; NOTE: This is a modified version of FORMAT-EXPONENTIAL from CMUCL to
2129 ;; support printing of bfloats.
2131 (defun format-e (stream number colonp atp
2132 &optional w d e k
2133 overflowchar padchar exponentchar)
2134 (typecase number
2135 (bigfloat
2136 (maxima::bfloat-format-e stream (real-value number) colonp atp
2137 w d e (or k 1)
2138 overflowchar
2139 (or padchar #\space)
2140 (or exponentchar #\b)))
2141 (complex-bigfloat
2142 ;; FIXME: Do something better than this since this doesn't honor
2143 ;; any of the parameters.
2144 (princ number stream))
2145 (otherwise
2146 ;; We were given some other kind of object. Just use CL's normal
2147 ;; ~E printer to print it.
2148 (let ((f
2149 (with-output-to-string (s)
2150 ;; Construct a suitable ~E format string from the given
2151 ;; parameters. First, handle w,d,e,k.
2152 (write-string "~V,V,V,V," s)
2153 (if overflowchar
2154 (format s "'~C," overflowchar)
2155 (write-string "," s))
2156 (if padchar
2157 (format s "'~C," padchar)
2158 (write-string "," s))
2159 (when exponentchar
2160 (format s "'~C" exponentchar))
2161 (when colonp
2162 (write-char #\: s))
2163 (when atp
2164 (write-char #\@ s))
2165 (write-char #\E s))))
2166 (format stream f w d e k number)))))
2169 (defmacro assert-equal (expected form)
2170 (let ((result (gensym))
2171 (e (gensym)))
2172 `(let ((,e ,expected)
2173 (,result ,form))
2174 (unless (equal ,e ,result)
2175 (format *debug-io* "Assertion failed: Expected ~S but got ~S~%" ,e ,result)))))
2177 (assert-equal " 0.990E+00" (format nil
2178 "~11,3,2,0,'*,,'E/bigfloat::format-e/"
2179 (bigfloat:bigfloat 99/100)))
2180 (assert-equal " 0.999E+00" (format nil
2181 "~11,3,2,0,'*,,'E/bigfloat::format-e/"
2182 (bigfloat:bigfloat 999/1000)))
2183 ;; Actually " 0.100E+01", but format-e doesn't round the output.
2184 (assert-equal " 0.999E+00" (format nil
2185 "~11,3,2,0,'*,,'E/bigfloat::format-e/"
2186 (bigfloat:bigfloat 9999/10000)))
2187 (assert-equal " 0.999E-04" (format nil
2188 "~11,3,2,0,'*,,'E/bigfloat::format-e/"
2189 (bigfloat:bigfloat 0000999/10000000)))
2190 ;; Actually " 0.100E-03", but format-e doesn't round the output.
2191 (assert-equal " 0.999E-0e" (format nil
2192 "~11,3,2,0,'*,,'E/bigfloat::format-e/"
2193 (bigfloat:bigfloat 00009999/100000000)))
2194 (assert-equal " 9.999E-05" (format nil
2195 "~11,3,2,,'*,,'E/bigfloat::format-e/"
2196 (bigfloat:bigfloat 00009999/100000000)))
2197 ;; Actually " 1.000E-04", but format-e doesn't round the output.
2198 (assert-equal " 9.999E-05" (format nil
2199 "~11,3,2,,'*,,'E/bigfloat::format-e/"
2200 (bigfloat:bigfloat 000099999/1000000000)))
2201 ;; All of these currently fail.
2202 (assert-equal ".00123d+6" (format nil
2203 "~9,,,-2/bigfloat::format-e/"
2204 (bigfloat:bigfloat 1.2345689d3)))
2205 (assert-equal "-.0012d+6" (format nil
2206 "~9,,,-2/bigfloat::format-e/"
2207 (bigfloat:bigfloat -1.2345689d3)))
2208 (assert-equal ".00123d+0" (format nil
2209 "~9,,,-2/bigfloat::format-e/"
2210 (bigfloat:bigfloat 1.2345689d-3)))
2211 (assert-equal "-.0012d+0" (format nil
2212 "~9,,,-2/bigfloat::format-e/"
2213 (bigfloat:bigfloat -1.2345689d-3)))
2215 ;; These fail because too many digits are printed and because the
2216 ;; scale factor isn't properly applied.
2217 (assert-equal ".00000003d+8" (format nil
2218 "~9,4,,-7E"
2219 (bigfloat:bigfloat pi)))
2220 (assert-equal ".000003d+6" (format nil
2221 "~9,4,,-5E"
2222 (bigfloat:bigfloat pi)))
2223 (assert-equal "3141600.d-6" (format nil
2224 "~5,4,,7E"
2225 (bigfloat:bigfloat pi)))
2226 (assert-equal " 314.16d-2" (format nil
2227 "~11,4,,3E"
2228 (bigfloat:bigfloat pi)))
2229 (assert-equal " 31416.d-4" (format nil
2230 "~11,4,,5E"
2231 (bigfloat:bigfloat pi)))
2232 (assert-equal " 0.3142d+1" (format nil
2233 "~11,4,,0E"
2234 (bigfloat:bigfloat pi)))
2235 (assert-equal ".03142d+2" (format nil
2236 "~9,,,-1E"
2237 (bigfloat:bigfloat pi)))
2238 (assert-equal "0.003141592653589793d+3" (format nil
2239 "~,,,-2E"
2240 (bigfloat:bigfloat pi)))
2241 (assert-equal "31.41592653589793d-1" (format nil
2242 "~,,,2E"
2243 (bigfloat:bigfloat pi)))
2244 ;; Fails because exponent is printed as "b0" instead of "b+0"
2245 (assert-equal "3.141592653589793b+0" (format nil "~E" (bigfloat:bigfloat pi)))
2248 ;; These fail because too many digits are printed and because the
2249 ;; scale factor isn't properly applied.
2250 (assert-equal ".03142d+2" (format nil "~9,5,,-1E" (bigfloat:bigfloat pi)))
2251 (assert-equal " 0.03142d+2" (format nil "~11,5,,-1E" (bigfloat:bigfloat pi)))
2252 (assert-equal "| 3141593.d-06|" (format nil "|~13,6,2,7E|" (bigfloat:bigfloat pi)))
2253 (assert-equal "0.314d+01" (format nil "~9,3,2,0,'%E" (bigfloat:bigfloat pi)))
2254 (assert-equal "+.003d+03" (format nil "~9,3,2,-2,'%@E" (bigfloat:bigfloat pi)))
2255 (assert-equal "+0.003d+03" (format nil "~10,3,2,-2,'%@E" (bigfloat:bigfloat pi)))
2256 (assert-equal "=====+0.003d+03" (format nil "~15,3,2,-2,'%,'=@E" (bigfloat:bigfloat pi)))
2257 (assert-equal "0.003d+03" (format nil "~9,3,2,-2,'%E" (bigfloat:bigfloat pi)))
2258 (assert-equal "%%%%%%%%" (format nil "~8,3,2,-2,'%@E" (bigfloat:bigfloat pi)))
2260 ;; Works
2261 (assert-equal "0.0f+0" (format nil "~e" 0))
2263 ;; Fails because exponent is printed as "b0" instead of "b+0'
2264 (assert-equal "0.0b+0" (format nil "~e" (bigfloat:bigfloat 0d0)))
2265 ;; Fails because exponent is printed as "b0 " instead of "b+0000"
2266 (assert-equal "0.0b+0000" (format nil "~9,,4e" (bigfloat:bigfloat 0d0)))
2267 ;; Fails because exponent is printed as "b4" isntead of "b+4"
2268 (assert-equal "1.2345678901234567b+4" (format nil "~E"
2269 (bigfloat:bigfloat 1.234567890123456789d4)))
2271 ;; Fails because exponent is printed as "b36" instead of "b+36"
2272 (assert-equal "1.32922799578492b+36" (format nil "~20E"
2273 (bigfloat:bigfloat (expt 2d0 120))))
2274 ;; Fails because too many digits are printed and the exponent doesn't include "+".
2275 (assert-equal " 1.32922800b+36" (format nil "~21,8E"
2276 (bigfloat:bigfloat (expt 2d0 120))))
2280 ;; Format bigfloats using ~F format. This is suitable as a ~// format.
2282 ;; NOTE: This is a modified version of FORMAT-FIXED from CMUCL to
2283 ;; support printing of bfloats.
2285 (defun format-f (stream number colonp atp
2286 &optional w d k overflowchar padchar)
2287 (typecase number
2288 (bigfloat
2289 (maxima::bfloat-format-f stream (real-value number) colonp atp
2290 w d (or k 0)
2291 overflowchar
2292 (or padchar #\space)))
2293 (complex-bigfloat
2294 ;; FIXME: Do something better than this since this doesn't honor
2295 ;; any of the parameters.
2296 (princ number stream))
2297 (otherwise
2298 ;; We were given some other kind of object. Just use CL's normal
2299 ;; ~F printer to print it.
2300 (let ((f
2301 (with-output-to-string (s)
2302 ;; Construct a suitable ~F format string from the given
2303 ;; parameters. First handle w,d,k.
2304 (write-string "~V,V,V," s)
2305 (if overflowchar
2306 (format s "'~C," overflowchar)
2307 (write-string "," s))
2308 (if (char= padchar #\space)
2309 (write-string "," s)
2310 (format s "'~C," padchar))
2311 (when colonp
2312 (write-char #\: s))
2313 (when atp
2314 (write-char #\@ s))
2315 (write-char #\F s))))
2316 (format stream f w d k number)))))
2318 ;; Format bigfloats using ~G format. This is suitable as a ~// format.
2320 ;; NOTE: This is a modified version of FORMAT-GENERAL from CMUCL to
2321 ;; support printing of bfloats.
2323 (defun format-g (stream number colonp atp
2324 &optional w d e k overflowchar padchar marker)
2325 (typecase number
2326 (bigfloat
2327 (maxima::bfloat-format-g stream (real-value number) colonp atp
2328 w d e (or k 1)
2329 overflowchar
2330 (or padchar #\space)
2331 (or marker #\b)))
2332 (complex-bigfloat
2333 ;; FIXME: Do something better than this since this doesn't honor
2334 ;; any of the parameters.
2335 (princ number stream))
2336 (otherwise
2337 ;; We were given some other kind of object. Just use CL's normal
2338 ;; ~G printer to print it.
2339 (let ((f
2340 (with-output-to-string (s)
2341 ;; Construct a suitable ~E format string from the given
2342 ;; parameters. First, handle w,d,e,k.
2343 (write-string "~V,V,V,V," s)
2344 (if overflowchar
2345 (format s "'~C," overflowchar)
2346 (write-string "," s))
2347 (if padchar
2348 (format s "'~C," padchar)
2349 (write-string "," s))
2350 (when marker
2351 (format s "'~C" marker))
2352 (when colonp
2353 (write-char #\: s))
2354 (when atp
2355 (write-char #\@ s))
2356 (write-char #\G s))))
2357 (format stream f w d e k number)))))