calc-poly.el 39 KB

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