1 ;; Calculator for GNU Emacs, part II [calc-poly.el]
2 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc.
3 ;; Written by Dave Gillespie, daveg@synaptics.com.
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is distributed in the hope that it will be useful,
8 ;; but WITHOUT ANY WARRANTY. No author or distributor
9 ;; accepts responsibility to anyone for the consequences of using it
10 ;; or for whether it serves any particular purpose or works at all,
11 ;; unless he says so in writing. Refer to the GNU Emacs General Public
12 ;; License for full details.
14 ;; Everyone is granted permission to copy, modify and redistribute
15 ;; GNU Emacs, but only under the conditions described in the
16 ;; GNU Emacs General Public License. A copy of this license is
17 ;; supposed to have been given to you along with GNU Emacs so you
18 ;; can know your rights and responsibilities. It should be in a
19 ;; file named COPYING. Among other things, the copyright notice
20 ;; and this notice must be preserved on all copies.
24 ;; This file is autoloaded from calc-ext.el.
29 (defun calc-Need-calc-poly () nil)
32 (defun calcFunc-pcont (expr &optional var)
33 (cond ((Math-primp expr)
34 (cond ((Math-zerop expr) 1)
35 ((Math-messy-integerp expr) (math-trunc expr))
36 ((Math-objectp expr) expr)
37 ((or (equal expr var) (not var)) 1)
40 (math-mul (calcFunc-pcont (nth 1 expr) var)
41 (calcFunc-pcont (nth 2 expr) var)))
43 (math-div (calcFunc-pcont (nth 1 expr) var)
44 (calcFunc-pcont (nth 2 expr) var)))
45 ((and (eq (car expr) '^) (Math-natnump (nth 2 expr)))
46 (math-pow (calcFunc-pcont (nth 1 expr) var) (nth 2 expr)))
47 ((memq (car expr) '(neg polar))
48 (calcFunc-pcont (nth 1 expr) var))
50 (let ((p (math-is-polynomial expr var)))
52 (let ((lead (nth (1- (length p)) p))
53 (cont (math-poly-gcd-list p)))
54 (if (math-guess-if-neg lead)
58 ((memq (car expr) '(+ - cplx sdev))
59 (let ((cont (calcFunc-pcont (nth 1 expr) var)))
62 (let ((c2 (calcFunc-pcont (nth 2 expr) var)))
63 (if (and (math-negp cont)
64 (if (eq (car expr) '-) (math-posp c2) (math-negp c2)))
65 (math-neg (math-poly-gcd cont c2))
66 (math-poly-gcd cont c2))))))
70 (defun calcFunc-pprim (expr &optional var)
71 (let ((cont (calcFunc-pcont expr var)))
72 (if (math-equal-int cont 1)
74 (math-poly-div-exact expr cont var))))
76 (defun math-div-poly-const (expr c)
77 (cond ((memq (car-safe expr) '(+ -))
79 (math-div-poly-const (nth 1 expr) c)
80 (math-div-poly-const (nth 2 expr) c)))
81 (t (math-div expr c))))
83 (defun calcFunc-pdeg (expr &optional var)
85 '(neg (var inf var-inf))
87 (or (math-polynomial-p expr var)
88 (math-reject-arg expr "Expected a polynomial"))
89 (math-poly-degree expr))))
91 (defun math-poly-degree (expr)
92 (cond ((Math-primp expr)
93 (if (eq (car-safe expr) 'var) 1 0))
95 (math-poly-degree (nth 1 expr)))
97 (+ (math-poly-degree (nth 1 expr))
98 (math-poly-degree (nth 2 expr))))
100 (- (math-poly-degree (nth 1 expr))
101 (math-poly-degree (nth 2 expr))))
102 ((and (eq (car expr) '^) (natnump (nth 2 expr)))
103 (* (math-poly-degree (nth 1 expr)) (nth 2 expr)))
104 ((memq (car expr) '(+ -))
105 (max (math-poly-degree (nth 1 expr))
106 (math-poly-degree (nth 2 expr))))
109 (defun calcFunc-plead (expr var)
110 (cond ((eq (car-safe expr) '*)
111 (math-mul (calcFunc-plead (nth 1 expr) var)
112 (calcFunc-plead (nth 2 expr) var)))
113 ((eq (car-safe expr) '/)
114 (math-div (calcFunc-plead (nth 1 expr) var)
115 (calcFunc-plead (nth 2 expr) var)))
116 ((and (eq (car-safe expr) '^) (math-natnump (nth 2 expr)))
117 (math-pow (calcFunc-plead (nth 1 expr) var) (nth 2 expr)))
123 (let ((p (math-is-polynomial expr var)))
125 (nth (1- (length p)) p)
132 ;;; Polynomial quotient, remainder, and GCD.
133 ;;; Originally by Ove Ewerlid (ewerlid@mizar.DoCS.UU.SE).
134 ;;; Modifications and simplifications by daveg.
136 (setq math-poly-modulus 1)
138 ;;; Return gcd of two polynomials
139 (defun calcFunc-pgcd (pn pd)
140 (if (math-any-floats pn)
141 (math-reject-arg pn "Coefficients must be rational"))
142 (if (math-any-floats pd)
143 (math-reject-arg pd "Coefficients must be rational"))
144 (let ((calc-prefer-frac t)
145 (math-poly-modulus (math-poly-modulus pn pd)))
146 (math-poly-gcd pn pd)))
148 ;;; Return only quotient to top of stack (nil if zero)
149 (defun calcFunc-pdiv (pn pd &optional base)
150 (let* ((calc-prefer-frac t)
151 (math-poly-modulus (math-poly-modulus pn pd))
152 (res (math-poly-div pn pd base)))
153 (setq calc-poly-div-remainder (cdr res))
156 ;;; Return only remainder to top of stack
157 (defun calcFunc-prem (pn pd &optional base)
158 (let ((calc-prefer-frac t)
159 (math-poly-modulus (math-poly-modulus pn pd)))
160 (cdr (math-poly-div pn pd base))))
162 (defun calcFunc-pdivrem (pn pd &optional base)
163 (let* ((calc-prefer-frac t)
164 (math-poly-modulus (math-poly-modulus pn pd))
165 (res (math-poly-div pn pd base)))
166 (list 'vec (car res) (cdr res))))
168 (defun calcFunc-pdivide (pn pd &optional base)
169 (let* ((calc-prefer-frac t)
170 (math-poly-modulus (math-poly-modulus pn pd))
171 (res (math-poly-div pn pd base)))
172 (math-add (car res) (math-div (cdr res) pd))))
175 ;;; Multiply two terms, expanding out products of sums.
176 (defun math-mul-thru (lhs rhs)
177 (if (memq (car-safe lhs) '(+ -))
179 (math-mul-thru (nth 1 lhs) rhs)
180 (math-mul-thru (nth 2 lhs) rhs))
181 (if (memq (car-safe rhs) '(+ -))
183 (math-mul-thru lhs (nth 1 rhs))
184 (math-mul-thru lhs (nth 2 rhs)))
185 (math-mul lhs rhs))))
187 (defun math-div-thru (num den)
188 (if (memq (car-safe num) '(+ -))
190 (math-div-thru (nth 1 num) den)
191 (math-div-thru (nth 2 num) den))
195 ;;; Sort the terms of a sum into canonical order.
196 (defun math-sort-terms (expr)
197 (if (memq (car-safe expr) '(+ -))
199 (sort (math-sum-to-list expr)
200 (function (lambda (a b) (math-beforep (car a) (car b))))))
203 (defun math-list-to-sum (lst)
205 (list (if (cdr (car lst)) '- '+)
206 (math-list-to-sum (cdr lst))
209 (math-neg (car (car lst)))
212 (defun math-sum-to-list (tree &optional neg)
213 (cond ((eq (car-safe tree) '+)
214 (nconc (math-sum-to-list (nth 1 tree) neg)
215 (math-sum-to-list (nth 2 tree) neg)))
216 ((eq (car-safe tree) '-)
217 (nconc (math-sum-to-list (nth 1 tree) neg)
218 (math-sum-to-list (nth 2 tree) (not neg))))
219 (t (list (cons tree neg)))))
221 ;;; Check if the polynomial coefficients are modulo forms.
222 (defun math-poly-modulus (expr &optional expr2)
223 (or (math-poly-modulus-rec expr)
224 (and expr2 (math-poly-modulus-rec expr2))
227 (defun math-poly-modulus-rec (expr)
228 (if (and (eq (car-safe expr) 'mod) (Math-natnump (nth 2 expr)))
229 (list 'mod 1 (nth 2 expr))
230 (and (memq (car-safe expr) '(+ - * /))
231 (or (math-poly-modulus-rec (nth 1 expr))
232 (math-poly-modulus-rec (nth 2 expr))))))
235 ;;; Divide two polynomials. Return (quotient . remainder).
236 (defun math-poly-div (u v &optional math-poly-div-base)
237 (if math-poly-div-base
238 (math-do-poly-div u v)
239 (math-do-poly-div (calcFunc-expand u) (calcFunc-expand v))))
240 (setq math-poly-div-base nil)
242 (defun math-poly-div-exact (u v &optional base)
243 (let ((res (math-poly-div u v base)))
246 (math-reject-arg (list 'vec u v) "Argument is not a polynomial"))))
248 (defun math-do-poly-div (u v)
249 (cond ((math-constp u)
251 (cons (math-div u v) 0)
256 (if (memq (car-safe u) '(+ -))
257 (math-add-or-sub (math-poly-div-exact (nth 1 u) v)
258 (math-poly-div-exact (nth 2 u) v)
263 (cons math-poly-modulus 0))
264 ((and (math-atomic-factorp u) (math-atomic-factorp v))
265 (cons (math-simplify (math-div u v)) 0))
267 (let ((base (or math-poly-div-base
268 (math-poly-div-base u v)))
271 (null (setq vp (math-is-polynomial v base nil 'gen))))
273 (setq up (math-is-polynomial u base nil 'gen)
274 res (math-poly-div-coefs up vp))
275 (cons (math-build-polynomial-expr (car res) base)
276 (math-build-polynomial-expr (cdr res) base)))))))
278 (defun math-poly-div-rec (u v)
279 (cond ((math-constp u)
284 (if (memq (car-safe u) '(+ -))
285 (math-add-or-sub (math-poly-div-rec (nth 1 u) v)
286 (math-poly-div-rec (nth 2 u) v)
289 ((Math-equal u v) math-poly-modulus)
290 ((and (math-atomic-factorp u) (math-atomic-factorp v))
291 (math-simplify (math-div u v)))
295 (let ((base (math-poly-div-base u v))
298 (null (setq vp (math-is-polynomial v base nil 'gen))))
300 (setq up (math-is-polynomial u base nil 'gen)
301 res (math-poly-div-coefs up vp))
302 (math-add (math-build-polynomial-expr (car res) base)
303 (math-div (math-build-polynomial-expr (cdr res) base)
306 ;;; Divide two polynomials in coefficient-list form. Return (quot . rem).
307 (defun math-poly-div-coefs (u v)
308 (cond ((null v) (math-reject-arg nil "Division by zero"))
309 ((< (length u) (length v)) (cons nil u))
315 (let ((qk (math-poly-div-rec (math-simplify (car urev))
319 (if (or q (not (math-zerop qk)))
320 (setq q (cons qk q)))
321 (while (setq up (cdr up) vp (cdr vp))
322 (setcar up (math-sub (car up) (math-mul-thru qk (car vp)))))
323 (setq urev (cdr urev))
325 (while (and urev (Math-zerop (car urev)))
326 (setq urev (cdr urev)))
327 (cons q (nreverse (mapcar 'math-simplify urev)))))
329 (cons (list (math-poly-div-rec (car u) (car v)))
332 ;;; Perform a pseudo-division of polynomials. (See Knuth section 4.6.1.)
333 ;;; This returns only the remainder from the pseudo-division.
334 (defun math-poly-pseudo-div (u v)
336 ((< (length u) (length v)) u)
337 ((or (cdr u) (cdr v))
338 (let ((urev (reverse u))
344 (while (setq up (cdr up) vp (cdr vp))
345 (setcar up (math-sub (math-mul-thru (car vrev) (car up))
346 (math-mul-thru (car urev) (car vp)))))
347 (setq urev (cdr urev))
350 (setcar up (math-mul-thru (car vrev) (car up)))
352 (while (and urev (Math-zerop (car urev)))
353 (setq urev (cdr urev)))
354 (nreverse (mapcar 'math-simplify urev))))
357 ;;; Compute the GCD of two multivariate polynomials.
358 (defun math-poly-gcd (u v)
359 (cond ((Math-equal u v) u)
363 (calcFunc-gcd u (calcFunc-pcont v))))
367 (calcFunc-gcd v (calcFunc-pcont u))))
369 (let ((base (math-poly-gcd-base u v)))
373 (math-build-polynomial-expr
374 (math-poly-gcd-coefs (math-is-polynomial u base nil 'gen)
375 (math-is-polynomial v base nil 'gen))
377 (calcFunc-gcd (calcFunc-pcont u) (calcFunc-pcont u)))))))
379 (defun math-poly-div-list (lst a)
383 (math-mul-list lst a)
384 (mapcar (function (lambda (x) (math-poly-div-exact x a))) lst))))
386 (defun math-mul-list (lst a)
390 (mapcar 'math-neg lst)
392 (mapcar (function (lambda (x) (math-mul x a))) lst)))))
394 ;;; Run GCD on all elements in a list.
395 (defun math-poly-gcd-list (lst)
396 (if (or (memq 1 lst) (memq -1 lst))
397 (math-poly-gcd-frac-list lst)
398 (let ((gcd (car lst)))
399 (while (and (setq lst (cdr lst)) (not (eq gcd 1)))
401 (setq gcd (math-poly-gcd gcd (car lst)))))
402 (if lst (setq lst (math-poly-gcd-frac-list lst)))
405 (defun math-poly-gcd-frac-list (lst)
406 (while (and lst (not (eq (car-safe (car lst)) 'frac)))
407 (setq lst (cdr lst)))
409 (let ((denom (nth 2 (car lst))))
410 (while (setq lst (cdr lst))
411 (if (eq (car-safe (car lst)) 'frac)
412 (setq denom (calcFunc-lcm denom (nth 2 (car lst))))))
413 (list 'frac 1 denom))
416 ;;; Compute the GCD of two monovariate polynomial lists.
417 ;;; Knuth section 4.6.1, algorithm C.
418 (defun math-poly-gcd-coefs (u v)
419 (let ((d (math-poly-gcd (math-poly-gcd-list u)
420 (math-poly-gcd-list v)))
421 (g 1) (h 1) (z 0) hh r delta ghd)
422 (while (and u v (Math-zerop (car u)) (Math-zerop (car v)))
423 (setq u (cdr u) v (cdr v) z (1+ z)))
425 (setq u (math-poly-div-list u d)
426 v (math-poly-div-list v d)))
428 (setq delta (- (length u) (length v)))
430 (setq r u u v v r delta (- delta)))
431 (setq r (math-poly-pseudo-div u v))
434 v (math-poly-div-list r (math-mul g (math-pow h delta)))
435 g (nth (1- (length u)) u)
437 (math-mul (math-pow g delta) (math-pow h (- 1 delta)))
438 (math-poly-div-exact (math-pow g delta)
439 (math-pow h (1- delta))))))
442 (math-mul-list (math-poly-div-list v (math-poly-gcd-list v)) d)))
443 (if (math-guess-if-neg (nth (1- (length v)) v))
444 (setq v (math-mul-list v -1)))
445 (while (>= (setq z (1- z)) 0)
450 ;;; Return true if is a factor containing no sums or quotients.
451 (defun math-atomic-factorp (expr)
452 (cond ((eq (car-safe expr) '*)
453 (and (math-atomic-factorp (nth 1 expr))
454 (math-atomic-factorp (nth 2 expr))))
455 ((memq (car-safe expr) '(+ - /))
457 ((memq (car-safe expr) '(^ neg))
458 (math-atomic-factorp (nth 1 expr)))
461 ;;; Find a suitable base for dividing a by b.
462 ;;; The base must exist in both expressions.
463 ;;; The degree in the numerator must be higher or equal than the
464 ;;; degree in the denominator.
465 ;;; If the above conditions are not met the quotient is just a remainder.
466 ;;; Return nil if this is the case.
468 (defun math-poly-div-base (a b)
470 (and (setq a-base (math-total-polynomial-base a))
471 (setq b-base (math-total-polynomial-base b))
474 (let ((maybe (assoc (car (car a-base)) b-base)))
476 (if (>= (nth 1 (car a-base)) (nth 1 maybe))
477 (throw 'return (car (car a-base))))))
478 (setq a-base (cdr a-base)))))))
480 ;;; Same as above but for gcd algorithm.
481 ;;; Here there is no requirement that degree(a) > degree(b).
482 ;;; Take the base that has the highest degree considering both a and b.
483 ;;; ("a^20+b^21+x^3+a+b", "a+b^2+x^5+a^22+b^10") --> (a 22)
485 (defun math-poly-gcd-base (a b)
487 (and (setq a-base (math-total-polynomial-base a))
488 (setq b-base (math-total-polynomial-base b))
490 (while (and a-base b-base)
491 (if (> (nth 1 (car a-base)) (nth 1 (car b-base)))
492 (if (assoc (car (car a-base)) b-base)
493 (throw 'return (car (car a-base)))
494 (setq a-base (cdr a-base)))
495 (if (assoc (car (car b-base)) a-base)
496 (throw 'return (car (car b-base)))
497 (setq b-base (cdr b-base)))))))))
499 ;;; Sort a list of polynomial bases.
500 (defun math-sort-poly-base-list (lst)
501 (sort lst (function (lambda (a b)
502 (or (> (nth 1 a) (nth 1 b))
503 (and (= (nth 1 a) (nth 1 b))
504 (math-beforep (car a) (car b))))))))
506 ;;; Given an expression find all variables that are polynomial bases.
507 ;;; Return list in the form '( (var1 degree1) (var2 degree2) ... ).
508 ;;; Note dynamic scope of mpb-total-base.
509 (defun math-total-polynomial-base (expr)
510 (let ((mpb-total-base nil))
511 (math-polynomial-base expr 'math-polynomial-p1)
512 (math-sort-poly-base-list mpb-total-base)))
514 (defun math-polynomial-p1 (subexpr)
515 (or (assoc subexpr mpb-total-base)
516 (memq (car subexpr) '(+ - * / neg))
517 (and (eq (car subexpr) '^) (natnump (nth 2 subexpr)))
518 (let* ((math-poly-base-variable subexpr)
519 (exponent (math-polynomial-p mpb-top-expr subexpr)))
521 (setq mpb-total-base (cons (list subexpr exponent)
528 (defun calcFunc-factors (expr &optional var)
529 (let ((math-factored-vars (if var t nil))
531 (calc-prefer-frac t))
533 (setq var (math-polynomial-base expr)))
534 (let ((res (math-factor-finish
535 (or (catch 'factor (math-factor-expr-try var))
537 (math-simplify (if (math-vectorp res)
539 (list 'vec (list 'vec res 1)))))))
541 (defun calcFunc-factor (expr &optional var)
542 (let ((math-factored-vars nil)
544 (calc-prefer-frac t))
545 (math-simplify (math-factor-finish
547 (let ((math-factored-vars t))
548 (or (catch 'factor (math-factor-expr-try var)) expr))
549 (math-factor-expr expr))))))
551 (defun math-factor-finish (x)
554 (if (eq (car x) 'calcFunc-Fac-Prot)
555 (math-factor-finish (nth 1 x))
556 (cons (car x) (mapcar 'math-factor-finish (cdr x))))))
558 (defun math-factor-protect (x)
559 (if (memq (car-safe x) '(+ -))
560 (list 'calcFunc-Fac-Prot x)
563 (defun math-factor-expr (expr)
564 (cond ((eq math-factored-vars t) expr)
565 ((or (memq (car-safe expr) '(* / ^ neg))
566 (assq (car-safe expr) calc-tweak-eqn-table))
567 (cons (car expr) (mapcar 'math-factor-expr (cdr expr))))
568 ((memq (car-safe expr) '(+ -))
569 (let* ((math-factored-vars math-factored-vars)
570 (y (catch 'factor (math-factor-expr-part expr))))
576 (defun math-factor-expr-part (x) ; uses "expr"
577 (if (memq (car-safe x) '(+ - * / ^ neg))
578 (while (setq x (cdr x))
579 (math-factor-expr-part (car x)))
580 (and (not (Math-objvecp x))
581 (not (assoc x math-factored-vars))
582 (> (math-factor-contains expr x) 1)
583 (setq math-factored-vars (cons (list x) math-factored-vars))
584 (math-factor-expr-try x))))
586 (defun math-factor-expr-try (x)
587 (if (eq (car-safe expr) '*)
588 (let ((res1 (catch 'factor (let ((expr (nth 1 expr)))
589 (math-factor-expr-try x))))
590 (res2 (catch 'factor (let ((expr (nth 2 expr)))
591 (math-factor-expr-try x)))))
593 (throw 'factor (math-accum-factors (or res1 (nth 1 expr)) 1
594 (or res2 (nth 2 expr))))))
595 (let* ((p (math-is-polynomial expr x 30 'gen))
596 (math-poly-modulus (math-poly-modulus expr))
599 (setq res (math-factor-poly-coefs p))
600 (throw 'factor res)))))
602 (defun math-accum-factors (fac pow facs)
604 (if (math-vectorp fac)
606 (while (setq fac (cdr fac))
607 (setq facs (math-accum-factors (nth 1 (car fac))
608 (* pow (nth 2 (car fac)))
611 (if (and (eq (car-safe fac) '^) (natnump (nth 2 fac)))
612 (setq pow (* pow (nth 2 fac))
616 (or (math-vectorp facs)
617 (setq facs (if (eq facs 1) '(vec)
618 (list 'vec (list 'vec facs 1)))))
620 (while (and (setq found (cdr found))
621 (not (equal fac (nth 1 (car found))))))
624 (setcar (cdr (cdr (car found))) (+ pow (nth 2 (car found))))
626 ;; Put constant term first.
627 (if (and (cdr facs) (Math-ratp (nth 1 (nth 1 facs))))
628 (cons 'vec (cons (nth 1 facs) (cons (list 'vec fac pow)
630 (cons 'vec (cons (list 'vec fac pow) (cdr facs))))))))
631 (math-mul (math-pow fac pow) facs)))
633 (defun math-factor-poly-coefs (p &optional square-free) ; uses "x"
638 ;; Strip off multiples of x.
639 ((Math-zerop (car p))
641 (while (and p (Math-zerop (car p)))
642 (setq z (1+ z) p (cdr p)))
644 (setq p (math-factor-poly-coefs p square-free))
645 (setq p (math-sort-terms (math-factor-expr (car p)))))
646 (math-accum-factors x z (math-factor-protect p))))
648 ;; Factor out content.
649 ((and (not square-free)
650 (not (eq 1 (setq t1 (math-mul (math-poly-gcd-list p)
651 (if (math-guess-if-neg
652 (nth (1- (length p)) p))
654 (math-accum-factors t1 1 (math-factor-poly-coefs
655 (math-poly-div-list p t1) 'cont)))
657 ;; Check if linear in x.
659 (math-add (math-factor-protect
661 (math-factor-expr (car p))))
662 (math-mul x (math-factor-protect
664 (math-factor-expr (nth 1 p)))))))
666 ;; If symbolic coefficients, use FactorRules.
668 (while (and pp (or (Math-ratp (car pp))
669 (and (eq (car (car pp)) 'mod)
670 (Math-integerp (nth 1 (car pp)))
671 (Math-integerp (nth 2 (car pp))))))
674 (let ((res (math-rewrite
675 (list 'calcFunc-thecoefs x (cons 'vec p))
676 '(var FactorRules var-FactorRules))))
677 (or (and (eq (car-safe res) 'calcFunc-thefactors)
679 (math-vectorp (nth 2 res))
682 (while (setq vec (cdr vec))
683 (setq facs (math-accum-factors (car vec) 1 facs)))
685 (math-build-polynomial-expr p x))))
687 ;; Check if rational coefficients (i.e., not modulo a prime).
688 ((eq math-poly-modulus 1)
690 ;; Check if there are any squared terms, or a content not = 1.
691 (if (or (eq square-free t)
692 (equal (setq t1 (math-poly-gcd-coefs
693 p (setq t2 (math-poly-deriv-coefs p))))
696 ;; We now have a square-free polynomial with integer coefs.
697 ;; For now, we use a kludgey method that finds linear and
698 ;; quadratic terms using floating-point root-finding.
699 (if (setq t1 (let ((calc-symbolic-mode nil))
700 (math-poly-all-roots nil p t)))
701 (let ((roots (car t1))
702 (csign (if (math-negp (nth (1- (length p)) p)) -1 1))
707 (let ((coef0 (car (car roots)))
708 (coef1 (cdr (car roots))))
709 (setq expr (math-accum-factors
711 (let ((den (math-lcm-denoms
713 (setq scale (math-div scale den))
716 (math-mul den (math-pow x 2))
717 (math-mul (math-mul coef1 den) x))
718 (math-mul coef0 den)))
719 (let ((den (math-lcm-denoms coef0)))
720 (setq scale (math-div scale den))
721 (math-add (math-mul den x)
722 (math-mul coef0 den))))
725 (setq expr (math-accum-factors
728 (math-build-polynomial-expr
729 (math-mul-list (nth 1 t1) scale)
731 (math-build-polynomial-expr p x)) ; can't factor it.
733 ;; Separate out the squared terms (Knuth exercise 4.6.2-34).
734 ;; This step also divides out the content of the polynomial.
735 (let* ((cabs (math-poly-gcd-list p))
736 (csign (if (math-negp (nth (1- (length p)) p)) -1 1))
737 (t1s (math-mul-list t1 csign))
739 (v (car (math-poly-div-coefs p t1s)))
740 (w (car (math-poly-div-coefs t2 t1s))))
742 (not (math-poly-zerop
743 (setq t2 (math-poly-simplify
745 w 1 (math-poly-deriv-coefs v) -1)))))
746 (setq t1 (math-poly-gcd-coefs v t2)
748 v (car (math-poly-div-coefs v t1))
749 w (car (math-poly-div-coefs t2 t1))))
751 t2 (math-accum-factors (math-factor-poly-coefs v t)
754 (setq t2 (math-accum-factors (math-factor-poly-coefs
759 (math-accum-factors (math-mul cabs csign) 1 t2))))
761 ;; Factoring modulo a prime.
762 ((and (= (length (setq temp (math-poly-gcd-coefs
763 p (math-poly-deriv-coefs p))))
767 (setq temp (nthcdr (nth 2 math-poly-modulus) temp)
768 p (cons (car temp) p)))
769 (and (setq temp (math-factor-poly-coefs p))
770 (math-pow temp (nth 2 math-poly-modulus))))
772 (math-reject-arg nil "*Modulo factorization not yet implemented")))))
774 (defun math-poly-deriv-coefs (p)
777 (while (setq p (cdr p))
778 (setq dp (cons (math-mul (car p) n) dp)
782 (defun math-factor-contains (x a)
785 (if (memq (car-safe x) '(+ - * / neg))
787 (while (setq x (cdr x))
788 (setq sum (+ sum (math-factor-contains (car x) a))))
790 (if (and (eq (car-safe x) '^)
792 (* (math-factor-contains (nth 1 x) a) (nth 2 x))
799 ;;; Merge all quotients and expand/simplify the numerator
800 (defun calcFunc-nrat (expr)
801 (if (math-any-floats expr)
802 (setq expr (calcFunc-pfrac expr)))
803 (if (or (math-vectorp expr)
804 (assq (car-safe expr) calc-tweak-eqn-table))
805 (cons (car expr) (mapcar 'calcFunc-nrat (cdr expr)))
806 (let* ((calc-prefer-frac t)
807 (res (math-to-ratpoly expr))
808 (num (math-simplify (math-sort-terms (calcFunc-expand (car res)))))
809 (den (math-simplify (math-sort-terms (calcFunc-expand (cdr res)))))
810 (g (math-poly-gcd num den)))
812 (let ((num2 (math-poly-div num g))
813 (den2 (math-poly-div den g)))
814 (and (eq (cdr num2) 0) (eq (cdr den2) 0)
815 (setq num (car num2) den (car den2)))))
816 (math-simplify (math-div num den)))))
818 ;;; Returns expressions (num . denom).
819 (defun math-to-ratpoly (expr)
820 (let ((res (math-to-ratpoly-rec expr)))
821 (cons (math-simplify (car res)) (math-simplify (cdr res)))))
823 (defun math-to-ratpoly-rec (expr)
824 (cond ((Math-primp expr)
826 ((memq (car expr) '(+ -))
827 (let ((r1 (math-to-ratpoly-rec (nth 1 expr)))
828 (r2 (math-to-ratpoly-rec (nth 2 expr))))
829 (if (equal (cdr r1) (cdr r2))
830 (cons (list (car expr) (car r1) (car r2)) (cdr r1))
832 (cons (list (car expr)
833 (math-mul (car r1) (cdr r2))
837 (cons (list (car expr)
839 (math-mul (car r2) (cdr r1)))
841 (let ((g (math-poly-gcd (cdr r1) (cdr r2))))
842 (let ((d1 (and (not (eq g 1)) (math-poly-div (cdr r1) g)))
843 (d2 (and (not (eq g 1)) (math-poly-div
844 (math-mul (car r1) (cdr r2))
846 (if (and (eq (cdr d1) 0) (eq (cdr d2) 0))
847 (cons (list (car expr) (car d2)
848 (math-mul (car r2) (car d1)))
849 (math-mul (car d1) (cdr r2)))
850 (cons (list (car expr)
851 (math-mul (car r1) (cdr r2))
852 (math-mul (car r2) (cdr r1)))
853 (math-mul (cdr r1) (cdr r2)))))))))))
855 (let* ((r1 (math-to-ratpoly-rec (nth 1 expr)))
856 (r2 (math-to-ratpoly-rec (nth 2 expr)))
857 (g (math-mul (math-poly-gcd (car r1) (cdr r2))
858 (math-poly-gcd (cdr r1) (car r2)))))
860 (cons (math-mul (car r1) (car r2))
861 (math-mul (cdr r1) (cdr r2)))
862 (cons (math-poly-div-exact (math-mul (car r1) (car r2)) g)
863 (math-poly-div-exact (math-mul (cdr r1) (cdr r2)) g)))))
865 (let* ((r1 (math-to-ratpoly-rec (nth 1 expr)))
866 (r2 (math-to-ratpoly-rec (nth 2 expr))))
867 (if (and (eq (cdr r1) 1) (eq (cdr r2) 1))
868 (cons (car r1) (car r2))
869 (let ((g (math-mul (math-poly-gcd (car r1) (car r2))
870 (math-poly-gcd (cdr r1) (cdr r2)))))
872 (cons (math-mul (car r1) (cdr r2))
873 (math-mul (cdr r1) (car r2)))
874 (cons (math-poly-div-exact (math-mul (car r1) (cdr r2)) g)
875 (math-poly-div-exact (math-mul (cdr r1) (car r2))
877 ((and (eq (car expr) '^) (integerp (nth 2 expr)))
878 (let ((r1 (math-to-ratpoly-rec (nth 1 expr))))
879 (if (> (nth 2 expr) 0)
880 (cons (math-pow (car r1) (nth 2 expr))
881 (math-pow (cdr r1) (nth 2 expr)))
882 (cons (math-pow (cdr r1) (- (nth 2 expr)))
883 (math-pow (car r1) (- (nth 2 expr)))))))
884 ((eq (car expr) 'neg)
885 (let ((r1 (math-to-ratpoly-rec (nth 1 expr))))
886 (cons (math-neg (car r1)) (cdr r1))))
890 (defun math-ratpoly-p (expr &optional var)
891 (cond ((equal expr var) 1)
892 ((Math-primp expr) 0)
893 ((memq (car expr) '(+ -))
894 (let ((p1 (math-ratpoly-p (nth 1 expr) var))
896 (and p1 (setq p2 (math-ratpoly-p (nth 2 expr) var))
899 (let ((p1 (math-ratpoly-p (nth 1 expr) var))
901 (and p1 (setq p2 (math-ratpoly-p (nth 2 expr) var))
903 ((eq (car expr) 'neg)
904 (math-ratpoly-p (nth 1 expr) var))
906 (let ((p1 (math-ratpoly-p (nth 1 expr) var))
908 (and p1 (setq p2 (math-ratpoly-p (nth 2 expr) var))
910 ((and (eq (car expr) '^)
911 (integerp (nth 2 expr)))
912 (let ((p1 (math-ratpoly-p (nth 1 expr) var)))
913 (and p1 (* p1 (nth 2 expr)))))
915 ((math-poly-depends expr var) nil)
919 (defun calcFunc-apart (expr &optional var)
920 (cond ((Math-primp expr) expr)
922 (math-add (calcFunc-apart (nth 1 expr) var)
923 (calcFunc-apart (nth 2 expr) var)))
925 (math-sub (calcFunc-apart (nth 1 expr) var)
926 (calcFunc-apart (nth 2 expr) var)))
927 ((not (math-ratpoly-p expr var))
928 (math-reject-arg expr "Expected a rational function"))
930 (let* ((calc-prefer-frac t)
931 (rat (math-to-ratpoly expr))
934 (qr (math-poly-div num den))
938 (setq var (math-polynomial-base den)))
939 (math-add q (or (and var
940 (math-expr-contains den var)
941 (math-partial-fractions r den var))
942 (math-div r den)))))))
945 (defun math-padded-polynomial (expr var deg)
946 (let ((p (math-is-polynomial expr var deg)))
947 (append p (make-list (- deg (length p)) 0))))
949 (defun math-partial-fractions (r den var)
950 (let* ((fden (calcFunc-factors den var))
951 (tdeg (math-polynomial-p den var))
956 (tz (make-list (1- tdeg) 0))
957 (calc-matrix-mode 'scalar))
958 (and (not (and (= (length fden) 2) (eq (nth 2 (nth 1 fden)) 1)))
960 (while (setq fp (cdr fp))
961 (let ((rpt (nth 2 (car fp)))
962 (deg (math-polynomial-p (nth 1 (car fp)) var))
968 (setq dvar (append '(vec) lz '(1) tz)
972 dnum (math-add dnum (math-mul dvar
973 (math-pow var deg2)))
974 dlist (cons (and (= deg2 (1- deg))
975 (math-pow (nth 1 (car fp)) rpt))
979 (while (setq fpp (cdr fpp))
981 (setq mult (math-mul mult
982 (math-pow (nth 1 (car fpp))
983 (nth 2 (car fpp)))))))
984 (setq dnum (math-mul dnum mult)))
985 (setq eqns (math-add eqns (math-mul dnum
991 (setq eqns (math-div (cons 'vec (math-padded-polynomial r var tdeg))
997 (cons 'vec (math-padded-polynomial
1000 (and (math-vectorp eqns)
1003 (setq eqns (nreverse eqns))
1005 (setq num (cons (car eqns) num)
1008 (setq num (math-build-polynomial-expr
1010 res (math-add res (math-div num (car dlist)))
1012 (setq dlist (cdr dlist)))
1013 (math-normalize res)))))))
1017 (defun math-expand-term (expr)
1018 (cond ((and (eq (car-safe expr) '*)
1019 (memq (car-safe (nth 1 expr)) '(+ -)))
1020 (math-add-or-sub (list '* (nth 1 (nth 1 expr)) (nth 2 expr))
1021 (list '* (nth 2 (nth 1 expr)) (nth 2 expr))
1022 nil (eq (car (nth 1 expr)) '-)))
1023 ((and (eq (car-safe expr) '*)
1024 (memq (car-safe (nth 2 expr)) '(+ -)))
1025 (math-add-or-sub (list '* (nth 1 expr) (nth 1 (nth 2 expr)))
1026 (list '* (nth 1 expr) (nth 2 (nth 2 expr)))
1027 nil (eq (car (nth 2 expr)) '-)))
1028 ((and (eq (car-safe expr) '/)
1029 (memq (car-safe (nth 1 expr)) '(+ -)))
1030 (math-add-or-sub (list '/ (nth 1 (nth 1 expr)) (nth 2 expr))
1031 (list '/ (nth 2 (nth 1 expr)) (nth 2 expr))
1032 nil (eq (car (nth 1 expr)) '-)))
1033 ((and (eq (car-safe expr) '^)
1034 (memq (car-safe (nth 1 expr)) '(+ -))
1035 (integerp (nth 2 expr))
1036 (if (> (nth 2 expr) 0)
1037 (or (and (or (> mmt-many 500000) (< mmt-many -500000))
1038 (math-expand-power (nth 1 expr) (nth 2 expr)
1042 (list '^ (nth 1 expr) (1- (nth 2 expr)))))
1043 (if (< (nth 2 expr) 0)
1044 (list '/ 1 (list '^ (nth 1 expr) (- (nth 2 expr))))))))
1047 (defun calcFunc-expand (expr &optional many)
1048 (math-normalize (math-map-tree 'math-expand-term expr many)))
1050 (defun math-expand-power (x n &optional var else-nil)
1051 (or (and (natnump n)
1052 (memq (car-safe x) '(+ -))
1055 (while (memq (car-safe x) '(+ -))
1056 (setq terms (cons (if (eq (car x) '-)
1057 (math-neg (nth 2 x))
1061 (setq terms (cons x terms))
1065 (or (math-expr-contains (car p) var)
1066 (setq terms (delq (car p) terms)
1067 cterms (cons (car p) cterms)))
1070 (setq terms (cons (apply 'calcFunc-add cterms)
1072 (if (= (length terms) 2)
1076 (setq accum (list '+ accum
1077 (list '* (calcFunc-choose n i)
1079 (list '^ (nth 1 terms) i)
1080 (list '^ (car terms)
1089 (setq accum (list '+ accum
1090 (list '^ (car p1) 2))
1092 (while (setq p2 (cdr p2))
1093 (setq accum (list '+ accum
1104 (setq accum (list '+ accum (list '^ (car p1) 3))
1106 (while (setq p2 (cdr p2))
1107 (setq accum (list '+
1113 (list '^ (car p1) 2)
1118 (list '^ (car p2) 2))))
1120 (while (setq p3 (cdr p3))
1121 (setq accum (list '+ accum
1133 (defun calcFunc-expandpow (x n)
1134 (math-normalize (math-expand-power x n)))
1136 ;;; calc-poly.el ends here