Add harmonic_number and harmonic_to_psi
[maxima.git] / src / rpart.lisp
blobacb0fc267ed91a551662b8544d5a7495ff00e3c7
1 ;;; -*- Mode: Lisp; Package: Maxima; Syntax: Common-Lisp; Base: 10 -*- ;;;;
2 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 ;;; The data in this file contains enhancements. ;;;;;
4 ;;; ;;;;;
5 ;;; Copyright (c) 1984,1987 by William Schelter,University of Texas ;;;;;
6 ;;; All rights reserved ;;;;;
7 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
8 ;;; (c) Copyright 1982 Massachusetts Institute of Technology ;;;
9 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
11 (in-package :maxima)
13 (macsyma-module rpart)
15 ;;; Complex variable utilities
16 ;;;
17 ;;; Macsyma functions: $realpart $imagpart $rectform $polarform
18 ;;; $cabs $carg
19 ;;; Utility functions: trisplit risplit absarg cabs andmapc andmapcar
21 (load-macsyma-macros rzmac)
23 (declare-top (special $radexpand
24 $keepfloat))
26 ;;; Realpart gives the real part of an expr.
28 (defmfun $realpart (xx) (car (trisplit xx)))
30 (defprop $realpart %realpart verb)
31 (defprop %realpart $realpart noun)
32 (defprop %realpart simp-realpart operators)
34 (defun risplit-signum (x) ;rectangular form for a signum expression
35 (let* ((z (risplit (cadr x))) (a (car z)) (b (cdr z)) (r)) ;signum(a+%i b), where a and b are real
36 (cond ((eq t (meqp b 0)) ;signum(a) -> signum(a) + 0 %i
37 (cons (take '(%signum) a) 0))
38 ((or (eq t (mnqp a 0)) (eq t (mnqp b 0))) ;signum(a + %i b) --> a/sqrt(a^2+b^2) + %i b/sqrt(a^2+b^2)
39 (setq r (take '(%sqrt) (add (power a 2) (power b 2))))
40 (cons (div a r) (div b r)))
41 (t (cons (take '(%realpart) x) (take '(%imagpart) x)))))) ;nothing known
43 (setf (get '%signum 'risplit-function) 'risplit-signum)
45 (defun simp-realpart (expr z simpflag)
46 (oneargcheck expr)
47 (setq z (simpcheck (cadr expr) simpflag))
48 (let ((sgn nil))
49 (cond ((mnump z) z)
50 ((eq (setq sgn ($csign z)) '$imaginary)
52 ((eq sgn '$complex)
53 (cond ((complex-number-p ($expand z) 'bigfloat-or-number-p)
54 ($realpart z))
55 (t
56 (eqtest (list '(%realpart) z) expr))))
57 (t
58 (eqtest (list '(%realpart) z) expr)))))
60 ;;; Imagpart gives the imaginary part of an expr.
62 (defmfun $imagpart (xx) (cdr (trisplit xx)))
64 (defprop $imagpart %imagpart verb)
65 (defprop %imagpart $imagpart noun)
66 (defprop %imagpart simp-imagpart operators)
68 (defun simp-imagpart (expr z simpflag)
69 (oneargcheck expr)
70 (setq z (simpcheck (cadr expr) simpflag))
71 (let ((sgn nil))
72 (cond ((mnump z) 0)
73 ((eq (setq sgn ($csign z)) '$imaginary)
74 (mul -1 '$%i z))
75 ((eq sgn '$complex)
76 (cond ((complex-number-p ($expand z) 'bigfloat-or-number-p)
77 ($imagpart z))
78 (t
79 (eqtest (list '(%imagpart) z) expr))))
80 (t
81 (eqtest (list '(%imagpart) z) expr)))))
83 ;;; Rectform gives a result of the form a+b*%i.
85 (defmfun ($rectform :properties ((evfun t))) (xx)
86 (let ((ris (trisplit xx)))
87 (add (car ris) (mul (cdr ris) '$%i))))
89 ;;; Polarform gives a result of the form a*%e^(%i*b).
91 (defmfun ($polarform :properties ((evfun t))) (xx)
92 (cond ((mbagp xx)
93 (cons (car xx) (mapcar #'$polarform (cdr xx))))
95 (let ((aas (absarg xx)) ($%emode nil))
96 (mul (car aas) (powers '$%e (mul '$%i (cdr aas))))))))
98 ;;; Cabs gives the complex absolute value. Nota bene: an expression may
99 ;;; be syntactically real without being real (e.g. sqrt(x), x<0). Thus
100 ;;; Cabs must lead an independent existence from Abs.
102 (defmfun $cabs (xx) (cabs xx))
104 (defprop $cabs %cabs verb)
105 (defprop %cabs $cabs noun)
106 (defprop %cabs simp-cabs operators)
108 (defun simp-cabs (expr z simpflag)
109 (oneargcheck expr)
110 (setq z (simpcheck (cadr expr) simpflag))
111 (let ((sgn nil))
112 (cond ((member (setq sgn ($csign z)) '($complex $imaginary))
113 (cond ((complex-number-p ($expand z) 'bigfloat-or-number-p)
114 (simplify (list '(mabs) z)))
116 (eqtest (list '(mabs) z) expr))))
117 ((eq sgn '$zero)
119 ((member sgn '($pos $pz))
121 ((eq sgn '$neg)
122 (mul -1 z))
124 (eqtest (list '(mabs) z) expr)))))
126 ;;; Carg gives the complex argument.
128 (defmfun $carg (xx)
129 (cond ((mbagp xx)
130 (cons (car xx) (mapcar #'$carg (cdr xx))))
131 (t (cdr (absarg xx)))))
133 (defprop $carg %carg verb)
134 (defprop %carg $carg noun)
135 (defprop %carg simp-carg operators)
137 (defun simp-carg (expr z simpflag)
138 (oneargcheck expr)
139 (setq z (simpcheck (cadr expr) simpflag))
140 (let ((sgn nil))
141 (cond ((eq z '$%i)
142 (div '$%pi 2))
143 ((member (setq sgn ($csign z)) '($complex $imaginary))
144 (cond ((complex-number-p ($expand z) 'bigfloat-or-number-p)
145 ($carg z))
147 (eqtest (list '(%carg) z) expr))))
148 ((member sgn '($pos $pz $zero))
150 ((eq sgn '$neg)
151 '$%pi)
153 (eqtest (list '(%carg) z) expr)))))
155 ;; The internal cabs, used by other Macsyma programs.
156 (defun cabs (xx) (car (absarg xx t)))
158 ;; Some objects can only appear at the top level of a legal simplified
159 ;; expression: CRE forms and equations in particular.
161 (defun trisplit (el) ; Top level of risplit
162 (cond ((atom el) (risplit el))
163 ((specrepp el) (trisplit (specdisrep el)))
164 ((eq (caar el) 'mequal) (dot-sp-ri (cdr el) '(mequal simp)))
165 (t (risplit el))))
167 ;;; Auxiliaries
169 ;; These are Macsyma equivalents to (mapcar 'trisplit ...). They must
170 ;; differ from other maps for two reasons: the lists are Macsyma lists,
171 ;; and therefore prefixed with list indicators; and the results must
172 ;; be separated: ((a . b) (c . d)) becomes something like ([a,c].[b,d]).
174 (defun dsrl (el) (dot-sp-ri (cdr el) '(mlist simp)))
176 (defun dot-sp-ri (el ind)
177 (dot--ri (mapcar #'trisplit el) ind))
179 ;; Dot--ri does the ((a.b)(c.d))->([a,c].[b,d]) transformation with
180 ;; minimal Cons'ing.
182 (defun dot--ri (el ind)
183 (do ((i el (cdr i)) (k))
184 ((null i) (cons (cons ind (nreverse k)) (cons ind el)))
185 (let ((cdari (cdar i)))
186 (setq k (rplacd (car i) k))
187 (rplaca i cdari))))
189 (defun risplit-mplus (l)
190 (do ((rpart) (ipart) (m (cdr l) (cdr m)))
191 ((null m) (cons (addn rpart t) (addn ipart t)))
192 (let ((sp (risplit (car m))))
193 (cond ((=0 (car sp)))
194 (t (setq rpart (cons (car sp) rpart))))
195 (cond ((=0 (cdr sp)))
196 (t (setq ipart (cons (cdr sp) ipart)))))))
198 (defun risplit-times (l)
199 (let ((risl (do ((purerl nil)
200 (compl nil)
201 (l (cdr l) (cdr l)))
202 ((null l) (cons purerl compl))
203 (let ((sp (risplit (car l))))
204 (cond ((=0 (cdr sp))
205 (setq purerl (rplacd sp purerl)))
206 ((or (atom (car sp)) (atom (cdr sp)))
207 (setq compl (cons sp compl)))
208 ((and (eq (caaar sp) 'mtimes)
209 ;;;Try risplit z/w and notice denominator. If this check were not made,
210 ;;; the real and imaginary parts would not each be over a common denominator.
211 (eq (caadr sp) 'mtimes)
212 (let ((nr (nreverse (cdar sp)))
213 (ni (nreverse (cddr sp))))
214 (cond ((equal (car nr) (car ni))
215 (push (car nr) purerl)
216 (push (cons (muln (nreverse (cdr nr)) t)
217 (muln (nreverse (cdr ni)) t))
218 compl))
220 (setq nr (nreverse nr))
221 (setq ni (nreverse ni))
222 nil)))))
224 (push sp compl)))))))
225 (cond ((null (cdr risl))
226 (cons (muln (car risl) t) 0))
228 (do ((rpart 1) (ipart 0) (m (cdr risl) (cdr m)))
229 ((null m)
230 (cons (muln (cons rpart (car risl)) t)
231 (muln (cons ipart (car risl)) t)))
232 (psetq rpart (sub (mul rpart (caar m)) (mul ipart (cdar m)))
233 ipart (add (mul ipart (caar m)) (mul rpart (cdar m)))))))))
235 ;; Split L = ((mexpt) BASE POW) into real and imaginary parts.
236 (defun risplit-expt (l)
237 (let* ((base (cadr l)) (pow (caddr l))
238 ;; Disable 'simplifications' like sqrt(-x) -> %i*sqrt(x)
239 ($radexpand nil)
240 (sp (risplit base)))
241 (cond
242 ((fixnump pow)
243 (risplit-expt-fixnum-pow sp pow))
245 ((and (ratnump pow)
246 (fixnump (cadr pow))
247 (not (< (cadr pow) (- $maxnegex)))
248 (not (> (cadr pow) $maxposex))
249 (or (= (caddr pow) 2) (=0 (cdr sp))))
250 (if (=0 (cdr sp))
251 (risplit-expt-real^rat base pow)
252 (risplit-expt-sqrt-pow base sp pow)))
254 ((and (floatp base) (floatp pow))
255 (risplit (let (($numer t)) (exptrl base pow))))
258 (destructuring-bind (alpha . beta) (risplit pow)
259 (destructuring-bind (r . theta) (absarg1 base)
260 (risplit-expt-general-form r theta alpha beta)))))))
262 ;; Split BASE^POWER into real and imaginary parts. POWER is assumed to be a
263 ;; fixnum. SP is (RISPLIT BASE)
264 (defun risplit-expt-fixnum-pow (sp power)
265 ;; We use the squared absolute value of BASE several times
266 ;; below. Unfortunately, we can't calculate it at the start, since that causes
267 ;; floating point under/overflows in the case mentioned in the comment
268 ;; below. Instead, we calculate it when it's needed (a maximum of once).
269 (destructuring-bind (real . imag) sp
270 (cond
271 ((= power -1)
272 ;; Handle the case of 1/(x+%i*y) carefully. This
273 ;; is needed if x and y are (Lisp) numbers to
274 ;; prevent spurious underflows/overflows. See bug 1908.
275 (if (and (or (numberp real) (ratnump real))
276 (or (numberp imag) (ratnump imag)))
277 (sprecip sp)
278 (let ((abs2 (spabs sp)))
279 (cons (div real abs2) (mul -1 (div imag abs2))))))
281 ((> (abs power) $maxposex)
282 (if (=0 imag)
283 (cons (powers real power) 0)
284 (let ((abs^n (powers (spabs sp) (*red power 2)))
285 (natan (mul power (genatan imag real))))
286 (cons (mul abs^n (take '(%cos) natan))
287 (mul abs^n (take '(%sin) natan))))))
289 ((> power 0)
290 (expanintexpt sp power))
293 (let ((abbas (powers (spabs sp) (- power)))
294 (basspli (expanintexpt sp (- power))))
295 (cons (div (car basspli) abbas)
296 (neg (div (cdr basspli) abbas))))))))
298 ;; Return the "general form" for RISPLIT applied to
299 ;; (r*exp(%i*theta))^(alpha+%i*beta), whose rectform is
301 ;; pre * cos(post) + %i * pre * sin(post)
303 ;; where pre = exp(-theta*beta) * r^alpha
304 ;; and post = beta*log(r) + alpha*theta
305 (defun risplit-expt-general-form (r theta alpha beta)
306 (let ((pre (mul (powers '$%e (mul -1 theta beta))
307 (powers r alpha)))
308 (post (add (mul beta (take '(%log) r))
309 (mul alpha theta))))
310 (cons (mul pre (take '(%cos) post))
311 (mul pre (take '(%sin) post)))))
313 ;; Split BASE^POWER into real and imaginary parts. We assume that BASE is real
314 ;; and that POWER is a rational number.
315 (defun risplit-expt-real^rat (base power)
316 (case (cond ((mnegp base) '$neg)
317 (implicit-real '$pos)
318 (t ($sign base))) ; Use $sign not asksign
319 ($neg (risplit (mul2 (power -1 power) (power (neg base) power))))
320 ($zero (cons (power 0 power) 0))
321 ($pos (cons (power base power) 0))
323 (destructuring-bind (r . theta) (absarg1 base)
324 (risplit-expt-general-form r theta power 0)))))
326 ;; Split BASE^POWER into real and imaginary parts. SP is (RISPLIT BASE). We
327 ;; assume that POWER is a rational number. Moreover, we assume that the
328 ;; denominator of POWER is 2.
329 (defun risplit-expt-sqrt-pow (base sp power)
330 ;; n = abs(2*power) is a non-negative integer
331 (destructuring-bind (real . imag) sp
332 (let* ((abs2 (spabs sp)) (abs (power abs2 1//2))
333 (n (abs (cadr power)))
334 (pos? (> (cadr power) -1))
335 (imag-sign ($sign imag)))
336 (cond
337 ((member imag-sign '($neg $pos))
338 ;; Here, we use the half-angle formulas for cos and sin. Assuming we
339 ;; are always taking the "principal square root" (that with argument
340 ;; less than equal to the argument of base), these come out as
342 ;; cos(arg/2) = +- sqrt((1+real/abs)/2)
343 ;; sin(arg/2) = +- sqrt((1-real/abs)/2)
345 ;; We know that real+%i*imag = abs*exp(%i*arg). Taking square roots,
346 ;; you get that
348 ;; sqrt(real+%i*imag) = sqrt(abs)*exp(%i*arg/2).
349 ;; = sqrt(abs)*cos(arg/2) +
350 ;; %i * sqrt(abs)*sin(arg/2)
351 ;; = (sqrt(abs+real) + %i*sqrt(abs-real))/sqrt(2)
353 ;; but possibly with signs on the square roots. This function always
354 ;; chooses the square root with the non-negative real part. As such, we
355 ;; have to switch the sign of the sine term when we are raising to a
356 ;; positive power and imag < 0 or if raising to a negative power and
357 ;; imag > 0. To see that the first argument of the PORM call below is
358 ;; correct, write out the 2x2 truth table...
359 (divcarcdr
360 (expanintexpt
361 (cons (power (add abs real) 1//2)
362 (porm (eq (eq imag-sign '$pos) pos?)
363 (power (sub abs real) 1//2)))
365 (if pos?
366 (power 2 (div n 2))
367 (power (mul 2 abs2) (div n 2)))))
370 (destructuring-bind (alpha . beta) (risplit power)
371 (destructuring-bind (r . theta) (absarg1 base)
372 (risplit-expt-general-form r theta alpha beta))))))))
374 (defun risplit-noun (l)
375 (cons (simplify (list '(%realpart) l)) (simplify (list '(%imagpart) l))))
378 (defun absarg1 (arg)
379 (let ((arg1 arg) ($keepfloat t))
380 (cond ((and (or (free arg '$%i)
381 (free (setq arg1 (sratsimp arg)) '$%i))
382 (not (eq (csign arg1) t)))
383 (setq arg arg1)
384 (if implicit-real
385 (cons arg 0)
386 (unwind-protect
387 (prog2 (assume `(($notequal) ,arg 0))
388 (absarg arg))
389 (forget `(($notequal) ,arg 0)))))
390 (t (absarg arg)))))
392 ;;; Main function
393 ;;; Takes an expression and returns the dotted pair
394 ;;; (<Real part> . <imaginary part>).
396 (defun risplit (l)
397 (let (($domain '$complex) ($m1pbranch t) $logarc op)
398 (cond ((atom l)
399 ;; Symbols are assumed to represent real values, unless they have
400 ;; been declared to be complex. If they have been declared to be both
401 ;; real and complex, they are taken to be real.
402 (cond ((eq l '$%i) (cons 0 1))
403 ((eq l '$infinity) (cons '$und '$und))
404 ((and (decl-complexp l) (not (decl-realp l))) (risplit-noun l))
405 (t (cons l 0))))
406 ((eq (caar l) 'rat) (cons l 0))
407 ((eq (caar l) 'mplus) (risplit-mplus l))
408 ((eq (caar l) 'mtimes) (risplit-times l))
409 ((eq (caar l) 'mexpt) (risplit-expt l))
410 ((eq (caar l) '%log)
411 (let ((aa (absarg1 (cadr l))))
412 (rplaca aa (take '(%log) (car aa)))))
413 ((eq (caar l) 'bigfloat) (cons l 0)) ;All numbers are real.
414 ((and (member (caar l) '(%integrate %derivative %laplace %sum) :test #'eq)
415 (freel (cddr l) '$%i))
416 (let ((ris (risplit (cadr l))))
417 (cons (simplify (list* (ncons (caar l)) (car ris) (cddr l)))
418 (simplify (list* (ncons (caar l)) (cdr ris) (cddr l))))))
419 ((eq (caar l) '$conjugate)
420 (cons (simplify (list '(%realpart) (cadr l)))
421 (mul -1 (simplify (list '(%imagpart) (cadr l))))))
422 ((let ((ass (assoc (caar l)
423 '((%sin %cosh %cos . %sinh)
424 (%cos %cosh %sin . %sinh)
425 (%sinh %cos %cosh . %sin)
426 (%cosh %cos %sinh . %sin)) :test #'eq)))
427 ;;;This clause handles the very similar trigonometric and hyperbolic functions.
428 ;;; It is driven by the table at the end of the lambda.
429 (and ass
430 (let ((ri (risplit (cadr l))))
431 (cond ((=0 (cdr ri)) ;Pure real case.
432 (cons (take (list (car ass)) (car ri)) 0))
434 (cons (mul (take (list (car ass)) (car ri))
435 (take (list (cadr ass)) (cdr ri)))
436 (negate-if (eq (caar l) '%cos)
437 (mul (take (list (caddr ass)) (car ri))
438 (take (list (cdddr ass)) (cdr ri)))))))))))
439 ((member (caar l) '(%tan %tanh) :test #'eq)
440 (let ((sp (risplit (cadr l))))
441 ;;;The similar tan and tanh cases.
442 (cond ((=0 (cdr sp))
443 (cons l 0))
445 (let* ((2rl (mul (car sp) 2))
446 (2im (mul (cdr sp) 2))
447 (denom (inv (if (eq (caar l) '%tan)
448 (add (take '(%cosh) 2im) (take '(%cos) 2rl))
449 (add (take '(%cos) 2im) (take '(%cosh) 2rl))))))
450 (if (eq (caar l) '%tan)
451 (cons (mul (take '(%sin) 2rl) denom)
452 (mul (take '(%sinh) 2im) denom))
453 (cons (mul (take '(%sinh) 2rl) denom)
454 (mul (take '(%sin) 2im) denom))))))))
455 ((and (member (caar l) '(%atan %csc %sec %cot %csch %sech %coth) :test #'eq)
456 (=0 (cdr (risplit (cadr l)))))
457 (cons l 0))
458 ((and (eq (caar l) '$atan2)
459 (not (zerop1 (caddr l)))
460 (=0 (cdr (risplit (div (cadr l) (caddr l))))))
461 ;; Case atan2(y,x) and y/x a real expression.
462 (cons l 0))
463 ((or (arcp (caar l)) (eq (caar l) '$atan2))
464 (let ((ans (risplit (logarc (caar l) (cadr l)))))
465 (when (eq (caar l) '$atan2)
466 (setq ans (cons (sratsimp (car ans)) (sratsimp (cdr ans)))))
467 (if (and (free l '$%i) (=0 (cdr ans)))
468 (cons l 0)
469 ans)))
470 ((eq (caar l) '%plog)
471 ;; (princ '|Warning: Principal value not guaranteed for Plog in Rectform/|)
472 (risplit (cons '(%log) (cdr l))))
473 ;; Look for a risplit-function on the property list to handle the
474 ;; realpart and imagpart for this function.
475 ((setq op (safe-get (mop l) 'risplit-function))
476 (funcall op l))
477 ;;; ^ All the above are guaranteed pure real.
478 ;;; The handling of lists and matrices below has to be thought through.
479 ((eq (caar l) 'mlist) (dsrl l))
480 ((eq (caar l) '$matrix)
481 (dot--ri (mapcar #'dsrl (cdr l)) '($matrix simp)))
482 ;;;The Coversinemyfoot clause covers functions which can be converted
483 ;;; to functions known by risplit, such as the more useless trigonometrics.
484 ((let ((foot (coversinemyfoot l)))
485 (and foot (risplit foot))))
486 ((or (safe-get (mop l) 'real-valued)
487 (decl-realp (mop l)))
488 ;; Simplification for a real-valued function
489 (cons l 0))
490 ((or (safe-get (mop l) 'commutes-with-conjugate)
491 (safe-get (mop l) 'conjugate-function))
492 ;; A function with Mirror symmetry. The general expressions for
493 ;; the realpart and imagpart simplifies accordingly.
494 (cons (mul (div 1 2)
495 (add (simplify (list '($conjugate) l)) l))
496 (mul (div 1 2) '$%i
497 (sub (simplify (list '($conjugate) l)) l))))
498 ;;; A MAJOR ASSUMPTION:
499 ;;; All random functions are pure real, regardless of argument.
500 ;;; This is evidently assumed by some of the integration functions.
501 ;;; Perhaps the best compromise is to return 'realpart/'imagpart
502 ;;; under the control of a switch set by the integrators. First
503 ;;; all such dependencies must be found in the integ
504 ((and rp-polylogp (mqapplyp l) (eq (subfunname l) '$li)) (cons l 0))
505 ((prog2 (setq op (if (eq (caar l) 'mqapply) (caaadr l) (caar l)))
506 (decl-complexp op))
507 (risplit-noun l))
508 ((and (eq (caar l) '%product) (not (free (cadr l) '$%i)))
509 (risplit-noun l))
510 (($subvarp l)
511 ;; return a real answer for subscripted variable
512 (cons l 0))
514 (cons (list '(%realpart simp) l)
515 (list '(%imagpart simp) l))))))
517 (defun coversinemyfoot (l)
518 (prog (recip)
519 (cond ((not (member (caar l) '(%csc %sec %cot %csch %sech %coth) :test #'eq)))
520 ((null (setq recip (get (caar l) 'recip))))
521 (t (return (div 1 (cons (list recip) (cdr l))))))))
523 (defun powers (c d)
524 (cond ((=1 d) c)
525 ((equal d 0) 1) ;equal to preclude 0^(pdl 0)->0:
526 ((=0 c) 0) ; see comment before =0.
527 ((=1 c) 1)
528 (t (power c d))))
530 (defun spabs (sp)
531 ;; SP is a cons of the real part and imaginary part of a complex
532 ;; number. SPABS computes the sum of squares of the real and
533 ;; imaginary parts.
534 (add (powers (car sp) 2)
535 (powers (cdr sp) 2)))
537 ;; Compute 1/(x+%i*y) when both x and y are Lisp numbers or Maxima
538 ;; rationals. Return a cons of the real and imaginary part of the
539 ;; result. We count on the underlying Lisp to be able to compute (/
540 ;; (complex x y)) accurately and without unnecessary overflow or
541 ;; underflow.. If not, complain to your Lisp vendor. Well, it seems
542 ;; that Clisp, CMUCL, and SBCL do a nice job. But others apparently
543 ;; do not. (I tested ecl 9.12.3 and ccl 1.4, which both fail.)
544 ;; Workaround those deficiencies.
545 (defun sprecip (sp)
546 (destructuring-bind (x . y)
548 #+(or cmu sbcl)
549 (let* ((x (bigfloat:to x))
550 (y (bigfloat:to y))
551 (q (bigfloat:/ (bigfloat:complex x y))))
552 (cons (to (bigfloat:realpart q))
553 (to (bigfloat:imagpart q))))
554 #-(or cmu sbcl)
555 (let ((x (bigfloat:to x))
556 (y (bigfloat:to y)))
557 ;; 1/(x+%i*y).
559 ;; Assume abs(x) > abs(y). Let r = y/x. Then
560 ;; 1/(x+%i*y) = 1/x/(1+%i*r)
561 ;; = (1-%i*r)/(x*(1+r*r))
563 ;; The case for abs(x) <= abs(y) is similar with r = x/y:
564 ;; 1/(x+%i*y) = 1/y/(r+%i)
565 ;; = (r-%i)/(y*(1+r^2))
566 (if (> (bigfloat:abs x) (bigfloat:abs y))
567 (let* ((r (bigfloat:/ y x))
568 (dn (bigfloat:* x (bigfloat:+ 1 (bigfloat:* r r)))))
569 (cons (to (bigfloat:/ dn))
570 (to (bigfloat:/ (bigfloat:- r) dn))))
571 (let* ((r (bigfloat:/ x y))
572 (dn (bigfloat:* y (bigfloat:+ 1 (bigfloat:* r r)))))
573 (cons (to (bigfloat:/ r dn))
574 (to (bigfloat:/ (bigfloat:- dn)))))))))
579 (defvar negp* (let ((l (list nil nil t t))) (nconc l l)))
581 (defun divcarcdr (a b)
582 (cons (div (car a) b) (div (cdr a) b)))
585 ;;Expand bas^n, where bas is (<real part> . <imaginary part>)
587 (defun expanintexpt (bas n)
588 (cond ((= n 1) bas)
589 (t (do ((rp (car bas))
590 (ip (cdr bas))
591 (c 1 (quotient (* c ex) i))
592 (ex n (1- ex)) (i 1 (1+ i))
593 (rori t (not rori)) (negp negp* (cdr negp))
594 (rpt nil) (ipt nil))
595 ((< ex 0) (cons (addn rpt t) (addn ipt t)))
596 (declare (fixnum ex i))
597 (set-either rpt ipt
598 rori
599 (cons (negate-if (car negp)
600 (mul c
601 (powers rp ex)
602 (powers ip (1- i))))
603 (cond (rori rpt) (t ipt))))))))
607 ;;; Subtract out multiples of 2*%pi with a minimum of consing.
608 ;;; Attempts to reduce to interval (-pi,pi].
610 (defun 2pistrip (exp)
611 (cond ((atom exp) exp)
612 ((eq (caar exp) 'mtimes)
613 (cond ((and (mnump (cadr exp))
614 (eq (caddr exp) '$%pi)
615 (null (cdddr exp)))
616 (cond ((integerp (cadr exp)) ; 5*%pi
617 (mul (mod (cadr exp) 2) '$%pi))
618 ((floatp (cadr exp)) ; 1.5*%pi
619 (mul (1- (mod (1+ (cadr exp)) 2))
620 '$%pi))
621 ;; Neither 0 nor 1 appears as a coef
622 ((and (listp (cadr exp))
623 (eq 'rat (caaadr exp))) ;5/2*%pi
624 (mul (list* '(rat simp)
625 (- (mod (+ (cadadr exp) (car (cddadr exp)))
626 (* 2 (car (cddadr exp))))
627 (car (cddadr exp)))
628 (cddadr exp))
629 '$%pi))
630 (t exp)))
631 (t exp)))
632 ((eq (caar exp) 'mplus)
633 (let ((res (2pirec (cdr exp))))
634 (if (eq res (cdr exp))
636 (addn res t))))
637 (t exp)))
639 (defun 2pirec (fm) ;Takes a list of exprs
640 (cond ((null (cdr fm)) ;If monad, just return.
641 (let ((2pf (2pistrip (car fm))))
642 (cond ((eq 2pf (car fm)) fm)
643 ((=0 2pf) nil)
644 (t (list 2pf)))))
646 (let ((2pfma (2pistrip (car fm)))
647 (2pfmd (2pirec (cdr fm))))
648 (cond ((or (null 2pfmd) (=0 2pfmd)) 2pfma)
649 ((and (eq 2pfmd (cdr fm)) (eq 2pfma (car fm))) fm)
650 (t (cons 2pfma 2pfmd)))))))
652 ;;; Rectify into polar form; Arguments similar to risplit
654 (defun argnum (n)
655 (if (minusp n)
656 (simplify '$%pi)
660 ;; absarg
661 ;; returns pair (abs . arg)
662 ;; if absflag is true, arg result is not guaranteed to be correct
664 ;; The function of Absflag is to communicate that only the absolute
665 ;; value part of the result is wanted. This allows Absarg to avoid asking
666 ;; questions irrelevant to the absolute value. For instance, Cabs(x) is
667 ;; invariably Abs(x), while the complex phase may be 0 or %pi. Note also
668 ;; the steps taken in Absarg to assure that Asksign's will happen before Sign's
669 ;; as often as possible, so that, for instance, Abs(x) can be simplified to
670 ;; x or -x if the sign of x must be known for some other reason. These
671 ;; techniques, however, are not perfect.
673 (defmacro half () ''((rat simp) 1 2)) ;1/2
675 (defun absarg (l &optional (absflag nil))
676 ;; Commenting out the the expansion of the expression l. It seems to be not
677 ;; necessary, but can cause expression swelling (DK 01/2010).
678 ; (setq l ($expand l))
679 (cond ((atom l)
680 (cond ((eq l '$%i)
681 (cons 1 (simplify '((mtimes) ((rat simp) 1 2) $%pi))))
682 ((numberp l)
683 (cons (abs l) (argnum l)))
684 ((member l '($%e $%pi) :test #'eq) (cons l 0))
685 ((eq l '$infinity) (cons '$inf '$ind))
686 ((decl-complexp l)
687 (cons (list '(mabs simp) l) ; noun form with mabs
688 (list '(%carg simp) l)))
689 (absflag (cons (take '(mabs) l) 0))
691 ;; At this point l is representing a real value. Try to
692 ;; determine the sign and return a general form when the sign is
693 ;; unknown.
694 (let ((gs (if (eq rischp l) '$pos ($sign l))))
695 (cond ((member gs '($pos $pz)) (cons l 0))
696 ((eq gs '$zero) (cons 0 0))
697 ((eq gs '$neg)
698 (cons (neg l) (simplify '$%pi)))
699 (t (cons (take '(mabs) l) (genatan 0 l))))))))
700 ((eq '$zero (let ((sign-imag-errp nil)) (catch 'sign-imag-err ($sign l))))
701 (cond ((some-bfloatp l)
702 (cons bigfloatzero bigfloatzero)) ; contagious
703 ((some-floatp l)
704 (cons 0.0 0.0))
705 (t (cons 0 0))))
706 ((member (caar l) '(rat bigfloat) :test #'eq)
707 (cons (list (car l) (abs (cadr l)) (caddr l))
708 (argnum (cadr l))))
709 ((eq (caar l) 'mtimes)
710 (do ((n (cdr l) (cdr n))
711 (abars)
712 (argl () (cons (cdr abars) argl))
713 (absl () (rplacd abars absl)))
714 (())
715 (unless n
716 (return (cons (muln absl t) (2pistrip (addn argl t)))))
717 (setq abars (absarg (car n) absflag))))
718 ((eq (caar l) 'mexpt)
719 ;; An expression z^a
720 (let ((aa (absarg (cadr l) nil)) ; (abs(z) . arg(z))
721 (sp (risplit (caddr l))) ; (realpart(a) . imagpart(a))
722 ($radexpand nil))
723 (cond ((and (zerop1 (cdr sp))
724 (eq ($sign (sub 1 (take '(mabs) (car sp)))) '$pos))
725 ;; Special case: a is real and abs(a) < 1.
726 ;; This simplifies e.g. carg(sqrt(z)) -> carg(z)/2
727 (cons (mul (power (car aa) (car sp))
728 (power '$%e (neg (mul (cdr aa) (cdr sp)))))
729 (mul (caddr l) (cdr aa))))
731 ;; General case for z and a
732 (let ((arg (add (mul (cdr sp) (take '(%log) (car aa)))
733 (mul (cdr aa) (car sp)))))
734 (cons (mul (power (car aa) (car sp))
735 (power '$%e (neg (mul (cdr aa) (cdr sp)))))
736 (if generate-atan2
737 (take '($atan2)
738 (take '(%sin) arg)
739 (take '(%cos) arg))
740 (take '(%atan) (take '(%tan) arg)))))))))
741 ((and (member (caar l) '(%tan %tanh) :test #'eq)
742 (not (=0 (cdr (risplit (cadr l))))))
743 (let* ((sp (risplit (cadr l)))
744 (2frst (mul (cdr sp) 2))
745 (2scnd (mul (car sp) 2)))
746 (when (eq (caar l) '%tanh)
747 (psetq 2frst 2scnd 2scnd 2frst))
748 (cons (let ((cosh (take '(%cosh) 2frst))
749 (cos (take '(%cos) 2scnd)))
750 (root (div (add cosh (neg cos))
751 (add cosh cos))
753 (take '(%atan)
754 (if (eq (caar l) '%tan)
755 (div (take '(%sinh) 2frst) (take '(%sin) 2scnd))
756 (div (take '(%sin) 2frst) (take '(%sinh) 2scnd)))))))
757 ((specrepp l) (absarg (specdisrep l) absflag))
758 ((let ((foot (coversinemyfoot l)))
759 (and foot (not (=0 (cdr (risplit (cadr l))))) (absarg foot absflag))))
761 (let ((ris (trisplit l)))
762 (xcons
763 ;;; Arguments must be in this order so that the side-effect of the Atan2,
764 ;;; that is, determining the Asksign of the argument, can happen before
765 ;;; Take Mabs does its Sign. Blame JPG for noticing this lossage.
766 (if absflag 0 (genatan (cdr ris) (car ris)))
767 (cond ((equal (car ris) 0) (absarg-mabs (cdr ris)))
768 ((equal (cdr ris) 0) (absarg-mabs (car ris)))
769 (t (powers ($expand (add (powers (car ris) 2)
770 (powers (cdr ris) 2))
771 1 0)
772 (half)))))))))
774 (defun genatan (num den)
775 (let ((arg (take '($atan2) num den)))
776 (if (or generate-atan2
777 (zerop1 den)
778 (free arg '$atan2))
780 (take '(%atan) (div num den)))))
782 (defun absarg-mabs (l)
783 (cond ((eq (csign l) t)
784 (if (member (caar l) '(mabs %cabs) :test #'eq)
786 (list '(mabs simp) l))) ; mabs and not %cabs as noun form
787 ((member ($csign l) '($complex $imaginary))
788 ;; Do not try to simplify a complex expression at this point,
789 ;; this would cause an endless loop. Return a noun form.
790 (list '(mabs simp) l))
792 (take '(mabs) l))))