Difference between revisions of "User talk:PaoloMartini"

From HaskellWiki
Jump to navigation Jump to search
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
  +
<math>p(x) = \sum_{i=0}^n a_i x^i, a_n \neq 0 </math>
  +
  +
<math>p(x) = a_n x^n + \sum_{i=0}^{n-1} a_i x^i </math>
  +
  +
<math>p(x-1) = a_n (x-1)^n + \sum_{i=0}^{n-1} a_i (x-1)^i </math>
  +
  +
<math>(x-1)^n = \sum_{k=0}^n {n \choose k} x^{n-k} (-1)^k </math>
  +
  +
<math>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 </math>
  +
  +
<math>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 </math>
  +
  +
<haskell>
  +
{-# 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
  +
</haskell>

Latest revision as of 20:41, 15 September 2006

{-# 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