More efficient polyval algorithm in math.polynomials

db4
Doug Coleman 2011-02-12 22:41:13 -06:00
parent 02ecc45165
commit fd7e5ffee8
2 changed files with 10 additions and 1 deletions

View File

@ -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

View File

@ -91,7 +91,10 @@ PRIVATE>
dup length iota v* rest ;
: polyval ( x p -- p[x] )
[ length swap powers ] [ nip ] 2bi v. ;
! Horner scheme
[ nip <reversed> unclip-slice swap ]
[ drop ] 2bi
'[ [ _ * ] dip + ] each ;
MACRO: polyval* ( p -- )
reverse