Copyright | (c) 2019 Andrew Lelechenko |
---|---|
License | BSD3 |
Maintainer | Andrew Lelechenko <andrew.lelechenko@gmail.com> |
Safe Haskell | None |
Language | Haskell2010 |
Sparse polynomials with Semiring
instance.
Synopsis
- data Poly v a
- type VPoly = Poly Vector
- type UPoly = Poly Vector
- unPoly :: Poly v a -> v (Word, a)
- leading :: Vector v (Word, a) => Poly v a -> Maybe (Word, a)
- toPoly :: (Eq a, Semiring a, Vector v (Word, a)) => v (Word, a) -> Poly v a
- monomial :: (Eq a, Semiring a, Vector v (Word, a)) => Word -> a -> Poly v a
- scale :: (Eq a, Semiring a, Vector v (Word, a)) => Word -> a -> Poly v a -> Poly v a
- pattern X :: (Eq a, Semiring a, Vector v (Word, a), Eq (v (Word, a))) => Poly v a
- eval :: (Semiring a, Vector v (Word, a)) => Poly v a -> a -> a
- deriv :: (Eq a, Semiring a, Vector v (Word, a)) => Poly v a -> Poly v a
- integral :: (Eq a, Field a, Vector v (Word, a)) => Poly v a -> Poly v a
- gcdExt :: (Eq a, Field a, Vector v (Word, a), Eq (v (Word, a))) => Poly v a -> Poly v a -> (Poly v a, Poly v a)
Documentation
Polynomials of one variable with coefficients from a
,
backed by a Vector
v
(boxed, unboxed, storable, etc.).
Use pattern X
for construction:
>>>
(X + 1) + (X - 1) :: VPoly Integer
2 * X>>>
(X + 1) * (X - 1) :: UPoly Int
1 * X^2 + (-1)
Polynomials are stored normalized, without
zero coefficients, so 0 * X
+ 1 equals to 1.
Ord
instance does not make much sense mathematically,
it is defined only for the sake of Set
, Map
, etc.
Instances
(Eq a, Semiring a, Vector v (Word, a)) => IsList (Poly v a) Source # | |
Eq (v (Word, a)) => Eq (Poly v a) Source # | |
(Eq a, Num a, Vector v (Word, a)) => Num (Poly v a) Source # | |
Ord (v (Word, a)) => Ord (Poly v a) Source # | |
Defined in Data.Poly.Internal.Sparse | |
(Show a, Vector v (Word, a)) => Show (Poly v a) Source # | |
NFData (v (Word, a)) => NFData (Poly v a) Source # | |
Defined in Data.Poly.Internal.Sparse | |
(Eq a, Ring a, GcdDomain a, Eq (v (Word, a)), Vector v (Word, a)) => GcdDomain (Poly v a) Source # | Consider using |
(Eq a, Eq (v (Word, a)), Field a, Vector v (Word, a)) => Euclidean (Poly v a) Source # | |
(Eq a, Semiring a, Vector v (Word, a)) => Semiring (Poly v a) Source # | |
(Eq a, Ring a, Vector v (Word, a)) => Ring (Poly v a) Source # | |
Defined in Data.Poly.Internal.Sparse | |
type Item (Poly v a) Source # | |
Defined in Data.Poly.Internal.Sparse |
unPoly :: Poly v a -> v (Word, a) Source #
Convert Poly
to a vector of coefficients
(first element corresponds to a constant term).
leading :: Vector v (Word, a) => Poly v a -> Maybe (Word, a) Source #
Return a leading power and coefficient of a non-zero polynomial.
>>>
leading ((2 * X + 1) * (2 * X^2 - 1) :: UPoly Int)
Just (3,4)>>>
leading (0 :: UPoly Int)
Nothing
Semiring interface
toPoly :: (Eq a, Semiring a, Vector v (Word, a)) => v (Word, a) -> Poly v a Source #
Make Poly
from a list of (power, coefficient) pairs.
(first element corresponds to a constant term).
>>>
:set -XOverloadedLists
>>>
toPoly [(0,1),(1,2),(2,3)] :: VPoly Integer
3 * X^2 + 2 * X + 1>>>
S.toPoly [(0,0),(1,0),(2,0)] :: UPoly Int
0
monomial :: (Eq a, Semiring a, Vector v (Word, a)) => Word -> a -> Poly v a Source #
Create a monomial from a power and a coefficient.
scale :: (Eq a, Semiring a, Vector v (Word, a)) => Word -> a -> Poly v a -> Poly v a Source #
Multiply a polynomial by a monomial, expressed as a power and a coefficient.
>>>
scale 2 3 (X^2 + 1) :: UPoly Int
3 * X^4 + 3 * X^2
pattern X :: (Eq a, Semiring a, Vector v (Word, a), Eq (v (Word, a))) => Poly v a Source #
Create an identity polynomial.
eval :: (Semiring a, Vector v (Word, a)) => Poly v a -> a -> a Source #
Evaluate at a given point.
>>>
eval (X^2 + 1 :: UPoly Int) 3
10>>>
eval (X^2 + 1 :: VPoly (UPoly Int)) (X + 1)
1 * X^2 + 2 * X + 2
deriv :: (Eq a, Semiring a, Vector v (Word, a)) => Poly v a -> Poly v a Source #
Take a derivative.
>>>
deriv (X^3 + 3 * X) :: UPoly Int
3 * X^2 + 3
integral :: (Eq a, Field a, Vector v (Word, a)) => Poly v a -> Poly v a Source #
Compute an indefinite integral of a polynomial, setting constant term to zero.
>>>
integral (3 * X^2 + 3) :: UPoly Double
1.0 * X^3 + 3.0 * X
Polynomials over Field
gcdExt :: (Eq a, Field a, Vector v (Word, a), Eq (v (Word, a))) => Poly v a -> Poly v a -> (Poly v a, Poly v a) Source #
Execute the extended Euclidean algorithm.
For polynomials a
and b
, compute their unique greatest common divisor g
and the unique coefficient polynomial s
satisfying as + bt = g
,
such that either g
is monic, or g = 0
and s
is monic, or g = s = 0
.
>>>
gcdExt (X^2 + 1 :: UPoly Double) (X^3 + 3 * X :: UPoly Double)
(1.0, 0.5 * X^2 + (-0.0) * X + 1.0)>>>
gcdExt (X^3 + 3 * X :: UPoly Double) (3 * X^4 + 3 * X^2 :: UPoly Double)
(1.0 * X + 0.0,(-0.16666666666666666) * X^2 + (-0.0) * X + 0.3333333333333333)