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 1981 Massachusetts Institute of Technology ;;;
9 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
13 (macsyma-module lesfac
)
15 (load-macsyma-macros rzmac ratmac
)
17 (defun getunhack (gen)
18 (or (get gen
'unhacked
) (pget gen
)))
23 (defmacro setcall
(fn a b
&rest args
)
25 (first (setq ,a
(,fn
,a
,b
,@args
)))
30 (let ((qq (testdivide p q
)))
38 (cons (cons (car a
) (cadr a
)) (polyst (caddr a
)))))
40 (defun cdinf (a b both
)
41 (cond ((or (pcoefp a
) (pcoefp b
))
44 (setq a
(ncons (copy-tree a
))
45 b
(ncons (if both
(copy-tree b
) b
)))
46 (values (cd1 a b both
) (car a
) (car b
)))))
49 (cond ((or (pcoefp (car a
)) (pcoefp (car b
))) 1)
50 ((eq (caar a
) (caar b
))
51 (ptimes (pexpt (pget (caar a
)) ;CHECK FOR ALG. TRUNC.
52 (prog1 (cond (both (+ (cadar a
) (cadar b
))) (t (cadar a
)))
54 (cond (both (rplaca b
(caddar b
)))
55 (t (setq b
(cddar b
))))))
57 ((pointergp (caar a
) (caar b
)) (cd1 (cddar a
) b both
))
58 (t (cd1 a
(cddar b
) both
))))
63 ((get (car p
) 'unhacked
)
64 (lmake (caddr p
) (cons (cons (car p
) (cadr p
)) l
)))
66 (setq l
(lmake (caddr p
) l
))
67 (rplaca l
(list (car p
) (cadr p
) (car l
))))))
71 (mapc #'(lambda (x) (rplaca x
(getunhack (car x
)))) (cdr l
))
74 (rplaca l
(cons (car l
) 1))))
81 ((numberp (caar l
)) ;CLAUSE SHOULD BE ELIMINATED ASAP
82 (ptimes (cexpt (caar l
) (cdar l
)) (pmake (cdr l
))))
84 (ptimes (list (caar l
) (cdar l
) 1) (pmake (cdr l
))))))
88 (gcdl nil
)) ;FACTORED PGCDCOFACTS
89 (if (or (pcoefp p
) (pcoefp q
))
90 (values-list (pgcdcofacts p q
))
91 (values (ptimeschk (setcall pgcdcofacts p q
)
92 (car (setq p
(lmake p nil
)
94 gcdl
(mapcar #'pmake
(lgcd1 (cdr p
) (cdr q
))))))
95 (ptimeschk (car p
) (cadr gcdl
))
96 (ptimeschk (car q
) (caddr gcdl
))))))
98 (defun facmgcd (pl) ;GCD OF POLY LIST FOR EZGCD WITH RATFAC
99 (do ((l (cdr pl
) (cdr l
))
102 ((null l
) (cons gcd
(nreverse ans
)))
103 (multiple-value-bind (g x y
) (fpgcdco gcd
(car l
))
105 (return (cons 1 pl
)))
109 (do ((l2 ans
(cdr l2
)))
111 (rplaca l2
(ptimes x
(car l2
))))))
115 ;;; NOTE: ITEMS ON VARLIST ARE POS. NORMAL
116 ;;; INTEGER COEF GCD=1 AND LEADCOEF. IS POS.
119 (prog (ptlist g bj c t1 d1 d2 dummy
)
120 (setq ptlist
(mapcar #'(lambda (ig) (declare (ignore ig
)) b
) a
))
122 (ptlist ptlist
(cdr ptlist
)))
124 (do ((ai (getunhack (caar a
)))
125 (b (car ptlist
) (cdr b
)))
127 (and (zerop (cdar b
)) (go nextb
))
129 (setq bj
(getunhack (caar b
)))
130 (setq c
(cond ((pirredp (caar a
))
131 (if (pirredp (caar b
))
133 (multiple-value-setq (dummy bj ai
) (pquocof bj ai
))))
135 (multiple-value-setq (dummy ai bj
) (pquocof ai bj
)))
137 (setcall pgcdcofacts ai bj
))))
138 (cond ((equal c
1) (go nextb
))
139 ((equal ai
1) (go bloop
)))
141 (when (setq t1
(testdivide ai c
))
152 (setq bj t1 d2
(1+ d2
))))
153 (setq g
(cons (cons (makprodg c t
)
154 (min (setq d1
(* d1
(cdar a
)))
155 (setq d2
(* d2
(cdar b
)))))
157 (cond ((> d1
(cdar g
))
159 (ncons (cons (caar g
) (- d1
(cdar g
)))))
160 (rplacd (last ptlist
) (ncons (cdr b
)))))
161 (cond ((> d2
(cdar g
))
163 (ncons (cons (caar g
) (- d2
(cdar g
)))))))
164 (rplaca (car a
) (makprodg ai t
))
165 (rplaca (car b
) (makprodg bj t
))
166 (and (equal bj
1) (rplacd (car b
) 0))
167 (and (equal ai
1) (rplacd (car a
) 0) (return nil
))
169 (return (list g a b
))))
171 (defun makprodg (p sw
)
174 (car (makprod p sw
))))
176 (defun dopgcdcofacts (x y
)
179 (unless (member $gcd
*gcdl
* :test
#'eq
)
181 (values-list (pgcdcofacts x y
))))
183 (defun facrplus (x y
)
189 (multiple-value-setq (x a c
) (dopgcdcofacts a c
))
190 (multiple-value-setq (y b d
) (fpgcdco b d
))
191 (setq a
(makprod (pplus (pflatten (ptimeschk a d
))
192 (pflatten (ptimeschk b c
))) nil
))
193 (setq b
(ptimeschk b d
))
195 (setq y
(ptimeschk y b
))
196 (multiple-value-setq (dummy y a
) (fpgcdco y a
)) ;for unexpected gcd
197 (cons (ptimes x a
) y
))
199 (multiple-value-setq (c y b
) (cdinf y b nil
))
200 (multiple-value-setq (dummy y a
) (fpgcdco y a
))
201 (cons (ptimes x a
) (ptimeschk y
(ptimeschk c b
)))))))
204 (let (($gcd
(or $gcd
'$ez
))
207 (setq g
(oldcontent2 (sort (copy-list l
) 'contodr
) 0))
209 ((do ((a (pflatten (pquotient (car l
) g
))
210 (pplus a
(pflatten (pquotient (car ll
) g
))))
211 (ll (cdr l
) (cdr ll
)))
212 ((null ll
) (ptimes g
(makprod a nil
))))))))
214 (defun facrtimes (x y gcdsw
)
216 (cons (ptimes (car x
) (car y
)) (ptimeschk (cdr x
) (cdr y
)))
218 (multiple-value-bind (g1 g2 g3
) (cdinf (car x
) (car y
) t
)
219 (multiple-value-bind (h1 h2 h3
) (cdinf (cdr x
) (cdr y
) t
)
220 (multiple-value-bind (x1 x2 x3
) (fpgcdco g2 h3
)
221 (declare (ignore x1
))
222 (multiple-value-bind (y1 y2 y3
) (fpgcdco g3 h2
)
223 (declare (ignore y1
))
224 (cons (ptimes g1
(ptimes x2 y2
))
225 (ptimeschk h1
(ptimeschk x3 y3
)))))))))
227 (defun pfacprod (poly) ;FOR RAT3D
230 (nconc (pfacprod (caddr poly
)) (list (pget (car poly
)) (cadr poly
)))))
232 (defun fpcontent (poly)
233 (let (($ratfac nil
)) ;algebraic uses
234 (setq poly
(oldcontent poly
)) ;rattimes?
235 (let ((a (lowdeg (cdadr poly
)))) ;main var. content
237 (setq a
(list (caadr poly
) a
1))
238 (setq poly
(list (ptimes (car poly
) a
)
239 (pquotient (cadr poly
) a
)))))
240 (if (pminusp (cadr poly
))
241 (list (pminus (car poly
)) (pminus (cadr poly
)))
244 ;; LOWDEG written to compute the lowest degree of a polynomial. - RZ
248 ((null (cddr l
)) (car l
))))
250 (defun makprod (poly contswitch
)
251 (cond ((pureprod poly
) poly
)
253 (ptimes (list (car poly
) (cadr poly
) 1)
254 (makprod (caddr poly
) contswitch
)))
257 (t (setq poly
(fpcontent poly
))
258 (ptimes (makprod (car poly
) contswitch
) (makprod1 (cadr poly
))))))
260 (defun makprod1 (poly)
261 (do ((v varlist
(cdr v
))
264 ((null v
) (maksymp poly
))
265 (and (alike1 p
(car v
)) (return (pget (car g
))))))
270 (putprop g e
'disrep
)
271 (valput g
(1- (valget (car genvar
))))
274 (putprop g p
'unhacked
)
283 (do ((m (listovars h
) (cdr m
)))
284 ((null m
) (return-from pflatten h
))
285 (unless (let ((p (getunhack (car m
))))
286 (or (null p
) (eq (car m
) (car p
))))
287 (return-from pflatten
(let (($ratfac nil
)) (pflat1 h
))))))
292 (ptimes (pexpt (getunhack (car p
)) (cadr p
)) (pflat1 (caddr p
))))
293 (t (do ((val (getunhack (car p
)))
294 (ld (cadr p
) (car a
))
295 (a (cdddr p
) (cddr a
))
296 (ans (pflat1 (caddr p
))))
297 ((null a
) (ptimes ans
(pexpt val ld
)))
298 (setq ans
(pplus (ptimes ans
(pexpt val
(- ld
(car a
))))
299 (pflat1 (cadr a
))))))))
302 (and (setq x
(get x
'disrep
))
303 (or (atom x
) (member 'irreducible
(cdar x
) :test
#'eq
))))