Forgot to load lapack in a few examples
[maxima.git] / share / contrib / format / coeflist.lisp
blob30d2deefccf8fac355379998ad85820c6829a1d9
1 ;;; -*- Mode: LISP; Syntax: Common-lisp; Package: CLIMAX; Base: 10 -*-
2 ;;;>******************************************************************************************
3 ;;;> Software developed by Bruce R. Miller
4 ;;;> using Symbolics Common Lisp (system 425.111, ivory revision 4)
5 ;;;> at NIST - Computing and Applied Mathematics Laboratory
6 ;;;> a part of the U.S. Government; it is therefore not subject to copyright.
7 ;;;>******************************************************************************************
9 ;(in-package 'climax)
11 ;;; To run in Schelter's Maxima comment the above and uncomment these:
12 (in-package :maxima)
13 (defmacro mexp-lookup (item alist) `(assolike ,item ,alist))
14 (defmacro mlist* (arg1 &rest more-args) `(list* '(mlist simp) ,arg1 ,@more-args))
16 ;;;;******************************************************************************************
17 ;;;; Needed, but unrelated, stuff. Possibly useful in its own right
18 ;;;;******************************************************************************************
19 ;;; Returns an mlist of all subexpressions of expr which `match' predicate.
20 ;;; Predicate(expr,args,...) returns non-nil if the expression matches.
21 ;;; [eg. a function constructed by $defmatch]
22 (defun $matching_parts (expr predicate &rest args)
23 (let ((matches nil))
24 (labels ((srch (expr)
25 (when (specrepp expr)
26 (setq expr (specdisrep expr)))
27 (when (apply #'mfuncall predicate expr args)
28 (pushnew expr matches :test #'alike1))
29 (unless (atom expr)
30 (mapc #'srch (cdr expr)))))
31 (srch expr)
32 (mlist* matches))))
34 ;;; Return an mlist of all unique function calls of the function(s) FCNS in EXPR.
35 ;;; (FCNS can also be a single function)
36 (defun $function_calls (expr &rest functions)
37 ;; Coerce fcns to a list of function names.
38 (let ((fcns (mapcar #'(lambda (x)(or (get (setq x (getopr x)) 'verb) x)) functions)))
39 ($matching_parts expr #'(lambda (p)(and (listp p)(member (caar p) fcns))))))
41 ;;; Return an mlist of all unique arguments used by FCNS in EXPR.
42 (defun $function_arguments (expr &rest functions)
43 (mlist* (remove-duplicates (cdr (map1 #'$args (apply #'$function_calls expr functions)))
44 :test #'alike1)))
46 ;;; $totaldisrep only `disrep's CRE (mrat), but not POIS!
47 (defun totalspecdisrep (expr)
48 (cond ((atom expr) expr)
49 ((not (or (among 'mrat expr)(among 'mpois expr))) expr)
50 ((eq (caar expr) 'mrat)(ratdisrep expr))
51 ((eq (caar expr) 'mpois) ($outofpois expr))
52 (t (cons (remove 'ratsimp (car expr))(mapcar 'totalspecdisrep (cdr expr))))))
54 ;;;;******************************************************************************************
55 ;;;; Variable Lists
56 ;;; A variable list consists of a list of variables, simple expressions and specs like
57 ;;; OPERATOR(fcn) or OPERATOR(fcn,...) represents ALL calls to fcn in the expression.
58 ;;; MATCH(fcn,arg..) represents subexpressions of expression which pass FCN(subexpr,args..)
59 ;;; Instantiating the variable list involves replacing those special cases with those
60 ;;; subexpressions of the relevant expression which pass the test.
61 ;;;;******************************************************************************************
63 (defun instanciate-variable-list (vars expr caller &optional max-vars)
64 (let ((ivars (mapcan #'(lambda (var)
65 (setq var (totalspecdisrep var))
66 (case (and (listp var)(caar var))
67 ($operator (cdr (apply #'$function_calls expr (cdr var))))
68 ($match (cdr (apply #'$matching_parts expr (cdr var))))
69 (t (list var))))
70 vars)))
71 (when (and max-vars (> (length ivars) max-vars))
72 (merror "Too many variables for ~M: ~M" caller (mlist* ivars)))
73 ivars))
75 ;;;;******************************************************************************************
76 ;;;; Helpers
77 ;;;;******************************************************************************************
79 ;;; Similar to lisp:reduce with the :key keyword.
80 ;;; Apparently, the Lisp underneath the Sun version doesn't support it. Ugh.
81 ; (defmacro cl-reduce (function list key) `(lisp:reduce ,function ,list :key ,key))
83 (defun cl-reduce (function list key)
84 (if (null list) nil
85 (let ((result (funcall key (car list))))
86 (dolist (item (cdr list))
87 (setq result (funcall function result (funcall key item))))
88 result)))
90 (defun map-mlist (list) (mapcar #'(lambda (e)(mlist* e)) list))
92 ;;;******************************************************************************************
93 ;;; Coefficient List = Pseudo-polynomial : as list of ( (coefficient power(s) ...) ...)
94 ;;; coefficient: the coefficient of the monomial (anything algebraic thing)
95 ;;; power(s) : the power(s) of the variable(s) in the monomial (any algebraic thing)
96 ;;; Pairs are sorted into increasing order of the power(s).
97 ;;; 0 is represented by NIL.
98 ;;;******************************************************************************************
99 ;;; NOTE on ordering of terms. The Macsyma predicate GREAT (& friends lessthan, etc)
100 ;;; define a total ordering, but if non-numeric elements are allowed, the ordering is not
101 ;;; robust under addition, eg L=[1,2,m] is in order, but L+m=[m,m+2,2*m] is not.
102 ;;; We define the ordering of A & B by determining the `sign' of A-B, where the sign is
103 ;;; the sign of the coefficient of the leading (highest degree) term. We can use SIGNUM1
104 ;;; for this.
105 ;;;******************************************************************************************
107 ;;;******************************************************************************************
108 ;;; CLIST Arithmetic
110 ;;; Add two CLISTs
111 (defun clist-add (l1 l2)
112 (do ((result nil))
113 ((not (and l1 l2)) (if (or l1 l2)(nconc (nreverse result)(or l1 l2)) (nreverse result)))
114 (do ((p1 (cdar l1) (cdr p1))
115 (p2 (cdar l2) (cdr p2)))
116 ((or (null p1) (not (alike1 (car p1)(car p2))))
117 (if p1
118 (push (if (plusp (signum1 (sub (car p1)(car p2))))(pop l2)(pop l1)) result)
119 (let ((c3 (add (caar l1)(caar l2)))) ;If power is same, combine
120 (unless (zerop1 c3) ;And accumulate result, unless zero.
121 (push (cons c3 (cdar l1)) result))
122 (pop l1)(pop l2)))))))
124 ;;; Multiply two CLISTs
125 ;;; Optional ORDER is for use by series arithmetic (single variable): truncates powers>order
126 (defun clist-mul (l1 l2 &optional order)
127 (when (and l1 l2)
128 (when (> (length l1)(length l2)) ; make l1 be shortest
129 (psetq l1 l2 l2 l1))
130 (let ((rl2 (reverse l2)))
131 (flet ((mul1 (pair1)
132 (let ((c1 (car pair1)) (p1 (cdr pair1)) result)
133 (dolist (i2 rl2)
134 (let ((p (mapcar #'add p1 (cdr i2))))
135 (unless (and order (great (car p) order))
136 (push (cons (mul c1 (car i2)) p) result))))
137 result)))
138 (cl-reduce #'clist-add l1 #'mul1)))))
140 ;;; Take the Nth power of a CLIST, using "binary expansion of the exponent" method.
141 ;;; Built-in code to handle P^2, instead of P*P.
142 (defun clist-pow (l n) ; Assumes n>0
143 (cond ((null l) nil) ; l=0 -> 0 (nil)
144 ((null (cdr l)) ; single term, trivial
145 `((,(power (caar l) n) ,@(mapcar #'(lambda (p)($expand (mul p n)))(cdar l)))))
146 (t (let ((l^i l) (l^n (if (logtest n 1) l)))
147 (do ((bits (ash n -1)(ash bits -1)))
148 ((zerop bits) l^n)
149 (do ((sq nil) ; Square l^i
150 (ll (reverse l^i) (cdr ll)))
151 ((null ll) (setq l^i sq))
152 (let* ((c1 (caar ll)) (2c1 (mul 2 c1))(p1 (cdar ll))
153 (psq (list (cons (power c1 2)(mapcar #'add p1 p1)))))
154 (dolist (lll (cdr ll))
155 (push (cons (mul 2c1 (car lll))(mapcar #'add p1 (cdr lll))) psq))
156 (setq sq (if sq (clist-add sq psq) psq))))
157 (if (logtest bits 1) (setq l^n (if l^n (clist-mul l^n l^i) l^i))))))))
159 ;;; An MBAG includes lists, arrays and equations.
160 ;;; Given the list of {list|array|equation} elements which have been converted to CLIST's,
161 ;;; this function combines them into a single clist whose coefficients
162 ;;; are {list|array|equation}s
164 (defun clist-mbag (op clists)
165 (let ((z (if (eq op '$matrix) ; the `zero' of a matrix is an mlist of 0's!!!
166 (mlist* (make-list (length (cdaaar clists)) :initial-element 0))
167 0)))
168 (flet ((keylessp (l1 l2) ; does key l1 precede l2?
169 (do ((l1 l1 (cdr l1))
170 (l2 l2 (cdr l2)))
171 ((or (null l1)(not (alike1 (car l1)(car l2))))
172 (and l1 (minusp (signum1 (sub (car l1)(car l2)))))))))
173 (mapcar #'(lambda (p)
174 `(((,op)
175 ,@(mapcar #'(lambda (l)(or (car (rassoc p l :test #'alike)) z)) clists))
176 ,@p))
177 (sort (cl-reduce #'union1 clists #'(lambda (e)(mapcar #'cdr e))) #'keylessp)))))
179 ;;;;******************************************************************************************
180 ;;;; Transform an expression into its polynomial coefficient list form.
182 (defun $coeffs (expr &rest vars)
183 (setq expr (totalspecdisrep expr))
184 (let* ((vs (instanciate-variable-list vars expr '$coeffs))
185 (zeros (make-list (length vs) :initial-element 0))
186 (cache nil))
187 (dolist (v vs) ; preload the cache w/ encoded variables
188 (let ((u (copy-list zeros)))
189 (setf (nth (position v vs) u) 1)
190 (push (cons v (list (cons 1 u))) cache)))
191 (labels ((gcf (expr) ; Get coefficients.
192 (or (mexp-lookup expr cache) ; reuse cached value
193 (cdar (push (cons expr (gcf1 expr)) cache)))) ; or compute & store
194 (gcf1 (expr)
195 (let ((op (and (listp expr)(caar expr))) x y)
196 (cond ((mbagp expr) (clist-mbag op (mapcar #'gcf (cdr expr))))
197 ((or (null op)(not (dependsall expr vs))) `((,expr . ,zeros)))
198 ((eq op 'mplus) (cl-reduce #'clist-add (cdr expr) #'gcf))
199 ((eq op 'mtimes) (cl-reduce #'clist-mul (cdr expr) #'gcf))
200 ((and (eq op 'mexpt) ; Check that we can actually compute X^Y:
201 (setq x (gcf (second expr)) y (third expr))
202 (or (and (integerp y)(plusp y)) ; Either integer y > 0
203 (and (null (cdr x)) ; or x is a single monomial
204 (not (dependsall y vs)) ; w/ y must be free of vars
205 (or (eql $radexpand '$all) ; & dont care about cuts
206 (integerp y) ; or y is an integer
207 (every #'(lambda (p)(or (zerop1 p)(onep p)))
208 (cdar x)))))) ; or x is linear in vars.
209 (clist-pow x y)) ; OK, so we CAN compute x^y (whew).
210 (t `((,expr . ,zeros)))))))
211 (mlist* (mlist* '$%poly vs)(map-mlist (gcf expr))))))
213 ; Inverse of above: make an expression out of clist.
214 ;;; Actually works for SERIES & Taylor too.
215 (defun unclist (clist vars)
216 (addn (mapcar #'(lambda (e)(mul (cadr e)(muln (mapcar #'power vars (cddr e)) t))) clist) t))
218 ;;;********************************************************************************
219 ;;; TRIG SERIES
220 ;;; Given an expression and a list of variables, v_i, construct the list of sine & cosine
221 ;;; coefficients & multiples of the variables in the expression:
222 ;;; [[%trig, v_1, ...] sine_list, cosine_list]
223 ;;; sine_list: [[c,m_1,...],[c',m_1',...]....]
224 ;;; cosine_list: " "
225 ;;; This version carries out `trig arithmetic' on coefficient lists (does NOT use the
226 ;;; enhanced poisson package (pois2m)
227 ;;;********************************************************************************
229 ;;;;******************************************************************************************
230 ;;;; TLIST Arithmetic.
232 ;;; Is L1 < (0 0 0 ...)? ie. is first non-zero element negative?
233 (defun list-negp (l1)
234 (dolist (i1 l1)
235 (unless (zerop1 i1)
236 (return-from list-negp (eql -1 (signum1 i1))))))
238 (defun tlist-add (l1 l2) (mapcar #'clist-add l1 l2))
240 ; multiply a cos or sin list by another. c1p is T if L1 is cosine list, c2p ditto.
241 (defun tlist-mul1 (l1 l2 c1p c2p)
242 (when (> (length l2)(length l1)) ; Swap so that L2 is the shortest.
243 (psetq l1 l2 l2 l1)
244 (psetq c1p c2p c2p c1p))
245 (when l2
246 (let ((s1 (if (and c1p (not c2p)) -1 +1)) ; cos * sines -> -1, else +1
247 (s2 (if (or c1p c2p) +1 -1)) ; either are cosines -> +1 else -1
248 (s3 (if (xor c1p c2p) -1 +1)) ; result is sines -> -1 else +1
249 (rl1 (reverse l1)))
250 (flet ((mul1 (pr2)
251 (let ((c2 (car pr2))(m2 (cdr pr2)))
252 (if (every #'zerop1 m2)
253 (when c2p
254 (mapcar #'(lambda (pr) (cons (mul c2 (car pr))(cdr pr))) l1))
255 (let ((t1 nil)(t2 nil)(t3 nil))
256 (dolist (i1 rl1)
257 (let* ((c1 (car i1))(m1 (cdr i1))
258 (cc (div (mul c1 c2) 2)))
259 (push (cons (mul s1 cc)(mapcar #'sub m1 m2)) t1)
260 (push (cons (mul s2 cc)(mapcar #'add m1 m2)) t2)))
261 (do () ((not (and t1 (list-negp (cdar t1)))))
262 (push (cons (mul s3 (caar t1))(mapcar #'neg (cdar t1))) t3)
263 (pop t1))
264 (when (and (minusp s3)(every #'zerop1 (cdar t1))) ; sin(0) ?
265 (pop t1)) ; remove it.
266 (clist-add (clist-add t1 t3) t2))))))
267 (cl-reduce #'clist-add l2 #'mul1)))))
269 (defun tlist-mul (l1 l2)
270 (let ((sin1 (first l1))(cos1 (second l1))
271 (sin2 (first l2))(cos2 (second l2)))
272 (list (clist-add (tlist-mul1 cos1 sin2 t nil)
273 (tlist-mul1 sin1 cos2 nil t))
274 (clist-add (tlist-mul1 cos1 cos2 t t)
275 (tlist-mul1 sin1 sin2 nil nil)))))
277 (defun tlist-pow (l n zeros)
278 (let ((sin (first l))(cos (second l)))
279 (flet ((pow1 (coef m sinp) ; single {cos or sin}^n: use explicit formula
280 (let* ((s (if sinp -1 +1)) ; by this point, n better be a fixnum!
281 (c (mul (if (and sinp (oddp (floor n 2))) -1 +1)
282 (div (power coef n) (power 2 (1- n)))))
283 (pow (do ((result nil)
284 (k 0)(kk n (- kk 2)))
285 ((not (plusp kk)) result)
286 (push (cons c (mapcar #'(lambda (mm)(mul kk mm)) m)) result)
287 (setq c (mul c (div (* s (- n k))(setq k (+ k 1))))))))
288 (cond ((evenp n)(list nil (cons (cons (div c 2) zeros) pow)))
289 (sinp (list pow nil))
290 (t (list nil pow))))))
291 (cond ((and (null cos)(null sin)) (list nil nil)) ; 0^n
292 ((zerop1 n) `( nil ((1 ,@ zeros))))
293 ((and (null (cdr cos))(null sin)) (pow1 (caar cos)(cdar cos) nil)) ; cos^n
294 ((and (null (cdr sin))(null cos)) (pow1 (caar sin)(cdar sin) t)) ; sin^n
295 (t (let (l^i l^n) ; Compute using "binary expansion" method.
296 (do ((bits n (ash bits -1)))
297 ((zerop bits) l^n)
298 (setq l^i (if l^i (tlist-mul l^i l^i) l))
299 (when (oddp bits)
300 (setq l^n (if l^n (tlist-mul l^n l^i) l^i))))))))))
302 ;;;;******************************************************************************************
303 ;;;; Extracting Trigonometric sum coefficients.
305 ;;; Encode a call to %sin or %cos
306 (defun encode-tlist (expr vs)
307 (let* ((arg ($expand (cadr expr))) ; trig(arg)
308 (m (mapcar #'(lambda (v)
309 (let ((mm ($coeff arg v)))
310 (setq arg (sub arg (mul mm v)))
311 mm))
312 vs))
313 (sign +1))
314 (when (list-negp m) ; Make sure multiples are normalized
315 (setq m (mapcar #'neg m) sign -1))
316 (if (eql (caar expr) '%cos)
317 (if (zerop1 arg) `(()((1 . ,m)))
318 `(((,(mul (- sign) ($sin arg)) . ,m)) ((,($cos arg) . ,m))))
319 (if (zerop1 arg) `(((,sign . ,m))())
320 `(((,(mul sign ($cos arg)) . ,m)) ((,($sin arg) . ,m)))))))
322 ;;; Transform an expression into its trigonometric coefficient list form.
323 (defun $trig_coeffs (expr &rest vars)
324 (setq expr (totalspecdisrep expr))
325 (let* ((vars (instanciate-variable-list vars expr '$alt_trig_coeffs))
326 (zeros (make-list (length vars) :initial-element 0))
327 (cache nil))
328 (labels ((gcf (expr)
329 (or (mexp-lookup expr cache)
330 (cdar (push (cons expr (gcf1 expr)) cache))))
331 (gcf1 (expr)
332 (let ((op (and (listp expr)(caar expr))) x y)
333 (cond ((mbagp expr) (let ((elements (mapcar #'gcf (cdr expr))))
334 (list (clist-mbag op (mapcar #'car elements))
335 (clist-mbag op (mapcar #'cadr elements)))))
336 ((or (null op)(not (dependsall expr vars))) `(()((,expr . ,zeros))))
337 ((member op '(%sin %cos) :test #'eq) (encode-tlist expr vars))
338 ((eq op 'mplus) (cl-reduce #'tlist-add (cdr expr) #'gcf))
339 ((eq op 'mtimes) (cl-reduce #'tlist-mul (cdr expr) #'gcf))
340 ((and (eq op 'mexpt) ; x^y Check that we can actually compute:
341 (setq x (gcf (second expr)) y (third expr))
342 (and (integerp y)(plusp y))) ; need int y >0
343 (tlist-pow x y zeros))
344 (t `(()((,expr . ,zeros))))))))
345 (mlist* (mlist* '$%trig vars)(map-mlist (mapcar #'map-mlist (gcf expr)))))))
347 (defun untlist (tlist vars)
348 (flet ((un1 (list trig)
349 (flet ((un2 (e)(mul (cadr e)(ftake* trig (multl (cddr e) vars)))))
350 (addn (mapcar #'un2 list) t))))
351 (addn (mapcar #'un1 tlist '(%sin %cos)) t)))
353 ;;;********************************************************************************
354 ;;; SERIES & TAYLOR
355 ;;; Given an expression, a variable and an order, compute the coefficients of the
356 ;;; expansion of the expression about variable=0 to order ORDER.
357 ;;; -> [[%series,variable,order],[c,p],[c',p'],...]
358 ;;; or [[%taylor,variable,order],[c,p],[c',p'],...]
359 ;;; The difference is that TAYLOR computes the Taylor expansion, whereas
360 ;;; SERIES only carries out the expansion over arithmetic functions (+,*,exp) and thus
361 ;;; is significantly faster.
362 ;;;********************************************************************************
364 (defun $taylor_coeffs (expr var order)
365 (setq expr (totalspecdisrep expr))
366 (let ((var (car (instanciate-variable-list (list var) expr '$taylor_coeffs 1))))
367 (labels ((make1 (expr)
368 (cond ((mbagp expr) (clist-mbag (caar expr) (mapcar #'make1 (cdr expr))))
369 ((freeof var expr) (list (list expr 0)))
370 (t (let* ((r ($taylor expr var 0 order))
371 (ohdr (car r))
372 (hdr (list (first ohdr)(second ohdr)(third ohdr)(fourth ohdr))))
373 (if (eq (second r) 'ps)
374 (mapcar #'(lambda (p)
375 (list (specdisrep (cons hdr (cdr p)))
376 (ftake* 'rat (caar p)(cdar p))))
377 (cddddr r))
378 (list (list (specdisrep (cons hdr (cdr r))) 0))))))))
379 (mlist* (mlist* '$%taylor var order nil)(map-mlist (make1 expr))))))
381 ;;;;******************************************************************************************
382 ;;;; SLIST Arithmetic.
383 ;;; The addition & multiplication of polynomial arithmetic are used.
385 ;;; compute the N-th power of S through ORDER.
386 (defun slist-pow (s n order)
387 (when s
388 (let* ((m (cadar s))
389 (nm (mul n m))
390 (s_m (caar s))
391 (p (list (list (power s_m n) nm)))) ; 1st term of result
392 (if (null (cdr s)) ; Single term
393 (or (great nm order) p) ; then trivial single term (unless high order)
394 (let* ((g (cl-reduce #'$gcd s #'(lambda (x)(sub (cadr x) m))))
395 (kmax (div (sub order nm) g)))
396 (do ((k 1 (1+ k)))
397 ((great k kmax) (nreverse p))
398 (let ((ff (div (add 1 n) k))
399 (trms nil))
400 (dolist (s (cdr s))
401 (let ((i (div (sub (cadr s) m) g)))
402 (when (lessthan k i)(return))
403 (let ((e (member (add nm (mul (sub k i) g)) p :key #'cadr :test #'like)))
404 (when e ; multthru limits expression depth
405 (push ($multthru (mul (sub (mul i ff) 1) (car s) (caar e))) trms)))))
406 (let ((pk ($multthru (div (addn trms t) s_m))))
407 (unless (zerop1 pk)
408 (push (list pk (add nm (mul k g))) p))))))))))
410 ;;;;******************************************************************************************
411 ;;;; Extracting Series Coefficients.
413 (defun $series_coeffs (expr var order)
414 (setq expr (totalspecdisrep expr))
415 (let ((v (car (instanciate-variable-list (list var) expr '$series_coeffs 1))))
416 (setq v ($ratdisrep v))
417 (labels ((mino (expr) ; find minimum power of v in expr (for mult)
418 (let ((op (and (listp expr)(caar expr))))
419 (cond ((like expr v) 1) ; Trivial case: expr is V itself
420 ((or ($atom expr)(freeof v expr)) 0) ; `constant' case
421 ((member op '(mplus mlist mequal $matrix))
422 (cl-reduce #'(lambda (u v) ($lmin `((mlist simp) ,u ,v))) (cdr expr) #'mino))
423 ((eq op 'mtimes) (cl-reduce #'add (cdr expr) #'mino))
424 ((and (eq op 'mexpt)($numberp (third expr))) ; can we compute?
425 (mul (mino (second expr)) (third expr)))
426 (t 0)))) ; oh, well, Treat it as constant.
427 (gcf (expr order)
428 (let ((op (and (listp expr)(caar expr))))
429 (cond ((like expr v) `((1 1))) ; Trivial case: expr is V itself
430 ((or ($atom expr)(freeof v expr)) `((,expr 0))) ; `constant' case
431 ((mbagp expr)
432 (clist-mbag op (mapcar #'(lambda (el)(gcf el order))(cdr expr))))
433 ((eq op 'mplus)
434 (cl-reduce #'clist-add (cdr expr) #'(lambda (el)(gcf el order))))
435 ((eq op 'mtimes)
436 (let* ((ms (mapcar #'mino (cdr expr)))
437 (mtot (addn ms t))
438 (prod '((1 0))))
439 (unless (great mtot order)
440 (do ((terms (cdr expr)(cdr terms))
441 (m ms (cdr m)))
442 ((null terms) prod)
443 (let ((term (gcf (car terms) (sub (add order (car m)) mtot))))
444 (setq prod (clist-mul term prod order)))))))
445 ((and (eq op 'mexpt)($numberp (third expr))) ; can we compute?
446 (slist-pow (gcf (second expr) order)(third expr) order))
447 (t `((,expr 0))))))) ; just treat it as constant.
448 (mlist* (mlist* '$%series v order nil)(map-mlist (gcf expr order))))))
450 (defun unslist (clist vars)
451 ($trunc (unclist clist vars)))
453 ;;;********************************************************************************
454 ;;; Find the coefficient associated with keys (powers or multiples) in the
455 ;;; coefficient list clist.
456 (defun $get_coef (clist &rest keys)
457 (let ((sublist (case (and ($listp clist)($listp (cadr clist))(cadr (cadr clist)))
458 (($%poly $%series $%taylor) (cddr clist))
459 ($%trig (case (car keys)
460 (($sin %sin) (cdr (third clist)))
461 (($cos %cos) (cdr (fourth clist)))
462 (otherwise (merror "First KEY must be SIN or COS"))))
463 (otherwise (merror "Unknown coefficient list type: ~M" clist)))))
464 (or (cadar (member keys sublist :test #'alike :key #'cddr)) 0)))
466 ;;; Reconstruct a macsyma expression from a coefficient list.
467 (defun $uncoef (cl)
468 (let ((spec (and ($listp cl)(second cl))))
469 (case (and ($listp spec)(second spec))
470 ($%poly (unclist (cddr cl) (cddr spec)))
471 (($%series $%taylor) (unslist (cddr cl) (cddr spec)))
472 ($%trig (untlist (mapcar #'cdr (cddr cl)) (cddr spec)))
473 (otherwise (merror "UNCOEF: Unrecognized COEFFS form: ~M" cl)))))
475 ;;;********************************************************************************
476 ;;; Partition a polynomial, trig series or series into those terms whose
477 ;;; powers (or multiples) pass a certain test, and those who dont.
478 ;;; Returns the pair [passed, failed].
479 ;;; The TEST is applied to the exponents or multiples of each term.
481 (defun partition-clist (list test)
482 (cond ((null test) (values nil list))
483 ((eq test t) (values list nil))
484 (t (let ((pass nil)(fail nil))
485 (dolist (item list)
486 (if ($is-boole-eval (mapply test (cddr item) '$partition_test))
487 (push item pass)
488 (push item fail)))
489 (values (nreverse pass)(nreverse fail))))))
491 (defun $partition_poly (expr test &rest vars)
492 (let* ((clist (apply #'$coeffs expr vars))
493 (vars (cddr (second clist))))
494 (multiple-value-bind (p f)(partition-clist (cddr clist) test)
495 (mlist* (unclist p vars)(unclist f vars) nil))))
497 (defun $partition_trig (expr sintest costest &rest vars)
498 (let* ((tlist (apply #'$trig_coeffs expr vars))
499 (vars (cddr (second tlist))))
500 (multiple-value-bind (sp sf)(partition-clist (cdr (third tlist)) sintest)
501 (multiple-value-bind (cp cf)(partition-clist (cdr (fourth tlist)) costest)
502 (mlist* (untlist (list sp cp) vars) (untlist (list sf cf) vars) nil)))))
504 (defun $partition_series (expr test var order)
505 (let* ((clist ($series_coeffs expr var order))
506 (var (caddr (second clist))))
507 (multiple-value-bind (p f)(partition-clist (cddr clist) test)
508 (mlist* (unslist p var)(unslist f var) nil))))
510 (defun $partition_taylor (expr test var order)
511 (let* ((clist ($taylor_coeffs expr var order))
512 (var (caddr (second clist))))
513 (multiple-value-bind (p f)(partition-clist (cddr clist) test)
514 (mlist* (unslist p var)(unslist f var) nil))))