1 ;;; calc-poly.el --- polynomial functions for Calc
3 ;; Copyright (C) 1990-1993, 2001-2014 Free Software Foundation, Inc.
5 ;; Author: David Gillespie <daveg@synaptics.com>
6 ;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; This file is autoloaded from calc-ext.el.
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 (defvar 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)
150 ;; calc-poly-div-remainder is a local variable for
151 ;; calc-poly-div (in calc-alg.el), but is used by
152 ;; calcFunc-pdiv, which is called by calc-poly-div.
153 (defvar calc-poly-div-remainder
)
155 (defun calcFunc-pdiv (pn pd
&optional base
)
156 (let* ((calc-prefer-frac t
)
157 (math-poly-modulus (math-poly-modulus pn pd
))
158 (res (math-poly-div pn pd base
)))
159 (setq calc-poly-div-remainder
(cdr res
))
162 ;;; Return only remainder to top of stack
163 (defun calcFunc-prem (pn pd
&optional base
)
164 (let ((calc-prefer-frac t
)
165 (math-poly-modulus (math-poly-modulus pn pd
)))
166 (cdr (math-poly-div pn pd base
))))
168 (defun calcFunc-pdivrem (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 (list 'vec
(car res
) (cdr res
))))
174 (defun calcFunc-pdivide (pn pd
&optional base
)
175 (let* ((calc-prefer-frac t
)
176 (math-poly-modulus (math-poly-modulus pn pd
))
177 (res (math-poly-div pn pd base
)))
178 (math-add (car res
) (math-div (cdr res
) pd
))))
181 ;;; Multiply two terms, expanding out products of sums.
182 (defun math-mul-thru (lhs rhs
)
183 (if (memq (car-safe lhs
) '(+ -
))
185 (math-mul-thru (nth 1 lhs
) rhs
)
186 (math-mul-thru (nth 2 lhs
) rhs
))
187 (if (memq (car-safe rhs
) '(+ -
))
189 (math-mul-thru lhs
(nth 1 rhs
))
190 (math-mul-thru lhs
(nth 2 rhs
)))
191 (math-mul lhs rhs
))))
193 (defun math-div-thru (num den
)
194 (if (memq (car-safe num
) '(+ -
))
196 (math-div-thru (nth 1 num
) den
)
197 (math-div-thru (nth 2 num
) den
))
201 ;;; Sort the terms of a sum into canonical order.
202 (defun math-sort-terms (expr)
203 (if (memq (car-safe expr
) '(+ -
))
205 (sort (math-sum-to-list expr
)
206 (function (lambda (a b
) (math-beforep (car a
) (car b
))))))
209 (defun math-list-to-sum (lst)
211 (list (if (cdr (car lst
)) '-
'+)
212 (math-list-to-sum (cdr lst
))
215 (math-neg (car (car lst
)))
218 (defun math-sum-to-list (tree &optional neg
)
219 (cond ((eq (car-safe tree
) '+)
220 (nconc (math-sum-to-list (nth 1 tree
) neg
)
221 (math-sum-to-list (nth 2 tree
) neg
)))
222 ((eq (car-safe tree
) '-
)
223 (nconc (math-sum-to-list (nth 1 tree
) neg
)
224 (math-sum-to-list (nth 2 tree
) (not neg
))))
225 (t (list (cons tree neg
)))))
227 ;;; Check if the polynomial coefficients are modulo forms.
228 (defun math-poly-modulus (expr &optional expr2
)
229 (or (math-poly-modulus-rec expr
)
230 (and expr2
(math-poly-modulus-rec expr2
))
233 (defun math-poly-modulus-rec (expr)
234 (if (and (eq (car-safe expr
) 'mod
) (Math-natnump (nth 2 expr
)))
235 (list 'mod
1 (nth 2 expr
))
236 (and (memq (car-safe expr
) '(+ -
* /))
237 (or (math-poly-modulus-rec (nth 1 expr
))
238 (math-poly-modulus-rec (nth 2 expr
))))))
241 ;;; Divide two polynomials. Return (quotient . remainder).
242 (defvar math-poly-div-base nil
)
243 (defun math-poly-div (u v
&optional math-poly-div-base
)
244 (if math-poly-div-base
245 (math-do-poly-div u v
)
246 (math-do-poly-div (calcFunc-expand u
) (calcFunc-expand v
))))
248 (defun math-poly-div-exact (u v
&optional base
)
249 (let ((res (math-poly-div u v base
)))
252 (math-reject-arg (list 'vec u v
) "Argument is not a polynomial"))))
254 (defun math-do-poly-div (u v
)
255 (cond ((math-constp u
)
257 (cons (math-div u v
) 0)
262 (if (memq (car-safe u
) '(+ -
))
263 (math-add-or-sub (math-poly-div-exact (nth 1 u
) v
)
264 (math-poly-div-exact (nth 2 u
) v
)
269 (cons math-poly-modulus
0))
270 ((and (math-atomic-factorp u
) (math-atomic-factorp v
))
271 (cons (math-simplify (math-div u v
)) 0))
273 (let ((base (or math-poly-div-base
274 (math-poly-div-base u v
)))
277 (null (setq vp
(math-is-polynomial v base nil
'gen
))))
279 (setq up
(math-is-polynomial u base nil
'gen
)
280 res
(math-poly-div-coefs up vp
))
281 (cons (math-build-polynomial-expr (car res
) base
)
282 (math-build-polynomial-expr (cdr res
) base
)))))))
284 (defun math-poly-div-rec (u v
)
285 (cond ((math-constp u
)
290 (if (memq (car-safe u
) '(+ -
))
291 (math-add-or-sub (math-poly-div-rec (nth 1 u
) v
)
292 (math-poly-div-rec (nth 2 u
) v
)
295 ((Math-equal u v
) math-poly-modulus
)
296 ((and (math-atomic-factorp u
) (math-atomic-factorp v
))
297 (math-simplify (math-div u v
)))
301 (let ((base (math-poly-div-base u v
))
304 (null (setq vp
(math-is-polynomial v base nil
'gen
))))
306 (setq up
(math-is-polynomial u base nil
'gen
)
307 res
(math-poly-div-coefs up vp
))
308 (math-add (math-build-polynomial-expr (car res
) base
)
309 (math-div (math-build-polynomial-expr (cdr res
) base
)
312 ;;; Divide two polynomials in coefficient-list form. Return (quot . rem).
313 (defun math-poly-div-coefs (u v
)
314 (cond ((null v
) (math-reject-arg nil
"Division by zero"))
315 ((< (length u
) (length v
)) (cons nil u
))
321 (let ((qk (math-poly-div-rec (math-simplify (car urev
))
325 (if (or q
(not (math-zerop qk
)))
326 (setq q
(cons qk q
)))
327 (while (setq up
(cdr up
) vp
(cdr vp
))
328 (setcar up
(math-sub (car up
) (math-mul-thru qk
(car vp
)))))
329 (setq urev
(cdr urev
))
331 (while (and urev
(Math-zerop (car urev
)))
332 (setq urev
(cdr urev
)))
333 (cons q
(nreverse (mapcar 'math-simplify urev
)))))
335 (cons (list (math-poly-div-rec (car u
) (car v
)))
338 ;;; Perform a pseudo-division of polynomials. (See Knuth section 4.6.1.)
339 ;;; This returns only the remainder from the pseudo-division.
340 (defun math-poly-pseudo-div (u v
)
342 ((< (length u
) (length v
)) u
)
343 ((or (cdr u
) (cdr v
))
344 (let ((urev (reverse u
))
350 (while (setq up
(cdr up
) vp
(cdr vp
))
351 (setcar up
(math-sub (math-mul-thru (car vrev
) (car up
))
352 (math-mul-thru (car urev
) (car vp
)))))
353 (setq urev
(cdr urev
))
356 (setcar up
(math-mul-thru (car vrev
) (car up
)))
358 (while (and urev
(Math-zerop (car urev
)))
359 (setq urev
(cdr urev
)))
360 (nreverse (mapcar 'math-simplify urev
))))
363 ;;; Compute the GCD of two multivariate polynomials.
364 (defun math-poly-gcd (u v
)
365 (cond ((Math-equal u v
) u
)
369 (calcFunc-gcd u
(calcFunc-pcont v
))))
373 (calcFunc-gcd v
(calcFunc-pcont u
))))
375 (let ((base (math-poly-gcd-base u v
)))
379 (math-build-polynomial-expr
380 (math-poly-gcd-coefs (math-is-polynomial u base nil
'gen
)
381 (math-is-polynomial v base nil
'gen
))
383 (calcFunc-gcd (calcFunc-pcont u
) (calcFunc-pcont u
)))))))
385 (defun math-poly-div-list (lst a
)
389 (math-mul-list lst a
)
390 (mapcar (function (lambda (x) (math-poly-div-exact x a
))) lst
))))
392 (defun math-mul-list (lst a
)
396 (mapcar 'math-neg lst
)
398 (mapcar (function (lambda (x) (math-mul x a
))) lst
)))))
400 ;;; Run GCD on all elements in a list.
401 (defun math-poly-gcd-list (lst)
402 (if (or (memq 1 lst
) (memq -
1 lst
))
403 (math-poly-gcd-frac-list lst
)
404 (let ((gcd (car lst
)))
405 (while (and (setq lst
(cdr lst
)) (not (eq gcd
1)))
407 (setq gcd
(math-poly-gcd gcd
(car lst
)))))
408 (if lst
(setq lst
(math-poly-gcd-frac-list lst
)))
411 (defun math-poly-gcd-frac-list (lst)
412 (while (and lst
(not (eq (car-safe (car lst
)) 'frac
)))
413 (setq lst
(cdr lst
)))
415 (let ((denom (nth 2 (car lst
))))
416 (while (setq lst
(cdr lst
))
417 (if (eq (car-safe (car lst
)) 'frac
)
418 (setq denom
(calcFunc-lcm denom
(nth 2 (car lst
))))))
419 (list 'frac
1 denom
))
422 ;;; Compute the GCD of two univariate polynomial lists.
423 ;;; Knuth section 4.6.1, algorithm C.
424 (defun math-poly-gcd-coefs (u v
)
425 (let ((d (math-poly-gcd (math-poly-gcd-list u
)
426 (math-poly-gcd-list v
)))
427 (g 1) (h 1) (z 0) hh r delta ghd
)
428 (while (and u v
(Math-zerop (car u
)) (Math-zerop (car v
)))
429 (setq u
(cdr u
) v
(cdr v
) z
(1+ z
)))
431 (setq u
(math-poly-div-list u d
)
432 v
(math-poly-div-list v d
)))
434 (setq delta
(- (length u
) (length v
)))
436 (setq r u u v v r delta
(- delta
)))
437 (setq r
(math-poly-pseudo-div u v
))
440 v
(math-poly-div-list r
(math-mul g
(math-pow h delta
)))
441 g
(nth (1- (length u
)) u
)
443 (math-mul (math-pow g delta
) (math-pow h
(- 1 delta
)))
444 (math-poly-div-exact (math-pow g delta
)
445 (math-pow h
(1- delta
))))))
448 (math-mul-list (math-poly-div-list v
(math-poly-gcd-list v
)) d
)))
449 (if (math-guess-if-neg (nth (1- (length v
)) v
))
450 (setq v
(math-mul-list v -
1)))
451 (while (>= (setq z
(1- z
)) 0)
456 ;;; Return true if is a factor containing no sums or quotients.
457 (defun math-atomic-factorp (expr)
458 (cond ((eq (car-safe expr
) '*)
459 (and (math-atomic-factorp (nth 1 expr
))
460 (math-atomic-factorp (nth 2 expr
))))
461 ((memq (car-safe expr
) '(+ -
/))
463 ((memq (car-safe expr
) '(^ neg
))
464 (math-atomic-factorp (nth 1 expr
)))
467 ;;; Find a suitable base for dividing a by b.
468 ;;; The base must exist in both expressions.
469 ;;; The degree in the numerator must be higher or equal than the
470 ;;; degree in the denominator.
471 ;;; If the above conditions are not met the quotient is just a remainder.
472 ;;; Return nil if this is the case.
474 (defun math-poly-div-base (a b
)
476 (and (setq a-base
(math-total-polynomial-base a
))
477 (setq b-base
(math-total-polynomial-base b
))
480 (let ((maybe (assoc (car (car a-base
)) b-base
)))
482 (if (>= (nth 1 (car a-base
)) (nth 1 maybe
))
483 (throw 'return
(car (car a-base
))))))
484 (setq a-base
(cdr a-base
)))))))
486 ;;; Same as above but for gcd algorithm.
487 ;;; Here there is no requirement that degree(a) > degree(b).
488 ;;; Take the base that has the highest degree considering both a and b.
489 ;;; ("a^20+b^21+x^3+a+b", "a+b^2+x^5+a^22+b^10") --> (a 22)
491 (defun math-poly-gcd-base (a b
)
493 (and (setq a-base
(math-total-polynomial-base a
))
494 (setq b-base
(math-total-polynomial-base b
))
496 (while (and a-base b-base
)
497 (if (> (nth 1 (car a-base
)) (nth 1 (car b-base
)))
498 (if (assoc (car (car a-base
)) b-base
)
499 (throw 'return
(car (car a-base
)))
500 (setq a-base
(cdr a-base
)))
501 (if (assoc (car (car b-base
)) a-base
)
502 (throw 'return
(car (car b-base
)))
503 (setq b-base
(cdr b-base
)))))))))
505 ;;; Sort a list of polynomial bases.
506 (defun math-sort-poly-base-list (lst)
507 (sort lst
(function (lambda (a b
)
508 (or (> (nth 1 a
) (nth 1 b
))
509 (and (= (nth 1 a
) (nth 1 b
))
510 (math-beforep (car a
) (car b
))))))))
512 ;;; Given an expression find all variables that are polynomial bases.
513 ;;; Return list in the form '( (var1 degree1) (var2 degree2) ... ).
515 ;; The variable math-poly-base-total-base is local to
516 ;; math-total-polynomial-base, but is used by math-polynomial-p1,
517 ;; which is called by math-total-polynomial-base.
518 (defvar math-poly-base-total-base
)
520 (defun math-total-polynomial-base (expr)
521 (let ((math-poly-base-total-base nil
))
522 (math-polynomial-base expr
'math-polynomial-p1
)
523 (math-sort-poly-base-list math-poly-base-total-base
)))
525 ;; The variable math-poly-base-top-expr is local to math-polynomial-base
526 ;; in calc-alg.el, but is used by math-polynomial-p1 which is called
527 ;; by math-polynomial-base.
528 (defvar math-poly-base-top-expr
)
530 (defun math-polynomial-p1 (subexpr)
531 (or (assoc subexpr math-poly-base-total-base
)
532 (memq (car subexpr
) '(+ -
* / neg
))
533 (and (eq (car subexpr
) '^
) (natnump (nth 2 subexpr
)))
534 (let* ((math-poly-base-variable subexpr
)
535 (exponent (math-polynomial-p math-poly-base-top-expr subexpr
)))
537 (setq math-poly-base-total-base
(cons (list subexpr exponent
)
538 math-poly-base-total-base
)))))
541 ;; The variable math-factored-vars is local to calcFunc-factors and
542 ;; calcFunc-factor, but is used by math-factor-expr and
543 ;; math-factor-expr-part, which are called (directly and indirectly) by
544 ;; calcFunc-factor and calcFunc-factors.
545 (defvar math-factored-vars
)
547 ;; The variable math-fact-expr is local to calcFunc-factors,
548 ;; calcFunc-factor and math-factor-expr, but is used by math-factor-expr-try
549 ;; and math-factor-expr-part, which are called (directly and indirectly) by
550 ;; calcFunc-factor, calcFunc-factors and math-factor-expr.
551 (defvar math-fact-expr
)
553 ;; The variable math-to-list is local to calcFunc-factors and
554 ;; calcFunc-factor, but is used by math-accum-factors, which is
555 ;; called (indirectly) by calcFunc-factors and calcFunc-factor.
556 (defvar math-to-list
)
558 (defun calcFunc-factors (math-fact-expr &optional var
)
559 (let ((math-factored-vars (if var t nil
))
561 (calc-prefer-frac t
))
563 (setq var
(math-polynomial-base math-fact-expr
)))
564 (let ((res (math-factor-finish
565 (or (catch 'factor
(math-factor-expr-try var
))
567 (math-simplify (if (math-vectorp res
)
569 (list 'vec
(list 'vec res
1)))))))
571 (defun calcFunc-factor (math-fact-expr &optional var
)
572 (let ((math-factored-vars nil
)
574 (calc-prefer-frac t
))
575 (math-simplify (math-factor-finish
577 (let ((math-factored-vars t
))
578 (or (catch 'factor
(math-factor-expr-try var
)) math-fact-expr
))
579 (math-factor-expr math-fact-expr
))))))
581 (defun math-factor-finish (x)
584 (if (eq (car x
) 'calcFunc-Fac-Prot
)
585 (math-factor-finish (nth 1 x
))
586 (cons (car x
) (mapcar 'math-factor-finish
(cdr x
))))))
588 (defun math-factor-protect (x)
589 (if (memq (car-safe x
) '(+ -
))
590 (list 'calcFunc-Fac-Prot x
)
593 (defun math-factor-expr (math-fact-expr)
594 (cond ((eq math-factored-vars t
) math-fact-expr
)
595 ((or (memq (car-safe math-fact-expr
) '(* / ^ neg
))
596 (assq (car-safe math-fact-expr
) calc-tweak-eqn-table
))
597 (cons (car math-fact-expr
) (mapcar 'math-factor-expr
(cdr math-fact-expr
))))
598 ((memq (car-safe math-fact-expr
) '(+ -
))
599 (let* ((math-factored-vars math-factored-vars
)
600 (y (catch 'factor
(math-factor-expr-part math-fact-expr
))))
606 (defun math-factor-expr-part (x) ; uses "expr"
607 (if (memq (car-safe x
) '(+ -
* / ^ neg
))
608 (while (setq x
(cdr x
))
609 (math-factor-expr-part (car x
)))
610 (and (not (Math-objvecp x
))
611 (not (assoc x math-factored-vars
))
612 (> (math-factor-contains math-fact-expr x
) 1)
613 (setq math-factored-vars
(cons (list x
) math-factored-vars
))
614 (math-factor-expr-try x
))))
616 ;; The variable math-fet-x is local to math-factor-expr-try, but is
617 ;; used by math-factor-poly-coefs, which is called by math-factor-expr-try.
620 (defun math-factor-expr-try (math-fet-x)
621 (if (eq (car-safe math-fact-expr
) '*)
622 (let ((res1 (catch 'factor
(let ((math-fact-expr (nth 1 math-fact-expr
)))
623 (math-factor-expr-try math-fet-x
))))
624 (res2 (catch 'factor
(let ((math-fact-expr (nth 2 math-fact-expr
)))
625 (math-factor-expr-try math-fet-x
)))))
627 (throw 'factor
(math-accum-factors (or res1
(nth 1 math-fact-expr
)) 1
628 (or res2
(nth 2 math-fact-expr
))))))
629 (let* ((p (math-is-polynomial math-fact-expr math-fet-x
30 'gen
))
630 (math-poly-modulus (math-poly-modulus math-fact-expr
))
633 (setq res
(math-factor-poly-coefs p
))
634 (throw 'factor res
)))))
636 (defun math-accum-factors (fac pow facs
)
638 (if (math-vectorp fac
)
640 (while (setq fac
(cdr fac
))
641 (setq facs
(math-accum-factors (nth 1 (car fac
))
642 (* pow
(nth 2 (car fac
)))
645 (if (and (eq (car-safe fac
) '^
) (natnump (nth 2 fac
)))
646 (setq pow
(* pow
(nth 2 fac
))
650 (or (math-vectorp facs
)
651 (setq facs
(if (eq facs
1) '(vec)
652 (list 'vec
(list 'vec facs
1)))))
654 (while (and (setq found
(cdr found
))
655 (not (equal fac
(nth 1 (car found
))))))
658 (setcar (cdr (cdr (car found
))) (+ pow
(nth 2 (car found
))))
660 ;; Put constant term first.
661 (if (and (cdr facs
) (Math-ratp (nth 1 (nth 1 facs
))))
662 (cons 'vec
(cons (nth 1 facs
) (cons (list 'vec fac pow
)
664 (cons 'vec
(cons (list 'vec fac pow
) (cdr facs
))))))))
665 (math-mul (math-pow fac pow
) (math-factor-protect facs
))))
667 (defun math-factor-poly-coefs (p &optional square-free
) ; uses "x"
672 ;; Strip off multiples of math-fet-x.
673 ((Math-zerop (car p
))
675 (while (and p
(Math-zerop (car p
)))
676 (setq z
(1+ z
) p
(cdr p
)))
678 (setq p
(math-factor-poly-coefs p square-free
))
679 (setq p
(math-sort-terms (math-factor-expr (car p
)))))
680 (math-accum-factors math-fet-x z
(math-factor-protect p
))))
682 ;; Factor out content.
683 ((and (not square-free
)
684 (not (eq 1 (setq t1
(math-mul (math-poly-gcd-list p
)
685 (if (math-guess-if-neg
686 (nth (1- (length p
)) p
))
688 (math-accum-factors t1
1 (math-factor-poly-coefs
689 (math-poly-div-list p t1
) 'cont
)))
691 ;; Check if linear in math-fet-x.
694 (math-add (math-factor-protect
696 (math-factor-expr (car p
))))
697 (math-mul math-fet-x
(math-factor-protect
699 (math-factor-expr (nth 1 p
))))))))
701 ;; If symbolic coefficients, use FactorRules.
703 (while (and pp
(or (Math-ratp (car pp
))
704 (and (eq (car (car pp
)) 'mod
)
705 (Math-integerp (nth 1 (car pp
)))
706 (Math-integerp (nth 2 (car pp
))))))
709 (let ((res (math-rewrite
710 (list 'calcFunc-thecoefs math-fet-x
(cons 'vec p
))
711 '(var FactorRules var-FactorRules
))))
712 (or (and (eq (car-safe res
) 'calcFunc-thefactors
)
714 (math-vectorp (nth 2 res
))
717 (while (setq vec
(cdr vec
))
718 (setq facs
(math-accum-factors (car vec
) 1 facs
)))
720 (math-build-polynomial-expr p math-fet-x
))))
722 ;; Check if rational coefficients (i.e., not modulo a prime).
723 ((eq math-poly-modulus
1)
725 ;; Check if there are any squared terms, or a content not = 1.
726 (if (or (eq square-free t
)
727 (equal (setq t1
(math-poly-gcd-coefs
728 p
(setq t2
(math-poly-deriv-coefs p
))))
731 ;; We now have a square-free polynomial with integer coefs.
732 ;; For now, we use a kludgy method that finds linear and
733 ;; quadratic terms using floating-point root-finding.
734 (if (setq t1
(let ((calc-symbolic-mode nil
))
735 (math-poly-all-roots nil p t
)))
736 (let ((roots (car t1
))
737 (csign (if (math-negp (nth (1- (length p
)) p
)) -
1 1))
742 (let ((coef0 (car (car roots
)))
743 (coef1 (cdr (car roots
))))
744 (setq expr
(math-accum-factors
746 (let ((den (math-lcm-denoms
748 (setq scale
(math-div scale den
))
751 (math-mul den
(math-pow math-fet-x
2))
752 (math-mul (math-mul coef1 den
)
754 (math-mul coef0 den
)))
755 (let ((den (math-lcm-denoms coef0
)))
756 (setq scale
(math-div scale den
))
757 (math-add (math-mul den math-fet-x
)
758 (math-mul coef0 den
))))
761 (setq expr
(math-accum-factors
764 (math-build-polynomial-expr
765 (math-mul-list (nth 1 t1
) scale
)
767 (math-build-polynomial-expr p math-fet-x
)) ; can't factor it.
769 ;; Separate out the squared terms (Knuth exercise 4.6.2-34).
770 ;; This step also divides out the content of the polynomial.
771 (let* ((cabs (math-poly-gcd-list p
))
772 (csign (if (math-negp (nth (1- (length p
)) p
)) -
1 1))
773 (t1s (math-mul-list t1 csign
))
775 (v (car (math-poly-div-coefs p t1s
)))
776 (w (car (math-poly-div-coefs t2 t1s
))))
778 (not (math-poly-zerop
779 (setq t2
(math-poly-simplify
781 w
1 (math-poly-deriv-coefs v
) -
1)))))
782 (setq t1
(math-poly-gcd-coefs v t2
)
784 v
(car (math-poly-div-coefs v t1
))
785 w
(car (math-poly-div-coefs t2 t1
))))
787 t2
(math-accum-factors (math-factor-poly-coefs v t
)
790 (setq t2
(math-accum-factors (math-factor-poly-coefs
795 (math-accum-factors (math-mul cabs csign
) 1 t2
))))
797 ;; Factoring modulo a prime.
798 ((and (= (length (setq temp
(math-poly-gcd-coefs
799 p
(math-poly-deriv-coefs p
))))
803 (setq temp
(nthcdr (nth 2 math-poly-modulus
) temp
)
804 p
(cons (car temp
) p
)))
805 (and (setq temp
(math-factor-poly-coefs p
))
806 (math-pow temp
(nth 2 math-poly-modulus
))))
808 (math-reject-arg nil
"*Modulo factorization not yet implemented")))))
810 (defun math-poly-deriv-coefs (p)
813 (while (setq p
(cdr p
))
814 (setq dp
(cons (math-mul (car p
) n
) dp
)
818 (defun math-factor-contains (x a
)
821 (if (memq (car-safe x
) '(+ -
* / neg
))
823 (while (setq x
(cdr x
))
824 (setq sum
(+ sum
(math-factor-contains (car x
) a
))))
826 (if (and (eq (car-safe x
) '^
)
828 (* (math-factor-contains (nth 1 x
) a
) (nth 2 x
))
835 ;;; Merge all quotients and expand/simplify the numerator
836 (defun calcFunc-nrat (expr)
837 (if (math-any-floats expr
)
838 (setq expr
(calcFunc-pfrac expr
)))
839 (if (or (math-vectorp expr
)
840 (assq (car-safe expr
) calc-tweak-eqn-table
))
841 (cons (car expr
) (mapcar 'calcFunc-nrat
(cdr expr
)))
842 (let* ((calc-prefer-frac t
)
843 (res (math-to-ratpoly expr
))
844 (num (math-simplify (math-sort-terms (calcFunc-expand (car res
)))))
845 (den (math-simplify (math-sort-terms (calcFunc-expand (cdr res
)))))
846 (g (math-poly-gcd num den
)))
848 (let ((num2 (math-poly-div num g
))
849 (den2 (math-poly-div den g
)))
850 (and (eq (cdr num2
) 0) (eq (cdr den2
) 0)
851 (setq num
(car num2
) den
(car den2
)))))
852 (math-simplify (math-div num den
)))))
854 ;;; Returns expressions (num . denom).
855 (defun math-to-ratpoly (expr)
856 (let ((res (math-to-ratpoly-rec expr
)))
857 (cons (math-simplify (car res
)) (math-simplify (cdr res
)))))
859 (defun math-to-ratpoly-rec (expr)
860 (cond ((Math-primp expr
)
862 ((memq (car expr
) '(+ -
))
863 (let ((r1 (math-to-ratpoly-rec (nth 1 expr
)))
864 (r2 (math-to-ratpoly-rec (nth 2 expr
))))
865 (if (equal (cdr r1
) (cdr r2
))
866 (cons (list (car expr
) (car r1
) (car r2
)) (cdr r1
))
868 (cons (list (car expr
)
869 (math-mul (car r1
) (cdr r2
))
873 (cons (list (car expr
)
875 (math-mul (car r2
) (cdr r1
)))
877 (let ((g (math-poly-gcd (cdr r1
) (cdr r2
))))
878 (let ((d1 (and (not (eq g
1)) (math-poly-div (cdr r1
) g
)))
879 (d2 (and (not (eq g
1)) (math-poly-div
880 (math-mul (car r1
) (cdr r2
))
882 (if (and (eq (cdr d1
) 0) (eq (cdr d2
) 0))
883 (cons (list (car expr
) (car d2
)
884 (math-mul (car r2
) (car d1
)))
885 (math-mul (car d1
) (cdr r2
)))
886 (cons (list (car expr
)
887 (math-mul (car r1
) (cdr r2
))
888 (math-mul (car r2
) (cdr r1
)))
889 (math-mul (cdr r1
) (cdr r2
)))))))))))
891 (let* ((r1 (math-to-ratpoly-rec (nth 1 expr
)))
892 (r2 (math-to-ratpoly-rec (nth 2 expr
)))
893 (g (math-mul (math-poly-gcd (car r1
) (cdr r2
))
894 (math-poly-gcd (cdr r1
) (car r2
)))))
896 (cons (math-mul (car r1
) (car r2
))
897 (math-mul (cdr r1
) (cdr r2
)))
898 (cons (math-poly-div-exact (math-mul (car r1
) (car r2
)) g
)
899 (math-poly-div-exact (math-mul (cdr r1
) (cdr r2
)) g
)))))
901 (let* ((r1 (math-to-ratpoly-rec (nth 1 expr
)))
902 (r2 (math-to-ratpoly-rec (nth 2 expr
))))
903 (if (and (eq (cdr r1
) 1) (eq (cdr r2
) 1))
904 (cons (car r1
) (car r2
))
905 (let ((g (math-mul (math-poly-gcd (car r1
) (car r2
))
906 (math-poly-gcd (cdr r1
) (cdr r2
)))))
908 (cons (math-mul (car r1
) (cdr r2
))
909 (math-mul (cdr r1
) (car r2
)))
910 (cons (math-poly-div-exact (math-mul (car r1
) (cdr r2
)) g
)
911 (math-poly-div-exact (math-mul (cdr r1
) (car r2
))
913 ((and (eq (car expr
) '^
) (integerp (nth 2 expr
)))
914 (let ((r1 (math-to-ratpoly-rec (nth 1 expr
))))
915 (if (> (nth 2 expr
) 0)
916 (cons (math-pow (car r1
) (nth 2 expr
))
917 (math-pow (cdr r1
) (nth 2 expr
)))
918 (cons (math-pow (cdr r1
) (- (nth 2 expr
)))
919 (math-pow (car r1
) (- (nth 2 expr
)))))))
920 ((eq (car expr
) 'neg
)
921 (let ((r1 (math-to-ratpoly-rec (nth 1 expr
))))
922 (cons (math-neg (car r1
)) (cdr r1
))))
926 (defun math-ratpoly-p (expr &optional var
)
927 (cond ((equal expr var
) 1)
928 ((Math-primp expr
) 0)
929 ((memq (car expr
) '(+ -
))
930 (let ((p1 (math-ratpoly-p (nth 1 expr
) var
))
932 (and p1
(setq p2
(math-ratpoly-p (nth 2 expr
) var
))
935 (let ((p1 (math-ratpoly-p (nth 1 expr
) var
))
937 (and p1
(setq p2
(math-ratpoly-p (nth 2 expr
) var
))
939 ((eq (car expr
) 'neg
)
940 (math-ratpoly-p (nth 1 expr
) var
))
942 (let ((p1 (math-ratpoly-p (nth 1 expr
) var
))
944 (and p1
(setq p2
(math-ratpoly-p (nth 2 expr
) var
))
946 ((and (eq (car expr
) '^
)
947 (integerp (nth 2 expr
)))
948 (let ((p1 (math-ratpoly-p (nth 1 expr
) var
)))
949 (and p1
(* p1
(nth 2 expr
)))))
951 ((math-poly-depends expr var
) nil
)
955 (defun calcFunc-apart (expr &optional var
)
956 (cond ((Math-primp expr
) expr
)
958 (math-add (calcFunc-apart (nth 1 expr
) var
)
959 (calcFunc-apart (nth 2 expr
) var
)))
961 (math-sub (calcFunc-apart (nth 1 expr
) var
)
962 (calcFunc-apart (nth 2 expr
) var
)))
963 ((and var
(not (math-ratpoly-p expr var
)))
964 (math-reject-arg expr
"Expected a rational function"))
966 (let* ((calc-prefer-frac t
)
967 (rat (math-to-ratpoly expr
))
971 (setq var
(math-polynomial-base den
)))
972 (if (not (math-ratpoly-p expr var
))
973 (math-reject-arg expr
"Expected a rational function")
974 (let* ((qr (math-poly-div num den
))
977 (math-add q
(or (and var
978 (math-expr-contains den var
)
979 (math-partial-fractions r den var
))
980 (math-div r den
)))))))))
983 (defun math-padded-polynomial (expr var deg
)
984 "Return a polynomial as list of coefficients.
985 If EXPR is of the form \"a + bx + cx^2 + ...\" in the variable VAR, return
986 the list (a b c ...) with at least DEG elements, else return NIL."
987 (let ((p (math-is-polynomial expr var deg
)))
988 (append p
(make-list (- deg
(length p
)) 0))))
990 (defun math-partial-fractions (r den var
)
991 "Return R divided by DEN expressed in partial fractions of VAR.
992 All whole factors of DEN have already been split off from R.
993 If no partial fraction representation can be found, return nil."
994 (let* ((fden (calcFunc-factors den var
))
995 (tdeg (math-polynomial-p den var
))
1000 (tz (make-list (1- tdeg
) 0))
1001 (calc-matrix-mode 'scalar
))
1002 (and (not (and (= (length fden
) 2) (eq (nth 2 (nth 1 fden
)) 1)))
1004 (while (setq fp
(cdr fp
))
1005 (let ((rpt (nth 2 (car fp
)))
1006 (deg (math-polynomial-p (nth 1 (car fp
)) var
))
1012 (setq dvar
(append '(vec) lz
'(1) tz
)
1016 dnum
(math-add dnum
(math-mul dvar
1017 (math-pow var deg2
)))
1018 dlist
(cons (and (= deg2
(1- deg
))
1019 (math-pow (nth 1 (car fp
)) rpt
))
1023 (while (setq fpp
(cdr fpp
))
1025 (setq mult
(math-mul mult
1026 (math-pow (nth 1 (car fpp
))
1027 (nth 2 (car fpp
)))))))
1028 (setq dnum
(math-mul dnum mult
)))
1029 (setq eqns
(math-add eqns
(math-mul dnum
1035 (setq eqns
(math-div (cons 'vec
(math-padded-polynomial r var tdeg
))
1041 (cons 'vec
(math-padded-polynomial
1044 (and (math-vectorp eqns
)
1047 (setq eqns
(nreverse eqns
))
1049 (setq num
(cons (car eqns
) num
)
1052 (setq num
(math-build-polynomial-expr
1054 res
(math-add res
(math-div num
(car dlist
)))
1056 (setq dlist
(cdr dlist
)))
1057 (math-normalize res
)))))))
1061 (defun math-expand-term (expr)
1062 (cond ((and (eq (car-safe expr
) '*)
1063 (memq (car-safe (nth 1 expr
)) '(+ -
)))
1064 (math-add-or-sub (list '* (nth 1 (nth 1 expr
)) (nth 2 expr
))
1065 (list '* (nth 2 (nth 1 expr
)) (nth 2 expr
))
1066 nil
(eq (car (nth 1 expr
)) '-
)))
1067 ((and (eq (car-safe expr
) '*)
1068 (memq (car-safe (nth 2 expr
)) '(+ -
)))
1069 (math-add-or-sub (list '* (nth 1 expr
) (nth 1 (nth 2 expr
)))
1070 (list '* (nth 1 expr
) (nth 2 (nth 2 expr
)))
1071 nil
(eq (car (nth 2 expr
)) '-
)))
1072 ((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 1 expr
)) '(+ -
))
1079 (integerp (nth 2 expr
))
1081 (or (math-known-matrixp (nth 1 (nth 1 expr
)))
1082 (math-known-matrixp (nth 2 (nth 1 expr
)))
1085 (not (eq calc-matrix-mode
'scalar
))
1086 (not (and (math-known-scalarp (nth 1 (nth 1 expr
)))
1087 (math-known-scalarp (nth 2 (nth 1 expr
)))))))
1089 (if (= (nth 2 expr
) 2)
1090 (math-add-or-sub (list '* (nth 1 (nth 1 expr
)) (nth 1 expr
))
1091 (list '* (nth 2 (nth 1 expr
)) (nth 1 expr
))
1092 nil
(eq (car (nth 1 expr
)) '-
))
1093 (math-add-or-sub (list '* (nth 1 (nth 1 expr
))
1094 (list '^
(nth 1 expr
)
1096 (list '* (nth 2 (nth 1 expr
))
1097 (list '^
(nth 1 expr
)
1099 nil
(eq (car (nth 1 expr
)) '-
)))
1100 (if (> (nth 2 expr
) 0)
1101 (or (and (or (> math-mt-many
500000) (< math-mt-many -
500000))
1102 (math-expand-power (nth 1 expr
) (nth 2 expr
)
1106 (list '^
(nth 1 expr
) (1- (nth 2 expr
)))))
1107 (if (< (nth 2 expr
) 0)
1108 (list '/ 1 (list '^
(nth 1 expr
) (- (nth 2 expr
)))))))))
1111 (defun calcFunc-expand (expr &optional many
)
1112 (math-normalize (math-map-tree 'math-expand-term expr many
)))
1114 (defun math-expand-power (x n
&optional var else-nil
)
1115 (or (and (natnump n
)
1116 (memq (car-safe x
) '(+ -
))
1119 (while (memq (car-safe x
) '(+ -
))
1120 (setq terms
(cons (if (eq (car x
) '-
)
1121 (math-neg (nth 2 x
))
1125 (setq terms
(cons x terms
))
1129 (or (math-expr-contains (car p
) var
)
1130 (setq terms
(delq (car p
) terms
)
1131 cterms
(cons (car p
) cterms
)))
1134 (setq terms
(cons (apply 'calcFunc-add cterms
)
1136 (if (= (length terms
) 2)
1140 (setq accum
(list '+ accum
1141 (list '* (calcFunc-choose n i
)
1143 (list '^
(nth 1 terms
) i
)
1144 (list '^
(car terms
)
1153 (setq accum
(list '+ accum
1154 (list '^
(car p1
) 2))
1156 (while (setq p2
(cdr p2
))
1157 (setq accum
(list '+ accum
1168 (setq accum
(list '+ accum
(list '^
(car p1
) 3))
1170 (while (setq p2
(cdr p2
))
1171 (setq accum
(list '+
1177 (list '^
(car p1
) 2)
1182 (list '^
(car p2
) 2))))
1184 (while (setq p3
(cdr p3
))
1185 (setq accum
(list '+ accum
1197 (defun calcFunc-expandpow (x n
)
1198 (math-normalize (math-expand-power x n
)))
1200 (provide 'calc-poly
)
1202 ;;; calc-poly.el ends here