yap-0.2: yet another prelude - a simplistic refactoring with algebraic classes

Safe HaskellSafe-Infered




An example instance of the new classes: polynomials. Some of these functions work with infinite polynomials, i.e. formal power series.



data Polynomial a Source


Functor Polynomial 
(Eq a, AbelianGroup a) => Eq (Polynomial a) 
(Eq a, Show a, AbelianGroup a) => Show (Polynomial a) 
(Eq a, Field a) => EuclideanDomain (Polynomial a)

If b is non-zero, mod a b has a smaller degree than b. If a is non-zero, associate a has a leading coefficient of 1.

Ring a => Ring (Polynomial a) 
AbelianGroup a => AbelianGroup (Polynomial a) 

polynomial :: [a] -> Polynomial aSource

Construct a polynomial from a list of coefficients, least significant first.


Finite polynomials

coefficients :: (Eq a, AbelianGroup a) => Polynomial a -> [a]Source

The coefficients of a finite polynomial, least significant first and with no trailing zeroes.

degree :: (Eq a, AbelianGroup a) => Polynomial a -> IntSource

The degree of a finite polynomial.

degree p = length (coefficients p)

evaluate :: Ring a => Polynomial a -> a -> aSource

Evaluate a polynomial for a given value of x.

evaluate a x = zipWith (*) (coefficients a) (iterate (*x) 1)

(The implementation uses Horner's rule.)

pretty :: (Ord a, Show a, Ring a) => Polynomial a -> String -> StringSource

Pretty-print a polynomial, e.g.

pretty (polynomial [3, 4, 0, 1, 5]) "x" = "5x^4 + x^3 + 4x + 3"

Formal power series

approximations :: Ring a => Polynomial a -> a -> [a]Source

The infinite list of evaluations of truncations of the polynomial or power series.


compose :: Ring a => Polynomial a -> Polynomial a -> Polynomial aSource

Composition of polynomials:

evaluate (compose a b) = evaluate a . evaluate b

differentiate :: Ring a => Polynomial a -> Polynomial aSource

Symbolic differentiation of polynomials.

integrate :: Field a => Polynomial a -> Polynomial aSource

Symbolic integration of polynomials.