2015-10-06 77 views
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)。

請告訴我我的錯誤。如果多餘的部分被錯誤地定義,你能否修復它,或者你知道一個不錯的選擇?

回答

0

poly-remainder

(p+ (poly-scale poly2 (* scale -1)) poly1) 

的目的是爲了抵消POLY1的首項。

但是......當poly2的度數小於poly1的度數時會發生什麼?

您需要將poly2與x^(m-n)相乘,其中m和n分別爲poly1和poly2的度數。我相信你可以通過在poly2上追加m-n零來實現。

相關問題