Working on 2.92

main
Felix Martin 2020-11-30 11:57:11 -05:00
parent 164757c739
commit 443665a099
1 changed files with 63 additions and 11 deletions

View File

@ -788,6 +788,7 @@
(define (coeff term) (cadr term))
(define (add-terms L1 L2)
;(display "ADD-TERMS ") (display L1) (display L2) (newline)
(cond ((empty-termlist? L1) L2)
((empty-termlist? L2) L1)
(else
@ -805,13 +806,57 @@
(add-terms (rest-terms L1)
(rest-terms L2)))))))))
(define (get-coercion-target p1 p2)
(let ((v1 (variable p1))
(v2 (variable p2)))
(cond
; Here we could introduce an ordering where we find the variable with
; the highest ordering and return it, but for testing purposes let's
; just hardcode it.
((and (eq? v1 'y) (eq? v2 'x)) 'x)
((and (eq? v1 'x) (eq? v2 'y)) 'x)
(else (error "Coercion not supported -- GET-COERCION-TARGET"
(list p1 p2))))))
(define (coerce-terms terms target-var)
(display "COERCE-TERMS ") (display terms) (newline)
(define (coerce-term t)
(display "COERCE-TERM ") (display t) (newline)
; XXX: implement this
(make-term (order t) 1))
(if (empty-termlist? terms)
terms
(adjoin-term (coerce-term (first-term terms))
(coerce-terms (rest-terms terms) target-var))))
(define (coerce-poly p target-var)
(display "COERCE-POLY ") (display p) (newline)
(if (eq? (variable p) target-var)
p
(make-poly target-var (coerce-terms (term-list p) target-var))))
(define (coerce-polys p1 p2)
(let ((coercion-target-variable (get-coercion-target p1 p2)))
(if coercion-target-variable
(list (coerce-poly p1 coercion-target-variable)
(coerce-poly p2 coercion-target-variable))
#f)))
(define (add-poly p1 p2)
;(display "ADD-POLY ") (display p1) (display p2) (newline)
(if (same-variable? (variable p1) (variable p2))
(make-poly (variable p1)
(add-terms (term-list p1)
(term-list p2)))
(error "Polys not in same var -- ADD-POLY"
(list p1 p2))))
(let ((coerced-polys (coerce-polys p1 p2)))
(display "COERCED-POLYS ") (newline)
(display "1 ") (display (car coerced-polys)) (newline)
(display "2 ") (display (cadr coerced-polys)) (newline)
(if coerced-polys
(add-poly (car coerced-polys) (cadr coerced-polys))
(error "Polys not in same var -- ADD-POLY"
(list p1 p2))))))
(define (sub-poly p1 p2)
(define (negate-term term)
@ -856,14 +901,6 @@
(error "Polys not in same var -- MUL-POLY"
(list p1 p2))))
; Division can be performed via long division. That is, divide the
; highest-order term of the dividend by the highest-order term of the
; divisor. The result is the first term of the quotient. Next, multiply the
; result by the divisor, subtract that from the dividend, and produce the
; rest of the answer by recursively dividing the difference by the divisor.
; Stop when the order of the divisor exceeds the order of the dividend and
; declare the dividend to be the remainder. Also, if the dividend ever
; becomes zero, return zero as both quotient and remainder.
(define (div-terms L1 L2)
(define (negate-term t)
(make-term (order t) (negate (coeff t))))
@ -958,5 +995,20 @@
(assert (cadr result) (make-poly-dense 'x '((1 1) (0 -1)))))
(display "\nex-2.92\n")
(display "\nex-2.92 - coerce polynomials\n")
(define p1 (make-poly-dense
'x
(list (list 1 (make-poly-dense
'y
'((2 1) (0 1)))))))
(define p2 (make-poly-dense
'y
(list (list 2 (make-poly-dense
'x
'((2 1) (0 1)))))))
(display (add p1 p2)) (newline)
(display "\nex-2.93\n")