rcf.scm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. #| -*-Scheme-*-
  2. Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
  3. 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
  4. 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Massachusetts
  5. Institute of Technology
  6. This file is part of MIT/GNU Scheme.
  7. MIT/GNU Scheme is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or (at
  10. your option) any later version.
  11. MIT/GNU Scheme is distributed in the hope that it will be useful, but
  12. WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with MIT/GNU Scheme; if not, write to the Free Software
  17. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301,
  18. USA.
  19. |#
  20. ;;;; Rational Forms constructed over polynomials
  21. (declare (usual-integrations))
  22. (define rcf-tag '*RCF*)
  23. (define (ratform? object)
  24. (and (pair? object)
  25. (eq? (car object) rcf-tag)))
  26. (define (make-ratform n d)
  27. (list rcf-tag n d))
  28. (define ratform-numerator cadr)
  29. (define ratform-denominator caddr)
  30. (define rcf:zero poly:zero)
  31. (define rcf:one poly:one)
  32. (define (rcf:zero? q)
  33. (and (not (ratform? q)) (poly:zero? q)))
  34. (define (rcf:one? q)
  35. (and (not (ratform? q)) (poly:one? q)))
  36. (define (rcf:arity q)
  37. (define (check-same-arity p1 p2)
  38. (let ((a1 (poly:arity p1)) (a2 (poly:arity p2)))
  39. (cond ((fix:= a1 0) a2)
  40. ((fix:= a2 0) a1)
  41. ((fix:= a1 a2) a1)
  42. (else
  43. (error "Unequal arities in RCF" q)))))
  44. (cond ((ratform? q)
  45. (check-same-arity (ratform-numerator q)
  46. (ratform-denominator q)))
  47. ((pcf? q) (poly:arity q))
  48. (else (error "Wrong type -- RCF:ARITY" q))))
  49. #|
  50. (define (make-rcf n d)
  51. (cond ((poly:zero? d)
  52. (error "Zero denominator -- MAKE-RCF" n d))
  53. ((or (poly:zero? n) (poly:one? d)) n)
  54. ((number? d) (poly:* (/ 1 d) n))
  55. (else
  56. (let ((dn (poly:leading-base-coefficient d)))
  57. (if (poly:one? dn)
  58. (make-ratform n d)
  59. (make-ratform (poly:normalize-by n dn)
  60. (poly:normalize-by d dn)))))))
  61. |#
  62. (define (make-rcf n d)
  63. (cond ((poly:zero? d)
  64. (error "Zero denominator -- MAKE-RCF" n d))
  65. ((or (poly:zero? n) (poly:one? d)) n)
  66. (else
  67. (let ((b ((set->list numbers)
  68. ((union-sets numbers)
  69. (poly/base-coefficients n)
  70. (poly/base-coefficients d)))))
  71. (let ((c (* (reduce-left lcm base/one
  72. (map denominator
  73. (filter ratnum? b)))
  74. (sgn (poly:leading-base-coefficient d)))))
  75. (if (base/one? c)
  76. (make-ratform n d)
  77. (make-ratform (poly:* n c)
  78. (poly:* d c))))))))
  79. (define (rcf:rcf? object)
  80. (or (ratform? object) (pcf? object)))
  81. (define (rcf:pcf? object)
  82. (and (not (ratform? object)) (pcf? object)))
  83. (define (rcf:= q r)
  84. (if (ratform? q)
  85. (if (ratform? r)
  86. (and (poly:= (ratform-numerator q) (ratform-numerator r))
  87. (poly:= (ratform-denominator q) (ratform-denominator r)))
  88. (if (pcf? r)
  89. #f
  90. (error "Wrong type -- RCF:=" r)))
  91. (if (ratform? r)
  92. (if (pcf? q)
  93. #f
  94. (error "Wrong type -- RCF:=" q))
  95. (poly:= q r))))
  96. ;;; The notation here is from Knuth (p. 291).
  97. ;;; In various places we take the gcd of two polynomials
  98. ;;; and then use quotient to reduce those polynomials.
  99. (define (rcf:+ u/u* v/v*)
  100. (rcf:binary-operator u/u* v/v*
  101. poly:+
  102. (lambda (u v v*)
  103. (if (poly:zero? u)
  104. v/v*
  105. (make-rcf (poly:+ (poly:* u v*) v) v*)))
  106. (lambda (u u* v)
  107. (if (poly:zero? v)
  108. u/u*
  109. (make-rcf (poly:+ u (poly:* u* v)) u*)))
  110. (lambda (u u* v v*)
  111. (if (poly:= u* v*)
  112. (let* ((n (poly:+ u v)) (g (poly:gcd u* n)))
  113. (if (poly:one? g)
  114. (make-rcf n u*)
  115. (make-rcf (poly:quotient n g) (poly:quotient u* g))))
  116. (let ((d1 (poly:gcd u* v*)))
  117. (if (poly:one? d1)
  118. (make-rcf (poly:+ (poly:* u v*) (poly:* u* v))
  119. (poly:* u* v*))
  120. (let* ((u*/d1 (poly:quotient u* d1))
  121. (t (poly:+ (poly:* u (poly:quotient v* d1))
  122. (poly:* u*/d1 v))))
  123. (if (poly:zero? t)
  124. rcf:zero
  125. (let ((d2 (poly:gcd t d1)))
  126. (if (poly:one? d2)
  127. (make-rcf t (poly:* u*/d1 v*))
  128. (make-rcf
  129. (poly:quotient t d2)
  130. (poly:* u*/d1
  131. (poly:quotient v* d2)))))))))))))
  132. (define (rcf:- u/u* v/v*)
  133. (rcf:binary-operator u/u* v/v*
  134. poly:-
  135. (lambda (u v v*)
  136. (if (poly:zero? u)
  137. (make-ratform (poly:negate v) v*)
  138. (make-rcf (poly:- (poly:* u v*) v) v*)))
  139. (lambda (u u* v)
  140. (if (poly:zero? v)
  141. u/u*
  142. (make-rcf (poly:- u (poly:* u* v)) u*)))
  143. (lambda (u u* v v*)
  144. (if (poly:= u* v*)
  145. (let* ((n (poly:- u v)) (g (poly:gcd u* n)))
  146. (if (poly:one? g)
  147. (make-rcf n u*)
  148. (make-rcf (poly:quotient n g) (poly:quotient u* g))))
  149. (let ((d1 (poly:gcd u* v*)))
  150. (if (poly:one? d1)
  151. (make-rcf (poly:- (poly:* u v*) (poly:* u* v))
  152. (poly:* u* v*))
  153. (let* ((u*/d1 (poly:quotient u* d1))
  154. (t (poly:- (poly:* u (poly:quotient v* d1))
  155. (poly:* u*/d1 v))))
  156. (if (poly:zero? t)
  157. rcf:zero
  158. (let ((d2 (poly:gcd t d1)))
  159. (if (poly:one? d2)
  160. (make-rcf t (poly:* u*/d1 v*))
  161. (make-rcf
  162. (poly:quotient t d2)
  163. (poly:* u*/d1
  164. (poly:quotient v* d2)))))))))))))
  165. (define (rcf:negate v/v*)
  166. (if (ratform? v/v*)
  167. (make-ratform (poly:negate (ratform-numerator v/v*))
  168. (ratform-denominator v/v*))
  169. (poly:negate v/v*)))
  170. (define (rcf:* u/u* v/v*)
  171. (rcf:binary-operator u/u* v/v*
  172. poly:*
  173. (lambda (u v v*)
  174. (cond ((poly:zero? u) rcf:zero)
  175. ((poly:one? u) v/v*)
  176. (else
  177. (let ((d (poly:gcd u v*)))
  178. (if (poly:one? d)
  179. (make-rcf (poly:* u v) v*)
  180. (make-rcf (poly:* (poly:quotient u d) v)
  181. (poly:quotient v* d)))))))
  182. (lambda (u u* v)
  183. (cond ((poly:zero? v) rcf:zero)
  184. ((poly:one? v) u/u*)
  185. (else
  186. (let ((d (poly:gcd u* v)))
  187. (if (poly:one? d)
  188. (make-rcf (poly:* u v) u*)
  189. (make-rcf (poly:* u (poly:quotient v d))
  190. (poly:quotient u* d)))))))
  191. (lambda (u u* v v*)
  192. (let ((d1 (poly:gcd u v*))
  193. (d2 (poly:gcd u* v)))
  194. (if (poly:one? d1)
  195. (if (poly:one? d2)
  196. (make-rcf (poly:* u v) (poly:* u* v*))
  197. (make-rcf (poly:* u (poly:quotient v d2))
  198. (poly:* (poly:quotient u* d2) v*)))
  199. (if (poly:one? d2)
  200. (make-rcf (poly:* (poly:quotient u d1) v)
  201. (poly:* u* (poly:quotient v* d1)))
  202. (make-rcf (poly:* (poly:quotient u d1)
  203. (poly:quotient v d2))
  204. (poly:* (poly:quotient u* d2)
  205. (poly:quotient v* d1)))))))))
  206. (define (rcf:square q)
  207. (if (ratform? q)
  208. (make-ratform (let ((n (ratform-numerator q))) (poly:* n n))
  209. (let ((d (ratform-denominator q))) (poly:* d d)))
  210. (poly:* q q)))
  211. (define (rcf:/ u/u* v/v*)
  212. (rcf:* u/u* (rcf:invert v/v*)))
  213. (define (rcf:invert v/v*)
  214. (make-rcf (rcf:denominator v/v*)
  215. (rcf:numerator v/v*)))
  216. (define (rcf:gcd u/u* v/v*)
  217. (rcf:binary-operator u/u* v/v*
  218. poly:gcd
  219. (lambda (u v v*)
  220. (cond ((poly:zero? u) v/v*)
  221. ((poly:one? u) poly:one)
  222. (else (poly:gcd u v))))
  223. (lambda (u u* v)
  224. (cond ((poly:zero? v) u/u*)
  225. ((poly:one? v) poly:one)
  226. (else (poly:gcd u v))))
  227. (lambda (u u* v v*)
  228. (let ((d1 (poly:gcd u v))
  229. (d2 (poly:gcd u* v*)))
  230. (make-rcf d1 d2)))))
  231. (define (rcf:binary-operator u/u* v/v* int*int int*rat rat*int rat*rat)
  232. (if (ratform? u/u*)
  233. (if (ratform? v/v*)
  234. (rat*rat (ratform-numerator u/u*)
  235. (ratform-denominator u/u*)
  236. (ratform-numerator v/v*)
  237. (ratform-denominator v/v*))
  238. (rat*int (ratform-numerator u/u*)
  239. (ratform-denominator u/u*)
  240. v/v*))
  241. (if (ratform? v/v*)
  242. (int*rat u/u*
  243. (ratform-numerator v/v*)
  244. (ratform-denominator v/v*))
  245. (int*int u/u* v/v*))))
  246. (define (rcf:numerator q)
  247. (cond ((ratform? q) (ratform-numerator q))
  248. ((pcf? q) q)
  249. (else (error "Wrong type -- NUMERATOR" q))))
  250. (define (rcf:denominator q)
  251. (cond ((ratform? q) (ratform-denominator q))
  252. ((pcf? q) poly:one)
  253. (else (error "Wrong type -- DENOMINATOR" q))))
  254. (define (rcf:expt base exponent)
  255. (define (expt-iter x count answer)
  256. (if (fix:zero? count)
  257. answer
  258. (if (even? count)
  259. (expt-iter (rcf:square x) (fix:quotient count 2) answer)
  260. (expt-iter x (fix:-1+ count) (rcf:* x answer)))))
  261. (cond ((not (exact-integer? exponent))
  262. (error "Can only raise a RCF to an exact integer power" base exponent))
  263. ((fix:negative? exponent)
  264. (rcf:invert (expt-iter base (int:negate exponent) rcf:one)))
  265. (else (expt-iter base exponent rcf:one))))
  266. (define (rcf:arg-scale r points)
  267. (if (ratform? r)
  268. (rcf:/ (apply poly:arg-scale (ratform-numerator r) points)
  269. (apply poly:arg-scale (ratform-denominator r) points))
  270. (apply poly:arg-scale r points)))
  271. (define (rcf:arg-shift r points)
  272. (if (ratform? r)
  273. (rcf:/ (apply poly:arg-shift (ratform-numerator r) points)
  274. (apply poly:arg-shift (ratform-denominator r) points))
  275. (apply poly:arg-shift r points)))
  276. (define (rcf:value r points)
  277. (if (ratform? r)
  278. (rcf:/ (apply poly:value (ratform-numerator r) points)
  279. (apply poly:value (ratform-denominator r) points))
  280. (apply poly:value r points)))
  281. ;;; The following only plugs r2 in for the principal indeterminate.
  282. (define (rcf:compose r1 r2)
  283. (if (ratform? r2)
  284. (let ((nr1 (ratform-numerator r1)) (nr2 (ratform-numerator r2))
  285. (dr1 (ratform-denominator r1)) (dr2 (ratform-denominator r2)))
  286. (let ((dn (poly:degree nr1))
  287. (dd (poly:degree dr1))
  288. (narity (fix:+ (poly:arity dr1) 1)))
  289. (let ((nnr1 (poly:extend 1 (poly:principal-reverse nr1)))
  290. (ndr1 (poly:extend 1 (poly:principal-reverse dr1))))
  291. (let ((scales (list (cadr (poly:new-variables narity)) 1)))
  292. (let ((pn (poly:value (poly:principal-reverse
  293. (poly:arg-scale nnr1 scales))
  294. nr2
  295. dr2))
  296. (pd (poly:value (poly:principal-reverse
  297. (poly:arg-scale ndr1 scales))
  298. nr2
  299. dr2)))
  300. (cond ((fix:> dn dd)
  301. (rcf:/ pn (poly:* (poly:expt dr2 (fix:- dn dd)) pd)))
  302. ((fix:< dn dd)
  303. (rcf:/ (poly:* (poly:expt dr2 (fix:- dd dn)) pn) pd))
  304. (else (rcf:/ pn pd))))))))
  305. (rcf:/ (poly:value (ratform-numerator r1) r2)
  306. (poly:value (ratform-denominator r1) r2))))
  307. ;;; FBE: calls 'poly:derivative' with wrong argument count! -> comment out
  308. ;; (define (rcf:derivative r varnum)
  309. ;; (if (ratform? r)
  310. ;; (let ((u (ratform-numerator r)) (v (ratform-denominator r)))
  311. ;; (rcf:/ (poly:- (poly:* (poly:derivative u varnum) v)
  312. ;; (poly:* u (poly:derivative v varnum)))
  313. ;; (poly:* v v)))
  314. ;; (poly:derivative r varnum)))
  315. ;;; I don't know if this stuff is ever important...GJS
  316. (define (assoc-accumulation rat:op poly:op rat:identity)
  317. (define (operate rats)
  318. (cond ((null? rats) rat:identity)
  319. ((null? (cdr rats)) (car rats))
  320. ((ratform? (car rats))
  321. (cond ((ratform? (cadr rats))
  322. (operate (cons (rat:op (car rats) (cadr rats))
  323. (cddr rats))))
  324. ((null? (cddr rats)) (rat:op (car rats) (cadr rats)))
  325. ((not (ratform? (caddr rats)))
  326. (operate (cons (car rats)
  327. (cons (poly:op (cadr rats) (caddr rats))
  328. (cdddr rats)))))
  329. (else
  330. (operate (cons (rat:op (car rats) (cadr rats))
  331. (cddr rats))))))
  332. ((ratform? (cadr rats))
  333. (operate (cons (rat:op (car rats) (cadr rats))
  334. (cddr rats))))
  335. (else
  336. (operate (cons (poly:op (car rats) (cadr rats))
  337. (cddr rats))))))
  338. (lambda rats (operate rats)))
  339. (define +$rcf (assoc-accumulation rcf:+ poly:+ rcf:zero))
  340. (define *$rcf (assoc-accumulation rcf:* poly:* rcf:one))
  341. (define (assoc-inverse-accumulation rat:inv-op rat:op rat:invert poly:op rat:identity)
  342. (let ((direct-op (assoc-accumulation rat:op poly:op rat:identity)))
  343. (define (operate . rats)
  344. (cond ((null? rats) rat:identity)
  345. ((null? (cdr rats)) (rat:invert (car rats)))
  346. (else (rat:inv-op (car rats) (apply direct-op (cdr rats))))))
  347. operate))
  348. (define -$rcf
  349. (assoc-inverse-accumulation rcf:- rcf:+ rcf:negate poly:+ rcf:zero))
  350. (define /$rcf
  351. (assoc-inverse-accumulation rcf:/ rcf:* rcf:invert poly:* rcf:one))
  352. ;;; For simplifier
  353. (define (rcf:->expression p vars)
  354. (if (pcf? p)
  355. (pcf:->expression p vars)
  356. (symb:/ (pcf:->expression (ratform-numerator p) vars)
  357. (pcf:->expression (ratform-denominator p) vars))))
  358. (define* (rcf:expression-> expr cont #:optional less?)
  359. ;; cont = (lambda (ratp vars) ... )
  360. (let ((evars
  361. (sort (list-difference (variables-in expr)
  362. rcf:operators-known)
  363. (if (default-object? less?) variable<? less?))))
  364. (cont ((expression-walker
  365. (pair-up evars
  366. (poly:new-variables (length evars))
  367. rcf:operator-table))
  368. expr)
  369. evars)))
  370. ;;; FBE: missing definition of 'poly->function'
  371. ;; (define (rcf:->lambda p)
  372. ;; (if (pcf? p)
  373. ;; (poly->function p)
  374. ;; (let* ((n (rcf:arity p))
  375. ;; (vars (generate-list-of-symbols 'x n))
  376. ;; (exp (rcf:->expression p vars)))
  377. ;; `(lambda ,vars ,exp))))
  378. (define rcf:operator-table
  379. `((+ ,+$rcf)
  380. (- ,-$rcf)
  381. (* ,*$rcf)
  382. (/ ,/$rcf)
  383. (negate ,rcf:negate)
  384. (invert ,rcf:invert)
  385. (expt ,rcf:expt)
  386. (square ,rcf:square)
  387. (gcd ,rcf:gcd)))
  388. (define rcf:operators-known
  389. (map car rcf:operator-table))