module Polynomial (
T, fromScalar, add, sub, neg, scale, mul,
differentiate, progression,
) where
type T a = [a]
fromScalar :: a -> [a]
fromScalar = (:[])
add :: Num a => [a] -> [a] -> [a]
add [] ys = ys
add xs [] = xs
add (x:xs) (y:ys) = x+y : add xs ys
sub :: Num a => [a] -> [a] -> [a]
sub [] ys = map negate ys
sub xs [] = xs
sub (x:xs) (y:ys) = x-y : sub xs ys
neg :: Num a => [a] -> [a]
neg = map negate
scale :: Num a => a -> [a] -> [a]
scale s = map (s*)
mul :: Num a => [a] -> [a] -> [a]
mul [] = const []
mul xs = foldr (\y zs -> add (scale y xs) (0:zs)) []
progression :: Num a => [a]
progression = iterate (1+) 1
differentiate :: (Num a) => [a] -> [a]
differentiate x = zipWith (*) (tail x) progression