module Symtegration.Polynomial
(
Polynomial (..),
monic,
mapCoefficients,
divide,
pseudoDivide,
extendedEuclidean,
diophantineEuclidean,
greatestCommonDivisor,
subresultant,
differentiate,
integrate,
squarefree,
)
where
import Data.Monoid (Sum (..))
class (Integral e, Num c) => Polynomial p e c where
degree :: p e c -> e
coefficient :: p e c -> e -> c
leadingCoefficient :: p e c -> c
deleteLeadingTerm :: p e c -> p e c
foldTerms :: (Monoid m) => (e -> c -> m) -> p e c -> m
scale :: c -> p e c -> p e c
power :: e -> p e c
monic :: (Polynomial p e c, Eq c, Fractional c) => p e c -> p e c
monic :: forall (p :: * -> * -> *) e c.
(Polynomial p e c, Eq c, Fractional c) =>
p e c -> p e c
monic p e c
p
| p e c -> c
forall (p :: * -> * -> *) e c. Polynomial p e c => p e c -> c
leadingCoefficient p e c
p c -> c -> Bool
forall a. Eq a => a -> a -> Bool
== c
0 = p e c
p
| Bool
otherwise = c -> p e c -> p e c
forall (p :: * -> * -> *) e c.
Polynomial p e c =>
c -> p e c -> p e c
scale (c
1 c -> c -> c
forall a. Fractional a => a -> a -> a
/ p e c -> c
forall (p :: * -> * -> *) e c. Polynomial p e c => p e c -> c
leadingCoefficient p e c
p) p e c
p
mapCoefficients ::
(Polynomial p e c, Polynomial p e c', Num (p e c), Num (p e c')) =>
(c -> c') ->
p e c ->
p e c'
mapCoefficients :: forall (p :: * -> * -> *) e c c'.
(Polynomial p e c, Polynomial p e c', Num (p e c), Num (p e c')) =>
(c -> c') -> p e c -> p e c'
mapCoefficients c -> c'
f p e c
p = Sum (p e c') -> p e c'
forall a. Sum a -> a
getSum (Sum (p e c') -> p e c') -> Sum (p e c') -> p e c'
forall a b. (a -> b) -> a -> b
$ (e -> c -> Sum (p e c')) -> p e c -> Sum (p e c')
forall m. Monoid m => (e -> c -> m) -> p e c -> m
forall (p :: * -> * -> *) e c m.
(Polynomial p e c, Monoid m) =>
(e -> c -> m) -> p e c -> m
foldTerms e -> c -> Sum (p e c')
forall {p :: * -> * -> *} {e}.
Polynomial p e c' =>
e -> c -> Sum (p e c')
convertTerm p e c
p
where
convertTerm :: e -> c -> Sum (p e c')
convertTerm e
e c
c = p e c' -> Sum (p e c')
forall a. a -> Sum a
Sum (p e c' -> Sum (p e c')) -> p e c' -> Sum (p e c')
forall a b. (a -> b) -> a -> b
$ c' -> p e c' -> p e c'
forall (p :: * -> * -> *) e c.
Polynomial p e c =>
c -> p e c -> p e c
scale (c -> c'
f c
c) (e -> p e c'
forall (p :: * -> * -> *) e c. Polynomial p e c => e -> p e c
power e
e)
divide ::
(Polynomial p e c, Eq (p e c), Num (p e c), Fractional c) =>
p e c ->
p e c ->
(p e c, p e c)
divide :: forall (p :: * -> * -> *) e c.
(Polynomial p e c, Eq (p e c), Num (p e c), Fractional c) =>
p e c -> p e c -> (p e c, p e c)
divide p e c
p p e c
q = p e c -> p e c -> (p e c, p e c)
go p e c
0 p e c
p
where
go :: p e c -> p e c -> (p e c, p e c)
go p e c
quotient p e c
remainder
| p e c
remainder p e c -> p e c -> Bool
forall a. Eq a => a -> a -> Bool
/= p e c
0, e
delta e -> e -> Bool
forall a. Ord a => a -> a -> Bool
>= e
0 = p e c -> p e c -> (p e c, p e c)
go (p e c
quotient p e c -> p e c -> p e c
forall a. Num a => a -> a -> a
+ p e c
t) (p e c
remainder' p e c -> p e c -> p e c
forall a. Num a => a -> a -> a
- p e c
qt')
| Bool
otherwise = (p e c
quotient, p e c
remainder)
where
delta :: e
delta = p e c -> e
forall (p :: * -> * -> *) e c. Polynomial p e c => p e c -> e
degree p e c
remainder e -> e -> e
forall a. Num a => a -> a -> a
- p e c -> e
forall (p :: * -> * -> *) e c. Polynomial p e c => p e c -> e
degree p e c
q
t :: p e c
t = c -> p e c -> p e c
forall (p :: * -> * -> *) e c.
Polynomial p e c =>
c -> p e c -> p e c
scale (p e c -> c
forall (p :: * -> * -> *) e c. Polynomial p e c => p e c -> c
leadingCoefficient p e c
remainder c -> c -> c
forall a. Fractional a => a -> a -> a
/ p e c -> c
forall (p :: * -> * -> *) e c. Polynomial p e c => p e c -> c
leadingCoefficient p e c
q) (p e c -> p e c) -> p e c -> p e c
forall a b. (a -> b) -> a -> b
$ e -> p e c
forall (p :: * -> * -> *) e c. Polynomial p e c => e -> p e c
power e
delta
remainder' :: p e c
remainder' = p e c -> p e c
forall (p :: * -> * -> *) e c. Polynomial p e c => p e c -> p e c
deleteLeadingTerm p e c
remainder
qt' :: p e c
qt' = p e c -> p e c
forall (p :: * -> * -> *) e c. Polynomial p e c => p e c -> p e c
deleteLeadingTerm (p e c -> p e c) -> p e c -> p e c
forall a b. (a -> b) -> a -> b
$ p e c
q p e c -> p e c -> p e c
forall a. Num a => a -> a -> a
* p e c
t
pseudoDivide ::
(Polynomial p e c, Eq (p e c), Num (p e c), Num c) =>
p e c ->
p e c ->
(p e c, p e c)
pseudoDivide :: forall (p :: * -> * -> *) e c.
(Polynomial p e c, Eq (p e c), Num (p e c), Num c) =>
p e c -> p e c -> (p e c, p e c)
pseudoDivide p e c
p p e c
q
| p e c -> e
forall (p :: * -> * -> *) e c. Polynomial p e c => p e c -> e
degree p e c
p e -> e -> Bool
forall a. Ord a => a -> a -> Bool
< p e c -> e
forall (p :: * -> * -> *) e c. Polynomial p e c => p e c -> e
degree p e c
q = (p e c
0, p e c
p)
| Bool
otherwise = e -> p e c -> p e c -> (p e c, p e c)
forall {b}. Integral b => b -> p e c -> p e c -> (p e c, p e c)
go (e
1 e -> e -> e
forall a. Num a => a -> a -> a
+ p e c -> e
forall (p :: * -> * -> *) e c. Polynomial p e c => p e c -> e
degree p e c
p e -> e -> e
forall a. Num a => a -> a -> a
- p e c -> e
forall (p :: * -> * -> *) e c. Polynomial p e c => p e c -> e
degree p e c
q) p e c
0 p e c
p
where
b :: c
b = p e c -> c
forall (p :: * -> * -> *) e c. Polynomial p e c => p e c -> c
leadingCoefficient p e c
q
go :: b -> p e c -> p e c -> (p e c, p e c)
go b
n p e c
quotient p e c
remainder
| p e c
remainder p e c -> p e c -> Bool
forall a. Eq a => a -> a -> Bool
/= p e c
0, e
delta e -> e -> Bool
forall a. Ord a => a -> a -> Bool
>= e
0 = b -> p e c -> p e c -> (p e c, p e c)
go (b
n b -> b -> b
forall a. Num a => a -> a -> a
- b
1) p e c
quotient' p e c
remainder'
| Bool
otherwise = (c -> p e c -> p e c
forall (p :: * -> * -> *) e c.
Polynomial p e c =>
c -> p e c -> p e c
scale (c
b c -> b -> c
forall a b. (Num a, Integral b) => a -> b -> a
^ b
n) p e c
quotient, c -> p e c -> p e c
forall (p :: * -> * -> *) e c.
Polynomial p e c =>
c -> p e c -> p e c
scale (c
b c -> b -> c
forall a b. (Num a, Integral b) => a -> b -> a
^ b
n) p e c
remainder)
where
delta :: e
delta = p e c -> e
forall (p :: * -> * -> *) e c. Polynomial p e c => p e c -> e
degree p e c
remainder e -> e -> e
forall a. Num a => a -> a -> a
- p e c -> e
forall (p :: * -> * -> *) e c. Polynomial p e c => p e c -> e
degree p e c
q
t :: p e c
t = c -> p e c -> p e c
forall (p :: * -> * -> *) e c.
Polynomial p e c =>
c -> p e c -> p e c
scale (p e c -> c
forall (p :: * -> * -> *) e c. Polynomial p e c => p e c -> c
leadingCoefficient p e c
remainder) (e -> p e c
forall (p :: * -> * -> *) e c. Polynomial p e c => e -> p e c
power e
delta)
quotient' :: p e c
quotient' = c -> p e c -> p e c
forall (p :: * -> * -> *) e c.
Polynomial p e c =>
c -> p e c -> p e c
scale c
b p e c
quotient p e c -> p e c -> p e c
forall a. Num a => a -> a -> a
+ p e c
t
remainder' :: p e c
remainder' = p e c -> p e c
forall (p :: * -> * -> *) e c. Polynomial p e c => p e c -> p e c
deleteLeadingTerm (c -> p e c -> p e c
forall (p :: * -> * -> *) e c.
Polynomial p e c =>
c -> p e c -> p e c
scale c
b p e c
remainder) p e c -> p e c -> p e c
forall a. Num a => a -> a -> a
- p e c -> p e c
forall (p :: * -> * -> *) e c. Polynomial p e c => p e c -> p e c
deleteLeadingTerm (p e c
t p e c -> p e c -> p e c
forall a. Num a => a -> a -> a
* p e c
q)
extendedEuclidean ::
(Polynomial p e c, Eq (p e c), Num (p e c), Fractional c) =>
p e c ->
p e c ->
(p e c, p e c, p e c)
extendedEuclidean :: forall (p :: * -> * -> *) e c.
(Polynomial p e c, Eq (p e c), Num (p e c), Fractional c) =>
p e c -> p e c -> (p e c, p e c, p e c)
extendedEuclidean p e c
u p e c
v = p e c
-> p e c
-> p e c
-> p e c
-> p e c
-> p e c
-> (p e c, p e c, p e c)
forall {p :: * -> * -> *} {e} {c}.
(Polynomial p e c, Fractional c, Eq (p e c), Num (p e c)) =>
p e c
-> p e c
-> p e c
-> p e c
-> p e c
-> p e c
-> (p e c, p e c, p e c)
descend p e c
u p e c
v p e c
1 p e c
0 p e c
0 p e c
1
where
descend :: p e c
-> p e c
-> p e c
-> p e c
-> p e c
-> p e c
-> (p e c, p e c, p e c)
descend p e c
g p e c
0 p e c
s p e c
t p e c
_ p e c
_ = (p e c
s, p e c
t, p e c
g)
descend p e c
a p e c
b p e c
a1 p e c
a2 p e c
b1 p e c
b2 = p e c
-> p e c
-> p e c
-> p e c
-> p e c
-> p e c
-> (p e c, p e c, p e c)
descend p e c
b p e c
r p e c
b1 p e c
b2 p e c
r1 p e c
r2
where
(p e c
q, p e c
r) = p e c -> p e c -> (p e c, p e c)
forall (p :: * -> * -> *) e c.
(Polynomial p e c, Eq (p e c), Num (p e c), Fractional c) =>
p e c -> p e c -> (p e c, p e c)
divide p e c
a p e c
b
r1 :: p e c
r1 = p e c
a1 p e c -> p e c -> p e c
forall a. Num a => a -> a -> a
- p e c
q p e c -> p e c -> p e c
forall a. Num a => a -> a -> a
* p e c
b1
r2 :: p e c
r2 = p e c
a2 p e c -> p e c -> p e c
forall a. Num a => a -> a -> a
- p e c
q p e c -> p e c -> p e c
forall a. Num a => a -> a -> a
* p e c
b2
diophantineEuclidean ::
(Polynomial p e c, Eq (p e c), Num (p e c), Fractional c) =>
p e c ->
p e c ->
p e c ->
Maybe (p e c, p e c)
diophantineEuclidean :: forall (p :: * -> * -> *) e c.
(Polynomial p e c, Eq (p e c), Num (p e c), Fractional c) =>
p e c -> p e c -> p e c -> Maybe (p e c, p e c)
diophantineEuclidean p e c
a p e c
b p e c
c
| p e c
r p e c -> p e c -> Bool
forall a. Eq a => a -> a -> Bool
/= p e c
0 = Maybe (p e c, p e c)
forall a. Maybe a
Nothing
| p e c
s' p e c -> p e c -> Bool
forall a. Eq a => a -> a -> Bool
/= p e c
0, p e c -> e
forall (p :: * -> * -> *) e c. Polynomial p e c => p e c -> e
degree p e c
s' e -> e -> Bool
forall a. Ord a => a -> a -> Bool
>= p e c -> e
forall (p :: * -> * -> *) e c. Polynomial p e c => p e c -> e
degree p e c
b = (p e c, p e c) -> Maybe (p e c, p e c)
forall a. a -> Maybe a
Just (p e c
r', p e c
t' p e c -> p e c -> p e c
forall a. Num a => a -> a -> a
+ p e c
q' p e c -> p e c -> p e c
forall a. Num a => a -> a -> a
* p e c
a)
| Bool
otherwise = (p e c, p e c) -> Maybe (p e c, p e c)
forall a. a -> Maybe a
Just (p e c
s', p e c
t')
where
(p e c
s, p e c
t, p e c
g) = p e c -> p e c -> (p e c, p e c, p e c)
forall (p :: * -> * -> *) e c.
(Polynomial p e c, Eq (p e c), Num (p e c), Fractional c) =>
p e c -> p e c -> (p e c, p e c, p e c)
extendedEuclidean p e c
a p e c
b
(p e c
q, p e c
r) = p e c -> p e c -> (p e c, p e c)
forall (p :: * -> * -> *) e c.
(Polynomial p e c, Eq (p e c), Num (p e c), Fractional c) =>
p e c -> p e c -> (p e c, p e c)
divide p e c
c p e c
g
s' :: p e c
s' = p e c
q p e c -> p e c -> p e c
forall a. Num a => a -> a -> a
* p e c
s
t' :: p e c
t' = p e c
q p e c -> p e c -> p e c
forall a. Num a => a -> a -> a
* p e c
t
(p e c
q', p e c
r') = p e c -> p e c -> (p e c, p e c)
forall (p :: * -> * -> *) e c.
(Polynomial p e c, Eq (p e c), Num (p e c), Fractional c) =>
p e c -> p e c -> (p e c, p e c)
divide p e c
s' p e c
b
greatestCommonDivisor ::
(Polynomial p e c, Eq (p e c), Num (p e c), Fractional c) =>
p e c ->
p e c ->
p e c
greatestCommonDivisor :: forall (p :: * -> * -> *) e c.
(Polynomial p e c, Eq (p e c), Num (p e c), Fractional c) =>
p e c -> p e c -> p e c
greatestCommonDivisor p e c
p p e c
q = p e c
g
where
(p e c
_, p e c
_, p e c
g) = p e c -> p e c -> (p e c, p e c, p e c)
forall (p :: * -> * -> *) e c.
(Polynomial p e c, Eq (p e c), Num (p e c), Fractional c) =>
p e c -> p e c -> (p e c, p e c, p e c)
extendedEuclidean p e c
p p e c
q
subresultant ::
(Polynomial p e c, Eq (p e c), Num (p e c), Num e, Fractional c) =>
p e c ->
p e c ->
(c, [p e c])
subresultant :: forall (p :: * -> * -> *) e c.
(Polynomial p e c, Eq (p e c), Num (p e c), Num e, Fractional c) =>
p e c -> p e c -> (c, [p e c])
subresultant p e c
p p e c
q
| p e c -> e
forall (p :: * -> * -> *) e c. Polynomial p e c => p e c -> e
degree p e c
p e -> e -> Bool
forall a. Ord a => a -> a -> Bool
>= p e c -> e
forall (p :: * -> * -> *) e c. Polynomial p e c => p e c -> e
degree p e c
q = ([p e c] -> [c] -> c
forall (p :: * -> * -> *) e c.
(Polynomial p e c, Eq (p e c), Num (p e c), Num e, Fractional c) =>
[p e c] -> [c] -> c
resultantFromSequence [p e c]
rs [c]
betas, [p e c]
rs)
| Bool
otherwise = ((-c
1) c -> e -> c
forall a b. (Num a, Integral b) => a -> b -> a
^ (p e c -> e
forall (p :: * -> * -> *) e c. Polynomial p e c => p e c -> e
degree p e c
q e -> e -> e
forall a. Num a => a -> a -> a
* p e c -> e
forall (p :: * -> * -> *) e c. Polynomial p e c => p e c -> e
degree p e c
p) c -> c -> c
forall a. Num a => a -> a -> a
* c
resultant, [p e c]
prs)
where
([p e c]
rs, [c]
betas) = (p e c, p e c) -> c -> c -> ([p e c], [c])
forall (p :: * -> * -> *) e c.
(Polynomial p e c, Eq (p e c), Num (p e c), Num e, Fractional c) =>
(p e c, p e c) -> c -> c -> ([p e c], [c])
subresultantRemainderSequence (p e c
p, p e c
q) c
gamma c
beta
gamma :: c
gamma = -c
1
beta :: c
beta = (-c
1) c -> e -> c
forall a b. (Num a, Integral b) => a -> b -> a
^ (e
1 e -> e -> e
forall a. Num a => a -> a -> a
+ e
delta)
delta :: e
delta = p e c -> e
forall (p :: * -> * -> *) e c. Polynomial p e c => p e c -> e
degree p e c
p e -> e -> e
forall a. Num a => a -> a -> a
- p e c -> e
forall (p :: * -> * -> *) e c. Polynomial p e c => p e c -> e
degree p e c
q
(c
resultant, [p e c]
prs) = p e c -> p e c -> (c, [p e c])
forall (p :: * -> * -> *) e c.
(Polynomial p e c, Eq (p e c), Num (p e c), Num e, Fractional c) =>
p e c -> p e c -> (c, [p e c])
subresultant p e c
q p e c
p
subresultantRemainderSequence ::
(Polynomial p e c, Eq (p e c), Num (p e c), Num e, Fractional c) =>
(p e c, p e c) ->
c ->
c ->
([p e c], [c])
subresultantRemainderSequence :: forall (p :: * -> * -> *) e c.
(Polynomial p e c, Eq (p e c), Num (p e c), Num e, Fractional c) =>
(p e c, p e c) -> c -> c -> ([p e c], [c])
subresultantRemainderSequence (p e c
rprev, p e c
rcurr) c
gamma c
beta
| p e c
rcurr p e c -> p e c -> Bool
forall a. Eq a => a -> a -> Bool
/= p e c
0 = (p e c
rprev p e c -> [p e c] -> [p e c]
forall a. a -> [a] -> [a]
: [p e c]
rs, c
beta c -> [c] -> [c]
forall a. a -> [a] -> [a]
: [c]
betas)
| Bool
otherwise = ([p e c
rprev, p e c
rcurr], [c
beta])
where
([p e c]
rs, [c]
betas) = (p e c, p e c) -> c -> c -> ([p e c], [c])
forall (p :: * -> * -> *) e c.
(Polynomial p e c, Eq (p e c), Num (p e c), Num e, Fractional c) =>
(p e c, p e c) -> c -> c -> ([p e c], [c])
subresultantRemainderSequence (p e c
rcurr, p e c
rnext) c
gamma' c
beta'
(p e c
_, p e c
r) = p e c -> p e c -> (p e c, p e c)
forall (p :: * -> * -> *) e c.
(Polynomial p e c, Eq (p e c), Num (p e c), Num c) =>
p e c -> p e c -> (p e c, p e c)
pseudoDivide p e c
rprev p e c
rcurr
rnext :: p e c
rnext = c -> p e c -> p e c
forall (p :: * -> * -> *) e c.
Polynomial p e c =>
c -> p e c -> p e c
scale (c
1 c -> c -> c
forall a. Fractional a => a -> a -> a
/ c
beta) p e c
r
lc :: c
lc = p e c -> c
forall (p :: * -> * -> *) e c. Polynomial p e c => p e c -> c
leadingCoefficient p e c
rcurr
delta :: e
delta = p e c -> e
forall (p :: * -> * -> *) e c. Polynomial p e c => p e c -> e
degree p e c
rprev e -> e -> e
forall a. Num a => a -> a -> a
- p e c -> e
forall (p :: * -> * -> *) e c. Polynomial p e c => p e c -> e
degree p e c
rcurr
delta' :: e
delta' = p e c -> e
forall (p :: * -> * -> *) e c. Polynomial p e c => p e c -> e
degree p e c
rcurr e -> e -> e
forall a. Num a => a -> a -> a
- p e c -> e
forall (p :: * -> * -> *) e c. Polynomial p e c => p e c -> e
degree p e c
rnext
gamma' :: c
gamma' = ((-c
lc) c -> e -> c
forall a b. (Num a, Integral b) => a -> b -> a
^ e
delta) c -> c -> c
forall a. Num a => a -> a -> a
* (c
gamma c -> e -> c
forall a b. (Fractional a, Integral b) => a -> b -> a
^^ (e
1 e -> e -> e
forall a. Num a => a -> a -> a
- e
delta))
beta' :: c
beta' = (-c
lc) c -> c -> c
forall a. Num a => a -> a -> a
* (c
gamma' c -> e -> c
forall a b. (Num a, Integral b) => a -> b -> a
^ e
delta')
resultantFromSequence ::
(Polynomial p e c, Eq (p e c), Num (p e c), Num e, Fractional c) =>
[p e c] ->
[c] ->
c
resultantFromSequence :: forall (p :: * -> * -> *) e c.
(Polynomial p e c, Eq (p e c), Num (p e c), Num e, Fractional c) =>
[p e c] -> [c] -> c
resultantFromSequence [p e c]
rs [c]
betas = [p e c] -> [c] -> c -> c -> c
forall {t} {b} {p :: * -> * -> *}.
(Fractional t, Polynomial p b t) =>
[p b t] -> [t] -> t -> t -> t
go [p e c]
rs [c]
betas c
1 c
1
where
go :: [p b t] -> [t] -> t -> t -> t
go (p b t
r : p b t
r' : p b t
r'' : [p b t]
rs') (t
beta : [t]
betas') t
c t
s
| [] <- [p b t]
rs', p b t -> b
forall (p :: * -> * -> *) e c. Polynomial p e c => p e c -> e
degree p b t
r' b -> b -> Bool
forall a. Ord a => a -> a -> Bool
> b
0 = t
0
| [] <- [p b t]
rs', p b t -> b
forall (p :: * -> * -> *) e c. Polynomial p e c => p e c -> e
degree p b t
r b -> b -> Bool
forall a. Eq a => a -> a -> Bool
== b
1 = p b t -> t
forall (p :: * -> * -> *) e c. Polynomial p e c => p e c -> c
leadingCoefficient p b t
r'
| [] <- [p b t]
rs' = t
s t -> t -> t
forall a. Num a => a -> a -> a
* t
c t -> t -> t
forall a. Num a => a -> a -> a
* p b t -> t
forall (p :: * -> * -> *) e c. Polynomial p e c => p e c -> c
leadingCoefficient p b t
r' t -> b -> t
forall a b. (Num a, Integral b) => a -> b -> a
^ p b t -> b
forall (p :: * -> * -> *) e c. Polynomial p e c => p e c -> e
degree p b t
r
| Bool
otherwise = [p b t] -> [t] -> t -> t -> t
go (p b t
r' p b t -> [p b t] -> [p b t]
forall a. a -> [a] -> [a]
: p b t
r'' p b t -> [p b t] -> [p b t]
forall a. a -> [a] -> [a]
: [p b t]
rs') [t]
betas' t
c' t
s'
where
s' :: t
s' | b -> Bool
forall a. Integral a => a -> Bool
odd (p b t -> b
forall (p :: * -> * -> *) e c. Polynomial p e c => p e c -> e
degree p b t
r), b -> Bool
forall a. Integral a => a -> Bool
odd (p b t -> b
forall (p :: * -> * -> *) e c. Polynomial p e c => p e c -> e
degree p b t
r') = -t
s | Bool
otherwise = t
s
c' :: t
c' = t
c t -> t -> t
forall a. Num a => a -> a -> a
* ((t
beta t -> t -> t
forall a. Fractional a => a -> a -> a
/ (t
lc t -> b -> t
forall a b. (Num a, Integral b) => a -> b -> a
^ (b
1 b -> b -> b
forall a. Num a => a -> a -> a
+ b
delta))) t -> b -> t
forall a b. (Num a, Integral b) => a -> b -> a
^ p b t -> b
forall (p :: * -> * -> *) e c. Polynomial p e c => p e c -> e
degree p b t
r') t -> t -> t
forall a. Num a => a -> a -> a
* (t
lc t -> b -> t
forall a b. (Num a, Integral b) => a -> b -> a
^ (p b t -> b
forall (p :: * -> * -> *) e c. Polynomial p e c => p e c -> e
degree p b t
r b -> b -> b
forall a. Num a => a -> a -> a
- p b t -> b
forall (p :: * -> * -> *) e c. Polynomial p e c => p e c -> e
degree p b t
r''))
lc :: t
lc = p b t -> t
forall (p :: * -> * -> *) e c. Polynomial p e c => p e c -> c
leadingCoefficient p b t
r'
delta :: b
delta = p b t -> b
forall (p :: * -> * -> *) e c. Polynomial p e c => p e c -> e
degree p b t
r b -> b -> b
forall a. Num a => a -> a -> a
- p b t -> b
forall (p :: * -> * -> *) e c. Polynomial p e c => p e c -> e
degree p b t
r'
go [p b t]
_ [t]
_ t
_ t
_ = t
0
differentiate :: (Polynomial p e c, Num (p e c), Num c) => p e c -> p e c
differentiate :: forall (p :: * -> * -> *) e c.
(Polynomial p e c, Num (p e c), Num c) =>
p e c -> p e c
differentiate p e c
p = Sum (p e c) -> p e c
forall a. Sum a -> a
getSum (Sum (p e c) -> p e c) -> Sum (p e c) -> p e c
forall a b. (a -> b) -> a -> b
$ (e -> c -> Sum (p e c)) -> p e c -> Sum (p e c)
forall m. Monoid m => (e -> c -> m) -> p e c -> m
forall (p :: * -> * -> *) e c m.
(Polynomial p e c, Monoid m) =>
(e -> c -> m) -> p e c -> m
foldTerms e -> c -> Sum (p e c)
forall {e} {p :: * -> * -> *} {c}.
(Polynomial p e c, Num (p e c)) =>
e -> c -> Sum (p e c)
diffTerm p e c
p
where
diffTerm :: e -> c -> Sum (p e c)
diffTerm e
0 c
_ = p e c -> Sum (p e c)
forall a. a -> Sum a
Sum p e c
0
diffTerm e
e c
c = p e c -> Sum (p e c)
forall a. a -> Sum a
Sum (p e c -> Sum (p e c)) -> p e c -> Sum (p e c)
forall a b. (a -> b) -> a -> b
$ c -> p e c -> p e c
forall (p :: * -> * -> *) e c.
Polynomial p e c =>
c -> p e c -> p e c
scale (e -> c
forall a b. (Integral a, Num b) => a -> b
fromIntegral e
e c -> c -> c
forall a. Num a => a -> a -> a
* c
c) (p e c -> p e c) -> p e c -> p e c
forall a b. (a -> b) -> a -> b
$ e -> p e c
forall (p :: * -> * -> *) e c. Polynomial p e c => e -> p e c
power (e
e e -> e -> e
forall a. Num a => a -> a -> a
- e
1)
integrate :: (Polynomial p e c, Num (p e c), Fractional c) => p e c -> p e c
integrate :: forall (p :: * -> * -> *) e c.
(Polynomial p e c, Num (p e c), Fractional c) =>
p e c -> p e c
integrate p e c
p = Sum (p e c) -> p e c
forall a. Sum a -> a
getSum (Sum (p e c) -> p e c) -> Sum (p e c) -> p e c
forall a b. (a -> b) -> a -> b
$ (e -> c -> Sum (p e c)) -> p e c -> Sum (p e c)
forall m. Monoid m => (e -> c -> m) -> p e c -> m
forall (p :: * -> * -> *) e c m.
(Polynomial p e c, Monoid m) =>
(e -> c -> m) -> p e c -> m
foldTerms e -> c -> Sum (p e c)
forall {p :: * -> * -> *} {e} {c}.
(Polynomial p e c, Fractional c) =>
e -> c -> Sum (p e c)
integrateTerm p e c
p
where
integrateTerm :: e -> c -> Sum (p e c)
integrateTerm e
e c
c = p e c -> Sum (p e c)
forall a. a -> Sum a
Sum (p e c -> Sum (p e c)) -> p e c -> Sum (p e c)
forall a b. (a -> b) -> a -> b
$ c -> p e c -> p e c
forall (p :: * -> * -> *) e c.
Polynomial p e c =>
c -> p e c -> p e c
scale (c
c c -> c -> c
forall a. Fractional a => a -> a -> a
/ (c
1 c -> c -> c
forall a. Num a => a -> a -> a
+ e -> c
forall a b. (Integral a, Num b) => a -> b
fromIntegral e
e)) (p e c -> p e c) -> p e c -> p e c
forall a b. (a -> b) -> a -> b
$ e -> p e c
forall (p :: * -> * -> *) e c. Polynomial p e c => e -> p e c
power (e
e e -> e -> e
forall a. Num a => a -> a -> a
+ e
1)
squarefree :: (Polynomial p e c, Eq (p e c), Num (p e c), Eq c, Fractional c) => p e c -> [p e c]
squarefree :: forall (p :: * -> * -> *) e c.
(Polynomial p e c, Eq (p e c), Num (p e c), Eq c, Fractional c) =>
p e c -> [p e c]
squarefree p e c
0 = [p e c
0]
squarefree p e c
p
| (p e c
x : [p e c]
xs) <- p e c -> p e c -> [p e c]
forall {p :: * -> * -> *} {e} {c}.
(Polynomial p e c, Fractional c, Eq c, Eq (p e c), Num (p e c)) =>
p e c -> p e c -> [p e c]
factor p e c
u p e c
v = c -> p e c -> p e c
forall (p :: * -> * -> *) e c.
Polynomial p e c =>
c -> p e c -> p e c
scale c
c p e c
x p e c -> [p e c] -> [p e c]
forall a. a -> [a] -> [a]
: [p e c]
xs
| Bool
otherwise = [c -> p e c -> p e c
forall (p :: * -> * -> *) e c.
Polynomial p e c =>
c -> p e c -> p e c
scale c
c p e c
1]
where
c :: c
c = p e c -> c
forall (p :: * -> * -> *) e c. Polynomial p e c => p e c -> c
leadingCoefficient p e c
p
q :: p e c
q = c -> p e c -> p e c
forall (p :: * -> * -> *) e c.
Polynomial p e c =>
c -> p e c -> p e c
scale (c
1 c -> c -> c
forall a. Fractional a => a -> a -> a
/ c
c) p e c
p
q' :: p e c
q' = p e c -> p e c
forall (p :: * -> * -> *) e c.
(Polynomial p e c, Num (p e c), Num c) =>
p e c -> p e c
differentiate p e c
q
g :: p e c
g = p e c -> p e c
forall (p :: * -> * -> *) e c.
(Polynomial p e c, Eq c, Fractional c) =>
p e c -> p e c
monic (p e c -> p e c) -> p e c -> p e c
forall a b. (a -> b) -> a -> b
$ p e c -> p e c -> p e c
forall (p :: * -> * -> *) e c.
(Polynomial p e c, Eq (p e c), Num (p e c), Fractional c) =>
p e c -> p e c -> p e c
greatestCommonDivisor p e c
q p e c
q'
(p e c
u, p e c
_) = p e c
q p e c -> p e c -> (p e c, p e c)
forall (p :: * -> * -> *) e c.
(Polynomial p e c, Eq (p e c), Num (p e c), Fractional c) =>
p e c -> p e c -> (p e c, p e c)
`divide` p e c
g
(p e c
v, p e c
_) = p e c
q' p e c -> p e c -> (p e c, p e c)
forall (p :: * -> * -> *) e c.
(Polynomial p e c, Eq (p e c), Num (p e c), Fractional c) =>
p e c -> p e c -> (p e c, p e c)
`divide` p e c
g
factor :: p e c -> p e c -> [p e c]
factor p e c
s p e c
y
| p e c
z p e c -> p e c -> Bool
forall a. Eq a => a -> a -> Bool
== p e c
0 = [p e c
s]
| Bool
otherwise = p e c
f p e c -> [p e c] -> [p e c]
forall a. a -> [a] -> [a]
: p e c -> p e c -> [p e c]
factor p e c
s' p e c
y'
where
z :: p e c
z = p e c
y p e c -> p e c -> p e c
forall a. Num a => a -> a -> a
- p e c -> p e c
forall (p :: * -> * -> *) e c.
(Polynomial p e c, Num (p e c), Num c) =>
p e c -> p e c
differentiate p e c
s
f :: p e c
f = p e c -> p e c
forall (p :: * -> * -> *) e c.
(Polynomial p e c, Eq c, Fractional c) =>
p e c -> p e c
monic (p e c -> p e c) -> p e c -> p e c
forall a b. (a -> b) -> a -> b
$ p e c -> p e c -> p e c
forall (p :: * -> * -> *) e c.
(Polynomial p e c, Eq (p e c), Num (p e c), Fractional c) =>
p e c -> p e c -> p e c
greatestCommonDivisor p e c
s p e c
z
(p e c
s', p e c
_) = p e c
s p e c -> p e c -> (p e c, p e c)
forall (p :: * -> * -> *) e c.
(Polynomial p e c, Eq (p e c), Num (p e c), Fractional c) =>
p e c -> p e c -> (p e c, p e c)
`divide` p e c
f
(p e c
y', p e c
_) = p e c
z p e c -> p e c -> (p e c, p e c)
forall (p :: * -> * -> *) e c.
(Polynomial p e c, Eq (p e c), Num (p e c), Fractional c) =>
p e c -> p e c -> (p e c, p e c)
`divide` p e c
f