1
下面我有這個功能,但似乎無法用它來手動評估聚師餘:您是否有Scheme中多項式餘項函數的定義?
(define (poly-scale poly n) (map (lambda (x) (* x n)) poly))
(define (poly-remainder poly1 poly2)
(let
((scale (/ (leading-coef poly1) (leading-coef poly2))))
(if (< (degree poly1) (degree poly2)) poly1
(poly-remainder (rest-of-poly (p+ (poly-scale poly2 (* scale -1)) poly1))
poly2))))
不幸的是,有一噸的幫助這裏的功能。程序5.14 on p. 23(pdf)包含其中大部分。 p^+的定義是Program 5.9 on p.18(pdf)。
兩個更多幫助功能:
(define list-of-zeros
(lambda(n)
(cond
((zero? n) '())
(else (cons 0 (list-of-zeros (sub1 n)))))))
(define sub1
(lambda(n)
(- n 1)))
這是我嘗試手動評估(X^2 - 2)的餘數/(X + 1):
(poly-remainder '(1 0 -2) '(1 1))
(poly-remainder (rest-of-poly (p+ (poly-scale poly2 (* scale -1)) poly1)) poly2))
(poly-remainder (rest-of-poly (p+ (poly-scale poly2 -1) poly1)) poly2))
(poly-remainder (rest-of-poly (p+ (-1 -1) poly1)) poly2))
(poly-remainder (rest-of-poly (1 -1 -3)) poly2))
(poly-remainder (-1 -3) poly2))
(poly-remainder (rest-of-poly (p+ (poly-scale poly2 (* scale -1)) poly1)) poly2))
(poly-remainder (rest-of-poly (p+ (1 1) poly1)) poly2))
(poly-remainder (rest-of-poly (0 -2)) poly2))
(poly-remainder (-2) poly2))
(-2)
但其餘應該是(-1)。
請告訴我我的錯誤。如果多餘的部分被錯誤地定義,你能否修復它,或者你知道一個不錯的選擇?