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