Implement 2.94

main
Felix Martin 2020-12-08 08:22:13 -05:00
parent d25e14aaa2
commit 7fbc842873
1 changed files with 24 additions and 0 deletions

View File

@ -986,6 +986,22 @@
(else (=zero?-terms (rest-terms terms)))))
(=zero?-terms (term-list p)))
(define (remainder-terms a b)
(cdr (div-terms a b)))
(define (gcd-terms a b)
(if (empty-termlist? b)
a
(gcd-terms b (remainder-terms a b))))
(define (gcd-poly p1 p2)
(if (same-variable? (variable p1) (variable p2))
(make-poly (variable p1)
(gcd-terms (term-list p1)
(term-list p2)))
(error "Polys not in same var -- ADD-POLY"
(list p1 p2))))
;; interface to rest of the system
(define (tag p) (attach-tag 'polynomial p))
(put 'add '(polynomial polynomial)
@ -996,6 +1012,8 @@
(lambda (p1 p2) (tag (sub-poly p1 p2))))
(put 'div '(polynomial polynomial)
(lambda (p1 p2) (div-poly p1 p2)))
(put 'greatest-comond-divisor '(polynomial polynomial)
(lambda (p1 p2) (tag (gcd-poly p1 p2))))
(put '=zero? '(polynomial) =zero?-poly)
(put 'make 'poly-sparse
(lambda (var terms) (tag (make-poly-sparse var terms))))
@ -1071,3 +1089,9 @@
(display "\nex-2.94 - polynomial gcd\n")
(define (greatest-common-divisor x y) (apply-generic 'greatest-comond-divisor x y))
(define p1 (make-poly-sparse 'x '((4 1) (3 -1) (2 -2) (1 2))))
(define p2 (make-poly-sparse 'x '((3 1) (1 -1))))
(display (greatest-common-divisor p1 p2)) (newline)
(display "\nex-2.95\n")