1 ;; Calculator for GNU Emacs, part II [calc-poly.el]
2 ;; Copyright (C) 1990, 1991, 1992, 1993 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))))))
71 (defun calcFunc-pprim (expr &optional var)
72 (let ((cont (calcFunc-pcont expr var)))
73 (if (math-equal-int cont 1)
75 (math-poly-div-exact expr cont var)))
78 (defun math-div-poly-const (expr c)
79 (cond ((memq (car-safe expr) '(+ -))
81 (math-div-poly-const (nth 1 expr) c)
82 (math-div-poly-const (nth 2 expr) c)))
83 (t (math-div expr c)))
86 (defun calcFunc-pdeg (expr &optional var)
88 '(neg (var inf var-inf))
90 (or (math-polynomial-p expr var)
91 (math-reject-arg expr "Expected a polynomial"))
92 (math-poly-degree expr)))
95 (defun math-poly-degree (expr)
96 (cond ((Math-primp expr)
97 (if (eq (car-safe expr) 'var) 1 0))
99 (math-poly-degree (nth 1 expr)))
101 (+ (math-poly-degree (nth 1 expr))
102 (math-poly-degree (nth 2 expr))))
104 (- (math-poly-degree (nth 1 expr))
105 (math-poly-degree (nth 2 expr))))
106 ((and (eq (car expr) '^) (natnump (nth 2 expr)))
107 (* (math-poly-degree (nth 1 expr)) (nth 2 expr)))
108 ((memq (car expr) '(+ -))
109 (max (math-poly-degree (nth 1 expr))
110 (math-poly-degree (nth 2 expr))))
114 (defun calcFunc-plead (expr var)
115 (cond ((eq (car-safe expr) '*)
116 (math-mul (calcFunc-plead (nth 1 expr) var)
117 (calcFunc-plead (nth 2 expr) var)))
118 ((eq (car-safe expr) '/)
119 (math-div (calcFunc-plead (nth 1 expr) var)
120 (calcFunc-plead (nth 2 expr) var)))
121 ((and (eq (car-safe expr) '^) (math-natnump (nth 2 expr)))
122 (math-pow (calcFunc-plead (nth 1 expr) var) (nth 2 expr)))
128 (let ((p (math-is-polynomial expr var)))
130 (nth (1- (length p)) p)
138 ;;; Polynomial quotient, remainder, and GCD.
139 ;;; Originally by Ove Ewerlid (ewerlid@mizar.DoCS.UU.SE).
140 ;;; Modifications and simplifications by daveg.
142 (setq math-poly-modulus 1)
144 ;;; Return gcd of two polynomials
145 (defun calcFunc-pgcd (pn pd)
146 (if (math-any-floats pn)
147 (math-reject-arg pn "Coefficients must be rational"))
148 (if (math-any-floats pd)
149 (math-reject-arg pd "Coefficients must be rational"))
150 (let ((calc-prefer-frac t)
151 (math-poly-modulus (math-poly-modulus pn pd)))
152 (math-poly-gcd pn pd))
155 ;;; Return only quotient to top of stack (nil if zero)
156 (defun calcFunc-pdiv (pn pd &optional base)
157 (let* ((calc-prefer-frac t)
158 (math-poly-modulus (math-poly-modulus pn pd))
159 (res (math-poly-div pn pd base)))
160 (setq calc-poly-div-remainder (cdr res))
164 ;;; Return only remainder to top of stack
165 (defun calcFunc-prem (pn pd &optional base)
166 (let ((calc-prefer-frac t)
167 (math-poly-modulus (math-poly-modulus pn pd)))
168 (cdr (math-poly-div pn pd base)))
171 (defun calcFunc-pdivrem (pn pd &optional base)
172 (let* ((calc-prefer-frac t)
173 (math-poly-modulus (math-poly-modulus pn pd))
174 (res (math-poly-div pn pd base)))
175 (list 'vec (car res) (cdr res)))
178 (defun calcFunc-pdivide (pn pd &optional base)
179 (let* ((calc-prefer-frac t)
180 (math-poly-modulus (math-poly-modulus pn pd))
181 (res (math-poly-div pn pd base)))
182 (math-add (car res) (math-div (cdr res) pd)))
186 ;;; Multiply two terms, expanding out products of sums.
187 (defun math-mul-thru (lhs rhs)
188 (if (memq (car-safe lhs) '(+ -))
190 (math-mul-thru (nth 1 lhs) rhs)
191 (math-mul-thru (nth 2 lhs) rhs))
192 (if (memq (car-safe rhs) '(+ -))
194 (math-mul-thru lhs (nth 1 rhs))
195 (math-mul-thru lhs (nth 2 rhs)))
199 (defun math-div-thru (num den)
200 (if (memq (car-safe num) '(+ -))
202 (math-div-thru (nth 1 num) den)
203 (math-div-thru (nth 2 num) den))
208 ;;; Sort the terms of a sum into canonical order.
209 (defun math-sort-terms (expr)
210 (if (memq (car-safe expr) '(+ -))
212 (sort (math-sum-to-list expr)
213 (function (lambda (a b) (math-beforep (car a) (car b))))))
217 (defun math-list-to-sum (lst)
219 (list (if (cdr (car lst)) '- '+)
220 (math-list-to-sum (cdr lst))
223 (math-neg (car (car lst)))
227 (defun math-sum-to-list (tree &optional neg)
228 (cond ((eq (car-safe tree) '+)
229 (nconc (math-sum-to-list (nth 1 tree) neg)
230 (math-sum-to-list (nth 2 tree) neg)))
231 ((eq (car-safe tree) '-)
232 (nconc (math-sum-to-list (nth 1 tree) neg)
233 (math-sum-to-list (nth 2 tree) (not neg))))
234 (t (list (cons tree neg))))
237 ;;; Check if the polynomial coefficients are modulo forms.
238 (defun math-poly-modulus (expr &optional expr2)
239 (or (math-poly-modulus-rec expr)
240 (and expr2 (math-poly-modulus-rec expr2))
244 (defun math-poly-modulus-rec (expr)
245 (if (and (eq (car-safe expr) 'mod) (Math-natnump (nth 2 expr)))
246 (list 'mod 1 (nth 2 expr))
247 (and (memq (car-safe expr) '(+ - * /))
248 (or (math-poly-modulus-rec (nth 1 expr))
249 (math-poly-modulus-rec (nth 2 expr)))))
253 ;;; Divide two polynomials. Return (quotient . remainder).
254 (defun math-poly-div (u v &optional math-poly-div-base)
255 (if math-poly-div-base
256 (math-do-poly-div u v)
257 (math-do-poly-div (calcFunc-expand u) (calcFunc-expand v)))
259 (setq math-poly-div-base nil)
261 (defun math-poly-div-exact (u v &optional base)
262 (let ((res (math-poly-div u v base)))
265 (math-reject-arg (list 'vec u v) "Argument is not a polynomial")))
268 (defun math-do-poly-div (u v)
269 (cond ((math-constp u)
271 (cons (math-div u v) 0)
276 (if (memq (car-safe u) '(+ -))
277 (math-add-or-sub (math-poly-div-exact (nth 1 u) v)
278 (math-poly-div-exact (nth 2 u) v)
283 (cons math-poly-modulus 0))
284 ((and (math-atomic-factorp u) (math-atomic-factorp v))
285 (cons (math-simplify (math-div u v)) 0))
287 (let ((base (or math-poly-div-base
288 (math-poly-div-base u v)))
291 (null (setq vp (math-is-polynomial v base nil 'gen))))
293 (setq up (math-is-polynomial u base nil 'gen)
294 res (math-poly-div-coefs up vp))
295 (cons (math-build-polynomial-expr (car res) base)
296 (math-build-polynomial-expr (cdr res) base))))))
299 (defun math-poly-div-rec (u v)
300 (cond ((math-constp u)
305 (if (memq (car-safe u) '(+ -))
306 (math-add-or-sub (math-poly-div-rec (nth 1 u) v)
307 (math-poly-div-rec (nth 2 u) v)
310 ((Math-equal u v) math-poly-modulus)
311 ((and (math-atomic-factorp u) (math-atomic-factorp v))
312 (math-simplify (math-div u v)))
316 (let ((base (math-poly-div-base u v))
319 (null (setq vp (math-is-polynomial v base nil 'gen))))
321 (setq up (math-is-polynomial u base nil 'gen)
322 res (math-poly-div-coefs up vp))
323 (math-add (math-build-polynomial-expr (car res) base)
324 (math-div (math-build-polynomial-expr (cdr res) base)
328 ;;; Divide two polynomials in coefficient-list form. Return (quot . rem).
329 (defun math-poly-div-coefs (u v)
330 (cond ((null v) (math-reject-arg nil "Division by zero"))
331 ((< (length u) (length v)) (cons nil u))
337 (let ((qk (math-poly-div-rec (math-simplify (car urev))
341 (if (or q (not (math-zerop qk)))
342 (setq q (cons qk q)))
343 (while (setq up (cdr up) vp (cdr vp))
344 (setcar up (math-sub (car up) (math-mul-thru qk (car vp)))))
345 (setq urev (cdr urev))
347 (while (and urev (Math-zerop (car urev)))
348 (setq urev (cdr urev)))
349 (cons q (nreverse (mapcar 'math-simplify urev)))))
351 (cons (list (math-poly-div-rec (car u) (car v)))
355 ;;; Perform a pseudo-division of polynomials. (See Knuth section 4.6.1.)
356 ;;; This returns only the remainder from the pseudo-division.
357 (defun math-poly-pseudo-div (u v)
359 ((< (length u) (length v)) u)
360 ((or (cdr u) (cdr v))
361 (let ((urev (reverse u))
367 (while (setq up (cdr up) vp (cdr vp))
368 (setcar up (math-sub (math-mul-thru (car vrev) (car up))
369 (math-mul-thru (car urev) (car vp)))))
370 (setq urev (cdr urev))
373 (setcar up (math-mul-thru (car vrev) (car up)))
375 (while (and urev (Math-zerop (car urev)))
376 (setq urev (cdr urev)))
377 (nreverse (mapcar 'math-simplify urev))))
381 ;;; Compute the GCD of two multivariate polynomials.
382 (defun math-poly-gcd (u v)
383 (cond ((Math-equal u v) u)
387 (calcFunc-gcd u (calcFunc-pcont v))))
391 (calcFunc-gcd v (calcFunc-pcont u))))
393 (let ((base (math-poly-gcd-base u v)))
397 (math-build-polynomial-expr
398 (math-poly-gcd-coefs (math-is-polynomial u base nil 'gen)
399 (math-is-polynomial v base nil 'gen))
401 (calcFunc-gcd (calcFunc-pcont u) (calcFunc-pcont u))))))
404 (defun math-poly-div-list (lst a)
408 (math-mul-list lst a)
409 (mapcar (function (lambda (x) (math-poly-div-exact x a))) lst)))
412 (defun math-mul-list (lst a)
416 (mapcar 'math-neg lst)
418 (mapcar (function (lambda (x) (math-mul x a))) lst))))
421 ;;; Run GCD on all elements in a list.
422 (defun math-poly-gcd-list (lst)
423 (if (or (memq 1 lst) (memq -1 lst))
424 (math-poly-gcd-frac-list lst)
425 (let ((gcd (car lst)))
426 (while (and (setq lst (cdr lst)) (not (eq gcd 1)))
428 (setq gcd (math-poly-gcd gcd (car lst)))))
429 (if lst (setq lst (math-poly-gcd-frac-list lst)))
433 (defun math-poly-gcd-frac-list (lst)
434 (while (and lst (not (eq (car-safe (car lst)) 'frac)))
435 (setq lst (cdr lst)))
437 (let ((denom (nth 2 (car lst))))
438 (while (setq lst (cdr lst))
439 (if (eq (car-safe (car lst)) 'frac)
440 (setq denom (calcFunc-lcm denom (nth 2 (car lst))))))
441 (list 'frac 1 denom))
445 ;;; Compute the GCD of two monovariate polynomial lists.
446 ;;; Knuth section 4.6.1, algorithm C.
447 (defun math-poly-gcd-coefs (u v)
448 (let ((d (math-poly-gcd (math-poly-gcd-list u)
449 (math-poly-gcd-list v)))
450 (g 1) (h 1) (z 0) hh r delta ghd)
451 (while (and u v (Math-zerop (car u)) (Math-zerop (car v)))
452 (setq u (cdr u) v (cdr v) z (1+ z)))
454 (setq u (math-poly-div-list u d)
455 v (math-poly-div-list v d)))
457 (setq delta (- (length u) (length v)))
459 (setq r u u v v r delta (- delta)))
460 (setq r (math-poly-pseudo-div u v))
463 v (math-poly-div-list r (math-mul g (math-pow h delta)))
464 g (nth (1- (length u)) u)
466 (math-mul (math-pow g delta) (math-pow h (- 1 delta)))
467 (math-poly-div-exact (math-pow g delta)
468 (math-pow h (1- delta))))))
471 (math-mul-list (math-poly-div-list v (math-poly-gcd-list v)) d)))
472 (if (math-guess-if-neg (nth (1- (length v)) v))
473 (setq v (math-mul-list v -1)))
474 (while (>= (setq z (1- z)) 0)
480 ;;; Return true if is a factor containing no sums or quotients.
481 (defun math-atomic-factorp (expr)
482 (cond ((eq (car-safe expr) '*)
483 (and (math-atomic-factorp (nth 1 expr))
484 (math-atomic-factorp (nth 2 expr))))
485 ((memq (car-safe expr) '(+ - /))
487 ((memq (car-safe expr) '(^ neg))
488 (math-atomic-factorp (nth 1 expr)))
492 ;;; Find a suitable base for dividing a by b.
493 ;;; The base must exist in both expressions.
494 ;;; The degree in the numerator must be higher or equal than the
495 ;;; degree in the denominator.
496 ;;; If the above conditions are not met the quotient is just a remainder.
497 ;;; Return nil if this is the case.
499 (defun math-poly-div-base (a b)
501 (and (setq a-base (math-total-polynomial-base a))
502 (setq b-base (math-total-polynomial-base b))
505 (let ((maybe (assoc (car (car a-base)) b-base)))
507 (if (>= (nth 1 (car a-base)) (nth 1 maybe))
508 (throw 'return (car (car a-base))))))
509 (setq a-base (cdr a-base))))))
512 ;;; Same as above but for gcd algorithm.
513 ;;; Here there is no requirement that degree(a) > degree(b).
514 ;;; Take the base that has the highest degree considering both a and b.
515 ;;; ("a^20+b^21+x^3+a+b", "a+b^2+x^5+a^22+b^10") --> (a 22)
517 (defun math-poly-gcd-base (a b)
519 (and (setq a-base (math-total-polynomial-base a))
520 (setq b-base (math-total-polynomial-base b))
522 (while (and a-base b-base)
523 (if (> (nth 1 (car a-base)) (nth 1 (car b-base)))
524 (if (assoc (car (car a-base)) b-base)
525 (throw 'return (car (car a-base)))
526 (setq a-base (cdr a-base)))
527 (if (assoc (car (car b-base)) a-base)
528 (throw 'return (car (car b-base)))
529 (setq b-base (cdr b-base))))))))
532 ;;; Sort a list of polynomial bases.
533 (defun math-sort-poly-base-list (lst)
534 (sort lst (function (lambda (a b)
535 (or (> (nth 1 a) (nth 1 b))
536 (and (= (nth 1 a) (nth 1 b))
537 (math-beforep (car a) (car b)))))))
540 ;;; Given an expression find all variables that are polynomial bases.
541 ;;; Return list in the form '( (var1 degree1) (var2 degree2) ... ).
542 ;;; Note dynamic scope of mpb-total-base.
543 (defun math-total-polynomial-base (expr)
544 (let ((mpb-total-base nil))
545 (math-polynomial-base expr 'math-polynomial-p1)
546 (math-sort-poly-base-list mpb-total-base))
549 (defun math-polynomial-p1 (subexpr)
550 (or (assoc subexpr mpb-total-base)
551 (memq (car subexpr) '(+ - * / neg))
552 (and (eq (car subexpr) '^) (natnump (nth 2 subexpr)))
553 (let* ((math-poly-base-variable subexpr)
554 (exponent (math-polynomial-p mpb-top-expr subexpr)))
556 (setq mpb-total-base (cons (list subexpr exponent)
564 (defun calcFunc-factors (expr &optional var)
565 (let ((math-factored-vars (if var t nil))
567 (calc-prefer-frac t))
569 (setq var (math-polynomial-base expr)))
570 (let ((res (math-factor-finish
571 (or (catch 'factor (math-factor-expr-try var))
573 (math-simplify (if (math-vectorp res)
575 (list 'vec (list 'vec res 1))))))
578 (defun calcFunc-factor (expr &optional var)
579 (let ((math-factored-vars nil)
581 (calc-prefer-frac t))
582 (math-simplify (math-factor-finish
584 (let ((math-factored-vars t))
585 (or (catch 'factor (math-factor-expr-try var)) expr))
586 (math-factor-expr expr)))))
589 (defun math-factor-finish (x)
592 (if (eq (car x) 'calcFunc-Fac-Prot)
593 (math-factor-finish (nth 1 x))
594 (cons (car x) (mapcar 'math-factor-finish (cdr x)))))
597 (defun math-factor-protect (x)
598 (if (memq (car-safe x) '(+ -))
599 (list 'calcFunc-Fac-Prot x)
603 (defun math-factor-expr (expr)
604 (cond ((eq math-factored-vars t) expr)
605 ((or (memq (car-safe expr) '(* / ^ neg))
606 (assq (car-safe expr) calc-tweak-eqn-table))
607 (cons (car expr) (mapcar 'math-factor-expr (cdr expr))))
608 ((memq (car-safe expr) '(+ -))
609 (let* ((math-factored-vars math-factored-vars)
610 (y (catch 'factor (math-factor-expr-part expr))))
617 (defun math-factor-expr-part (x) ; uses "expr"
618 (if (memq (car-safe x) '(+ - * / ^ neg))
619 (while (setq x (cdr x))
620 (math-factor-expr-part (car x)))
621 (and (not (Math-objvecp x))
622 (not (assoc x math-factored-vars))
623 (> (math-factor-contains expr x) 1)
624 (setq math-factored-vars (cons (list x) math-factored-vars))
625 (math-factor-expr-try x)))
628 (defun math-factor-expr-try (x)
629 (if (eq (car-safe expr) '*)
630 (let ((res1 (catch 'factor (let ((expr (nth 1 expr)))
631 (math-factor-expr-try x))))
632 (res2 (catch 'factor (let ((expr (nth 2 expr)))
633 (math-factor-expr-try x)))))
635 (throw 'factor (math-accum-factors (or res1 (nth 1 expr)) 1
636 (or res2 (nth 2 expr))))))
637 (let* ((p (math-is-polynomial expr x 30 'gen))
638 (math-poly-modulus (math-poly-modulus expr))
641 (setq res (math-factor-poly-coefs p))
642 (throw 'factor res))))
645 (defun math-accum-factors (fac pow facs)
647 (if (math-vectorp fac)
649 (while (setq fac (cdr fac))
650 (setq facs (math-accum-factors (nth 1 (car fac))
651 (* pow (nth 2 (car fac)))
654 (if (and (eq (car-safe fac) '^) (natnump (nth 2 fac)))
655 (setq pow (* pow (nth 2 fac))
659 (or (math-vectorp facs)
660 (setq facs (if (eq facs 1) '(vec)
661 (list 'vec (list 'vec facs 1)))))
663 (while (and (setq found (cdr found))
664 (not (equal fac (nth 1 (car found))))))
667 (setcar (cdr (cdr (car found))) (+ pow (nth 2 (car found))))
669 ;; Put constant term first.
670 (if (and (cdr facs) (Math-ratp (nth 1 (nth 1 facs))))
671 (cons 'vec (cons (nth 1 facs) (cons (list 'vec fac pow)
673 (cons 'vec (cons (list 'vec fac pow) (cdr facs))))))))
674 (math-mul (math-pow fac pow) facs))
677 (defun math-factor-poly-coefs (p &optional square-free) ; uses "x"
682 ;; Strip off multiples of x.
683 ((Math-zerop (car p))
685 (while (and p (Math-zerop (car p)))
686 (setq z (1+ z) p (cdr p)))
688 (setq p (math-factor-poly-coefs p square-free))
689 (setq p (math-sort-terms (math-factor-expr (car p)))))
690 (math-accum-factors x z (math-factor-protect p))))
692 ;; Factor out content.
693 ((and (not square-free)
694 (not (eq 1 (setq t1 (math-mul (math-poly-gcd-list p)
695 (if (math-guess-if-neg
696 (nth (1- (length p)) p))
698 (math-accum-factors t1 1 (math-factor-poly-coefs
699 (math-poly-div-list p t1) 'cont)))
701 ;; Check if linear in x.
703 (math-add (math-factor-protect
705 (math-factor-expr (car p))))
706 (math-mul x (math-factor-protect
708 (math-factor-expr (nth 1 p)))))))
710 ;; If symbolic coefficients, use FactorRules.
712 (while (and pp (or (Math-ratp (car pp))
713 (and (eq (car (car pp)) 'mod)
714 (Math-integerp (nth 1 (car pp)))
715 (Math-integerp (nth 2 (car pp))))))
718 (let ((res (math-rewrite
719 (list 'calcFunc-thecoefs x (cons 'vec p))
720 '(var FactorRules var-FactorRules))))
721 (or (and (eq (car-safe res) 'calcFunc-thefactors)
723 (math-vectorp (nth 2 res))
726 (while (setq vec (cdr vec))
727 (setq facs (math-accum-factors (car vec) 1 facs)))
729 (math-build-polynomial-expr p x))))
731 ;; Check if rational coefficients (i.e., not modulo a prime).
732 ((eq math-poly-modulus 1)
734 ;; Check if there are any squared terms, or a content not = 1.
735 (if (or (eq square-free t)
736 (equal (setq t1 (math-poly-gcd-coefs
737 p (setq t2 (math-poly-deriv-coefs p))))
740 ;; We now have a square-free polynomial with integer coefs.
741 ;; For now, we use a kludgey method that finds linear and
742 ;; quadratic terms using floating-point root-finding.
743 (if (setq t1 (let ((calc-symbolic-mode nil))
744 (math-poly-all-roots nil p t)))
745 (let ((roots (car t1))
746 (csign (if (math-negp (nth (1- (length p)) p)) -1 1))
751 (let ((coef0 (car (car roots)))
752 (coef1 (cdr (car roots))))
753 (setq expr (math-accum-factors
755 (let ((den (math-lcm-denoms
757 (setq scale (math-div scale den))
760 (math-mul den (math-pow x 2))
761 (math-mul (math-mul coef1 den) x))
762 (math-mul coef0 den)))
763 (let ((den (math-lcm-denoms coef0)))
764 (setq scale (math-div scale den))
765 (math-add (math-mul den x)
766 (math-mul coef0 den))))
769 (setq expr (math-accum-factors
772 (math-build-polynomial-expr
773 (math-mul-list (nth 1 t1) scale)
775 (math-build-polynomial-expr p x)) ; can't factor it.
777 ;; Separate out the squared terms (Knuth exercise 4.6.2-34).
778 ;; This step also divides out the content of the polynomial.
779 (let* ((cabs (math-poly-gcd-list p))
780 (csign (if (math-negp (nth (1- (length p)) p)) -1 1))
781 (t1s (math-mul-list t1 csign))
783 (v (car (math-poly-div-coefs p t1s)))
784 (w (car (math-poly-div-coefs t2 t1s))))
786 (not (math-poly-zerop
787 (setq t2 (math-poly-simplify
789 w 1 (math-poly-deriv-coefs v) -1)))))
790 (setq t1 (math-poly-gcd-coefs v t2)
792 v (car (math-poly-div-coefs v t1))
793 w (car (math-poly-div-coefs t2 t1))))
795 t2 (math-accum-factors (math-factor-poly-coefs v t)
798 (setq t2 (math-accum-factors (math-factor-poly-coefs
803 (math-accum-factors (math-mul cabs csign) 1 t2))))
805 ;; Factoring modulo a prime.
806 ((and (= (length (setq temp (math-poly-gcd-coefs
807 p (math-poly-deriv-coefs p))))
811 (setq temp (nthcdr (nth 2 math-poly-modulus) temp)
812 p (cons (car temp) p)))
813 (and (setq temp (math-factor-poly-coefs p))
814 (math-pow temp (nth 2 math-poly-modulus))))
816 (math-reject-arg nil "*Modulo factorization not yet implemented"))))
819 (defun math-poly-deriv-coefs (p)
822 (while (setq p (cdr p))
823 (setq dp (cons (math-mul (car p) n) dp)
828 (defun math-factor-contains (x a)
831 (if (memq (car-safe x) '(+ - * / neg))
833 (while (setq x (cdr x))
834 (setq sum (+ sum (math-factor-contains (car x) a))))
836 (if (and (eq (car-safe x) '^)
838 (* (math-factor-contains (nth 1 x) a) (nth 2 x))
846 ;;; Merge all quotients and expand/simplify the numerator
847 (defun calcFunc-nrat (expr)
848 (if (math-any-floats expr)
849 (setq expr (calcFunc-pfrac expr)))
850 (if (or (math-vectorp expr)
851 (assq (car-safe expr) calc-tweak-eqn-table))
852 (cons (car expr) (mapcar 'calcFunc-nrat (cdr expr)))
853 (let* ((calc-prefer-frac t)
854 (res (math-to-ratpoly expr))
855 (num (math-simplify (math-sort-terms (calcFunc-expand (car res)))))
856 (den (math-simplify (math-sort-terms (calcFunc-expand (cdr res)))))
857 (g (math-poly-gcd num den)))
859 (let ((num2 (math-poly-div num g))
860 (den2 (math-poly-div den g)))
861 (and (eq (cdr num2) 0) (eq (cdr den2) 0)
862 (setq num (car num2) den (car den2)))))
863 (math-simplify (math-div num den))))
866 ;;; Returns expressions (num . denom).
867 (defun math-to-ratpoly (expr)
868 (let ((res (math-to-ratpoly-rec expr)))
869 (cons (math-simplify (car res)) (math-simplify (cdr res))))
872 (defun math-to-ratpoly-rec (expr)
873 (cond ((Math-primp expr)
875 ((memq (car expr) '(+ -))
876 (let ((r1 (math-to-ratpoly-rec (nth 1 expr)))
877 (r2 (math-to-ratpoly-rec (nth 2 expr))))
878 (if (equal (cdr r1) (cdr r2))
879 (cons (list (car expr) (car r1) (car r2)) (cdr r1))
881 (cons (list (car expr)
882 (math-mul (car r1) (cdr r2))
886 (cons (list (car expr)
888 (math-mul (car r2) (cdr r1)))
890 (let ((g (math-poly-gcd (cdr r1) (cdr r2))))
891 (let ((d1 (and (not (eq g 1)) (math-poly-div (cdr r1) g)))
892 (d2 (and (not (eq g 1)) (math-poly-div
893 (math-mul (car r1) (cdr r2))
895 (if (and (eq (cdr d1) 0) (eq (cdr d2) 0))
896 (cons (list (car expr) (car d2)
897 (math-mul (car r2) (car d1)))
898 (math-mul (car d1) (cdr r2)))
899 (cons (list (car expr)
900 (math-mul (car r1) (cdr r2))
901 (math-mul (car r2) (cdr r1)))
902 (math-mul (cdr r1) (cdr r2)))))))))))
904 (let* ((r1 (math-to-ratpoly-rec (nth 1 expr)))
905 (r2 (math-to-ratpoly-rec (nth 2 expr)))
906 (g (math-mul (math-poly-gcd (car r1) (cdr r2))
907 (math-poly-gcd (cdr r1) (car r2)))))
909 (cons (math-mul (car r1) (car r2))
910 (math-mul (cdr r1) (cdr r2)))
911 (cons (math-poly-div-exact (math-mul (car r1) (car r2)) g)
912 (math-poly-div-exact (math-mul (cdr r1) (cdr r2)) g)))))
914 (let* ((r1 (math-to-ratpoly-rec (nth 1 expr)))
915 (r2 (math-to-ratpoly-rec (nth 2 expr))))
916 (if (and (eq (cdr r1) 1) (eq (cdr r2) 1))
917 (cons (car r1) (car r2))
918 (let ((g (math-mul (math-poly-gcd (car r1) (car r2))
919 (math-poly-gcd (cdr r1) (cdr r2)))))
921 (cons (math-mul (car r1) (cdr r2))
922 (math-mul (cdr r1) (car r2)))
923 (cons (math-poly-div-exact (math-mul (car r1) (cdr r2)) g)
924 (math-poly-div-exact (math-mul (cdr r1) (car r2))
926 ((and (eq (car expr) '^) (integerp (nth 2 expr)))
927 (let ((r1 (math-to-ratpoly-rec (nth 1 expr))))
928 (if (> (nth 2 expr) 0)
929 (cons (math-pow (car r1) (nth 2 expr))
930 (math-pow (cdr r1) (nth 2 expr)))
931 (cons (math-pow (cdr r1) (- (nth 2 expr)))
932 (math-pow (car r1) (- (nth 2 expr)))))))
933 ((eq (car expr) 'neg)
934 (let ((r1 (math-to-ratpoly-rec (nth 1 expr))))
935 (cons (math-neg (car r1)) (cdr r1))))
940 (defun math-ratpoly-p (expr &optional var)
941 (cond ((equal expr var) 1)
942 ((Math-primp expr) 0)
943 ((memq (car expr) '(+ -))
944 (let ((p1 (math-ratpoly-p (nth 1 expr) var))
946 (and p1 (setq p2 (math-ratpoly-p (nth 2 expr) var))
949 (let ((p1 (math-ratpoly-p (nth 1 expr) var))
951 (and p1 (setq p2 (math-ratpoly-p (nth 2 expr) var))
953 ((eq (car expr) 'neg)
954 (math-ratpoly-p (nth 1 expr) var))
956 (let ((p1 (math-ratpoly-p (nth 1 expr) var))
958 (and p1 (setq p2 (math-ratpoly-p (nth 2 expr) var))
960 ((and (eq (car expr) '^)
961 (integerp (nth 2 expr)))
962 (let ((p1 (math-ratpoly-p (nth 1 expr) var)))
963 (and p1 (* p1 (nth 2 expr)))))
965 ((math-poly-depends expr var) nil)
970 (defun calcFunc-apart (expr &optional var)
971 (cond ((Math-primp expr) expr)
973 (math-add (calcFunc-apart (nth 1 expr) var)
974 (calcFunc-apart (nth 2 expr) var)))
976 (math-sub (calcFunc-apart (nth 1 expr) var)
977 (calcFunc-apart (nth 2 expr) var)))
978 ((not (math-ratpoly-p expr var))
979 (math-reject-arg expr "Expected a rational function"))
981 (let* ((calc-prefer-frac t)
982 (rat (math-to-ratpoly expr))
985 (qr (math-poly-div num den))
989 (setq var (math-polynomial-base den)))
990 (math-add q (or (and var
991 (math-expr-contains den var)
992 (math-partial-fractions r den var))
993 (math-div r den))))))
997 (defun math-padded-polynomial (expr var deg)
998 (let ((p (math-is-polynomial expr var deg)))
999 (append p (make-list (- deg (length p)) 0)))
1002 (defun math-partial-fractions (r den var)
1003 (let* ((fden (calcFunc-factors den var))
1004 (tdeg (math-polynomial-p den var))
1009 (tz (make-list (1- tdeg) 0))
1010 (calc-matrix-mode 'scalar))
1011 (and (not (and (= (length fden) 2) (eq (nth 2 (nth 1 fden)) 1)))
1013 (while (setq fp (cdr fp))
1014 (let ((rpt (nth 2 (car fp)))
1015 (deg (math-polynomial-p (nth 1 (car fp)) var))
1021 (setq dvar (append '(vec) lz '(1) tz)
1025 dnum (math-add dnum (math-mul dvar
1026 (math-pow var deg2)))
1027 dlist (cons (and (= deg2 (1- deg))
1028 (math-pow (nth 1 (car fp)) rpt))
1032 (while (setq fpp (cdr fpp))
1034 (setq mult (math-mul mult
1035 (math-pow (nth 1 (car fpp))
1036 (nth 2 (car fpp)))))))
1037 (setq dnum (math-mul dnum mult)))
1038 (setq eqns (math-add eqns (math-mul dnum
1044 (setq eqns (math-div (cons 'vec (math-padded-polynomial r var tdeg))
1050 (cons 'vec (math-padded-polynomial
1053 (and (math-vectorp eqns)
1056 (setq eqns (nreverse eqns))
1058 (setq num (cons (car eqns) num)
1061 (setq num (math-build-polynomial-expr
1063 res (math-add res (math-div num (car dlist)))
1065 (setq dlist (cdr dlist)))
1066 (math-normalize res))))))
1071 (defun math-expand-term (expr)
1072 (cond ((and (eq (car-safe expr) '*)
1073 (memq (car-safe (nth 1 expr)) '(+ -)))
1074 (math-add-or-sub (list '* (nth 1 (nth 1 expr)) (nth 2 expr))
1075 (list '* (nth 2 (nth 1 expr)) (nth 2 expr))
1076 nil (eq (car (nth 1 expr)) '-)))
1077 ((and (eq (car-safe expr) '*)
1078 (memq (car-safe (nth 2 expr)) '(+ -)))
1079 (math-add-or-sub (list '* (nth 1 expr) (nth 1 (nth 2 expr)))
1080 (list '* (nth 1 expr) (nth 2 (nth 2 expr)))
1081 nil (eq (car (nth 2 expr)) '-)))
1082 ((and (eq (car-safe expr) '/)
1083 (memq (car-safe (nth 1 expr)) '(+ -)))
1084 (math-add-or-sub (list '/ (nth 1 (nth 1 expr)) (nth 2 expr))
1085 (list '/ (nth 2 (nth 1 expr)) (nth 2 expr))
1086 nil (eq (car (nth 1 expr)) '-)))
1087 ((and (eq (car-safe expr) '^)
1088 (memq (car-safe (nth 1 expr)) '(+ -))
1089 (integerp (nth 2 expr))
1090 (if (> (nth 2 expr) 0)
1091 (or (and (or (> mmt-many 500000) (< mmt-many -500000))
1092 (math-expand-power (nth 1 expr) (nth 2 expr)
1096 (list '^ (nth 1 expr) (1- (nth 2 expr)))))
1097 (if (< (nth 2 expr) 0)
1098 (list '/ 1 (list '^ (nth 1 expr) (- (nth 2 expr))))))))
1102 (defun calcFunc-expand (expr &optional many)
1103 (math-normalize (math-map-tree 'math-expand-term expr many))
1106 (defun math-expand-power (x n &optional var else-nil)
1107 (or (and (natnump n)
1108 (memq (car-safe x) '(+ -))
1111 (while (memq (car-safe x) '(+ -))
1112 (setq terms (cons (if (eq (car x) '-)
1113 (math-neg (nth 2 x))
1117 (setq terms (cons x terms))
1121 (or (math-expr-contains (car p) var)
1122 (setq terms (delq (car p) terms)
1123 cterms (cons (car p) cterms)))
1126 (setq terms (cons (apply 'calcFunc-add cterms)
1128 (if (= (length terms) 2)
1132 (setq accum (list '+ accum
1133 (list '* (calcFunc-choose n i)
1135 (list '^ (nth 1 terms) i)
1136 (list '^ (car terms)
1145 (setq accum (list '+ accum
1146 (list '^ (car p1) 2))
1148 (while (setq p2 (cdr p2))
1149 (setq accum (list '+ accum
1160 (setq accum (list '+ accum (list '^ (car p1) 3))
1162 (while (setq p2 (cdr p2))
1163 (setq accum (list '+
1169 (list '^ (car p1) 2)
1174 (list '^ (car p2) 2))))
1176 (while (setq p3 (cdr p3))
1177 (setq accum (list '+ accum
1190 (defun calcFunc-expandpow (x n)
1191 (math-normalize (math-expand-power x n))