Personal tools

User talk:PaoloMartini

From HaskellWiki

Jump to: navigation, search

p(x) = \sum_{i=0}^n a_i x^i, a_n \neq 0

p(x) = a_n x^n + \sum_{i=0}^{n-1} a_i x^i

p(x-1) = a_n (x-1)^n + \sum_{i=0}^{n-1} a_i (x-1)^i

(x-1)^n = \sum_{k=0}^n {n \choose k} x^{n-k} (-1)^k

p(x-1) = a_n \sum_{k=0}^n {n \choose k} x^{n-k} (-1)^k + \sum_{i=0}^{n-1} a_i (x-1)^i

p(x-1) = a_n x^n + a_n \sum_{k=1}^n {n \choose k} x^{n-k} (-1)^k + \sum_{i=0}^{n-1} a_i (x-1)^i

{-# OPTIONS_GHC -fglasgow-exts #-}
module Polynomial where
 
import Test.QuickCheck
 
-- *f*actorial
f :: Integer -> Integer
f n = product [1..n]
 
-- *c*hoose -- binomial coefficient
c :: Integer -> Integer -> Integer
n `c` k = f n `div` (f k * f (n-k))
 
-- *p*olynomial
p, q :: [Integer] -> Integer -> Integer
p a x = sum [(a!!fromIntegral i)*(x^i) | i <- [0..n]] where n = length a - 1
 
q a x = ((a!!n) * ((x+1)^n))
      + ((a!!n) * sum [(fromIntegral n `c` fromIntegral k)*((x+1)^(n-k))*((-1)^k) | k <- [1..n]])
      + sum [(a!!fromIntegral i)*(x^i) | i <- [0..n-1]] where n = length a - 1
 
-- *t*est
t = quickCheck $ \(x::Integer) (xs::[Integer]) ->
      not (null xs) && last xs /= 0  ==>  p xs x == q xs x