Skip to content

Commit

Permalink
Express polynomials evaluation using foldr1
Browse files Browse the repository at this point in the history
Old version had gratious multiplication. For example following
would be genrated for ax^3 + bx^62 +  cx + d assuming that everything
is inlined:

d + (c + (b + (a + 0*x)*x))*x

New version avoid multiplication by 0. Check for null vector
does not brack fusion.

For some reason performance is improved all cases. Even if vector is
large and not inlined.
  • Loading branch information
Shimuuar committed Sep 28, 2013
1 parent aa9f1a9 commit 3526590
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions Numeric/Polynomial.hs
Expand Up @@ -34,8 +34,9 @@ evaluatePolynomial :: (Vector v a, Num a)
-> v a -- ^ Coefficients
-> a
{-# INLINE evaluatePolynomial #-}
evaluatePolynomial x
= G.foldr (\a r -> a + r*x) 0
evaluatePolynomial x v
| G.null v = 0
| otherwise = G.foldr1 (\a r -> a + r*x) v

-- | Evaluate polynomial with only even powers using Horner's method.
-- Coefficients starts from lowest. In pseudocode:
Expand Down

0 comments on commit 3526590

Please sign in to comment.