Implement pseudo-remainder for exercise 2.96

This commit is contained in:
Oliver Payne 2022-04-15 22:53:28 +01:00
parent 6c40d82224
commit 102d945c86
1 changed files with 27 additions and 2 deletions

View File

@ -595,10 +595,35 @@
(define (remainder-terms L1 L2)
(cadr (div-terms L1 L2)))
(define (pseudo-remainder-terms L1 L2)
(let* ((c (coeff (first-term L2)))
(o1 (order (first-term L1)))
(o2 (order (first-term L2)))
(i-factor (exp c
(+ 1 (- o1 o2))))) ;; Only works for integer coefficients, so use non-generic
(cadr (div-terms
(mul-terms
(adjoin-term
(make-term 0 i-factor)
((get 'the-empty-termlist (type-tag L1))))
L1)
L2))))
(trace-define (gcd-terms a b)
(trace-define (coeff-gcd termlist)
(if (empty-termlist? termlist)
0
(gcd (coeff (first-term termlist))
(coeff-gcd (rest-terms termlist)))))
(trace-define (div-by-coeff-gcd termlist)
(div-terms
termlist
(adjoin-term
(make-term 0 (coeff-gcd termlist))
((get 'the-empty-termlist (type-tag termlist))))))
(if (empty-termlist? b)
a
(gcd-terms b (remainder-terms a b))))
(div-by-coeff-gcd a)
(gcd-terms b (pseudo-remainder-terms a b))))
;; Constructors