From 6282f0aaab6a0a6a32469b520eddb9bf0aa05f04 Mon Sep 17 00:00:00 2001 From: Felix Martin Date: Tue, 8 Dec 2020 19:16:44 -0500 Subject: [PATCH] Fix bug in poly-div and work on 2.95 --- ex-2_77-97.scm | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/ex-2_77-97.scm b/ex-2_77-97.scm index 6f9ba7c..9d85cbb 100644 --- a/ex-2_77-97.scm +++ b/ex-2_77-97.scm @@ -97,7 +97,7 @@ (let ((g (gcd n d))) (if (eq? g 'nogcd) (cons n d) - (cons (/ n g) (/ d g))))) + (cons (div n g) (div d g))))) (define (add-rat x y) (let ((new-n (add (mul (numer x) (denom y)) (mul (numer y) (denom x)))) @@ -959,7 +959,7 @@ (term-list (contents terms))) (cons term-type (map negate-term term-list)))) (if (empty-termlist? L1) - (list L1 L2) + (list L1 L1) (let ((t1 (first-term L1)) ; dividend (t2 (first-term L2))) ; divisor (if (> (order t2) (order t1)) @@ -987,14 +987,16 @@ (=zero?-terms (term-list p))) (define (remainder-terms a b) - (cdr (div-terms a b))) + (cadr (div-terms a b))) (define (gcd-terms a b) + ; (display "GCD-TERMS") (display a) (display b) (newline) (if (empty-termlist? b) a (gcd-terms b (remainder-terms a b)))) (define (gcd-poly p1 p2) + (display "GCD-POLY") (display p1) (display p2) (newline) (if (same-variable? (variable p1) (variable p2)) (make-poly (variable p1) (gcd-terms (term-list p1) @@ -1092,6 +1094,22 @@ (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) +(assert (greatest-common-divisor p1 p2) + (make-poly-sparse 'x '((2 -1) (1 1)))) (display "\nex-2.95\n") + +(define p1 (make-poly-sparse 'x '((2 1) (1 -2) (0 1)))) +(define p2 (make-poly-sparse 'x '((2 11) (0 7)))) +(define p3 (make-poly-sparse 'x '((1 13) (0 5)))) + +(define q1 (mul p1 p2)) +(define q2 (mul p1 p3)) + +(display q1) (newline) +(display q2) (newline) +(display (greatest-common-divisor q1 q2)) (newline) + + + +