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
)
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
)
35 (setq q
(oldcontent q
))
36 (setq rootfactor
(rootfac (cadr q
)))
38 (cons (ptimes (car q
) (car rootfactor
)) (cdr rootfactor
)))
39 (do ((pd (list (car rootfactor
)))
40 (rf (cdr rootfactor
) (cdr rf
))
42 ((null rf
) (setq pardenom
(reverse pd
)))
43 (push (pexpt (car rf
) n
) pd
))
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
))
59 ok
(setq pardenomc
(reverse pardenom
))
60 numc
(setq thebpg
(bprog (car pardenomc
) (car ppdenom
)))
62 (cons (cdr (ratdivide (ratti frpart
(cdr thebpg
) t
)
66 (cdr (ratdivide (ratti frpart
(car thebpg
) t
)
68 (setq pardenomc
(cdr pardenomc
))
69 (setq ppdenom
(cdr ppdenom
))
71 (return (setq parnumer
(cons frpart parnumer
))))
74 (defun polyint (p) (ratqu (polyint1 (ratnumerator p
)) (ratdenominator 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
))))
86 (prog (klth kx arootf deriv thebpg thetop thebot prod1 prod2 ans
)
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
))
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
))
106 (ratplus ans
(ratqu (ratminus prod2
) (ratti kx thebot t
))))
109 (ratqu (ratreduce (pderivative (car prod2
) var
)
112 (setq thetop
(cdr (ratdivide thetop thebot
)))
113 (cond ((= kx
1) (setq logptdx
(cons (ratqu thetop arootf
) logptdx
))
117 reset
(setq rootfactor
(cdr rootfactor
))
118 (setq parnumer
(cdr parnumer
))
121 simp
(push (ratqu (car parnumer
) (car rootfactor
)) logptdx
)
122 (if (equal ans
0) (return (disrep (polyint wholepart
))))
124 (cadr (pdivide (ratnumerator ans
) (ratdenominator ans
))))
125 (return (list '(mplus)
126 (disrep (polyint wholepart
))
127 (disrep (ratqu thetop
(ratdenominator ans
)))))))
130 (list '(%log
) (if $logabs
(simplify (list '(mabs) x
)) x
)))
133 (cond ((freeof '$%i exp
)
134 (learn `((mnotequal) ,exp
0) t
) (asksign exp
))
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
))))
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.
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
165 ,(div* (subst dummy variable ff
)
166 (subst dummy variable qprime
))
167 ((%log
) ,(sub* variable dummy
)))
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
))
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
)
180 (or (equal deg
5) (not (pminusp (car (last xx
)))))))
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)
211 (list '(mquotient) (disrep p1e
) (disrep p2e
))
212 (car (last varlist
)))))
213 e10
(return (list '(mtimes)
215 (ratqu (polcoef p1e
(pdegree p1e var
))
218 (logmabs (disrep p2e
))))
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
))
233 (disrep a2e
) (disrep (list var
1 1)))
234 (disrep (polcoef p2e
1))
235 (list '(mminus) a1e
))
238 (disrep a2e
) (disrep (list var
1 1)))
239 (disrep (polcoef p2e
1))
241 (cond ((zerop (pdegree p1e var
))
242 (return (list '(mtimes)
244 (list '(mquotient) (disrep (polcoef p1e
0)) a1e
)
250 (disrep (ratti allcc
(ratqu (polcoef p1e
(pdegree p1e var
)) a2e
) t
))
251 (logmabs (disrep p2e
)))
256 (disrep (ratti allcc
(ratqu (eprogratd a2e p1e p2e
) a2e
) t
))
259 e30
(setq a1e
(ratsqrt (ratminus discrim
)))
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
))
271 ,(disrep (pderivative p2e var
))
273 (if (zerop (pdegree p1e var
)) (return a3e
))
274 (return (list '(mplus)
277 (ratqu (polcoef p1e
(pdegree p1e var
)) a2e
)
279 (logmabs (disrep p2e
)))
287 (pexpt (ptimes 2 (polcoef p2e
2)) 2)
288 (pcoefadd 1 (ptimes 4 (ptimes (polcoef p2e
2)
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
)
296 (mapcar #'(lambda (j k
) (eprog (ratqu j k
))) parnumer pardenom
))
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))
309 (orderpointer varlist
)
311 (setq ratform
(car x
) ratr
(cdr x
))
316 (disrep (ratqu (r* cont
(r+ (r* a ratr ratr
) (r* b ratr
) c
))
318 (logmabs (disrep (ratpl (ratti -
1 ratr t
) var
*))))
319 (eprog (r* cont
(ratqu (r+ (r* (r+ (r* 2 a ratr ratr
)
323 (r+ (ratqu (r* -
1 a e
) d
)
327 (r+ (ratti var
* var
* t
)
329 (ratti ratr ratr t
))))))
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
))
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))
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))
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))
349 ;; if B<0 then (SQRT(A)*VAR^3 - SQRT(-B)) (SQRT(A)*VAR^3 + SQRT(-B))
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
)))
362 (if (eq '$neg
($asksign b
))
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))))))
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))
376 (setq denom
(mul2 (add2 denom term
) (sub denom term
))))))
378 (setq term
(mul3 (power a
'((rat simp
) 1 5))
379 (power b
'((rat simp
) 1 5))
381 (setq denom
(add2 (mul3 2 (power a
'((rat simp
) 2 5))
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
))
391 (if (eq '$neg
($asksign b
))
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))))))
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))
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)))
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
)))))
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
))
437 (everysecond (cddr a
)))))