Implement 2.92

That was a hard one. The code is not the cleanest, but I am happy that I
was able to get it done. Let's get this chapter done.
This commit is contained in:
Felix Martin 2020-12-02 13:27:09 -05:00
parent 443665a099
commit 47e979a30c

View File

@ -818,23 +818,49 @@
(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))
(define (scale-terms factor terms)
; (display "SCALE-TERMS ") (display factor) (display terms) (newline)
(if (empty-termlist? terms)
terms
(adjoin-term (coerce-term (first-term terms))
(coerce-terms (rest-terms terms) target-var))))
(let ((term (first-term terms))
(rest (rest-terms terms)))
(adjoin-term (make-term (order term) (mul (coeff term) factor))
(scale-terms factor rest)))))
(put 'mul '(scheme-number polynomial)
(lambda (s p) (tag (make-poly (variable p)
(scale-terms s (term-list p))))))
(define (coerce-terms terms source-var target-var)
; (display "COERCE-TERMS ") (display terms) (newline)
(define (coerce-term t)
; (display "COERCE-TERM ") (display t) (newline)
(let ((c (coeff t))
(o (order t))
(new-poly (tag (make-poly-dense
source-var
(list (list (order t) 1))))))
; (display "NEW-POLY ") (display new-poly) (newline)
(if (eq? (type-tag c) 'polynomial)
(let ((new-poly (tag (make-poly-dense source-var (list (list o 1))))))
(scale-terms new-poly (contents (term-list c))))
(let ((sub-poly (tag (make-poly-dense
source-var
(list (list o c))))))
(cons 'dense (list (list 0 sub-poly)))))))
(if (empty-termlist? terms)
terms
(add-terms (coerce-term (first-term terms))
(coerce-terms (rest-terms terms) source-var target-var))))
(define (coerce-poly p target-var)
(display "COERCE-POLY ") (display p) (newline)
; (display "COERCE-POLY ") (display p) (newline)
(if (eq? (variable p) target-var)
p
(make-poly target-var (coerce-terms (term-list p) target-var))))
(let ((coercion-result (coerce-terms (term-list p) (variable p) target-var)))
; (display "COERCE-POLY-RESULT ") (display coercion-result) (newline)
(make-poly target-var coercion-result))))
(define (coerce-polys p1 p2)
(let ((coercion-target-variable (get-coercion-target p1 p2)))
@ -850,9 +876,6 @@
(add-terms (term-list p1)
(term-list 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"
@ -1004,9 +1027,9 @@
'((2 1) (0 1)))))))
(define p2 (make-poly-dense
'y
(list (list 2 (make-poly-dense
'x
'((2 1) (0 1)))))))
(list
(list 4 3)
(list 2 (make-poly-dense 'x '((2 1) (0 8)))))))
(display (add p1 p2)) (newline)