Print a warning when translating subscripted functions
[maxima.git] / src / nrat4.lisp
blob08e6adcbf8ff939e5c1a8a0000c15dde6343f38a
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 nrat4)
15 (declare-top (special $ratsimpexpons *exp *exp2 *radsubst *loglist $radsubstflag
16 $logsimp *v *var radcanp))
18 (defmvar $radsubstflag nil
19 "`radsubstflag' `t' makes `ratsubs' call `radcan' when it appears useful")
22 (defun pdis (x) ($ratdisrep (pdis* x)))
24 (defun pdis* (x) `((mrat simp ,varlist ,genvar) ,x . 1))
26 (defun rdis (x) ($ratdisrep (rdis* x)))
28 (defun rdis* (x) `((mrat simp ,varlist ,genvar) . ,x))
30 (defun rform (x) (cdr (ratf x)))
32 (setq radcanp nil)
34 (defmfun $ratcoef (e x &optional (n 1))
35 (ratcoeff e x n)) ; The spelling "ratcoeff" is nicer.
37 (defun ratcoeff (a b c)
38 (let* ((formflag ($ratp a))
39 (taylorform (and formflag (member 'trunc (cdar a) :test #'eq))))
40 (cond ((zerop1 b) (improper-arg-err b '$ratcoeff))
41 ((mbagp a) (cons (car a)
42 (mapcar #'(lambda (a) (ratcoeff a b c))
43 (cdr a))))
44 ((and taylorform (mnump c) (assolike b (cadddr (cdar a))))
45 (pscoeff1 a b c))
46 ((and taylorform (mexptp b) (mnump c) (mnump (caddr b))
47 (assolike (cadr b) (cadddr (cdar a))))
48 (pscoeff1 a (cadr b) (mul2 c (caddr b))))
49 ((and taylorform (equal c 0)) a)
50 (t (if taylorform (setq a (ratdisrep a)))
51 (setq a (let ($ratwtlvl)
52 (if (equal c 0)
53 (ratcoef (mul2* a b) b)
54 (ratcoef a (if (equal c 1) b (list '(mexpt) b c))))))
55 (if (and formflag (not taylorform))
56 (minimize-varlist a)
57 (ratdisrep a))))))
59 (defun minimize-varlist (ratfun)
60 (if (not ($ratp ratfun)) (setq ratfun (ratf ratfun)))
61 (minvarlist-mrat (caddr (car ratfun)) (cadddr (car ratfun))
62 (cdr ratfun)))
64 (defun minvarlist-mrat (vars gens ratform)
65 (let ((newgens (union* (listovars (car ratform))
66 (listovars (cdr ratform)))))
67 (do ((lv vars (cdr lv))
68 (lg gens (cdr lg))
69 (nlv ())
70 (nlg ()))
71 ((null lg)
72 (cons (list 'mrat 'simp (nreverse nlv) (nreverse nlg))
73 ratform))
74 (cond ((member (car lg) newgens :test #'eq)
75 (push (car lg) nlg)
76 (push (car lv) nlv))))))
78 (defun ratcoef (exp var)
79 (prog (varlist genvar $ratfac $algebraic $ratwtlvl bas minvar)
80 (setq var (ratdisrep var))
81 (setq bas (if (and (mexptp var) (mnump (caddr var))) (cadr var) var))
82 (newvar var)
83 (newvar bas)
84 (setq minvar (car varlist))
85 (newvar exp)
86 (setq exp (cdr (ratrep* exp)))
87 (setq var (cdr (ratrep* var)))
88 (setq bas (cadr (ratrep* bas)))
89 (if (and (onep1 (cdr exp)) (onep1 (cdr var)) (pureprod (car var)))
90 (return (pdis* (prodcoef (car var) (car exp)))))
91 (setq exp (ratquotient exp var))
92 (if (null minvar) (return (pdis* (prodcoef (cdr exp) (car exp)))))
93 (setq minvar (caadr (ratrep* minvar)))
94 loop (if (or (pcoefp (cdr exp)) (pointergp minvar (cadr exp)))
95 (return (rdis* (cdr (ratdivide exp bas)))))
96 (setq exp (ratcoef1 (car exp) (cdr exp)))
97 (go loop)))
99 (defun ratcoef1 (num den)
100 (cond ((pcoefp num) (rzero))
101 ((eq (car num) (car den)) (car (pdivide num den)))
102 ((pointergp (car den) (car num)) (rzero))
103 (t (ratcoef1 (constcoef (cdr num)) den))))
105 (defun constcoef (p)
106 (cond ((null p) 0)
107 ((zerop (car p)) (cadr p))
108 (t (constcoef (cddr p)))))
110 (setq *radsubst nil)
112 (defmfun $ratsubst (a b c) ; NEEDS CODE FOR FAC. FORM
113 (prog (varlist newvarlist dontdisrepit $ratfac genvar $keepfloat $float $numer)
114 ;; hard to maintain user ordering info.
115 (if ($ratp c) (setq dontdisrepit t))
116 (if (and $radsubstflag
117 (prog2 (newvar b) (some #'mexptp varlist)))
118 (let (($factorflag t) *exp *exp2 *radsubst)
119 (setq a (fullratsimp a))
120 (setq b (fullratsimp b))
121 (setq c (fullratsimp c))
122 (setq varlist nil)
123 (fnewvar b)
124 (fnewvar c)
125 (setq *exp (cdr (ratrep* b)))
126 (setq *exp2 (cdr (ratrep* c)))
127 ;; since *radsubst is t, both *exp and *exp2 will be radcan simplified
128 (setq *radsubst t)
129 (spc0)
130 (setq b (rdis *exp) c (rdis *exp2))
131 (setq varlist nil))
132 (progn
133 (setq a ($rat a))
134 (setq b ($rat b))
135 (setq c ($rat c))
136 (setq varlist nil)))
137 (setq a ($ratdisrep a) b ($ratdisrep b) c ($ratdisrep c))
138 (cond ((integerp b) (setq c (ratf (maxima-substitute a b c)))
139 (return (cond (dontdisrepit c) (t ($ratdisrep c))))))
140 (newvar c)
141 (setq
142 newvarlist
143 (mapcar
144 #'(lambda (z)
145 (cond ((atom z) z)
146 (t (resimplify
147 (cons (car z)
148 (mapcar #'(lambda (zz)
149 (cond ((alike1 zz b) a)
150 ((atom zz) zz)
151 (t ($ratdisrep
152 ($ratsubst a b zz)))))
153 (cdr z)))))))
154 varlist))
155 (newvar a) (newvar b)
156 (setq newvarlist (reverse (pairoff (reverse varlist)
157 (reverse newvarlist))))
158 (setq a (cdr (ratrep* a)))
159 (setq b (cdr (ratrep* b)))
160 (setq c (cdr (ratrep* c)))
161 (when (pminusp (car b))
162 (setq b (ratminus b))
163 (setq a (ratminus a)))
164 (when (and (equal 1 (car b))
165 (not (equal 1 (cdr b)))
166 (not (equal 0 (car a))))
167 (setq a (ratinvert a))
168 (setq b (ratinvert b)))
169 (cond ((not (equal 1 (cdr b)))
170 (setq a (rattimes a (cons (cdr b) 1) t))
171 (setq b (cons (car b) 1))))
172 (setq c
173 (cond ((member (car b) '(0 1) :test #'equal)
174 (ratf (maxima-substitute (rdis a) b (rdis c))))
175 (t (cons (list 'mrat 'simp varlist genvar)
176 (if (equal (cdr a) 1)
177 (ratreduce (everysubst0 (car a) (car b) (car c))
178 (everysubst0 (car a) (car b) (cdr c)))
179 (allsubst00 a b c))))))
180 (unless (alike newvarlist varlist)
181 (setq varlist newvarlist
182 c (rdis (cdr c))
183 varlist nil
184 c (ratf c)))
185 (return (cond (dontdisrepit c) (t ($ratdisrep c))))))
187 (defun xptimes (x y) (if $ratwtlvl (wtptimes x y 0) (ptimes x y)))
189 (defun allsubst00 (a b c)
190 (cond ((equal a b) c)
191 ((not (equal (cdr b) 1)) c)
192 (t (ratquotient (everysubst00 a (car b) (car c))
193 (everysubst00 a (car b) (cdr c))))))
195 (defun everysubst00 (x i z)
196 (loop with ans = (rzero)
197 for (exp coef) on (everysubst i z *alpha) by #'cddr
198 do (setq ans (ratplus ans (rattimes (cons coef 1) (ratexpt x exp) t)))
199 finally (return ans)))
201 (defun everysubst0 (x i z)
202 (loop with ans = (pzero)
203 for (exp coef) on (everysubst i z *alpha) by #'cddr
204 do (setq ans (pplus ans (xptimes coef (pexpt x exp))))
205 finally (return ans)))
207 (defun everysubst1 (a b maxpow)
208 (loop for (exp coef) on (p-terms b) by #'cddr
209 for part = (everysubst a coef maxpow)
210 nconc (if (= 0 exp) part
211 (everysubst2 part (make-poly (p-var b) exp 1)))))
213 (defun everysubst2 (l h)
214 (do ((ptr l (cddr ptr)))
215 ((null ptr) l)
216 (setf (cadr ptr) (ptimes h (cadr ptr)))))
219 (defun pairoff (l m)
220 (cond ((null m) l) (t (cons (car m) (pairoff (cdr l) (cdr m))))))
222 ;;(DEFUN PAIROFF (L M)
223 ;; ;(COND ((NULL M) L) (T (CONS (CAR M) (PAIROFF (CDR L) (CDR M)))))
224 ;; (let ((ans nil))
225 ;; (dolist (x m (nreconc ans l))
226 ;; (push x ans) (setq l (cdr l)))))
228 (defun everysubst (a b maxpow)
229 (cond ((pcoefp a)
230 (cond ((equal a 1) (list maxpow b))
231 ((pcoefp b)
232 (list (setq maxpow
233 (do ((b b (quotient b a))
234 (ans 0 (1+ ans)))
235 ((or (> (abs a) (abs b))
236 (equal maxpow ans))
237 ans)))
238 (quotient b (setq maxpow (expt a maxpow)))
240 (rem b maxpow)))
241 (t (everysubst1 a b maxpow))))
242 ((or (pcoefp b) (pointergp (car a) (car b))) (list 0 b))
243 ((eq (car a) (car b))
244 (cond ((null (cdddr a)) (everypterms b (caddr a) (cadr a) maxpow))
245 (t (substforsum a b maxpow))))
246 (t (everysubst1 a b maxpow))))
248 (defun everypterms (x p n maxpow)
249 (if (< (cadr x) n)
250 (list 0 x)
251 (prog (k ans q part)
252 (setq k (car x))
253 (setq x (cdr x))
254 l (setq q (min maxpow (quotient (car x) n)))
255 m (when (equal q 0)
256 (return (if (null x)
258 (cons 0 (cons (psimp k x) ans)))))
259 (setq part (everysubst p (cadr x) q))
260 (setq ans (nconc (everypterms1 part k n (car x)) ans))
261 (setq x (cddr x))
262 (when (null x)
263 (setq q 0)
264 (go m))
265 (go l))))
267 (defun everypterms1 (l k n j)
268 (do ((ptr l (cddr ptr)))
269 ((null ptr) l)
270 (setf (cadr ptr)
271 (ptimes (psimp k (list (- j (* n (car ptr))) 1))
272 (cadr ptr)))))
274 (defun substforsum (a b maxpow)
275 (do ((pow 0 (1+ pow))
276 (quot) (zl-rem) (ans))
277 ((not (< pow maxpow)) (list* maxpow b ans))
278 (desetq (quot zl-rem) (pdivide b a))
279 (unless (and (equal (cdr quot) 1)
280 (not (pzerop (car quot)))
281 (equal (cdr zl-rem) 1))
282 (return (cons pow (cons b ans))))
283 (unless (pzerop (car zl-rem))
284 (setq ans (cons pow (cons (car zl-rem) ans))))
285 (setq b (car quot))))
287 (defun prodcoef (a b)
288 (cond ((pcoefp a)
289 (cond ((pcoefp b) (quotient b a)) (t (prodcoef1 a b))))
290 ((pcoefp b) (pzero))
291 ((pointergp (car a) (car b)) (pzero))
292 ((eq (car a) (car b))
293 (cond ((null (cdddr a))
294 (prodcoef (caddr a) (ptterm (cdr b) (cadr a))))
295 (t (sumcoef a b))))
296 (t (prodcoef1 a b))))
298 (defun sumcoef (a b)
299 (desetq (a b) (pdivide b a))
300 (if (and (equal (cdr a) 1) (equal (cdr b) 1))
301 (car a)
302 (pzero)))
304 (defun prodcoef1 (a b)
305 (loop with ans = (pzero)
306 for (bexp bcoef) on (p-terms b) by #'cddr
307 for part = (prodcoef a bcoef)
308 unless (pzerop part)
309 do (setq ans (pplus ans (psimp (p-var b) (list bexp part))))
310 finally (return ans)))
312 (defun pureprod (x)
313 (or (atom x)
314 (and (not (atom (cdr x)))
315 (null (cdddr x))
316 (pureprod (caddr x)))))
318 (defmfun $bothcoef (r var)
319 (prog (*var h varlist genvar $ratfac)
320 (unless ($ratp r)
321 (return `((mlist)
322 ,(setq h (coeff r var 1.))
323 ((mplus) ,r ((mtimes) -1 ,h ,var)))))
324 (newvar var)
325 (setq h (and varlist (car varlist)))
326 (newvar r)
327 (setq var (cdr (ratrep* var)))
328 (setq r (cdr (ratrep* r)))
329 (and h (setq h (caadr (ratrep* h))))
330 (cond ((and h (or (pcoefp (cdr r)) (pointergp h (cadr r)))
331 (equal 1 (cdr var)))
332 (setq var (bothprodcoef (car var) (car r)))
333 (return (list '(mlist)
334 (rdis* (ratreduce (car var) (cdr r)))
335 (rdis* (ratreduce (cdr var) (cdr r))))))
337 ;; CAN'T TELL WHAT BROUGHT US TO THIS POINT, SORRY
338 (merror (intl:gettext "bothcoef: invalid arguments."))))))
340 ;;COEFF OF A IN B
342 (defun bothprodcoef (a b)
343 (let ((c (prodcoef a b)))
344 (if (pzerop c) (cons (pzero) b) (cons c (pdifference b (ptimes c a))))))
346 (defvar argsfreeofp nil)
348 (defun argsfreeof (var e)
349 (let ((argsfreeofp t)) (freeof var e)))
351 ;;; This is a version of freeof for a list first argument
352 (defmfun $lfreeof (l e) "`freeof' for a list first argument"
353 (unless ($listp l)
354 (merror (intl:gettext "lfreeof: first argument must be a list; found: ~M") l))
355 (let ((exp ($totaldisrep e)))
356 (dolist (var (margs l) t)
357 (unless (freeof ($totaldisrep var) exp) (return nil)))))
359 (defmfun $freeof (&rest args)
360 (prog (l e)
361 (setq l (mapcar #'$totaldisrep (nreverse args))
362 e (car l))
363 loop (or (setq l (cdr l)) (return t))
364 (if (freeof (getopr (car l)) e) (go loop))
365 (return nil)))
367 (defun freeof (var e)
368 (cond ((alike1 var e) nil)
369 ((atom e) t)
370 ((and (not argsfreeofp)
371 (or (alike1 var ($verbify (caar e)))
372 (alike1 var ($nounify (caar e)))))
373 nil)
374 ((and (or (member (caar e) '(%product %sum %laplace) :test #'eq)
375 (and (eq (caar e) '%integrate) (cdddr e))
376 (and (eq (caar e) '%limit) (cddr e)))
377 (alike1 var (caddr e)))
378 (freeofl var (cdddr e)))
379 ((eq (caar e) '%at)
380 (cond ((not (freeofl var (hand-side (caddr e) 'r))) nil)
381 ((not (freeofl var (hand-side (caddr e) 'l))) t)
382 (t (freeof var (cadr e)))))
383 ((and (eq (caar e) 'lambda)
384 (not (member 'array (cdar e) :test #'eq))
385 ($listp (cadr e))
386 ; Check if var appears in the lambda list in any of the
387 ; following ways: var, 'var, [var] or ['var].
388 (some (lambda (v)
389 (or (eq v var)
390 (alike1 v `((mquote) ,var))
391 (alike1 v `((mlist) ,var))
392 (alike1 v `((mlist) ((mquote) ,var)))))
393 (cdadr e)))
395 ;; Check for a local variable in a block.
396 ((and (eq (caar e) 'mprog)
397 ($listp (cadr e))
398 ; Check if var appears in the variable list alone or
399 ; in an assignment
400 (some (lambda (v)
401 (or (eq v var)
402 (and (msetqp v)
403 (eq (cadr v) var))))
404 (cdadr e)))
406 ;; Check for a loop variable.
407 ((and (member (caar e) '(mdo mdoin) :test #'eq)
408 (alike1 var (cadr e)))
410 (argsfreeofp (freeofl var (margs e)))
411 (t (freeofl var (cdr e)))))
413 (defun freeofl (var l) (loop for x in l always (freeof var x)))
415 (defun hand-side (e flag)
416 (setq e (if (eq (caar e) 'mequal) (ncons e) (cdr e)))
417 (mapcar #'(lambda (u) (if (eq flag 'l) (cadr u) (caddr u))) e))
419 ;; subtitle radcan
421 (defmfun $radcan (exp)
422 (cond ((mbagp exp) (cons (car exp) (mapcar '$radcan (cdr exp))))
423 (t (let (($ratsimpexpons t))
424 (simplify (let (($expop 0) ($expon 0))
425 (radcan1 (fr1 exp nil))))))))
427 (defun radcan1 (*exp)
428 (cond ((atom *exp) *exp)
429 (t (let (($factorflag t) varlist genvar $ratfac $norepeat
430 ($gcd (or $gcd (car *gcdl*)))
431 (radcanp t))
432 (newvar *exp)
433 (setq *exp (cdr (ratrep* *exp)))
434 (setq varlist
435 (mapcar
436 #'(lambda (x) (cond
437 ((atom x) x)
438 (t (cons (car x)
439 (mapcar 'radcan1 (cdr x))))))
440 varlist))
441 (spc0)
442 (fr1 (rdis *exp) nil)))))
444 (defun spc0 ()
445 (prog (*v *loglist)
446 (if (allatoms varlist) (return nil))
447 (setq varlist (mapcar #'spc1 varlist)) ;make list of logs
448 (setq *loglist (factorlogs *loglist))
449 (mapc #'spc2 *loglist) ;subst log factorizations
450 (mapc #'spc3 varlist genvar) ;expand exponents
451 (mapc #'spc4 varlist) ;make exponent list
452 (desetq (varlist . genvar) (spc5 *v varlist genvar))
453 ;find expon dependencies
454 (setq varlist (mapcar #'rjfsimp varlist)) ;restore radicals
455 (mapc #'spc7 varlist))) ;simplify radicals
457 (defun allatoms (l)
458 (loop for x in l always (atom x)))
460 (defun rjfsimp (x &aux expon)
461 (cond ((and *radsubst $radsubstflag) x)
462 ((not (m$exp? (setq x (let ($logsimp) (resimplify x))))) x)
463 ((mlogp (setq expon (caddr x))) (cadr expon))
464 ((not (and (mtimesp expon) (or $logsimp *var))) x)
465 (t (do ((rischflag (and *var (not $logsimp) (not (freeof *var x))))
466 (power (cdr expon) (cdr power))) ;POWER IS A PRODUCT
467 ((null power) x)
468 (cond ((numberp (car power)))
469 ((mlogp (car power))
470 (and rischflag (cdr power) (return x))
471 (return
472 `((mexpt) ,(cadar power)
473 ,(muln (remove (car power) (cdr expon) :count 1 :test #'equal)
474 nil))))
475 (rischflag (return x)))))))
477 (defun dsubsta (x y zl)
478 (cond ((null zl) zl)
479 (t (cond ((alike1 y (car zl)) (rplaca zl x))
480 ((not (atom (car zl))) (dsubsta x y (cdar zl))))
481 (dsubsta x y (cdr zl))
482 zl)))
484 (defun radsubst (a b)
485 (setq *exp (allsubst00 a b *exp))
486 (if *radsubst (setq *exp2 (allsubst00 a b *exp2))))
488 (setq *var nil)
490 (defun spc1 (x)
491 (cond ((mlogp x) (putonloglist x))
492 ((and (mexptp x) (not (eq (cadr x) '$%e)))
493 ($exp-form (list '(mtimes)
494 (caddr x)
495 (putonloglist (list '(%log simp ratsimp)
496 (cadr x))))))
497 (t x)))
499 (defun putonloglist (l)
500 (unless (memalike l *loglist) (push l *loglist))
503 (defun spc2 (p)
504 (radsubst (rform (cdr p)) (rform (car p)))
505 (dsubsta (cdr p) (car p) varlist))
507 (defun spc2a (x) ;CONVERTS FACTORED
508 (let ((sum (mapcar #'spc2b x))) ;RFORM LOGAND TO SUM
509 (if (cdr sum) ;OF LOGS
510 (cons '(mplus) sum)
511 (car sum))))
513 (defun spc2b (x)
514 (let ((log `((%log simp ratsimp irreducible) ,(pdis (car x)))))
515 (if (equal 1 (cdr x)) log
516 (list '(mtimes) (cdr x) log))))
518 (defun spc3 (x v &aux y)
519 (when (and (m$exp? x)
520 (not (atom (setq y (caddr x))))
521 (mplusp (setq y (expand1 (if *var ($partfrac y *var) y) 10 10))))
522 (setq y (cons '(mtimes)
523 (mapcar #'(lambda (z) ($ratsimp ($exp-form z))) (cdr y))))
524 (radsubst (rform y) (rget v))
525 (dsubsta y x varlist)))
527 (defun spc4 (x)
528 (if (and (m$exp? x)
529 (not (memalike (caddr x) *v)))
530 (push (caddr x) *v)))
532 (defun rzcontent (r)
533 (destructuring-let (((c1 p) (pcontent (car r)))
534 ((c2 q) (pcontent (cdr r))))
535 (if (pminusp p) (setq p (pminus p) c1 (cminus c1)))
536 (cons (cons c1 c2) (cons p q))))
538 ;;The GCDLIST looks like (( GCM1pair occurrencepair11 occurrencepair12 ...) ...
539 ;;(GCMnpair occurrencepairn1 occurrencepairn2 ...))
540 ;;where GCMpairs are lists of ratforms and prefix forms for the greatest common
541 ;;multiple of the occurrencepairs. Each of these pairs is a list of a ratform
542 ;;and a prefix form. The prefix form is a pointer into the varlist.
543 ;;The occurrences are exponents of the base %E.
545 (defun spc5 (vl oldvarlist oldgenvar &aux gcdlist varlist genvar)
546 (dolist (v vl)
547 (destructuring-let* ((((c1 . c) . r) (rzcontent (rform v)))
548 (g (assoc r gcdlist :test #'equal)))
549 (cond (g (setf (cadr g) (plcm c (cadr g)))
550 (push (list ($exp-form (div* v c1)) c) (cddr g)))
551 (t (push (list r c (list ($exp-form (div* v c1)) c)) gcdlist)))))
552 (dolist (g gcdlist)
553 (let ((rd (rdis (car g))))
554 (when (and (mlogp rd) (memalike (cadr rd) oldvarlist))
555 (push (list (cadr rd) 1) (cddr g)))
556 (rplaca g ($exp-form (div rd (cadr g))))))
557 (spc5b gcdlist oldvarlist oldgenvar))
559 ;;(DEFUN SPC5B (V VARLIST GENVAR)
560 ;; (DOLIST (L V)
561 ;; (DOLIST (X (CDDR L))
562 ;; (UNLESS (EQUAL (CADR L) (CADR X))
563 ;; (RADSUBST (RATEXPT (RFORM (CAR L))
564 ;; (CAR (QUOTIENT (CADR X) (CADR L))))
565 ;; (RFORM (CAR X))))))
566 ;; (CONS VARLIST GENVAR))
569 (defun spc5b (v varlist genvar)
570 (dolist (l v)
571 (dolist (x (cddr l))
572 (unless (equal (cadr l) (cadr x))
573 (radsubst (ratexpt (rform (car l))
574 (quotient (cadr l) (cadr x)))
575 (rform (car x))))))
576 (cons varlist genvar))
578 (defun spc7 (x)
579 (if (eq x '$%i) (setq x '((mexpt) -1 ((rat) 1 2))))
580 (when (and (mexptp x)
581 (ratnump (caddr x)))
582 (let ((rad (rform x))
583 (rbase (rform (cadr x)))
584 (expon (caddr x)))
585 (radsubst (ratexpt rbase (cadr expon))
586 (ratexpt rad (caddr expon))))))
589 (defun goodform (l) ;;bad -> good
590 (loop for (exp coef) on l by #'cddr
591 collect (cons exp coef)))
593 (defun factorlogs (l)
594 (prog (negl posl maxpl maxnl maxn)
595 (dolist (log l)
596 (setq log
597 (cons log (goodform
598 (ratfact (rform (radcan1 (cadr log)))
599 #'pfactor))))
600 (cond ((equal (caadr log) -1) (push log negl))
601 (t (push log posl))))
602 (setq negl (flsort negl) posl (flsort posl) l (append negl posl))
603 (setq negl (mapcar #'cdr negl)
604 posl (mapcar #'cdr posl))
605 a (setq negl (delete '((-1 . 1)) negl :test #'equal))
606 (or negl
607 (return (mapc #'(lambda (x) (rplacd x (spc2a (cdr x)))) l)))
608 (setq maxnl (flmaxl negl)
609 maxn (caaar maxnl))
610 b (setq maxpl (flmaxl posl))
611 (cond ((and maxpl (flgreat (caaar maxpl) maxn))
612 (setq posl (flred posl (caaar maxpl)))
613 (go b))
614 ((and maxpl
615 (not (equal (caaar maxpl) maxn)))
616 (setq maxpl nil)))
617 (cond ((and (flevenp maxpl) (not (flevenp maxnl)))
618 (mapc #'(lambda (fp) (rplaca (car fp) (pminus (caar fp)))
619 (cond ((oddp (cdar fp))
620 (setq fp (delete '(-1 . 1) fp :test #'equal))
621 (setq negl (delete fp negl :test #'equal))
622 (and (cdr fp) (push (cdr fp) posl)))))
623 maxnl)
624 (go a))
625 (t (setq posl (flred posl maxn)
626 negl (flred negl maxn))
627 (go a)))))
629 (defun flevenp (pl)
630 (loop for l in pl never (oddp (cdar l))))
632 (defun flred (pl p)
633 (mapl #'(lambda (x) (if (equal p (caaar x))
634 (rplaca x (cdar x))))
636 (delete nil pl :test #'equal))
638 (defun flmaxl (fpl) ;lists of fac. polys
639 (cond ((null fpl) nil)
640 (t (do ((maxl (list (car fpl))
641 (cond ((equal (caaar maxl) (caaar ll))
642 (cons (car ll) maxl))
643 ((flgreat (caaar maxl) (caaar ll)) maxl)
644 (t (list (car ll)))))
645 (ll (cdr fpl) (cdr ll)))
646 ((null ll) maxl)))))
648 (defun flsort (fpl)
649 (mapc #'(lambda (x) (rplacd x (sort (cdr x) #'flgreat :key #'car)))
650 fpl))
652 (defun nmt (p any)
653 (cond ((pcoefp p)
654 (if (or any (cminusp p)) 1 0))
655 (t (loop for lp on (p-terms p) by #'cddr
656 sum (nmt (cadr lp) any)))))
658 (defun nmterms (p)
659 (cond ((equal p -1) (cons 0 0))
660 (t (cons (nmt p nil) (nmt p t)))))
662 (defun flgreat (p q)
663 (let ((pn (nmterms p)) (qn (nmterms q)))
664 (cond ((> (car pn) (car qn)) t)
665 ((< (car pn) (car qn)) nil)
666 ((> (cdr pn) (cdr qn)) t)
667 ((< (cdr pn) (cdr qn)) nil)
668 (t (flgreat1 p q)))))
670 (defun flgreat1 (p q)
671 (cond ((numberp p)
672 (cond ((numberp q) (> p q))
673 (t nil)))
674 ((numberp q) t)
675 ((pointergp (car p) (car q)) t)
676 ((pointergp (car q) (car p)) nil)
677 ((> (cadr p) (cadr q)) t)
678 ((< (cadr p) (cadr q)) nil)
679 (t (flgreat1 (caddr p) (caddr q)))))