Removed show, updated makebox in Itensor docs. Fixes #3890.
[maxima.git] / src / sinint.lisp
blob6452ddf52022bed2c1c1e818fc24b6ed4410f98b
1 ;;; -*- Mode: Lisp; Package: Maxima; Syntax: Common-Lisp; Base: 10 -*- ;;;;
2 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 ;;; The data in this file contains enhancments. ;;;;;
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 sinint)
15 (load-macsyma-macros ratmac)
17 (declare-top (special genvar checkfactors
18 exp var $factorflag $logabs $expop $expon
19 $keepfloat ratform rootfactor pardenom $algebraic
20 wholepart parnumer varlist logptdx switch1))
22 (defun rootfac (q)
23 (prog (nthdq nthdq1 simproots ans n-loops)
24 (setq nthdq (pgcd q (pderivative q var)))
25 (setq simproots (pquotient q nthdq))
26 (setq ans (list (pquotient simproots (pgcd nthdq simproots))))
27 (setq n-loops 0)
28 amen (cond
29 ((= n-loops $factor_max_degree)
30 (return (list q)))
31 ((or (pcoefp nthdq) (pointergp var (car nthdq)))
32 (return (reverse ans))))
33 (setq nthdq1 (pgcd (pderivative nthdq var) nthdq))
34 (push (pquotient (pgcd nthdq simproots) (pgcd nthdq1 simproots)) ans)
35 (setq nthdq nthdq1)
36 (incf n-loops)
37 (go amen)))
39 (defun aprog (q)
40 (setq q (oldcontent q))
41 (setq rootfactor (rootfac (cadr q)))
42 (setq rootfactor
43 (cons (ptimes (car q) (car rootfactor)) (cdr rootfactor)))
44 (do ((pd (list (car rootfactor)))
45 (rf (cdr rootfactor) (cdr rf))
46 (n 2 (1+ n)))
47 ((null rf) (setq pardenom (reverse pd)))
48 (push (pexpt (car rf) n) pd))
49 rootfactor)
51 (defun cprog (top bottom)
52 (prog (frpart pardenomc ppdenom thebpg)
53 (setq frpart (pdivide top bottom))
54 (setq wholepart (car frpart))
55 (setq frpart (cadr frpart))
56 (if (= (length pardenom) 1)
57 (return (setq parnumer (list frpart))))
58 (setq pardenomc (cdr pardenom))
59 (setq ppdenom (list (car pardenom)))
60 dseq (if (= (length pardenomc) 1) (go ok))
61 (setq ppdenom (cons (ptimes (car ppdenom) (car pardenomc)) ppdenom))
62 (setq pardenomc (cdr pardenomc))
63 (go dseq)
64 ok (setq pardenomc (reverse pardenom))
65 numc (setq thebpg (bprog (car pardenomc) (car ppdenom)))
66 (setq parnumer
67 (cons (cdr (ratdivide (ratti frpart (cdr thebpg) t)
68 (car pardenomc)))
69 parnumer))
70 (setq frpart
71 (cdr (ratdivide (ratti frpart (car thebpg) t)
72 (car ppdenom))))
73 (setq pardenomc (cdr pardenomc))
74 (setq ppdenom (cdr ppdenom))
75 (if (null ppdenom)
76 (return (setq parnumer (cons frpart parnumer))))
77 (go numc)))
79 (defun polyint (p) (ratqu (polyint1 (ratnumerator p)) (ratdenominator p)))
81 (defun polyint1 (p)
82 (cond ((or (null p) (equal p 0)) (cons 0 1))
83 ((atom p) (list var 1 p))
84 ((not (numberp (car p)))
85 (if (pointergp var (car p)) (list var 1 p) (polyint1 (cdr p))))
86 (t (ratplus (polyint2 p) (polyint1 (cddr p))))))
88 (defun polyint2 (p) (cons (list var (1+ (car p)) (cadr p)) (1+ (car p))))
90 (defun dprog (ratarg)
91 (prog (klth kx arootf deriv thebpg thetop thebot prod1 prod2 ans)
92 (setq ans (cons 0 1))
93 (if (or (pcoefp (cdr ratarg)) (pointergp var (cadr ratarg)))
94 (return (disrep (polyint ratarg))))
95 (aprog (ratdenominator ratarg))
96 (cprog (ratnumerator ratarg) (ratdenominator ratarg))
97 (setq rootfactor (reverse rootfactor))
98 (setq parnumer (reverse parnumer))
99 (setq klth (length rootfactor))
100 intg (if (= klth 1) (go simp))
101 (setq arootf (car rootfactor))
102 (if (zerop (pdegree arootf var)) (go reset))
103 (setq deriv (pderivative arootf var))
104 (setq thebpg (bprog arootf deriv))
105 (setq kx (1- klth))
106 (setq thetop (car parnumer))
107 iter (setq prod1 (ratti thetop (car thebpg) t))
108 (setq prod2 (ratti thetop (cdr thebpg) t))
109 (setq thebot (pexpt arootf kx))
110 (setq ans
111 (ratplus ans (ratqu (ratminus prod2) (ratti kx thebot t))))
112 (setq thetop
113 (ratplus prod1
114 (ratqu (ratreduce (pderivative (car prod2) var)
115 (cdr prod2))
116 kx)))
117 (setq thetop (cdr (ratdivide thetop thebot)))
118 (cond ((= kx 1) (setq logptdx (cons (ratqu thetop arootf) logptdx))
119 (go reset)))
120 (setq kx (1- kx))
121 (go iter)
122 reset(setq rootfactor (cdr rootfactor))
123 (setq parnumer (cdr parnumer))
124 (decf klth)
125 (go intg)
126 simp (push (ratqu (car parnumer) (car rootfactor)) logptdx)
127 (if (equal ans 0) (return (disrep (polyint wholepart))))
128 (setq thetop
129 (cadr (pdivide (ratnumerator ans) (ratdenominator ans))))
130 (return (list '(mplus)
131 (disrep (polyint wholepart))
132 (disrep (ratqu thetop (ratdenominator ans)))))))
134 (defun logmabs (x)
135 (list '(%log) (if $logabs (simplify (list '(mabs) x)) x)))
137 (defun npask (exp)
138 (cond ((freeof '$%i exp)
139 (learn `((mnotequal) ,exp 0) t) (asksign exp))
140 (t '$positive)))
142 (defvar $integrate_use_rootsof nil "Use the rootsof form for integrals when denominator does not factor")
144 (defun integrate-use-rootsof (f q variable)
145 (let ((dummy (make-param))
146 (qprime (disrep (pderivative q (p-var q))))
147 (ff (disrep f))
148 (qq (disrep q)))
149 ;; This basically does a partial fraction expansion and integrates
150 ;; the result. Let r be one (simple) root of the denominator
151 ;; polynomial q. Then the partial fraction expansion is
153 ;; f(x)/q(x) = A/(x-r) + similar terms.
155 ;; Then
157 ;; f(x) = A*q(x)/(x-r) + others
159 ;; Take the limit as x -> r.
161 ;; f(r) = A*limit(q(x)/(x-r),x,r) + others
162 ;; = A*at(diff(q(x),r), [x=r])
164 ;; Hence, A = f(r)/at(diff(q(x),x),[x=r])
166 ;; Then it follows that the integral is
168 ;; A*log(x-r)
170 ;; Note that we don't express the polynomial in terms of the
171 ;; variable of integration, but in our dummy variable instead.
172 ;; Using the variable of integration results in a wrong answer
173 ;; when a substitution was done previously, since when the
174 ;; substitution is finally undone, that modifies the polynomial.
175 `((%lsum) ((mtimes)
176 ,(div* (subst dummy variable ff)
177 (subst dummy variable qprime))
178 ((%log) ,(sub* variable dummy)))
179 ,dummy
180 (($rootsof) ,(subst dummy variable qq) ,dummy))))
182 (defun eprog (p)
183 (prog (p1e p2e a1e a2e a3e discrim repart sign ncc dcc allcc xx deg)
184 (if (or (equal p 0) (equal (car p) 0)) (return 0))
185 (setq p1e (ratnumerator p) p2e (ratdenominator p))
186 (cond ((or switch1
187 (and (not (atom p2e))
188 (eq (car (setq xx (cadr (oldcontent p2e)))) var)
189 (member (setq deg (pdegree xx var)) '(5 6) :test #'equal)
190 (zerocoefl xx deg)
191 (or (equal deg 5) (not (pminusp (car (last xx)))))))
192 (go efac)))
193 (setq a1e (intfactor p2e))
194 (if (> (length a1e) 1) (go e40))
195 efac (setq ncc (oldcontent p1e))
196 (setq p1e (cadr ncc))
197 (setq dcc (oldcontent p2e))
198 (setq p2e (cadr dcc))
199 (setq allcc (ratqu (car ncc) (car dcc)))
200 (setq deg (pdegree p2e var))
201 (setq a1e (pderivative p2e var))
202 (setq a2e (ratqu (polcoef p1e (pdegree p1e var))
203 (polcoef a1e (pdegree a1e var))))
204 (cond ((equal (ratti a2e a1e t) (cons p1e 1))
205 (return (list '(mtimes)
206 (disrep (ratti allcc a2e t))
207 (logmabs (disrep p2e))))))
208 (cond ((equal deg 1) (go e10))
209 ((equal deg 2) (go e20))
210 ((and (equal deg 3) (equal (polcoef p2e 2) 0)
211 (equal (polcoef p2e 1) 0))
212 (return (e3prog p1e p2e allcc)))
213 ((and (member deg '(4 5 6) :test #'equal) (zerocoefl p2e deg))
214 (return (enprog p1e p2e allcc deg))))
215 (cond ((and $integrate_use_rootsof (equal (car (psqfr p2e)) p2e))
216 (return (list '(mtimes) (disrep allcc)
217 (integrate-use-rootsof p1e p2e
218 (car (last varlist)))))))
219 (return (list '(mtimes)
220 (disrep allcc)
221 (list '(%integrate)
222 (list '(mquotient) (disrep p1e) (disrep p2e))
223 (car (last varlist)))))
224 e10 (return (list '(mtimes)
225 (disrep (ratti allcc
226 (ratqu (polcoef p1e (pdegree p1e var))
227 (polcoef p2e 1))
229 (logmabs (disrep p2e))))
230 e20 (setq discrim
231 (ratdifference (cons (pexpt (polcoef p2e 1) 2) 1)
232 (ratti 4 (ratti (polcoef p2e 2) (polcoef p2e 0) t) t)))
233 (setq a2e (ratti (polcoef p2e (pdegree p2e var)) 2 t))
234 (setq xx (simplify (disrep discrim)))
235 (when (equal ($imagpart xx) 0)
236 (setq sign (npask xx))
237 (cond ((eq sign '$negative) (go e30))
238 ((eq sign '$zero) (go zip))))
239 (setq a1e (ratsqrt discrim))
240 (setq a3e (logmabs
241 (list '(mquotient)
242 (list '(mplus)
243 (list '(mtimes)
244 (disrep a2e) (disrep (list var 1 1)))
245 (disrep (polcoef p2e 1))
246 (list '(mminus) a1e))
247 (list '(mplus)
248 (list '(mtimes)
249 (disrep a2e) (disrep (list var 1 1)))
250 (disrep (polcoef p2e 1))
251 a1e))))
252 (cond ((zerop (pdegree p1e var))
253 (return (list '(mtimes)
254 (disrep allcc)
255 (list '(mquotient) (disrep (polcoef p1e 0)) a1e)
256 a3e))))
257 (return
258 (list
259 '(mplus)
260 (list '(mtimes)
261 (disrep (ratti allcc (ratqu (polcoef p1e (pdegree p1e var)) a2e) t))
262 (logmabs (disrep p2e)))
263 (list
264 '(mtimes)
265 (list
266 '(mquotient)
267 (disrep (ratti allcc (ratqu (eprogratd a2e p1e p2e) a2e) t))
268 a1e)
269 a3e)))
270 e30 (setq a1e (ratsqrt (ratminus discrim)))
271 (setq
272 repart
273 (ratqu (cond ((zerop (pdegree p1e var)) (ratti a2e (polcoef p1e 0) t))
274 (t (eprogratd a2e p1e p2e)))
275 (polcoef p2e (pdegree p2e var))))
276 (setq a3e (cond ((equal 0 (car repart)) 0)
277 (t `((mtimes) ((mquotient)
278 ,(disrep (ratti allcc repart t))
279 ,a1e)
280 ((%atan)
281 ((mquotient)
282 ,(disrep (pderivative p2e var))
283 ,a1e))))))
284 (if (zerop (pdegree p1e var)) (return a3e))
285 (return (list '(mplus)
286 (list '(mtimes)
287 (disrep (ratti allcc
288 (ratqu (polcoef p1e (pdegree p1e var)) a2e)
290 (logmabs (disrep p2e)))
291 a3e))
292 zip (setq
294 (ratqu
295 (psimp
296 (p-var p2e)
297 (pcoefadd 2
298 (pexpt (ptimes 2 (polcoef p2e 2)) 2)
299 (pcoefadd 1 (ptimes 4 (ptimes (polcoef p2e 2)
300 (polcoef p2e 1)))
301 (pcoefadd 0 (pexpt (polcoef p2e 1) 2) ()))))
302 (ptimes 4 (polcoef p2e 2))))
303 (return (fprog (ratti allcc (ratqu p1e p2e) t)))
304 e40 (setq parnumer nil pardenom a1e switch1 t)
305 (cprog p1e p2e)
306 (setq a2e
307 (mapcar #'(lambda (j k) (eprog (ratqu j k))) parnumer pardenom))
308 (setq switch1 nil)
309 (return (cons '(mplus) a2e))))
311 (defun e3prog (num denom cont)
312 (prog (a b c d e r ratr var* x)
313 (setq a (polcoef num 2) b (polcoef num 1) c (polcoef num 0)
314 d (polcoef denom 3) e (polcoef denom 0))
315 (setq r (cond ((eq (npask (simplify (disrep (ratqu e d)))) '$negative)
316 (simpnrt (disrep (ratqu (ratti -1 e t) d)) 3))
317 (t (neg (simpnrt (disrep (ratqu e d)) 3)))))
318 (setq var* (list var 1 1))
319 (newvar r)
320 (orderpointer varlist)
321 (setq x (ratf r))
322 (setq ratform (car x) ratr (cdr x))
323 (return
324 (simplify
325 (list '(mplus)
326 (list '(mtimes)
327 (disrep (ratqu (r* cont (r+ (r* a ratr ratr) (r* b ratr) c))
328 (r* ratr ratr 3 d)))
329 (logmabs (disrep (ratpl (ratti -1 ratr t) var*))))
330 (eprog (r* cont (ratqu (r+ (r* (r+ (r* 2 a ratr ratr)
331 (r* -1 b ratr)
332 (r* -1 c))
333 var*)
334 (r+ (ratqu (r* -1 a e) d)
335 (r* b ratr ratr)
336 (r* -1 2 c ratr)))
337 (r* 3 d ratr ratr
338 (r+ (ratti var* var* t)
339 (ratti ratr var* t)
340 (ratti ratr ratr t))))))
341 )))))
343 (defun eprogratd (a2e p1e p2e)
344 (ratdifference (ratti a2e (polcoef p1e (1- (pdegree p1e var))) t)
345 (ratti (polcoef p2e (1- (pdegree p2e var)))
346 (polcoef p1e (pdegree p1e var))
347 t)))
349 (defun enprog (num denom cont deg)
350 ;; Denominator is (A*VAR^4+B) =
351 ;; if B<0 then (SQRT(A)*VAR^2 - SQRT(-B)) (SQRT(A)*VAR^2 + SQRT(-B))
352 ;; else
353 ;; (SQRT(A)*VAR^2 - SQRT(2)*A^(1/4)*B^(1/4)*VAR + SQRT(B)) *
354 ;; (SQRT(A)*VAR^2 + SQRT(2)*A^(1/4)*B^(1/4)*VAR + SQRT(B))
355 ;; or (A*VAR^5+B) =
356 ;; (1/4) * (A^(1/5)*VAR + B^(1/5)) *
357 ;; (2*A^(2/5)*VAR^2 + (-SQRT(5)-1)*A^(1/5)*B^(1/5)*VAR + 2*B^(2/5)) *
358 ;; (2*A^(2/5)*VAR^2 + (+SQRT(5)-1)*A^(1/5)*B^(1/5)*VAR + 2*B^(2/5))
359 ;; or (A*VAR^6+B) =
360 ;; if B<0 then (SQRT(A)*VAR^3 - SQRT(-B)) (SQRT(A)*VAR^3 + SQRT(-B))
361 ;; else
362 ;; (A^(1/3)*VAR^2 + B^(1/3)) *
363 ;; (A^(1/3)*VAR^2 - SQRT(3)*A^(1/6)*B^(1/6)*VAR + B^(1/3)) *
364 ;; (A^(1/3)*VAR^2 + SQRT(3)*A^(1/6)*B^(1/6)*VAR + B^(1/3))
365 (prog ($expop $expon a b term disvar $algebraic)
366 (setq $expop 0 $expon 0)
367 (setq a (simplify (disrep (polcoef denom deg)))
368 b (simplify (disrep (polcoef denom 0)))
369 disvar (simplify (get var 'disrep))
370 num (simplify (disrep num))
371 cont (simplify (disrep cont)))
372 (cond ((= deg 4)
373 (if (eq '$neg ($asksign b))
374 (setq denom
375 (mul2 (add2 (mul2 (power a '((rat simp) 1 2)) (power disvar 2))
376 (power (mul -1 b) '((rat simp) 1 2)))
377 (add2 (mul2 (power a '((rat simp) 1 2)) (power disvar 2))
378 (mul -1 (power (mul -1 b) '((rat simp) 1 2))))))
379 (progn
380 (setq denom (add2 (mul2 (power a '((rat simp) 1 2)) (power disvar 2))
381 (power b '((rat simp) 1 2)))
382 term (muln (list (power 2 '((rat simp) 1 2))
383 (power a '((rat simp) 1 4))
384 (power b '((rat simp) 1 4))
385 disvar)
387 (setq denom (mul2 (add2 denom term) (sub denom term))))))
388 ((= deg 5)
389 (setq term (mul3 (power a '((rat simp) 1 5))
390 (power b '((rat simp) 1 5))
391 disvar))
392 (setq denom (add2 (mul3 2 (power a '((rat simp) 2 5))
393 (power disvar 2))
394 (sub (mul2 2 (power b '((rat simp) 2 5))) term)))
395 (setq term (mul2 (power 5 '((rat simp) 1 2)) term))
396 (setq denom (muln (list '((rat simp) 1 4)
397 (add2 (mul2 (power a '((rat simp) 1 5)) disvar)
398 (power b '((rat simp) 1 5)))
399 (add2 denom term) (sub denom term))
400 t)))
402 (if (eq '$neg ($asksign b))
403 (setq denom
404 (mul2 (add2 (mul2 (power a '((rat simp) 1 2)) (power disvar 3))
405 (power (mul -1 b) '((rat simp) 1 2)))
406 (add2 (mul2 (power a '((rat simp) 1 2)) (power disvar 3))
407 (mul -1 (power (mul -1 b) '((rat simp) 1 2))))))
408 (progn
409 (setq denom (add2 (mul2 (power a '((rat simp) 1 3)) (power disvar 2))
410 (power b '((rat simp) 1 3)))
411 term (muln (list (power 3 '((rat simp) 1 2))
412 (power a '((rat simp) 1 6))
413 (power b '((rat simp) 1 6))
414 disvar)
416 (setq denom (mul3 denom (add2 denom term) (sub denom term))))
418 ;;Needs $ALGEBRAIC NIL so next call to RATF will preserve factorization.
419 (return (mul2 cont (ratint (div num denom) disvar)))))
421 (defun zerocoefl (e n)
422 (do ((i 1 (1+ i))) ((= i n) t)
423 (if (not (equal (polcoef e i) 0)) (return nil))))
425 (defun ratsqrt (a) (let (varlist) (simpnrt (disrep a) 2)))
427 (defun fprog (rat*)
428 (prog (rootfactor pardenom parnumer logptdx wholepart switch1)
429 (return (addn (cons (dprog rat*) (mapcar #'eprog logptdx)) nil))))
431 (defun ratint (exp var)
432 (prog (genvar checkfactors varlist ratarg ratform $keepfloat)
433 (setq varlist (list var))
434 (setq ratarg (ratf exp))
435 (setq ratform (car ratarg))
436 (setq var (caadr (ratf var)))
437 (return (fprog (cdr ratarg)))))
439 (defun intfactor (l)
440 (prog ($factorflag a b)
441 (setq a (oldcontent l) b (everysecond (pfactor (cadr a))))
442 (return (if (equal (car a) 1) b (cons (car a) b)))))
444 (defun everysecond (a)
445 (if a (cons (if (numberp (car a))
446 (pexpt (car a) (cadr a))
447 (car a))
448 (everysecond (cddr a)))))