Mercurial > emacs
annotate lisp/calc/calc-poly.el @ 63092:80ef8a2a052d
(debug): Don't bury the buffer unless it's in a dedicated window.
| author | Stefan Monnier <monnier@iro.umontreal.ca> |
|---|---|
| date | Mon, 06 Jun 2005 19:47:05 +0000 |
| parents | 7e497f7bc556 |
| children | 1db49616ce05 f042e7c0fe20 |
| rev | line source |
|---|---|
|
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
1 ;;; calc-poly.el --- polynomial functions for Calc |
|
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
2 |
|
62442
842c8b2c2940
Update years in copyright notice; nfc.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
62165
diff
changeset
|
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2005 Free Software Foundation, Inc. |
|
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
4 |
|
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
5 ;; Author: David Gillespie <daveg@synaptics.com> |
|
58302
78096f4dea9d
(calc-poly-div-remainder): Declared it.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58104
diff
changeset
|
6 ;; Maintainer: Jay Belanger <belanger@truman.edu> |
| 40785 | 7 |
| 8 ;; This file is part of GNU Emacs. | |
| 9 | |
| 10 ;; GNU Emacs is distributed in the hope that it will be useful, | |
| 11 ;; but WITHOUT ANY WARRANTY. No author or distributor | |
| 12 ;; accepts responsibility to anyone for the consequences of using it | |
| 13 ;; or for whether it serves any particular purpose or works at all, | |
| 14 ;; unless he says so in writing. Refer to the GNU Emacs General Public | |
| 15 ;; License for full details. | |
| 16 | |
| 17 ;; Everyone is granted permission to copy, modify and redistribute | |
| 18 ;; GNU Emacs, but only under the conditions described in the | |
| 19 ;; GNU Emacs General Public License. A copy of this license is | |
| 20 ;; supposed to have been given to you along with GNU Emacs so you | |
| 21 ;; can know your rights and responsibilities. It should be in a | |
| 22 ;; file named COPYING. Among other things, the copyright notice | |
| 23 ;; and this notice must be preserved on all copies. | |
| 24 | |
|
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
25 ;;; Commentary: |
| 40785 | 26 |
|
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
27 ;;; Code: |
| 40785 | 28 |
| 29 ;; This file is autoloaded from calc-ext.el. | |
|
58667
88f98983accf
Add a provide statement.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58507
diff
changeset
|
30 |
| 40785 | 31 (require 'calc-ext) |
| 32 (require 'calc-macs) | |
| 33 | |
| 34 (defun calcFunc-pcont (expr &optional var) | |
| 35 (cond ((Math-primp expr) | |
| 36 (cond ((Math-zerop expr) 1) | |
| 37 ((Math-messy-integerp expr) (math-trunc expr)) | |
| 38 ((Math-objectp expr) expr) | |
| 39 ((or (equal expr var) (not var)) 1) | |
| 40 (t expr))) | |
| 41 ((eq (car expr) '*) | |
| 42 (math-mul (calcFunc-pcont (nth 1 expr) var) | |
| 43 (calcFunc-pcont (nth 2 expr) var))) | |
| 44 ((eq (car expr) '/) | |
| 45 (math-div (calcFunc-pcont (nth 1 expr) var) | |
| 46 (calcFunc-pcont (nth 2 expr) var))) | |
| 47 ((and (eq (car expr) '^) (Math-natnump (nth 2 expr))) | |
| 48 (math-pow (calcFunc-pcont (nth 1 expr) var) (nth 2 expr))) | |
| 49 ((memq (car expr) '(neg polar)) | |
| 50 (calcFunc-pcont (nth 1 expr) var)) | |
| 51 ((consp var) | |
| 52 (let ((p (math-is-polynomial expr var))) | |
| 53 (if p | |
| 54 (let ((lead (nth (1- (length p)) p)) | |
| 55 (cont (math-poly-gcd-list p))) | |
| 56 (if (math-guess-if-neg lead) | |
| 57 (math-neg cont) | |
| 58 cont)) | |
| 59 1))) | |
| 60 ((memq (car expr) '(+ - cplx sdev)) | |
| 61 (let ((cont (calcFunc-pcont (nth 1 expr) var))) | |
| 62 (if (eq cont 1) | |
| 63 1 | |
| 64 (let ((c2 (calcFunc-pcont (nth 2 expr) var))) | |
| 65 (if (and (math-negp cont) | |
| 66 (if (eq (car expr) '-) (math-posp c2) (math-negp c2))) | |
| 67 (math-neg (math-poly-gcd cont c2)) | |
| 68 (math-poly-gcd cont c2)))))) | |
| 69 (var expr) | |
|
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
70 (t 1))) |
| 40785 | 71 |
| 72 (defun calcFunc-pprim (expr &optional var) | |
| 73 (let ((cont (calcFunc-pcont expr var))) | |
| 74 (if (math-equal-int cont 1) | |
| 75 expr | |
|
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
76 (math-poly-div-exact expr cont var)))) |
| 40785 | 77 |
| 78 (defun math-div-poly-const (expr c) | |
| 79 (cond ((memq (car-safe expr) '(+ -)) | |
| 80 (list (car expr) | |
| 81 (math-div-poly-const (nth 1 expr) c) | |
| 82 (math-div-poly-const (nth 2 expr) c))) | |
|
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
83 (t (math-div expr c)))) |
| 40785 | 84 |
| 85 (defun calcFunc-pdeg (expr &optional var) | |
| 86 (if (Math-zerop expr) | |
| 87 '(neg (var inf var-inf)) | |
| 88 (if var | |
| 89 (or (math-polynomial-p expr var) | |
| 90 (math-reject-arg expr "Expected a polynomial")) | |
|
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
91 (math-poly-degree expr)))) |
| 40785 | 92 |
| 93 (defun math-poly-degree (expr) | |
| 94 (cond ((Math-primp expr) | |
| 95 (if (eq (car-safe expr) 'var) 1 0)) | |
| 96 ((eq (car expr) 'neg) | |
| 97 (math-poly-degree (nth 1 expr))) | |
| 98 ((eq (car expr) '*) | |
| 99 (+ (math-poly-degree (nth 1 expr)) | |
| 100 (math-poly-degree (nth 2 expr)))) | |
| 101 ((eq (car expr) '/) | |
| 102 (- (math-poly-degree (nth 1 expr)) | |
| 103 (math-poly-degree (nth 2 expr)))) | |
| 104 ((and (eq (car expr) '^) (natnump (nth 2 expr))) | |
| 105 (* (math-poly-degree (nth 1 expr)) (nth 2 expr))) | |
| 106 ((memq (car expr) '(+ -)) | |
| 107 (max (math-poly-degree (nth 1 expr)) | |
| 108 (math-poly-degree (nth 2 expr)))) | |
|
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
109 (t 1))) |
| 40785 | 110 |
| 111 (defun calcFunc-plead (expr var) | |
| 112 (cond ((eq (car-safe expr) '*) | |
| 113 (math-mul (calcFunc-plead (nth 1 expr) var) | |
| 114 (calcFunc-plead (nth 2 expr) var))) | |
| 115 ((eq (car-safe expr) '/) | |
| 116 (math-div (calcFunc-plead (nth 1 expr) var) | |
| 117 (calcFunc-plead (nth 2 expr) var))) | |
| 118 ((and (eq (car-safe expr) '^) (math-natnump (nth 2 expr))) | |
| 119 (math-pow (calcFunc-plead (nth 1 expr) var) (nth 2 expr))) | |
| 120 ((Math-primp expr) | |
| 121 (if (equal expr var) | |
| 122 1 | |
| 123 expr)) | |
| 124 (t | |
| 125 (let ((p (math-is-polynomial expr var))) | |
| 126 (if (cdr p) | |
| 127 (nth (1- (length p)) p) | |
|
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
128 1))))) |
| 40785 | 129 |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 ;;; Polynomial quotient, remainder, and GCD. | |
| 135 ;;; Originally by Ove Ewerlid (ewerlid@mizar.DoCS.UU.SE). | |
| 136 ;;; Modifications and simplifications by daveg. | |
| 137 | |
|
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
138 (defvar math-poly-modulus 1) |
| 40785 | 139 |
| 140 ;;; Return gcd of two polynomials | |
| 141 (defun calcFunc-pgcd (pn pd) | |
| 142 (if (math-any-floats pn) | |
| 143 (math-reject-arg pn "Coefficients must be rational")) | |
| 144 (if (math-any-floats pd) | |
| 145 (math-reject-arg pd "Coefficients must be rational")) | |
| 146 (let ((calc-prefer-frac t) | |
| 147 (math-poly-modulus (math-poly-modulus pn pd))) | |
|
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
148 (math-poly-gcd pn pd))) |
| 40785 | 149 |
| 150 ;;; Return only quotient to top of stack (nil if zero) | |
|
58302
78096f4dea9d
(calc-poly-div-remainder): Declared it.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58104
diff
changeset
|
151 |
|
78096f4dea9d
(calc-poly-div-remainder): Declared it.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58104
diff
changeset
|
152 ;; calc-poly-div-remainder is a local variable for |
|
78096f4dea9d
(calc-poly-div-remainder): Declared it.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58104
diff
changeset
|
153 ;; calc-poly-div (in calc-alg.el), but is used by |
|
78096f4dea9d
(calc-poly-div-remainder): Declared it.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58104
diff
changeset
|
154 ;; calcFunc-pdiv, which is called by calc-poly-div. |
|
78096f4dea9d
(calc-poly-div-remainder): Declared it.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58104
diff
changeset
|
155 (defvar calc-poly-div-remainder) |
|
78096f4dea9d
(calc-poly-div-remainder): Declared it.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58104
diff
changeset
|
156 |
| 40785 | 157 (defun calcFunc-pdiv (pn pd &optional base) |
| 158 (let* ((calc-prefer-frac t) | |
| 159 (math-poly-modulus (math-poly-modulus pn pd)) | |
| 160 (res (math-poly-div pn pd base))) | |
| 161 (setq calc-poly-div-remainder (cdr res)) | |
|
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
162 (car res))) |
| 40785 | 163 |
| 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))) | |
|
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
168 (cdr (math-poly-div pn pd base)))) |
| 40785 | 169 |
| 170 (defun calcFunc-pdivrem (pn pd &optional base) | |
| 171 (let* ((calc-prefer-frac t) | |
| 172 (math-poly-modulus (math-poly-modulus pn pd)) | |
| 173 (res (math-poly-div pn pd base))) | |
|
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
174 (list 'vec (car res) (cdr res)))) |
| 40785 | 175 |
| 176 (defun calcFunc-pdivide (pn pd &optional base) | |
| 177 (let* ((calc-prefer-frac t) | |
| 178 (math-poly-modulus (math-poly-modulus pn pd)) | |
| 179 (res (math-poly-div pn pd base))) | |
|
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
180 (math-add (car res) (math-div (cdr res) pd)))) |
| 40785 | 181 |
| 182 | |
| 183 ;;; Multiply two terms, expanding out products of sums. | |
| 184 (defun math-mul-thru (lhs rhs) | |
| 185 (if (memq (car-safe lhs) '(+ -)) | |
| 186 (list (car lhs) | |
| 187 (math-mul-thru (nth 1 lhs) rhs) | |
| 188 (math-mul-thru (nth 2 lhs) rhs)) | |
| 189 (if (memq (car-safe rhs) '(+ -)) | |
| 190 (list (car rhs) | |
| 191 (math-mul-thru lhs (nth 1 rhs)) | |
| 192 (math-mul-thru lhs (nth 2 rhs))) | |
|
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
193 (math-mul lhs rhs)))) |
| 40785 | 194 |
| 195 (defun math-div-thru (num den) | |
| 196 (if (memq (car-safe num) '(+ -)) | |
| 197 (list (car num) | |
| 198 (math-div-thru (nth 1 num) den) | |
| 199 (math-div-thru (nth 2 num) den)) | |
|
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
200 (math-div num den))) |
| 40785 | 201 |
| 202 | |
| 203 ;;; Sort the terms of a sum into canonical order. | |
| 204 (defun math-sort-terms (expr) | |
| 205 (if (memq (car-safe expr) '(+ -)) | |
| 206 (math-list-to-sum | |
| 207 (sort (math-sum-to-list expr) | |
| 208 (function (lambda (a b) (math-beforep (car a) (car b)))))) | |
|
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
209 expr)) |
| 40785 | 210 |
| 211 (defun math-list-to-sum (lst) | |
| 212 (if (cdr lst) | |
| 213 (list (if (cdr (car lst)) '- '+) | |
| 214 (math-list-to-sum (cdr lst)) | |
| 215 (car (car lst))) | |
| 216 (if (cdr (car lst)) | |
| 217 (math-neg (car (car lst))) | |
|
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
218 (car (car lst))))) |
| 40785 | 219 |
| 220 (defun math-sum-to-list (tree &optional neg) | |
| 221 (cond ((eq (car-safe tree) '+) | |
| 222 (nconc (math-sum-to-list (nth 1 tree) neg) | |
| 223 (math-sum-to-list (nth 2 tree) neg))) | |
| 224 ((eq (car-safe tree) '-) | |
| 225 (nconc (math-sum-to-list (nth 1 tree) neg) | |
| 226 (math-sum-to-list (nth 2 tree) (not neg)))) | |
|
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
227 (t (list (cons tree neg))))) |
| 40785 | 228 |
| 229 ;;; Check if the polynomial coefficients are modulo forms. | |
| 230 (defun math-poly-modulus (expr &optional expr2) | |
| 231 (or (math-poly-modulus-rec expr) | |
| 232 (and expr2 (math-poly-modulus-rec expr2)) | |
|
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
233 1)) |
| 40785 | 234 |
| 235 (defun math-poly-modulus-rec (expr) | |
| 236 (if (and (eq (car-safe expr) 'mod) (Math-natnump (nth 2 expr))) | |
| 237 (list 'mod 1 (nth 2 expr)) | |
| 238 (and (memq (car-safe expr) '(+ - * /)) | |
| 239 (or (math-poly-modulus-rec (nth 1 expr)) | |
|
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
240 (math-poly-modulus-rec (nth 2 expr)))))) |
| 40785 | 241 |
| 242 | |
| 243 ;;; Divide two polynomials. Return (quotient . remainder). | |
|
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
244 (defvar math-poly-div-base nil) |
| 40785 | 245 (defun math-poly-div (u v &optional math-poly-div-base) |
| 246 (if math-poly-div-base | |
| 247 (math-do-poly-div u v) | |
|
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
248 (math-do-poly-div (calcFunc-expand u) (calcFunc-expand v)))) |
| 40785 | 249 |
| 250 (defun math-poly-div-exact (u v &optional base) | |
| 251 (let ((res (math-poly-div u v base))) | |
| 252 (if (eq (cdr res) 0) | |
| 253 (car res) | |
|
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
254 (math-reject-arg (list 'vec u v) "Argument is not a polynomial")))) |
| 40785 | 255 |
| 256 (defun math-do-poly-div (u v) | |
| 257 (cond ((math-constp u) | |
| 258 (if (math-constp v) | |
| 259 (cons (math-div u v) 0) | |
| 260 (cons 0 u))) | |
| 261 ((math-constp v) | |
| 262 (cons (if (eq v 1) | |
| 263 u | |
| 264 (if (memq (car-safe u) '(+ -)) | |
| 265 (math-add-or-sub (math-poly-div-exact (nth 1 u) v) | |
| 266 (math-poly-div-exact (nth 2 u) v) | |
| 267 nil (eq (car u) '-)) | |
| 268 (math-div u v))) | |
| 269 0)) | |
| 270 ((Math-equal u v) | |
| 271 (cons math-poly-modulus 0)) | |
| 272 ((and (math-atomic-factorp u) (math-atomic-factorp v)) | |
| 273 (cons (math-simplify (math-div u v)) 0)) | |
| 274 (t | |
| 275 (let ((base (or math-poly-div-base | |
| 276 (math-poly-div-base u v))) | |
| 277 vp up res) | |
| 278 (if (or (null base) | |
| 279 (null (setq vp (math-is-polynomial v base nil 'gen)))) | |
| 280 (cons 0 u) | |
| 281 (setq up (math-is-polynomial u base nil 'gen) | |
| 282 res (math-poly-div-coefs up vp)) | |
| 283 (cons (math-build-polynomial-expr (car res) base) | |
|
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
284 (math-build-polynomial-expr (cdr res) base))))))) |
| 40785 | 285 |
| 286 (defun math-poly-div-rec (u v) | |
| 287 (cond ((math-constp u) | |
| 288 (math-div u v)) | |
| 289 ((math-constp v) | |
| 290 (if (eq v 1) | |
| 291 u | |
| 292 (if (memq (car-safe u) '(+ -)) | |
| 293 (math-add-or-sub (math-poly-div-rec (nth 1 u) v) | |
| 294 (math-poly-div-rec (nth 2 u) v) | |
| 295 nil (eq (car u) '-)) | |
| 296 (math-div u v)))) | |
| 297 ((Math-equal u v) math-poly-modulus) | |
| 298 ((and (math-atomic-factorp u) (math-atomic-factorp v)) | |
| 299 (math-simplify (math-div u v))) | |
| 300 (math-poly-div-base | |
| 301 (math-div u v)) | |
| 302 (t | |
| 303 (let ((base (math-poly-div-base u v)) | |
| 304 vp up res) | |
| 305 (if (or (null base) | |
| 306 (null (setq vp (math-is-polynomial v base nil 'gen)))) | |
| 307 (math-div u v) | |
| 308 (setq up (math-is-polynomial u base nil 'gen) | |
| 309 res (math-poly-div-coefs up vp)) | |
| 310 (math-add (math-build-polynomial-expr (car res) base) | |
| 311 (math-div (math-build-polynomial-expr (cdr res) base) | |
|
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
312 v))))))) |
| 40785 | 313 |
| 314 ;;; Divide two polynomials in coefficient-list form. Return (quot . rem). | |
| 315 (defun math-poly-div-coefs (u v) | |
| 316 (cond ((null v) (math-reject-arg nil "Division by zero")) | |
| 317 ((< (length u) (length v)) (cons nil u)) | |
| 318 ((cdr u) | |
| 319 (let ((q nil) | |
| 320 (urev (reverse u)) | |
| 321 (vrev (reverse v))) | |
| 322 (while | |
| 323 (let ((qk (math-poly-div-rec (math-simplify (car urev)) | |
| 324 (car vrev))) | |
| 325 (up urev) | |
| 326 (vp vrev)) | |
| 327 (if (or q (not (math-zerop qk))) | |
| 328 (setq q (cons qk q))) | |
| 329 (while (setq up (cdr up) vp (cdr vp)) | |
| 330 (setcar up (math-sub (car up) (math-mul-thru qk (car vp))))) | |
| 331 (setq urev (cdr urev)) | |
| 332 up)) | |
| 333 (while (and urev (Math-zerop (car urev))) | |
| 334 (setq urev (cdr urev))) | |
| 335 (cons q (nreverse (mapcar 'math-simplify urev))))) | |
| 336 (t | |
| 337 (cons (list (math-poly-div-rec (car u) (car v))) | |
|
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
338 nil)))) |
| 40785 | 339 |
| 340 ;;; Perform a pseudo-division of polynomials. (See Knuth section 4.6.1.) | |
| 341 ;;; This returns only the remainder from the pseudo-division. | |
| 342 (defun math-poly-pseudo-div (u v) | |
| 343 (cond ((null v) nil) | |
| 344 ((< (length u) (length v)) u) | |
| 345 ((or (cdr u) (cdr v)) | |
| 346 (let ((urev (reverse u)) | |
| 347 (vrev (reverse v)) | |
| 348 up) | |
| 349 (while | |
| 350 (let ((vp vrev)) | |
| 351 (setq up urev) | |
| 352 (while (setq up (cdr up) vp (cdr vp)) | |
| 353 (setcar up (math-sub (math-mul-thru (car vrev) (car up)) | |
| 354 (math-mul-thru (car urev) (car vp))))) | |
| 355 (setq urev (cdr urev)) | |
| 356 up) | |
| 357 (while up | |
| 358 (setcar up (math-mul-thru (car vrev) (car up))) | |
| 359 (setq up (cdr up)))) | |
| 360 (while (and urev (Math-zerop (car urev))) | |
| 361 (setq urev (cdr urev))) | |
| 362 (nreverse (mapcar 'math-simplify urev)))) | |
|
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
363 (t nil))) |
| 40785 | 364 |
| 365 ;;; Compute the GCD of two multivariate polynomials. | |
| 366 (defun math-poly-gcd (u v) | |
| 367 (cond ((Math-equal u v) u) | |
| 368 ((math-constp u) | |
| 369 (if (Math-zerop u) | |
| 370 v | |
| 371 (calcFunc-gcd u (calcFunc-pcont v)))) | |
| 372 ((math-constp v) | |
| 373 (if (Math-zerop v) | |
| 374 v | |
| 375 (calcFunc-gcd v (calcFunc-pcont u)))) | |
| 376 (t | |
| 377 (let ((base (math-poly-gcd-base u v))) | |
| 378 (if base | |
| 379 (math-simplify | |
| 380 (calcFunc-expand | |
| 381 (math-build-polynomial-expr | |
| 382 (math-poly-gcd-coefs (math-is-polynomial u base nil 'gen) | |
| 383 (math-is-polynomial v base nil 'gen)) | |
| 384 base))) | |
|
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
385 (calcFunc-gcd (calcFunc-pcont u) (calcFunc-pcont u))))))) |
| 40785 | 386 |
| 387 (defun math-poly-div-list (lst a) | |
| 388 (if (eq a 1) | |
| 389 lst | |
| 390 (if (eq a -1) | |
| 391 (math-mul-list lst a) | |
|
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
392 (mapcar (function (lambda (x) (math-poly-div-exact x a))) lst)))) |
| 40785 | 393 |
| 394 (defun math-mul-list (lst a) | |
| 395 (if (eq a 1) | |
| 396 lst | |
| 397 (if (eq a -1) | |
| 398 (mapcar 'math-neg lst) | |
| 399 (and (not (eq a 0)) | |
|
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
400 (mapcar (function (lambda (x) (math-mul x a))) lst))))) |
| 40785 | 401 |
| 402 ;;; Run GCD on all elements in a list. | |
| 403 (defun math-poly-gcd-list (lst) | |
| 404 (if (or (memq 1 lst) (memq -1 lst)) | |
| 405 (math-poly-gcd-frac-list lst) | |
| 406 (let ((gcd (car lst))) | |
| 407 (while (and (setq lst (cdr lst)) (not (eq gcd 1))) | |
| 408 (or (eq (car lst) 0) | |
| 409 (setq gcd (math-poly-gcd gcd (car lst))))) | |
| 410 (if lst (setq lst (math-poly-gcd-frac-list lst))) | |
|
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
411 gcd))) |
| 40785 | 412 |
| 413 (defun math-poly-gcd-frac-list (lst) | |
| 414 (while (and lst (not (eq (car-safe (car lst)) 'frac))) | |
| 415 (setq lst (cdr lst))) | |
| 416 (if lst | |
| 417 (let ((denom (nth 2 (car lst)))) | |
| 418 (while (setq lst (cdr lst)) | |
| 419 (if (eq (car-safe (car lst)) 'frac) | |
| 420 (setq denom (calcFunc-lcm denom (nth 2 (car lst)))))) | |
| 421 (list 'frac 1 denom)) | |
|
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
422 1)) |
| 40785 | 423 |
| 424 ;;; Compute the GCD of two monovariate polynomial lists. | |
| 425 ;;; Knuth section 4.6.1, algorithm C. | |
| 426 (defun math-poly-gcd-coefs (u v) | |
| 427 (let ((d (math-poly-gcd (math-poly-gcd-list u) | |
| 428 (math-poly-gcd-list v))) | |
| 429 (g 1) (h 1) (z 0) hh r delta ghd) | |
| 430 (while (and u v (Math-zerop (car u)) (Math-zerop (car v))) | |
| 431 (setq u (cdr u) v (cdr v) z (1+ z))) | |
| 432 (or (eq d 1) | |
| 433 (setq u (math-poly-div-list u d) | |
| 434 v (math-poly-div-list v d))) | |
| 435 (while (progn | |
| 436 (setq delta (- (length u) (length v))) | |
| 437 (if (< delta 0) | |
| 438 (setq r u u v v r delta (- delta))) | |
| 439 (setq r (math-poly-pseudo-div u v)) | |
| 440 (cdr r)) | |
| 441 (setq u v | |
| 442 v (math-poly-div-list r (math-mul g (math-pow h delta))) | |
| 443 g (nth (1- (length u)) u) | |
| 444 h (if (<= delta 1) | |
| 445 (math-mul (math-pow g delta) (math-pow h (- 1 delta))) | |
| 446 (math-poly-div-exact (math-pow g delta) | |
| 447 (math-pow h (1- delta)))))) | |
| 448 (setq v (if r | |
| 449 (list d) | |
| 450 (math-mul-list (math-poly-div-list v (math-poly-gcd-list v)) d))) | |
| 451 (if (math-guess-if-neg (nth (1- (length v)) v)) | |
| 452 (setq v (math-mul-list v -1))) | |
| 453 (while (>= (setq z (1- z)) 0) | |
| 454 (setq v (cons 0 v))) | |
|
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
455 v)) |
| 40785 | 456 |
| 457 | |
| 458 ;;; Return true if is a factor containing no sums or quotients. | |
| 459 (defun math-atomic-factorp (expr) | |
| 460 (cond ((eq (car-safe expr) '*) | |
| 461 (and (math-atomic-factorp (nth 1 expr)) | |
| 462 (math-atomic-factorp (nth 2 expr)))) | |
| 463 ((memq (car-safe expr) '(+ - /)) | |
| 464 nil) | |
| 465 ((memq (car-safe expr) '(^ neg)) | |
| 466 (math-atomic-factorp (nth 1 expr))) | |
|
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
467 (t t))) |
| 40785 | 468 |
| 469 ;;; Find a suitable base for dividing a by b. | |
| 470 ;;; The base must exist in both expressions. | |
| 471 ;;; The degree in the numerator must be higher or equal than the | |
| 472 ;;; degree in the denominator. | |
| 473 ;;; If the above conditions are not met the quotient is just a remainder. | |
| 474 ;;; Return nil if this is the case. | |
| 475 | |
| 476 (defun math-poly-div-base (a b) | |
| 477 (let (a-base b-base) | |
| 478 (and (setq a-base (math-total-polynomial-base a)) | |
| 479 (setq b-base (math-total-polynomial-base b)) | |
| 480 (catch 'return | |
| 481 (while a-base | |
| 482 (let ((maybe (assoc (car (car a-base)) b-base))) | |
| 483 (if maybe | |
| 484 (if (>= (nth 1 (car a-base)) (nth 1 maybe)) | |
| 485 (throw 'return (car (car a-base)))))) | |
|
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
486 (setq a-base (cdr a-base))))))) |
| 40785 | 487 |
| 488 ;;; Same as above but for gcd algorithm. | |
| 489 ;;; Here there is no requirement that degree(a) > degree(b). | |
| 490 ;;; Take the base that has the highest degree considering both a and b. | |
| 491 ;;; ("a^20+b^21+x^3+a+b", "a+b^2+x^5+a^22+b^10") --> (a 22) | |
| 492 | |
| 493 (defun math-poly-gcd-base (a b) | |
| 494 (let (a-base b-base) | |
| 495 (and (setq a-base (math-total-polynomial-base a)) | |
| 496 (setq b-base (math-total-polynomial-base b)) | |
| 497 (catch 'return | |
| 498 (while (and a-base b-base) | |
| 499 (if (> (nth 1 (car a-base)) (nth 1 (car b-base))) | |
| 500 (if (assoc (car (car a-base)) b-base) | |
| 501 (throw 'return (car (car a-base))) | |
| 502 (setq a-base (cdr a-base))) | |
| 503 (if (assoc (car (car b-base)) a-base) | |
| 504 (throw 'return (car (car b-base))) | |
|
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
505 (setq b-base (cdr b-base))))))))) |
| 40785 | 506 |
| 507 ;;; Sort a list of polynomial bases. | |
| 508 (defun math-sort-poly-base-list (lst) | |
| 509 (sort lst (function (lambda (a b) | |
| 510 (or (> (nth 1 a) (nth 1 b)) | |
| 511 (and (= (nth 1 a) (nth 1 b)) | |
|
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
512 (math-beforep (car a) (car b)))))))) |
| 40785 | 513 |
| 514 ;;; Given an expression find all variables that are polynomial bases. | |
| 515 ;;; Return list in the form '( (var1 degree1) (var2 degree2) ... ). | |
|
58507
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
516 |
|
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
517 ;; The variable math-poly-base-total-base is local to |
|
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
518 ;; math-total-polynomial-base, but is used by math-polynomial-p1, |
|
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
519 ;; which is called by math-total-polynomial-base. |
|
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
520 (defvar math-poly-base-total-base) |
|
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
521 |
| 40785 | 522 (defun math-total-polynomial-base (expr) |
|
58507
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
523 (let ((math-poly-base-total-base nil)) |
| 40785 | 524 (math-polynomial-base expr 'math-polynomial-p1) |
|
58507
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
525 (math-sort-poly-base-list math-poly-base-total-base))) |
|
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
526 |
|
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
527 ;; The variable math-poly-base-top-expr is local to math-polynomial-base |
|
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
528 ;; in calc-alg.el, but is used by math-polynomial-p1 which is called |
|
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
529 ;; by math-polynomial-base. |
|
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
530 (defvar math-poly-base-top-expr) |
| 40785 | 531 |
| 532 (defun math-polynomial-p1 (subexpr) | |
|
58507
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
533 (or (assoc subexpr math-poly-base-total-base) |
| 40785 | 534 (memq (car subexpr) '(+ - * / neg)) |
| 535 (and (eq (car subexpr) '^) (natnump (nth 2 subexpr))) | |
| 536 (let* ((math-poly-base-variable subexpr) | |
|
58507
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
537 (exponent (math-polynomial-p math-poly-base-top-expr subexpr))) |
| 40785 | 538 (if exponent |
|
58507
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
539 (setq math-poly-base-total-base (cons (list subexpr exponent) |
|
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
540 math-poly-base-total-base))))) |
|
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
541 nil) |
| 40785 | 542 |
|
58507
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
543 ;; The variable math-factored-vars is local to calcFunc-factors and |
|
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
544 ;; calcFunc-factor, but is used by math-factor-expr and |
|
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
545 ;; math-factor-expr-part, which are called (directly and indirectly) by |
|
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
546 ;; calcFunc-factor and calcFunc-factors. |
|
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
547 (defvar math-factored-vars) |
| 40785 | 548 |
|
58507
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
549 ;; The variable math-fact-expr is local to calcFunc-factors, |
|
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
550 ;; calcFunc-factor and math-factor-expr, but is used by math-factor-expr-try |
|
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
551 ;; and math-factor-expr-part, which are called (directly and indirectly) by |
|
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
552 ;; calcFunc-factor, calcFunc-factors and math-factor-expr. |
|
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
553 (defvar math-fact-expr) |
| 40785 | 554 |
|
58507
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
555 ;; The variable math-to-list is local to calcFunc-factors and |
|
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
556 ;; calcFunc-factor, but is used by math-accum-factors, which is |
|
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
557 ;; called (indirectly) by calcFunc-factors and calcFunc-factor. |
|
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
558 (defvar math-to-list) |
|
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
559 |
|
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
560 (defun calcFunc-factors (math-fact-expr &optional var) |
| 40785 | 561 (let ((math-factored-vars (if var t nil)) |
| 562 (math-to-list t) | |
| 563 (calc-prefer-frac t)) | |
| 564 (or var | |
|
58507
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
565 (setq var (math-polynomial-base math-fact-expr))) |
| 40785 | 566 (let ((res (math-factor-finish |
| 567 (or (catch 'factor (math-factor-expr-try var)) | |
|
58507
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
568 math-fact-expr)))) |
| 40785 | 569 (math-simplify (if (math-vectorp res) |
| 570 res | |
|
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
571 (list 'vec (list 'vec res 1))))))) |
| 40785 | 572 |
|
58507
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
573 (defun calcFunc-factor (math-fact-expr &optional var) |
| 40785 | 574 (let ((math-factored-vars nil) |
| 575 (math-to-list nil) | |
| 576 (calc-prefer-frac t)) | |
| 577 (math-simplify (math-factor-finish | |
| 578 (if var | |
| 579 (let ((math-factored-vars t)) | |
|
58507
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
580 (or (catch 'factor (math-factor-expr-try var)) math-fact-expr)) |
|
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
581 (math-factor-expr math-fact-expr)))))) |
| 40785 | 582 |
| 583 (defun math-factor-finish (x) | |
| 584 (if (Math-primp x) | |
| 585 x | |
| 586 (if (eq (car x) 'calcFunc-Fac-Prot) | |
| 587 (math-factor-finish (nth 1 x)) | |
|
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
588 (cons (car x) (mapcar 'math-factor-finish (cdr x)))))) |
| 40785 | 589 |
| 590 (defun math-factor-protect (x) | |
| 591 (if (memq (car-safe x) '(+ -)) | |
| 592 (list 'calcFunc-Fac-Prot x) | |
|
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
593 x)) |
| 40785 | 594 |
|
58507
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
595 (defun math-factor-expr (math-fact-expr) |
|
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
596 (cond ((eq math-factored-vars t) math-fact-expr) |
|
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
597 ((or (memq (car-safe math-fact-expr) '(* / ^ neg)) |
|
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
598 (assq (car-safe math-fact-expr) calc-tweak-eqn-table)) |
|
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
599 (cons (car math-fact-expr) (mapcar 'math-factor-expr (cdr math-fact-expr)))) |
|
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
600 ((memq (car-safe math-fact-expr) '(+ -)) |
| 40785 | 601 (let* ((math-factored-vars math-factored-vars) |
|
58507
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
602 (y (catch 'factor (math-factor-expr-part math-fact-expr)))) |
| 40785 | 603 (if y |
| 604 (math-factor-expr y) | |
|
58507
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
605 math-fact-expr))) |
|
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
606 (t math-fact-expr))) |
| 40785 | 607 |
| 608 (defun math-factor-expr-part (x) ; uses "expr" | |
| 609 (if (memq (car-safe x) '(+ - * / ^ neg)) | |
| 610 (while (setq x (cdr x)) | |
| 611 (math-factor-expr-part (car x))) | |
| 612 (and (not (Math-objvecp x)) | |
| 613 (not (assoc x math-factored-vars)) | |
|
58507
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
614 (> (math-factor-contains math-fact-expr x) 1) |
| 40785 | 615 (setq math-factored-vars (cons (list x) math-factored-vars)) |
|
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
616 (math-factor-expr-try x)))) |
| 40785 | 617 |
|
58507
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
618 ;; The variable math-fet-x is local to math-factor-expr-try, but is |
|
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
619 ;; used by math-factor-poly-coefs, which is called by math-factor-expr-try. |
|
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
620 (defvar math-fet-x) |
|
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
621 |
|
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
622 (defun math-factor-expr-try (math-fet-x) |
|
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
623 (if (eq (car-safe math-fact-expr) '*) |
|
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
624 (let ((res1 (catch 'factor (let ((math-fact-expr (nth 1 math-fact-expr))) |
|
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
625 (math-factor-expr-try math-fet-x)))) |
|
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
626 (res2 (catch 'factor (let ((math-fact-expr (nth 2 math-fact-expr))) |
|
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
627 (math-factor-expr-try math-fet-x))))) |
| 40785 | 628 (and (or res1 res2) |
|
58507
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
629 (throw 'factor (math-accum-factors (or res1 (nth 1 math-fact-expr)) 1 |
|
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
630 (or res2 (nth 2 math-fact-expr)))))) |
|
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
631 (let* ((p (math-is-polynomial math-fact-expr math-fet-x 30 'gen)) |
|
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
632 (math-poly-modulus (math-poly-modulus math-fact-expr)) |
| 40785 | 633 res) |
| 634 (and (cdr p) | |
| 635 (setq res (math-factor-poly-coefs p)) | |
|
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
636 (throw 'factor res))))) |
| 40785 | 637 |
| 638 (defun math-accum-factors (fac pow facs) | |
| 639 (if math-to-list | |
| 640 (if (math-vectorp fac) | |
| 641 (progn | |
| 642 (while (setq fac (cdr fac)) | |
| 643 (setq facs (math-accum-factors (nth 1 (car fac)) | |
| 644 (* pow (nth 2 (car fac))) | |
| 645 facs))) | |
| 646 facs) | |
| 647 (if (and (eq (car-safe fac) '^) (natnump (nth 2 fac))) | |
| 648 (setq pow (* pow (nth 2 fac)) | |
| 649 fac (nth 1 fac))) | |
| 650 (if (eq fac 1) | |
| 651 facs | |
| 652 (or (math-vectorp facs) | |
| 653 (setq facs (if (eq facs 1) '(vec) | |
| 654 (list 'vec (list 'vec facs 1))))) | |
| 655 (let ((found facs)) | |
| 656 (while (and (setq found (cdr found)) | |
| 657 (not (equal fac (nth 1 (car found)))))) | |
| 658 (if found | |
| 659 (progn | |
| 660 (setcar (cdr (cdr (car found))) (+ pow (nth 2 (car found)))) | |
| 661 facs) | |
| 662 ;; Put constant term first. | |
| 663 (if (and (cdr facs) (Math-ratp (nth 1 (nth 1 facs)))) | |
| 664 (cons 'vec (cons (nth 1 facs) (cons (list 'vec fac pow) | |
| 665 (cdr (cdr facs))))) | |
| 666 (cons 'vec (cons (list 'vec fac pow) (cdr facs)))))))) | |
|
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
667 (math-mul (math-pow fac pow) facs))) |
| 40785 | 668 |
| 669 (defun math-factor-poly-coefs (p &optional square-free) ; uses "x" | |
|
58507
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
670 (let (t1 t2 temp) |
| 40785 | 671 (cond ((not (cdr p)) |
| 672 (or (car p) 0)) | |
| 673 | |
|
58507
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
674 ;; Strip off multiples of math-fet-x. |
| 40785 | 675 ((Math-zerop (car p)) |
| 676 (let ((z 0)) | |
| 677 (while (and p (Math-zerop (car p))) | |
| 678 (setq z (1+ z) p (cdr p))) | |
| 679 (if (cdr p) | |
| 680 (setq p (math-factor-poly-coefs p square-free)) | |
| 681 (setq p (math-sort-terms (math-factor-expr (car p))))) | |
|
58507
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
682 (math-accum-factors math-fet-x z (math-factor-protect p)))) |
| 40785 | 683 |
| 684 ;; Factor out content. | |
| 685 ((and (not square-free) | |
| 686 (not (eq 1 (setq t1 (math-mul (math-poly-gcd-list p) | |
| 687 (if (math-guess-if-neg | |
| 688 (nth (1- (length p)) p)) | |
| 689 -1 1)))))) | |
| 690 (math-accum-factors t1 1 (math-factor-poly-coefs | |
| 691 (math-poly-div-list p t1) 'cont))) | |
| 692 | |
|
58507
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
693 ;; Check if linear in math-fet-x. |
| 40785 | 694 ((not (cdr (cdr p))) |
|
62470
7e497f7bc556
(math-factor-poly-coefs): Make sure the terms in linear factors in proper
Jay Belanger <jay.p.belanger@gmail.com>
parents:
62442
diff
changeset
|
695 (math-sort-terms |
|
7e497f7bc556
(math-factor-poly-coefs): Make sure the terms in linear factors in proper
Jay Belanger <jay.p.belanger@gmail.com>
parents:
62442
diff
changeset
|
696 (math-add (math-factor-protect |
|
7e497f7bc556
(math-factor-poly-coefs): Make sure the terms in linear factors in proper
Jay Belanger <jay.p.belanger@gmail.com>
parents:
62442
diff
changeset
|
697 (math-sort-terms |
|
7e497f7bc556
(math-factor-poly-coefs): Make sure the terms in linear factors in proper
Jay Belanger <jay.p.belanger@gmail.com>
parents:
62442
diff
changeset
|
698 (math-factor-expr (car p)))) |
|
7e497f7bc556
(math-factor-poly-coefs): Make sure the terms in linear factors in proper
Jay Belanger <jay.p.belanger@gmail.com>
parents:
62442
diff
changeset
|
699 (math-mul math-fet-x (math-factor-protect |
|
7e497f7bc556
(math-factor-poly-coefs): Make sure the terms in linear factors in proper
Jay Belanger <jay.p.belanger@gmail.com>
parents:
62442
diff
changeset
|
700 (math-sort-terms |
|
7e497f7bc556
(math-factor-poly-coefs): Make sure the terms in linear factors in proper
Jay Belanger <jay.p.belanger@gmail.com>
parents:
62442
diff
changeset
|
701 (math-factor-expr (nth 1 p)))))))) |
| 40785 | 702 |
| 703 ;; If symbolic coefficients, use FactorRules. | |
| 704 ((let ((pp p)) | |
| 705 (while (and pp (or (Math-ratp (car pp)) | |
| 706 (and (eq (car (car pp)) 'mod) | |
| 707 (Math-integerp (nth 1 (car pp))) | |
| 708 (Math-integerp (nth 2 (car pp)))))) | |
| 709 (setq pp (cdr pp))) | |
| 710 pp) | |
| 711 (let ((res (math-rewrite | |
|
58507
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
712 (list 'calcFunc-thecoefs math-fet-x (cons 'vec p)) |
| 40785 | 713 '(var FactorRules var-FactorRules)))) |
| 714 (or (and (eq (car-safe res) 'calcFunc-thefactors) | |
| 715 (= (length res) 3) | |
| 716 (math-vectorp (nth 2 res)) | |
| 717 (let ((facs 1) | |
| 718 (vec (nth 2 res))) | |
| 719 (while (setq vec (cdr vec)) | |
| 720 (setq facs (math-accum-factors (car vec) 1 facs))) | |
| 721 facs)) | |
|
58507
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
722 (math-build-polynomial-expr p math-fet-x)))) |
| 40785 | 723 |
| 724 ;; Check if rational coefficients (i.e., not modulo a prime). | |
| 725 ((eq math-poly-modulus 1) | |
| 726 | |
| 727 ;; Check if there are any squared terms, or a content not = 1. | |
| 728 (if (or (eq square-free t) | |
| 729 (equal (setq t1 (math-poly-gcd-coefs | |
| 730 p (setq t2 (math-poly-deriv-coefs p)))) | |
| 731 '(1))) | |
| 732 | |
| 733 ;; We now have a square-free polynomial with integer coefs. | |
| 734 ;; For now, we use a kludgey method that finds linear and | |
| 735 ;; quadratic terms using floating-point root-finding. | |
| 736 (if (setq t1 (let ((calc-symbolic-mode nil)) | |
| 737 (math-poly-all-roots nil p t))) | |
| 738 (let ((roots (car t1)) | |
| 739 (csign (if (math-negp (nth (1- (length p)) p)) -1 1)) | |
| 740 (expr 1) | |
| 741 (unfac (nth 1 t1)) | |
| 742 (scale (nth 2 t1))) | |
| 743 (while roots | |
| 744 (let ((coef0 (car (car roots))) | |
| 745 (coef1 (cdr (car roots)))) | |
| 746 (setq expr (math-accum-factors | |
| 747 (if coef1 | |
| 748 (let ((den (math-lcm-denoms | |
| 749 coef0 coef1))) | |
| 750 (setq scale (math-div scale den)) | |
| 751 (math-add | |
| 752 (math-add | |
|
58507
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
753 (math-mul den (math-pow math-fet-x 2)) |
|
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
754 (math-mul (math-mul coef1 den) |
|
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
755 math-fet-x)) |
| 40785 | 756 (math-mul coef0 den))) |
| 757 (let ((den (math-lcm-denoms coef0))) | |
| 758 (setq scale (math-div scale den)) | |
|
58507
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
759 (math-add (math-mul den math-fet-x) |
| 40785 | 760 (math-mul coef0 den)))) |
| 761 1 expr) | |
| 762 roots (cdr roots)))) | |
| 763 (setq expr (math-accum-factors | |
| 764 expr 1 | |
| 765 (math-mul csign | |
| 766 (math-build-polynomial-expr | |
| 767 (math-mul-list (nth 1 t1) scale) | |
|
58507
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
768 math-fet-x))))) |
|
c8e117d0c5ff
(math-poly-base-top-expr): New variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58302
diff
changeset
|
769 (math-build-polynomial-expr p math-fet-x)) ; can't factor it. |
| 40785 | 770 |
| 771 ;; Separate out the squared terms (Knuth exercise 4.6.2-34). | |
| 772 ;; This step also divides out the content of the polynomial. | |
| 773 (let* ((cabs (math-poly-gcd-list p)) | |
| 774 (csign (if (math-negp (nth (1- (length p)) p)) -1 1)) | |
| 775 (t1s (math-mul-list t1 csign)) | |
| 776 (uu nil) | |
| 777 (v (car (math-poly-div-coefs p t1s))) | |
| 778 (w (car (math-poly-div-coefs t2 t1s)))) | |
| 779 (while | |
| 780 (not (math-poly-zerop | |
| 781 (setq t2 (math-poly-simplify | |
| 782 (math-poly-mix | |
| 783 w 1 (math-poly-deriv-coefs v) -1))))) | |
| 784 (setq t1 (math-poly-gcd-coefs v t2) | |
| 785 uu (cons t1 uu) | |
| 786 v (car (math-poly-div-coefs v t1)) | |
| 787 w (car (math-poly-div-coefs t2 t1)))) | |
| 788 (setq t1 (length uu) | |
| 789 t2 (math-accum-factors (math-factor-poly-coefs v t) | |
| 790 (1+ t1) 1)) | |
| 791 (while uu | |
| 792 (setq t2 (math-accum-factors (math-factor-poly-coefs | |
| 793 (car uu) t) | |
| 794 t1 t2) | |
| 795 t1 (1- t1) | |
| 796 uu (cdr uu))) | |
| 797 (math-accum-factors (math-mul cabs csign) 1 t2)))) | |
| 798 | |
| 799 ;; Factoring modulo a prime. | |
| 800 ((and (= (length (setq temp (math-poly-gcd-coefs | |
| 801 p (math-poly-deriv-coefs p)))) | |
| 802 (length p))) | |
| 803 (setq p (car temp)) | |
| 804 (while (cdr temp) | |
| 805 (setq temp (nthcdr (nth 2 math-poly-modulus) temp) | |
| 806 p (cons (car temp) p))) | |
| 807 (and (setq temp (math-factor-poly-coefs p)) | |
| 808 (math-pow temp (nth 2 math-poly-modulus)))) | |
| 809 (t | |
|
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
810 (math-reject-arg nil "*Modulo factorization not yet implemented"))))) |
| 40785 | 811 |
| 812 (defun math-poly-deriv-coefs (p) | |
| 813 (let ((n 1) | |
| 814 (dp nil)) | |
| 815 (while (setq p (cdr p)) | |
| 816 (setq dp (cons (math-mul (car p) n) dp) | |
| 817 n (1+ n))) | |
|
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
818 (nreverse dp))) |
| 40785 | 819 |
| 820 (defun math-factor-contains (x a) | |
| 821 (if (equal x a) | |
| 822 1 | |
| 823 (if (memq (car-safe x) '(+ - * / neg)) | |
| 824 (let ((sum 0)) | |
| 825 (while (setq x (cdr x)) | |
| 826 (setq sum (+ sum (math-factor-contains (car x) a)))) | |
| 827 sum) | |
| 828 (if (and (eq (car-safe x) '^) | |
| 829 (natnump (nth 2 x))) | |
| 830 (* (math-factor-contains (nth 1 x) a) (nth 2 x)) | |
|
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
831 0)))) |
| 40785 | 832 |
| 833 | |
| 834 | |
| 835 | |
| 836 | |
| 837 ;;; Merge all quotients and expand/simplify the numerator | |
| 838 (defun calcFunc-nrat (expr) | |
| 839 (if (math-any-floats expr) | |
| 840 (setq expr (calcFunc-pfrac expr))) | |
| 841 (if (or (math-vectorp expr) | |
| 842 (assq (car-safe expr) calc-tweak-eqn-table)) | |
| 843 (cons (car expr) (mapcar 'calcFunc-nrat (cdr expr))) | |
| 844 (let* ((calc-prefer-frac t) | |
| 845 (res (math-to-ratpoly expr)) | |
| 846 (num (math-simplify (math-sort-terms (calcFunc-expand (car res))))) | |
| 847 (den (math-simplify (math-sort-terms (calcFunc-expand (cdr res))))) | |
| 848 (g (math-poly-gcd num den))) | |
| 849 (or (eq g 1) | |
| 850 (let ((num2 (math-poly-div num g)) | |
| 851 (den2 (math-poly-div den g))) | |
| 852 (and (eq (cdr num2) 0) (eq (cdr den2) 0) | |
| 853 (setq num (car num2) den (car den2))))) | |
|
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
854 (math-simplify (math-div num den))))) |
| 40785 | 855 |
| 856 ;;; Returns expressions (num . denom). | |
| 857 (defun math-to-ratpoly (expr) | |
| 858 (let ((res (math-to-ratpoly-rec expr))) | |
|
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
859 (cons (math-simplify (car res)) (math-simplify (cdr res))))) |
| 40785 | 860 |
| 861 (defun math-to-ratpoly-rec (expr) | |
| 862 (cond ((Math-primp expr) | |
| 863 (cons expr 1)) | |
| 864 ((memq (car expr) '(+ -)) | |
| 865 (let ((r1 (math-to-ratpoly-rec (nth 1 expr))) | |
| 866 (r2 (math-to-ratpoly-rec (nth 2 expr)))) | |
| 867 (if (equal (cdr r1) (cdr r2)) | |
| 868 (cons (list (car expr) (car r1) (car r2)) (cdr r1)) | |
| 869 (if (eq (cdr r1) 1) | |
| 870 (cons (list (car expr) | |
| 871 (math-mul (car r1) (cdr r2)) | |
| 872 (car r2)) | |
| 873 (cdr r2)) | |
| 874 (if (eq (cdr r2) 1) | |
| 875 (cons (list (car expr) | |
| 876 (car r1) | |
| 877 (math-mul (car r2) (cdr r1))) | |
| 878 (cdr r1)) | |
| 879 (let ((g (math-poly-gcd (cdr r1) (cdr r2)))) | |
| 880 (let ((d1 (and (not (eq g 1)) (math-poly-div (cdr r1) g))) | |
| 881 (d2 (and (not (eq g 1)) (math-poly-div | |
| 882 (math-mul (car r1) (cdr r2)) | |
| 883 g)))) | |
| 884 (if (and (eq (cdr d1) 0) (eq (cdr d2) 0)) | |
| 885 (cons (list (car expr) (car d2) | |
| 886 (math-mul (car r2) (car d1))) | |
| 887 (math-mul (car d1) (cdr r2))) | |
| 888 (cons (list (car expr) | |
| 889 (math-mul (car r1) (cdr r2)) | |
| 890 (math-mul (car r2) (cdr r1))) | |
| 891 (math-mul (cdr r1) (cdr r2))))))))))) | |
| 892 ((eq (car expr) '*) | |
| 893 (let* ((r1 (math-to-ratpoly-rec (nth 1 expr))) | |
| 894 (r2 (math-to-ratpoly-rec (nth 2 expr))) | |
| 895 (g (math-mul (math-poly-gcd (car r1) (cdr r2)) | |
| 896 (math-poly-gcd (cdr r1) (car r2))))) | |
| 897 (if (eq g 1) | |
| 898 (cons (math-mul (car r1) (car r2)) | |
| 899 (math-mul (cdr r1) (cdr r2))) | |
| 900 (cons (math-poly-div-exact (math-mul (car r1) (car r2)) g) | |
| 901 (math-poly-div-exact (math-mul (cdr r1) (cdr r2)) g))))) | |
| 902 ((eq (car expr) '/) | |
| 903 (let* ((r1 (math-to-ratpoly-rec (nth 1 expr))) | |
| 904 (r2 (math-to-ratpoly-rec (nth 2 expr)))) | |
| 905 (if (and (eq (cdr r1) 1) (eq (cdr r2) 1)) | |
| 906 (cons (car r1) (car r2)) | |
| 907 (let ((g (math-mul (math-poly-gcd (car r1) (car r2)) | |
| 908 (math-poly-gcd (cdr r1) (cdr r2))))) | |
| 909 (if (eq g 1) | |
| 910 (cons (math-mul (car r1) (cdr r2)) | |
| 911 (math-mul (cdr r1) (car r2))) | |
| 912 (cons (math-poly-div-exact (math-mul (car r1) (cdr r2)) g) | |
| 913 (math-poly-div-exact (math-mul (cdr r1) (car r2)) | |
| 914 g))))))) | |
| 915 ((and (eq (car expr) '^) (integerp (nth 2 expr))) | |
| 916 (let ((r1 (math-to-ratpoly-rec (nth 1 expr)))) | |
| 917 (if (> (nth 2 expr) 0) | |
| 918 (cons (math-pow (car r1) (nth 2 expr)) | |
| 919 (math-pow (cdr r1) (nth 2 expr))) | |
| 920 (cons (math-pow (cdr r1) (- (nth 2 expr))) | |
| 921 (math-pow (car r1) (- (nth 2 expr))))))) | |
| 922 ((eq (car expr) 'neg) | |
| 923 (let ((r1 (math-to-ratpoly-rec (nth 1 expr)))) | |
| 924 (cons (math-neg (car r1)) (cdr r1)))) | |
|
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
925 (t (cons expr 1)))) |
| 40785 | 926 |
| 927 | |
| 928 (defun math-ratpoly-p (expr &optional var) | |
| 929 (cond ((equal expr var) 1) | |
| 930 ((Math-primp expr) 0) | |
| 931 ((memq (car expr) '(+ -)) | |
| 932 (let ((p1 (math-ratpoly-p (nth 1 expr) var)) | |
| 933 p2) | |
| 934 (and p1 (setq p2 (math-ratpoly-p (nth 2 expr) var)) | |
| 935 (max p1 p2)))) | |
| 936 ((eq (car expr) '*) | |
| 937 (let ((p1 (math-ratpoly-p (nth 1 expr) var)) | |
| 938 p2) | |
| 939 (and p1 (setq p2 (math-ratpoly-p (nth 2 expr) var)) | |
| 940 (+ p1 p2)))) | |
| 941 ((eq (car expr) 'neg) | |
| 942 (math-ratpoly-p (nth 1 expr) var)) | |
| 943 ((eq (car expr) '/) | |
| 944 (let ((p1 (math-ratpoly-p (nth 1 expr) var)) | |
| 945 p2) | |
| 946 (and p1 (setq p2 (math-ratpoly-p (nth 2 expr) var)) | |
| 947 (- p1 p2)))) | |
| 948 ((and (eq (car expr) '^) | |
| 949 (integerp (nth 2 expr))) | |
| 950 (let ((p1 (math-ratpoly-p (nth 1 expr) var))) | |
| 951 (and p1 (* p1 (nth 2 expr))))) | |
| 952 ((not var) 1) | |
| 953 ((math-poly-depends expr var) nil) | |
|
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
954 (t 0))) |
| 40785 | 955 |
| 956 | |
| 957 (defun calcFunc-apart (expr &optional var) | |
| 958 (cond ((Math-primp expr) expr) | |
| 959 ((eq (car expr) '+) | |
| 960 (math-add (calcFunc-apart (nth 1 expr) var) | |
| 961 (calcFunc-apart (nth 2 expr) var))) | |
| 962 ((eq (car expr) '-) | |
| 963 (math-sub (calcFunc-apart (nth 1 expr) var) | |
| 964 (calcFunc-apart (nth 2 expr) var))) | |
| 965 ((not (math-ratpoly-p expr var)) | |
| 966 (math-reject-arg expr "Expected a rational function")) | |
| 967 (t | |
| 968 (let* ((calc-prefer-frac t) | |
| 969 (rat (math-to-ratpoly expr)) | |
| 970 (num (car rat)) | |
| 971 (den (cdr rat)) | |
| 972 (qr (math-poly-div num den)) | |
| 973 (q (car qr)) | |
| 974 (r (cdr qr))) | |
| 975 (or var | |
| 976 (setq var (math-polynomial-base den))) | |
| 977 (math-add q (or (and var | |
| 978 (math-expr-contains den var) | |
| 979 (math-partial-fractions r den var)) | |
|
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
980 (math-div r den))))))) |
| 40785 | 981 |
| 982 | |
| 983 (defun math-padded-polynomial (expr var deg) | |
| 984 (let ((p (math-is-polynomial expr var deg))) | |
|
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
985 (append p (make-list (- deg (length p)) 0)))) |
| 40785 | 986 |
| 987 (defun math-partial-fractions (r den var) | |
| 988 (let* ((fden (calcFunc-factors den var)) | |
| 989 (tdeg (math-polynomial-p den var)) | |
| 990 (fp fden) | |
| 991 (dlist nil) | |
| 992 (eqns 0) | |
| 993 (lz nil) | |
| 994 (tz (make-list (1- tdeg) 0)) | |
| 995 (calc-matrix-mode 'scalar)) | |
| 996 (and (not (and (= (length fden) 2) (eq (nth 2 (nth 1 fden)) 1))) | |
| 997 (progn | |
| 998 (while (setq fp (cdr fp)) | |
| 999 (let ((rpt (nth 2 (car fp))) | |
| 1000 (deg (math-polynomial-p (nth 1 (car fp)) var)) | |
| 1001 dnum dvar deg2) | |
| 1002 (while (> rpt 0) | |
| 1003 (setq deg2 deg | |
| 1004 dnum 0) | |
| 1005 (while (> deg2 0) | |
| 1006 (setq dvar (append '(vec) lz '(1) tz) | |
| 1007 lz (cons 0 lz) | |
| 1008 tz (cdr tz) | |
| 1009 deg2 (1- deg2) | |
| 1010 dnum (math-add dnum (math-mul dvar | |
| 1011 (math-pow var deg2))) | |
| 1012 dlist (cons (and (= deg2 (1- deg)) | |
| 1013 (math-pow (nth 1 (car fp)) rpt)) | |
| 1014 dlist))) | |
| 1015 (let ((fpp fden) | |
| 1016 (mult 1)) | |
| 1017 (while (setq fpp (cdr fpp)) | |
| 1018 (or (eq fpp fp) | |
| 1019 (setq mult (math-mul mult | |
| 1020 (math-pow (nth 1 (car fpp)) | |
| 1021 (nth 2 (car fpp))))))) | |
| 1022 (setq dnum (math-mul dnum mult))) | |
| 1023 (setq eqns (math-add eqns (math-mul dnum | |
| 1024 (math-pow | |
| 1025 (nth 1 (car fp)) | |
| 1026 (- (nth 2 (car fp)) | |
| 1027 rpt)))) | |
| 1028 rpt (1- rpt))))) | |
| 1029 (setq eqns (math-div (cons 'vec (math-padded-polynomial r var tdeg)) | |
| 1030 (math-transpose | |
| 1031 (cons 'vec | |
| 1032 (mapcar | |
| 1033 (function | |
| 1034 (lambda (x) | |
| 1035 (cons 'vec (math-padded-polynomial | |
| 1036 x var tdeg)))) | |
| 1037 (cdr eqns)))))) | |
| 1038 (and (math-vectorp eqns) | |
| 1039 (let ((res 0) | |
| 1040 (num nil)) | |
| 1041 (setq eqns (nreverse eqns)) | |
| 1042 (while eqns | |
| 1043 (setq num (cons (car eqns) num) | |
| 1044 eqns (cdr eqns)) | |
| 1045 (if (car dlist) | |
| 1046 (setq num (math-build-polynomial-expr | |
| 1047 (nreverse num) var) | |
| 1048 res (math-add res (math-div num (car dlist))) | |
| 1049 num nil)) | |
| 1050 (setq dlist (cdr dlist))) | |
|
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1051 (math-normalize res))))))) |
| 40785 | 1052 |
| 1053 | |
| 1054 | |
| 1055 (defun math-expand-term (expr) | |
| 1056 (cond ((and (eq (car-safe expr) '*) | |
| 1057 (memq (car-safe (nth 1 expr)) '(+ -))) | |
| 1058 (math-add-or-sub (list '* (nth 1 (nth 1 expr)) (nth 2 expr)) | |
| 1059 (list '* (nth 2 (nth 1 expr)) (nth 2 expr)) | |
| 1060 nil (eq (car (nth 1 expr)) '-))) | |
| 1061 ((and (eq (car-safe expr) '*) | |
| 1062 (memq (car-safe (nth 2 expr)) '(+ -))) | |
| 1063 (math-add-or-sub (list '* (nth 1 expr) (nth 1 (nth 2 expr))) | |
| 1064 (list '* (nth 1 expr) (nth 2 (nth 2 expr))) | |
| 1065 nil (eq (car (nth 2 expr)) '-))) | |
| 1066 ((and (eq (car-safe expr) '/) | |
| 1067 (memq (car-safe (nth 1 expr)) '(+ -))) | |
| 1068 (math-add-or-sub (list '/ (nth 1 (nth 1 expr)) (nth 2 expr)) | |
| 1069 (list '/ (nth 2 (nth 1 expr)) (nth 2 expr)) | |
| 1070 nil (eq (car (nth 1 expr)) '-))) | |
| 1071 ((and (eq (car-safe expr) '^) | |
| 1072 (memq (car-safe (nth 1 expr)) '(+ -)) | |
| 1073 (integerp (nth 2 expr)) | |
| 1074 (if (> (nth 2 expr) 0) | |
|
58104
a8b2ccf9b0ee
(math-expand-form): Use declared variable math-mt-many.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
1075 (or (and (or (> math-mt-many 500000) (< math-mt-many -500000)) |
| 40785 | 1076 (math-expand-power (nth 1 expr) (nth 2 expr) |
| 1077 nil t)) | |
| 1078 (list '* | |
| 1079 (nth 1 expr) | |
| 1080 (list '^ (nth 1 expr) (1- (nth 2 expr))))) | |
| 1081 (if (< (nth 2 expr) 0) | |
| 1082 (list '/ 1 (list '^ (nth 1 expr) (- (nth 2 expr)))))))) | |
|
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1083 (t expr))) |
| 40785 | 1084 |
| 1085 (defun calcFunc-expand (expr &optional many) | |
|
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1086 (math-normalize (math-map-tree 'math-expand-term expr many))) |
| 40785 | 1087 |
| 1088 (defun math-expand-power (x n &optional var else-nil) | |
| 1089 (or (and (natnump n) | |
| 1090 (memq (car-safe x) '(+ -)) | |
| 1091 (let ((terms nil) | |
| 1092 (cterms nil)) | |
| 1093 (while (memq (car-safe x) '(+ -)) | |
| 1094 (setq terms (cons (if (eq (car x) '-) | |
| 1095 (math-neg (nth 2 x)) | |
| 1096 (nth 2 x)) | |
| 1097 terms) | |
| 1098 x (nth 1 x))) | |
| 1099 (setq terms (cons x terms)) | |
| 1100 (if var | |
| 1101 (let ((p terms)) | |
| 1102 (while p | |
| 1103 (or (math-expr-contains (car p) var) | |
| 1104 (setq terms (delq (car p) terms) | |
| 1105 cterms (cons (car p) cterms))) | |
| 1106 (setq p (cdr p))) | |
| 1107 (if cterms | |
| 1108 (setq terms (cons (apply 'calcFunc-add cterms) | |
| 1109 terms))))) | |
| 1110 (if (= (length terms) 2) | |
| 1111 (let ((i 0) | |
| 1112 (accum 0)) | |
| 1113 (while (<= i n) | |
| 1114 (setq accum (list '+ accum | |
| 1115 (list '* (calcFunc-choose n i) | |
| 1116 (list '* | |
| 1117 (list '^ (nth 1 terms) i) | |
| 1118 (list '^ (car terms) | |
| 1119 (- n i))))) | |
| 1120 i (1+ i))) | |
| 1121 accum) | |
| 1122 (if (= n 2) | |
| 1123 (let ((accum 0) | |
| 1124 (p1 terms) | |
| 1125 p2) | |
| 1126 (while p1 | |
| 1127 (setq accum (list '+ accum | |
| 1128 (list '^ (car p1) 2)) | |
| 1129 p2 p1) | |
| 1130 (while (setq p2 (cdr p2)) | |
| 1131 (setq accum (list '+ accum | |
| 1132 (list '* 2 (list '* | |
| 1133 (car p1) | |
| 1134 (car p2)))))) | |
| 1135 (setq p1 (cdr p1))) | |
| 1136 accum) | |
| 1137 (if (= n 3) | |
| 1138 (let ((accum 0) | |
| 1139 (p1 terms) | |
| 1140 p2 p3) | |
| 1141 (while p1 | |
| 1142 (setq accum (list '+ accum (list '^ (car p1) 3)) | |
| 1143 p2 p1) | |
| 1144 (while (setq p2 (cdr p2)) | |
| 1145 (setq accum (list '+ | |
| 1146 (list '+ | |
| 1147 accum | |
| 1148 (list '* 3 | |
| 1149 (list | |
| 1150 '* | |
| 1151 (list '^ (car p1) 2) | |
| 1152 (car p2)))) | |
| 1153 (list '* 3 | |
| 1154 (list | |
| 1155 '* (car p1) | |
| 1156 (list '^ (car p2) 2)))) | |
| 1157 p3 p2) | |
| 1158 (while (setq p3 (cdr p3)) | |
| 1159 (setq accum (list '+ accum | |
| 1160 (list '* 6 | |
| 1161 (list '* | |
| 1162 (car p1) | |
| 1163 (list | |
| 1164 '* (car p2) | |
| 1165 (car p3)))))))) | |
| 1166 (setq p1 (cdr p1))) | |
| 1167 accum)))))) | |
| 1168 (and (not else-nil) | |
|
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1169 (list '^ x n)))) |
| 40785 | 1170 |
| 1171 (defun calcFunc-expandpow (x n) | |
|
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1172 (math-normalize (math-expand-power x n))) |
| 40785 | 1173 |
|
58667
88f98983accf
Add a provide statement.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58507
diff
changeset
|
1174 (provide 'calc-poly) |
|
88f98983accf
Add a provide statement.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58507
diff
changeset
|
1175 |
| 52401 | 1176 ;;; arch-tag: d2566c51-2ccc-45f1-8c50-f3462c2953ff |
|
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1177 ;;; calc-poly.el ends here |
