Rename *ll* and *ul* to ll and ul in limit-subs
[maxima.git] / src / rpart.lisp
blob251ae5ffa348b7907ac957ac9e315a7e3f003b8e
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)
465 ;; atan2 has 2 args, unlike all
466 ;; the other inverse trig
467 ;; functions.
468 (if (eq (caar l) '%atan2)
469 (rest l)
470 (cadr l))))))
471 (when (eq (caar l) '%atan2)
472 (setq ans (cons (sratsimp (car ans)) (sratsimp (cdr ans)))))
473 (if (and (free l '$%i) (=0 (cdr ans)))
474 (cons l 0)
475 ans)))
476 ((eq (caar l) '%plog)
477 ;; (princ '|Warning: Principal value not guaranteed for Plog in Rectform/|)
478 (risplit (cons '(%log) (cdr l))))
479 ;; Look for a risplit-function on the property list to handle the
480 ;; realpart and imagpart for this function.
481 ((setq op (safe-get (mop l) 'risplit-function))
482 (funcall op l))
483 ;;; ^ All the above are guaranteed pure real.
484 ;;; The handling of lists and matrices below has to be thought through.
485 ((eq (caar l) 'mlist) (dsrl l))
486 ((eq (caar l) '$matrix)
487 (dot--ri (mapcar #'dsrl (cdr l)) '($matrix simp)))
488 ;;;The Coversinemyfoot clause covers functions which can be converted
489 ;;; to functions known by risplit, such as the more useless trigonometrics.
490 ((let ((foot (coversinemyfoot l)))
491 (and foot (risplit foot))))
492 ((or (safe-get (mop l) 'real-valued)
493 (decl-realp (mop l)))
494 ;; Simplification for a real-valued function
495 (cons l 0))
496 ((or (safe-get (mop l) 'commutes-with-conjugate)
497 (safe-get (mop l) 'conjugate-function))
498 ;; A function with Mirror symmetry. The general expressions for
499 ;; the realpart and imagpart simplifies accordingly.
500 (cons (mul (div 1 2)
501 (add (simplify (list '($conjugate) l)) l))
502 (mul (div 1 2) '$%i
503 (sub (simplify (list '($conjugate) l)) l))))
504 ;;; A MAJOR ASSUMPTION:
505 ;;; All random functions are pure real, regardless of argument.
506 ;;; This is evidently assumed by some of the integration functions.
507 ;;; Perhaps the best compromise is to return 'realpart/'imagpart
508 ;;; under the control of a switch set by the integrators. First
509 ;;; all such dependencies must be found in the integ
510 ((and rp-polylogp (mqapplyp l) (eq (subfunname l) '$li)) (cons l 0))
511 ((prog2 (setq op (if (eq (caar l) 'mqapply) (caaadr l) (caar l)))
512 (decl-complexp op))
513 (risplit-noun l))
514 ((and (eq (caar l) '%product) (not (free (cadr l) '$%i)))
515 (risplit-noun l))
516 (($subvarp l)
517 ;; return a real answer for subscripted variable
518 (cons l 0))
520 (cons (list '(%realpart simp) l)
521 (list '(%imagpart simp) l))))))
523 (defun coversinemyfoot (l)
524 (prog (recip)
525 (cond ((not (member (caar l) '(%csc %sec %cot %csch %sech %coth) :test #'eq)))
526 ((null (setq recip (get (caar l) 'recip))))
527 (t (return (div 1 (cons (list recip) (cdr l))))))))
529 (defun powers (c d)
530 (cond ((=1 d) c)
531 ((equal d 0) 1) ;equal to preclude 0^(pdl 0)->0:
532 ((=0 c) 0) ; see comment before =0.
533 ((=1 c) 1)
534 (t (power c d))))
536 (defun spabs (sp)
537 ;; SP is a cons of the real part and imaginary part of a complex
538 ;; number. SPABS computes the sum of squares of the real and
539 ;; imaginary parts.
540 (add (powers (car sp) 2)
541 (powers (cdr sp) 2)))
543 ;; Compute 1/(x+%i*y) when both x and y are Lisp numbers or Maxima
544 ;; rationals. Return a cons of the real and imaginary part of the
545 ;; result. We count on the underlying Lisp to be able to compute (/
546 ;; (complex x y)) accurately and without unnecessary overflow or
547 ;; underflow.. If not, complain to your Lisp vendor. Well, it seems
548 ;; that Clisp, CMUCL, and SBCL do a nice job. But others apparently
549 ;; do not. (I tested ecl 9.12.3 and ccl 1.4, which both fail.)
550 ;; Workaround those deficiencies.
551 (defun sprecip (sp)
552 (destructuring-bind (x . y)
554 #+(or cmu sbcl)
555 (let* ((x (bigfloat:to x))
556 (y (bigfloat:to y))
557 (q (bigfloat:/ (bigfloat:complex x y))))
558 (cons (to (bigfloat:realpart q))
559 (to (bigfloat:imagpart q))))
560 #-(or cmu sbcl)
561 (let ((x (bigfloat:to x))
562 (y (bigfloat:to y)))
563 ;; 1/(x+%i*y).
565 ;; Assume abs(x) > abs(y). Let r = y/x. Then
566 ;; 1/(x+%i*y) = 1/x/(1+%i*r)
567 ;; = (1-%i*r)/(x*(1+r*r))
569 ;; The case for abs(x) <= abs(y) is similar with r = x/y:
570 ;; 1/(x+%i*y) = 1/y/(r+%i)
571 ;; = (r-%i)/(y*(1+r^2))
572 (if (> (bigfloat:abs x) (bigfloat:abs y))
573 (let* ((r (bigfloat:/ y x))
574 (dn (bigfloat:* x (bigfloat:+ 1 (bigfloat:* r r)))))
575 (cons (to (bigfloat:/ dn))
576 (to (bigfloat:/ (bigfloat:- r) dn))))
577 (let* ((r (bigfloat:/ x y))
578 (dn (bigfloat:* y (bigfloat:+ 1 (bigfloat:* r r)))))
579 (cons (to (bigfloat:/ r dn))
580 (to (bigfloat:/ (bigfloat:- dn)))))))))
585 (defvar negp* (let ((l (list nil nil t t))) (nconc l l)))
587 (defun divcarcdr (a b)
588 (cons (div (car a) b) (div (cdr a) b)))
591 ;;Expand bas^n, where bas is (<real part> . <imaginary part>)
593 (defun expanintexpt (bas n)
594 (cond ((= n 1) bas)
595 (t (do ((rp (car bas))
596 (ip (cdr bas))
597 (c 1 (quotient (* c ex) i))
598 (ex n (1- ex)) (i 1 (1+ i))
599 (rori t (not rori)) (negp negp* (cdr negp))
600 (rpt nil) (ipt nil))
601 ((< ex 0) (cons (addn rpt t) (addn ipt t)))
602 (declare (fixnum ex i))
603 (set-either rpt ipt
604 rori
605 (cons (negate-if (car negp)
606 (mul c
607 (powers rp ex)
608 (powers ip (1- i))))
609 (cond (rori rpt) (t ipt))))))))
613 ;;; Subtract out multiples of 2*%pi with a minimum of consing.
614 ;;; Attempts to reduce to interval (-pi,pi].
616 (defun 2pistrip (exp)
617 (cond ((atom exp) exp)
618 ((eq (caar exp) 'mtimes)
619 (cond ((and (mnump (cadr exp))
620 (eq (caddr exp) '$%pi)
621 (null (cdddr exp)))
622 (cond ((integerp (cadr exp)) ; 5*%pi
623 (mul (mod (cadr exp) 2) '$%pi))
624 ((floatp (cadr exp)) ; 1.5*%pi
625 (mul (1- (mod (1+ (cadr exp)) 2))
626 '$%pi))
627 ;; Neither 0 nor 1 appears as a coef
628 ((and (listp (cadr exp))
629 (eq 'rat (caaadr exp))) ;5/2*%pi
630 (mul (list* '(rat simp)
631 (- (mod (+ (cadadr exp) (car (cddadr exp)))
632 (* 2 (car (cddadr exp))))
633 (car (cddadr exp)))
634 (cddadr exp))
635 '$%pi))
636 (t exp)))
637 (t exp)))
638 ((eq (caar exp) 'mplus)
639 (let ((res (2pirec (cdr exp))))
640 (if (eq res (cdr exp))
642 (addn res t))))
643 (t exp)))
645 (defun 2pirec (fm) ;Takes a list of exprs
646 (cond ((null (cdr fm)) ;If monad, just return.
647 (let ((2pf (2pistrip (car fm))))
648 (cond ((eq 2pf (car fm)) fm)
649 ((=0 2pf) nil)
650 (t (list 2pf)))))
652 (let ((2pfma (2pistrip (car fm)))
653 (2pfmd (2pirec (cdr fm))))
654 (cond ((or (null 2pfmd) (=0 2pfmd)) 2pfma)
655 ((and (eq 2pfmd (cdr fm)) (eq 2pfma (car fm))) fm)
656 (t (cons 2pfma 2pfmd)))))))
658 ;;; Rectify into polar form; Arguments similar to risplit
660 (defun argnum (n)
661 (if (minusp n)
662 (simplify '$%pi)
666 ;; absarg
667 ;; returns pair (abs . arg)
668 ;; if absflag is true, arg result is not guaranteed to be correct
670 ;; The function of Absflag is to communicate that only the absolute
671 ;; value part of the result is wanted. This allows Absarg to avoid asking
672 ;; questions irrelevant to the absolute value. For instance, Cabs(x) is
673 ;; invariably Abs(x), while the complex phase may be 0 or %pi. Note also
674 ;; the steps taken in Absarg to assure that Asksign's will happen before Sign's
675 ;; as often as possible, so that, for instance, Abs(x) can be simplified to
676 ;; x or -x if the sign of x must be known for some other reason. These
677 ;; techniques, however, are not perfect.
679 (defmacro half () ''((rat simp) 1 2)) ;1/2
681 (defun absarg (l &optional (absflag nil))
682 ;; Commenting out the the expansion of the expression l. It seems to be not
683 ;; necessary, but can cause expression swelling (DK 01/2010).
684 ; (setq l ($expand l))
685 (cond ((atom l)
686 (cond ((eq l '$%i)
687 (cons 1 (simplify '((mtimes) ((rat simp) 1 2) $%pi))))
688 ((numberp l)
689 (cons (abs l) (argnum l)))
690 ((member l '($%e $%pi) :test #'eq) (cons l 0))
691 ((eq l '$infinity) (cons '$inf '$ind))
692 ((decl-complexp l)
693 (cons (list '(mabs simp) l) ; noun form with mabs
694 (list '(%carg simp) l)))
695 (absflag (cons (take '(mabs) l) 0))
697 ;; At this point l is representing a real value. Try to
698 ;; determine the sign and return a general form when the sign is
699 ;; unknown.
700 (let ((gs (if (eq rischp l) '$pos ($sign l))))
701 (cond ((member gs '($pos $pz)) (cons l 0))
702 ((eq gs '$zero) (cons 0 0))
703 ((eq gs '$neg)
704 (cons (neg l) (simplify '$%pi)))
705 (t (cons (take '(mabs) l) (genatan 0 l))))))))
706 ((eq '$zero (let ((sign-imag-errp nil)) (catch 'sign-imag-err ($sign l))))
707 (cond ((some-bfloatp l)
708 (cons bigfloatzero bigfloatzero)) ; contagious
709 ((some-floatp l)
710 (cons 0.0 0.0))
711 (t (cons 0 0))))
712 ((member (caar l) '(rat bigfloat) :test #'eq)
713 (cons (list (car l) (abs (cadr l)) (caddr l))
714 (argnum (cadr l))))
715 ((eq (caar l) 'mtimes)
716 (do ((n (cdr l) (cdr n))
717 (abars)
718 (argl () (cons (cdr abars) argl))
719 (absl () (rplacd abars absl)))
720 (())
721 (unless n
722 (return (cons (muln absl t) (2pistrip (addn argl t)))))
723 (setq abars (absarg (car n) absflag))))
724 ((eq (caar l) 'mexpt)
725 ;; An expression z^a
726 (let ((aa (absarg (cadr l) nil)) ; (abs(z) . arg(z))
727 (sp (risplit (caddr l))) ; (realpart(a) . imagpart(a))
728 ($radexpand nil))
729 (cond ((and (zerop1 (cdr sp))
730 (eq ($sign (sub 1 (take '(mabs) (car sp)))) '$pos))
731 ;; Special case: a is real and abs(a) < 1.
732 ;; This simplifies e.g. carg(sqrt(z)) -> carg(z)/2
733 (cons (mul (power (car aa) (car sp))
734 (power '$%e (neg (mul (cdr aa) (cdr sp)))))
735 (mul (caddr l) (cdr aa))))
737 ;; General case for z and a
738 (let ((arg (add (mul (cdr sp) (take '(%log) (car aa)))
739 (mul (cdr aa) (car sp)))))
740 (cons (mul (power (car aa) (car sp))
741 (power '$%e (neg (mul (cdr aa) (cdr sp)))))
742 (if generate-atan2
743 (take '(%atan2)
744 (take '(%sin) arg)
745 (take '(%cos) arg))
746 (take '(%atan) (take '(%tan) arg)))))))))
747 ((and (member (caar l) '(%tan %tanh) :test #'eq)
748 (not (=0 (cdr (risplit (cadr l))))))
749 (let* ((sp (risplit (cadr l)))
750 (2frst (mul (cdr sp) 2))
751 (2scnd (mul (car sp) 2)))
752 (when (eq (caar l) '%tanh)
753 (psetq 2frst 2scnd 2scnd 2frst))
754 (cons (let ((cosh (take '(%cosh) 2frst))
755 (cos (take '(%cos) 2scnd)))
756 (root (div (add cosh (neg cos))
757 (add cosh cos))
759 (take '(%atan)
760 (if (eq (caar l) '%tan)
761 (div (take '(%sinh) 2frst) (take '(%sin) 2scnd))
762 (div (take '(%sin) 2frst) (take '(%sinh) 2scnd)))))))
763 ((specrepp l) (absarg (specdisrep l) absflag))
764 ((let ((foot (coversinemyfoot l)))
765 (and foot (not (=0 (cdr (risplit (cadr l))))) (absarg foot absflag))))
767 (let ((ris (trisplit l)))
768 (xcons
769 ;;; Arguments must be in this order so that the side-effect of the Atan2,
770 ;;; that is, determining the Asksign of the argument, can happen before
771 ;;; Take Mabs does its Sign. Blame JPG for noticing this lossage.
772 (if absflag 0 (genatan (cdr ris) (car ris)))
773 (cond ((equal (car ris) 0) (absarg-mabs (cdr ris)))
774 ((equal (cdr ris) 0) (absarg-mabs (car ris)))
775 (t (powers ($expand (add (powers (car ris) 2)
776 (powers (cdr ris) 2))
777 1 0)
778 (half)))))))))
780 (defun genatan (num den)
781 (let ((arg (take '(%atan2) num den)))
782 (if (or generate-atan2
783 (zerop1 den)
784 (free arg '%atan2))
786 (take '(%atan) (div num den)))))
788 (defun absarg-mabs (l)
789 (cond ((eq (csign l) t)
790 (if (member (caar l) '(mabs %cabs) :test #'eq)
792 (list '(mabs simp) l))) ; mabs and not %cabs as noun form
793 ((member ($csign l) '($complex $imaginary))
794 ;; Do not try to simplify a complex expression at this point,
795 ;; this would cause an endless loop. Return a noun form.
796 (list '(mabs simp) l))
798 (take '(mabs) l))))