1 ;;; -*- Mode: Lisp; Package: Maxima; Syntax: Common-Lisp; Base: 10 -*- ;;;;
2 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 ;;; The data in this file contains enhancments. ;;;;;
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
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
))
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
))))
29 ((= n-loops $factor_max_degree
)
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
)
40 (setq q
(oldcontent q
))
41 (setq rootfactor
(rootfac (cadr q
)))
43 (cons (ptimes (car q
) (car rootfactor
)) (cdr rootfactor
)))
44 (do ((pd (list (car rootfactor
)))
45 (rf (cdr rootfactor
) (cdr rf
))
47 ((null rf
) (setq pardenom
(reverse pd
)))
48 (push (pexpt (car rf
) n
) pd
))
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
))
64 ok
(setq pardenomc
(reverse pardenom
))
65 numc
(setq thebpg
(bprog (car pardenomc
) (car ppdenom
)))
67 (cons (cdr (ratdivide (ratti frpart
(cdr thebpg
) t
)
71 (cdr (ratdivide (ratti frpart
(car thebpg
) t
)
73 (setq pardenomc
(cdr pardenomc
))
74 (setq ppdenom
(cdr ppdenom
))
76 (return (setq parnumer
(cons frpart parnumer
))))
79 (defun polyint (p) (ratqu (polyint1 (ratnumerator p
)) (ratdenominator 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
))))
91 (prog (klth kx arootf deriv thebpg thetop thebot prod1 prod2 ans
)
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
))
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
))
111 (ratplus ans
(ratqu (ratminus prod2
) (ratti kx thebot t
))))
114 (ratqu (ratreduce (pderivative (car prod2
) var
)
117 (setq thetop
(cdr (ratdivide thetop thebot
)))
118 (cond ((= kx
1) (setq logptdx
(cons (ratqu thetop arootf
) logptdx
))
122 reset
(setq rootfactor
(cdr rootfactor
))
123 (setq parnumer
(cdr parnumer
))
126 simp
(push (ratqu (car parnumer
) (car rootfactor
)) logptdx
)
127 (if (equal ans
0) (return (disrep (polyint wholepart
))))
129 (cadr (pdivide (ratnumerator ans
) (ratdenominator ans
))))
130 (return (list '(mplus)
131 (disrep (polyint wholepart
))
132 (disrep (ratqu thetop
(ratdenominator ans
)))))))
135 (list '(%log
) (if $logabs
(simplify (list '(mabs) x
)) x
)))
138 (cond ((freeof '$%i exp
)
139 (learn `((mnotequal) ,exp
0) t
) (asksign exp
))
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
))))
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.
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
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.
176 ,(div* (subst dummy variable ff
)
177 (subst dummy variable qprime
))
178 ((%log
) ,(sub* variable dummy
)))
180 (($rootsof
) ,(subst dummy variable qq
) ,dummy
))))
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
))
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
)
191 (or (equal deg
5) (not (pminusp (car (last xx
)))))))
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)
222 (list '(mquotient) (disrep p1e
) (disrep p2e
))
223 (car (last varlist
)))))
224 e10
(return (list '(mtimes)
226 (ratqu (polcoef p1e
(pdegree p1e var
))
229 (logmabs (disrep p2e
))))
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
))
244 (disrep a2e
) (disrep (list var
1 1)))
245 (disrep (polcoef p2e
1))
246 (list '(mminus) a1e
))
249 (disrep a2e
) (disrep (list var
1 1)))
250 (disrep (polcoef p2e
1))
252 (cond ((zerop (pdegree p1e var
))
253 (return (list '(mtimes)
255 (list '(mquotient) (disrep (polcoef p1e
0)) a1e
)
261 (disrep (ratti allcc
(ratqu (polcoef p1e
(pdegree p1e var
)) a2e
) t
))
262 (logmabs (disrep p2e
)))
267 (disrep (ratti allcc
(ratqu (eprogratd a2e p1e p2e
) a2e
) t
))
270 e30
(setq a1e
(ratsqrt (ratminus discrim
)))
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
))
282 ,(disrep (pderivative p2e var
))
284 (if (zerop (pdegree p1e var
)) (return a3e
))
285 (return (list '(mplus)
288 (ratqu (polcoef p1e
(pdegree p1e var
)) a2e
)
290 (logmabs (disrep p2e
)))
298 (pexpt (ptimes 2 (polcoef p2e
2)) 2)
299 (pcoefadd 1 (ptimes 4 (ptimes (polcoef p2e
2)
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
)
307 (mapcar #'(lambda (j k
) (eprog (ratqu j k
))) parnumer pardenom
))
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))
320 (orderpointer varlist
)
322 (setq ratform
(car x
) ratr
(cdr x
))
327 (disrep (ratqu (r* cont
(r+ (r* a ratr ratr
) (r* b ratr
) c
))
329 (logmabs (disrep (ratpl (ratti -
1 ratr t
) var
*))))
330 (eprog (r* cont
(ratqu (r+ (r* (r+ (r* 2 a ratr ratr
)
334 (r+ (ratqu (r* -
1 a e
) d
)
338 (r+ (ratti var
* var
* t
)
340 (ratti ratr ratr t
))))))
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
))
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))
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))
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))
360 ;; if B<0 then (SQRT(A)*VAR^3 - SQRT(-B)) (SQRT(A)*VAR^3 + SQRT(-B))
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
)))
373 (if (eq '$neg
($asksign b
))
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))))))
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))
387 (setq denom
(mul2 (add2 denom term
) (sub denom term
))))))
389 (setq term
(mul3 (power a
'((rat simp
) 1 5))
390 (power b
'((rat simp
) 1 5))
392 (setq denom
(add2 (mul3 2 (power a
'((rat simp
) 2 5))
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
))
402 (if (eq '$neg
($asksign b
))
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))))))
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))
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)))
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
)))))
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
))
448 (everysecond (cddr a
)))))