diff --git a/basis/math/polynomials/polynomials-tests.factor b/basis/math/polynomials/polynomials-tests.factor index 22ac89bc7d..43217b452c 100644 --- a/basis/math/polynomials/polynomials-tests.factor +++ b/basis/math/polynomials/polynomials-tests.factor @@ -33,3 +33,9 @@ IN: math.polynomials.tests [ { 10 200 3000 } ] [ { 1 10 100 1000 } pdiff ] unit-test + +[ { -512 2304 -4608 5376 -4032 2016 -672 144 -18 1 } ] +[ { -2 1 } 9 p^ ] unit-test + +[ 0 ] +[ 2 { -2 1 } 9 p^ polyval ] unit-test diff --git a/basis/math/polynomials/polynomials.factor b/basis/math/polynomials/polynomials.factor index 241fd34be9..7e43ec94ba 100644 --- a/basis/math/polynomials/polynomials.factor +++ b/basis/math/polynomials/polynomials.factor @@ -91,7 +91,10 @@ PRIVATE> dup length iota v* rest ; : polyval ( x p -- p[x] ) - [ length swap powers ] [ nip ] 2bi v. ; + ! Horner scheme + [ nip unclip-slice swap ] + [ drop ] 2bi + '[ [ _ * ] dip + ] each ; MACRO: polyval* ( p -- ) reverse