module Symtegration.Integration.Rational
(
integrate,
hermiteReduce,
rationalIntegralLogTerms,
complexLogTermToAtan,
complexLogTermToRealTerm,
toRationalFunction,
RationalFunction (..),
)
where
import Data.List (find, intersect)
import Data.Monoid (Sum (..))
import Data.Text (Text)
import Symtegration.Polynomial hiding (integrate)
import Symtegration.Polynomial qualified as Polynomial
import Symtegration.Polynomial.Indexed
import Symtegration.Polynomial.Solve
import Symtegration.Polynomial.Symbolic
import Symtegration.Symbolic
import Symtegration.Symbolic.Simplify
integrate :: Text -> Expression -> Maybe Expression
integrate :: Text -> Expression -> Maybe Expression
integrate Text
v Expression
e
| (Expression
x :/: Expression
y) <- Expression
e',
(Just IndexedPolynomial
n) <- (Text -> Maybe IndexedPolynomial, Expression -> Maybe Rational)
-> Expression -> Maybe IndexedPolynomial
forall (p :: * -> * -> *) e c.
(Polynomial p e c, Num (p e c), Fractional c) =>
(Text -> Maybe (p e c), Expression -> Maybe c)
-> Expression -> Maybe (p e c)
fromExpression (Text
-> (Text -> Maybe IndexedPolynomial, Expression -> Maybe Rational)
forall (p :: * -> * -> *) e c.
(Polynomial p e c, Num (p e c), Fractional c) =>
Text -> (Text -> Maybe (p e c), Expression -> Maybe c)
forVariable Text
v) Expression
x,
(Just IndexedPolynomial
d) <- (Text -> Maybe IndexedPolynomial, Expression -> Maybe Rational)
-> Expression -> Maybe IndexedPolynomial
forall (p :: * -> * -> *) e c.
(Polynomial p e c, Num (p e c), Fractional c) =>
(Text -> Maybe (p e c), Expression -> Maybe c)
-> Expression -> Maybe (p e c)
fromExpression (Text
-> (Text -> Maybe IndexedPolynomial, Expression -> Maybe Rational)
forall (p :: * -> * -> *) e c.
(Polynomial p e c, Num (p e c), Fractional c) =>
Text -> (Text -> Maybe (p e c), Expression -> Maybe c)
forVariable Text
v) Expression
y,
IndexedPolynomial
d IndexedPolynomial -> IndexedPolynomial -> Bool
forall a. Eq a => a -> a -> Bool
/= IndexedPolynomial
0 =
IndexedPolynomial -> IndexedPolynomial -> Maybe Expression
integrate' IndexedPolynomial
n IndexedPolynomial
d
| Bool
otherwise = Maybe Expression
forall a. Maybe a
Nothing
where
e' :: Expression
e' = Text -> Expression -> Expression
simplifyForVariable Text
v Expression
e
integrate' :: IndexedPolynomial -> IndexedPolynomial -> Maybe Expression
integrate' IndexedPolynomial
n IndexedPolynomial
d = Expression -> Expression -> Expression
forall a. Num a => a -> a -> a
(+) Expression
reduced (Expression -> Expression)
-> (Expression -> Expression) -> Expression -> Expression
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Expression -> Expression -> Expression
forall a. Num a => a -> a -> a
(+) Expression
poly (Expression -> Expression) -> Maybe Expression -> Maybe Expression
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe Expression
logs
where
([RationalFunction]
g, RationalFunction
h) = RationalFunction -> ([RationalFunction], RationalFunction)
hermiteReduce (RationalFunction -> ([RationalFunction], RationalFunction))
-> RationalFunction -> ([RationalFunction], RationalFunction)
forall a b. (a -> b) -> a -> b
$ IndexedPolynomial -> IndexedPolynomial -> RationalFunction
toRationalFunction IndexedPolynomial
n IndexedPolynomial
d
reduced :: Expression
reduced = [Expression] -> Expression
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum ([Expression] -> Expression) -> [Expression] -> Expression
forall a b. (a -> b) -> a -> b
$ (RationalFunction -> Expression)
-> [RationalFunction] -> [Expression]
forall a b. (a -> b) -> [a] -> [b]
map RationalFunction -> Expression
fromRationalFunction [RationalFunction]
g
RationalFunction IndexedPolynomial
numer IndexedPolynomial
denom = RationalFunction
h
(IndexedPolynomial
q, IndexedPolynomial
r) = IndexedPolynomial
numer IndexedPolynomial
-> IndexedPolynomial -> (IndexedPolynomial, IndexedPolynomial)
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` IndexedPolynomial
denom
poly :: Expression
poly = Text -> (Rational -> Expression) -> IndexedPolynomial -> Expression
forall (p :: * -> * -> *) e c.
Polynomial p e c =>
Text -> (c -> Expression) -> p e c -> Expression
toExpression Text
v Rational -> Expression
forall c. Real c => c -> Expression
toRationalCoefficient (IndexedPolynomial -> Expression)
-> IndexedPolynomial -> Expression
forall a b. (a -> b) -> a -> b
$ IndexedPolynomial -> IndexedPolynomial
forall (p :: * -> * -> *) e c.
(Polynomial p e c, Num (p e c), Fractional c) =>
p e c -> p e c
Polynomial.integrate IndexedPolynomial
q
h' :: RationalFunction
h' = IndexedPolynomial -> IndexedPolynomial -> RationalFunction
toRationalFunction IndexedPolynomial
r IndexedPolynomial
denom
logTerms :: Maybe [(IndexedPolynomial, P Int IndexedPolynomial)]
logTerms = RationalFunction
-> Maybe [(IndexedPolynomial, P Int IndexedPolynomial)]
rationalIntegralLogTerms RationalFunction
h'
logs :: Maybe Expression
logs :: Maybe Expression
logs
| (Just [(IndexedPolynomial, P Int IndexedPolynomial)]
terms) <- Maybe [(IndexedPolynomial, P Int IndexedPolynomial)]
logTerms = [Expression] -> Expression
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum ([Expression] -> Expression)
-> Maybe [Expression] -> Maybe Expression
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Maybe Expression] -> Maybe [Expression]
forall a. [Maybe a] -> Maybe [a]
toMaybeList (((IndexedPolynomial, P Int IndexedPolynomial) -> Maybe Expression)
-> [(IndexedPolynomial, P Int IndexedPolynomial)]
-> [Maybe Expression]
forall a b. (a -> b) -> [a] -> [b]
map (Text
-> (IndexedPolynomial, P Int IndexedPolynomial) -> Maybe Expression
complexLogTermToRealExpression Text
v) [(IndexedPolynomial, P Int IndexedPolynomial)]
terms)
| Bool
otherwise = Maybe Expression
forall a. Maybe a
Nothing
fromRationalFunction :: RationalFunction -> Expression
fromRationalFunction (RationalFunction IndexedPolynomial
u IndexedPolynomial
w) = Expression
u' Expression -> Expression -> Expression
forall a. Fractional a => a -> a -> a
/ Expression
w'
where
u' :: Expression
u' = Text -> (Rational -> Expression) -> IndexedPolynomial -> Expression
forall (p :: * -> * -> *) e c.
Polynomial p e c =>
Text -> (c -> Expression) -> p e c -> Expression
toExpression Text
v Rational -> Expression
forall c. Real c => c -> Expression
toRationalCoefficient IndexedPolynomial
u
w' :: Expression
w' = Text -> (Rational -> Expression) -> IndexedPolynomial -> Expression
forall (p :: * -> * -> *) e c.
Polynomial p e c =>
Text -> (c -> Expression) -> p e c -> Expression
toExpression Text
v Rational -> Expression
forall c. Real c => c -> Expression
toRationalCoefficient IndexedPolynomial
w
data RationalFunction = RationalFunction IndexedPolynomial IndexedPolynomial
deriving (RationalFunction -> RationalFunction -> Bool
(RationalFunction -> RationalFunction -> Bool)
-> (RationalFunction -> RationalFunction -> Bool)
-> Eq RationalFunction
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: RationalFunction -> RationalFunction -> Bool
== :: RationalFunction -> RationalFunction -> Bool
$c/= :: RationalFunction -> RationalFunction -> Bool
/= :: RationalFunction -> RationalFunction -> Bool
Eq)
instance Show RationalFunction where
show :: RationalFunction -> String
show (RationalFunction IndexedPolynomial
n IndexedPolynomial
d) = String
"(" String -> ShowS
forall a. Semigroup a => a -> a -> a
<> IndexedPolynomial -> String
forall a. Show a => a -> String
show IndexedPolynomial
n String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
") / (" String -> ShowS
forall a. Semigroup a => a -> a -> a
<> IndexedPolynomial -> String
forall a. Show a => a -> String
show IndexedPolynomial
d String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
")"
instance Num RationalFunction where
(RationalFunction IndexedPolynomial
x IndexedPolynomial
y) + :: RationalFunction -> RationalFunction -> RationalFunction
+ (RationalFunction IndexedPolynomial
u IndexedPolynomial
v) =
IndexedPolynomial -> IndexedPolynomial -> RationalFunction
toRationalFunction (IndexedPolynomial
x IndexedPolynomial -> IndexedPolynomial -> IndexedPolynomial
forall a. Num a => a -> a -> a
* IndexedPolynomial
v IndexedPolynomial -> IndexedPolynomial -> IndexedPolynomial
forall a. Num a => a -> a -> a
+ IndexedPolynomial
u IndexedPolynomial -> IndexedPolynomial -> IndexedPolynomial
forall a. Num a => a -> a -> a
* IndexedPolynomial
y) (IndexedPolynomial
y IndexedPolynomial -> IndexedPolynomial -> IndexedPolynomial
forall a. Num a => a -> a -> a
* IndexedPolynomial
v)
(RationalFunction IndexedPolynomial
x IndexedPolynomial
y) - :: RationalFunction -> RationalFunction -> RationalFunction
- (RationalFunction IndexedPolynomial
u IndexedPolynomial
v) =
IndexedPolynomial -> IndexedPolynomial -> RationalFunction
toRationalFunction (IndexedPolynomial
x IndexedPolynomial -> IndexedPolynomial -> IndexedPolynomial
forall a. Num a => a -> a -> a
* IndexedPolynomial
v IndexedPolynomial -> IndexedPolynomial -> IndexedPolynomial
forall a. Num a => a -> a -> a
- IndexedPolynomial
u IndexedPolynomial -> IndexedPolynomial -> IndexedPolynomial
forall a. Num a => a -> a -> a
* IndexedPolynomial
y) (IndexedPolynomial
y IndexedPolynomial -> IndexedPolynomial -> IndexedPolynomial
forall a. Num a => a -> a -> a
* IndexedPolynomial
v)
(RationalFunction IndexedPolynomial
x IndexedPolynomial
y) * :: RationalFunction -> RationalFunction -> RationalFunction
* (RationalFunction IndexedPolynomial
u IndexedPolynomial
v) =
IndexedPolynomial -> IndexedPolynomial -> RationalFunction
toRationalFunction (IndexedPolynomial
x IndexedPolynomial -> IndexedPolynomial -> IndexedPolynomial
forall a. Num a => a -> a -> a
* IndexedPolynomial
u) (IndexedPolynomial
y IndexedPolynomial -> IndexedPolynomial -> IndexedPolynomial
forall a. Num a => a -> a -> a
* IndexedPolynomial
v)
abs :: RationalFunction -> RationalFunction
abs = RationalFunction -> RationalFunction
forall a. a -> a
id
signum :: RationalFunction -> RationalFunction
signum RationalFunction
0 = RationalFunction
0
signum RationalFunction
_ = RationalFunction
1
fromInteger :: Integer -> RationalFunction
fromInteger Integer
n = IndexedPolynomial -> IndexedPolynomial -> RationalFunction
RationalFunction (Integer -> IndexedPolynomial
forall a. Num a => Integer -> a
fromInteger Integer
n) IndexedPolynomial
1
instance Fractional RationalFunction where
fromRational :: Rational -> RationalFunction
fromRational Rational
q = IndexedPolynomial -> IndexedPolynomial -> RationalFunction
RationalFunction (Rational -> IndexedPolynomial -> IndexedPolynomial
forall (p :: * -> * -> *) e c.
Polynomial p e c =>
c -> p e c -> p e c
scale Rational
q IndexedPolynomial
1) IndexedPolynomial
1
recip :: RationalFunction -> RationalFunction
recip (RationalFunction IndexedPolynomial
p IndexedPolynomial
q) = IndexedPolynomial -> IndexedPolynomial -> RationalFunction
RationalFunction IndexedPolynomial
q IndexedPolynomial
p
toRationalFunction ::
IndexedPolynomial ->
IndexedPolynomial ->
RationalFunction
toRationalFunction :: IndexedPolynomial -> IndexedPolynomial -> RationalFunction
toRationalFunction IndexedPolynomial
x IndexedPolynomial
0 = IndexedPolynomial -> IndexedPolynomial -> RationalFunction
RationalFunction IndexedPolynomial
x IndexedPolynomial
0
toRationalFunction IndexedPolynomial
x IndexedPolynomial
y = IndexedPolynomial -> IndexedPolynomial -> RationalFunction
RationalFunction IndexedPolynomial
x' IndexedPolynomial
y'
where
g :: IndexedPolynomial
g = IndexedPolynomial -> IndexedPolynomial
forall (p :: * -> * -> *) e c.
(Polynomial p e c, Eq c, Fractional c) =>
p e c -> p e c
monic (IndexedPolynomial -> IndexedPolynomial)
-> IndexedPolynomial -> IndexedPolynomial
forall a b. (a -> b) -> a -> b
$ IndexedPolynomial -> IndexedPolynomial -> IndexedPolynomial
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 IndexedPolynomial
x IndexedPolynomial
y
(IndexedPolynomial
x', IndexedPolynomial
_) = IndexedPolynomial
x IndexedPolynomial
-> IndexedPolynomial -> (IndexedPolynomial, IndexedPolynomial)
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` IndexedPolynomial
g
(IndexedPolynomial
y', IndexedPolynomial
_) = IndexedPolynomial
y IndexedPolynomial
-> IndexedPolynomial -> (IndexedPolynomial, IndexedPolynomial)
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` IndexedPolynomial
g
hermiteReduce :: RationalFunction -> ([RationalFunction], RationalFunction)
hermiteReduce :: RationalFunction -> ([RationalFunction], RationalFunction)
hermiteReduce h :: RationalFunction
h@(RationalFunction IndexedPolynomial
_ IndexedPolynomial
0) = ([], RationalFunction
h)
hermiteReduce h :: RationalFunction
h@(RationalFunction IndexedPolynomial
x IndexedPolynomial
y)
| (Just ([RationalFunction], RationalFunction)
z) <- IndexedPolynomial
-> [RationalFunction]
-> IndexedPolynomial
-> Maybe ([RationalFunction], RationalFunction)
reduce IndexedPolynomial
x [] IndexedPolynomial
common = ([RationalFunction], RationalFunction)
z
| Bool
otherwise = ([], RationalFunction
h)
where
common :: IndexedPolynomial
common = IndexedPolynomial -> IndexedPolynomial
forall (p :: * -> * -> *) e c.
(Polynomial p e c, Eq c, Fractional c) =>
p e c -> p e c
monic (IndexedPolynomial -> IndexedPolynomial)
-> IndexedPolynomial -> IndexedPolynomial
forall a b. (a -> b) -> a -> b
$ IndexedPolynomial -> IndexedPolynomial -> IndexedPolynomial
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 IndexedPolynomial
y (IndexedPolynomial -> IndexedPolynomial)
-> IndexedPolynomial -> IndexedPolynomial
forall a b. (a -> b) -> a -> b
$ IndexedPolynomial -> IndexedPolynomial
forall (p :: * -> * -> *) e c.
(Polynomial p e c, Num (p e c), Num c) =>
p e c -> p e c
differentiate IndexedPolynomial
y
(IndexedPolynomial
divisor, IndexedPolynomial
_) = IndexedPolynomial
y IndexedPolynomial
-> IndexedPolynomial -> (IndexedPolynomial, IndexedPolynomial)
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` IndexedPolynomial
common
reduce :: IndexedPolynomial
-> [RationalFunction]
-> IndexedPolynomial
-> Maybe ([RationalFunction], RationalFunction)
reduce IndexedPolynomial
a [RationalFunction]
g IndexedPolynomial
d
| IndexedPolynomial -> Int
forall (p :: * -> * -> *) e c. Polynomial p e c => p e c -> e
degree IndexedPolynomial
d Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 = do
let d' :: IndexedPolynomial
d' = IndexedPolynomial -> IndexedPolynomial
forall (p :: * -> * -> *) e c.
(Polynomial p e c, Eq c, Fractional c) =>
p e c -> p e c
monic (IndexedPolynomial -> IndexedPolynomial)
-> IndexedPolynomial -> IndexedPolynomial
forall a b. (a -> b) -> a -> b
$ IndexedPolynomial -> IndexedPolynomial -> IndexedPolynomial
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 IndexedPolynomial
d (IndexedPolynomial -> IndexedPolynomial)
-> IndexedPolynomial -> IndexedPolynomial
forall a b. (a -> b) -> a -> b
$ IndexedPolynomial -> IndexedPolynomial
forall (p :: * -> * -> *) e c.
(Polynomial p e c, Num (p e c), Num c) =>
p e c -> p e c
differentiate IndexedPolynomial
d
let (IndexedPolynomial
d'', IndexedPolynomial
_) = IndexedPolynomial
d IndexedPolynomial
-> IndexedPolynomial -> (IndexedPolynomial, IndexedPolynomial)
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` IndexedPolynomial
d'
let (IndexedPolynomial
d''', IndexedPolynomial
_) = (IndexedPolynomial
divisor IndexedPolynomial -> IndexedPolynomial -> IndexedPolynomial
forall a. Num a => a -> a -> a
* IndexedPolynomial -> IndexedPolynomial
forall (p :: * -> * -> *) e c.
(Polynomial p e c, Num (p e c), Num c) =>
p e c -> p e c
differentiate IndexedPolynomial
d) IndexedPolynomial
-> IndexedPolynomial -> (IndexedPolynomial, IndexedPolynomial)
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` IndexedPolynomial
d
(b, c) <- IndexedPolynomial
-> IndexedPolynomial
-> IndexedPolynomial
-> Maybe (IndexedPolynomial, IndexedPolynomial)
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 (-IndexedPolynomial
d''') IndexedPolynomial
d'' IndexedPolynomial
a
let (b', _) = (differentiate b * divisor) `divide` d''
let a' = IndexedPolynomial
c IndexedPolynomial -> IndexedPolynomial -> IndexedPolynomial
forall a. Num a => a -> a -> a
- IndexedPolynomial
b'
let g' = IndexedPolynomial -> IndexedPolynomial -> RationalFunction
toRationalFunction IndexedPolynomial
b IndexedPolynomial
d RationalFunction -> [RationalFunction] -> [RationalFunction]
forall a. a -> [a] -> [a]
: [RationalFunction]
g
reduce a' g' d'
| Bool
otherwise = ([RationalFunction], RationalFunction)
-> Maybe ([RationalFunction], RationalFunction)
forall a. a -> Maybe a
Just ([RationalFunction]
g, IndexedPolynomial -> IndexedPolynomial -> RationalFunction
toRationalFunction IndexedPolynomial
a IndexedPolynomial
divisor)
rationalIntegralLogTerms ::
RationalFunction ->
Maybe [(IndexedPolynomial, IndexedPolynomialWith IndexedPolynomial)]
rationalIntegralLogTerms :: RationalFunction
-> Maybe [(IndexedPolynomial, P Int IndexedPolynomial)]
rationalIntegralLogTerms (RationalFunction IndexedPolynomial
a IndexedPolynomial
d) = do
let sa :: P Int RationalFunction
sa = (Rational -> RationalFunction)
-> IndexedPolynomial -> P Int RationalFunction
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 Rational -> RationalFunction
forall a. Fractional a => Rational -> a
fromRational IndexedPolynomial
a
let sd :: P Int RationalFunction
sd = (Rational -> RationalFunction)
-> IndexedPolynomial -> P Int RationalFunction
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 Rational -> RationalFunction
forall a. Fractional a => Rational -> a
fromRational IndexedPolynomial
d
let t :: RationalFunction
t = IndexedPolynomial -> IndexedPolynomial -> RationalFunction
RationalFunction (Int -> IndexedPolynomial
forall (p :: * -> * -> *) e c. Polynomial p e c => e -> p e c
power Int
1) IndexedPolynomial
1
let (RationalFunction
resultant, [P Int RationalFunction]
prs) = P Int RationalFunction
-> P Int RationalFunction
-> (RationalFunction, [P Int RationalFunction])
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 Int RationalFunction
sd (P Int RationalFunction
-> (RationalFunction, [P Int RationalFunction]))
-> P Int RationalFunction
-> (RationalFunction, [P Int RationalFunction])
forall a b. (a -> b) -> a -> b
$ P Int RationalFunction
sa P Int RationalFunction
-> P Int RationalFunction -> P Int RationalFunction
forall a. Num a => a -> a -> a
- RationalFunction
-> P Int RationalFunction -> P Int RationalFunction
forall (p :: * -> * -> *) e c.
Polynomial p e c =>
c -> p e c -> p e c
scale RationalFunction
t (P Int RationalFunction -> P Int RationalFunction
forall (p :: * -> * -> *) e c.
(Polynomial p e c, Num (p e c), Num c) =>
p e c -> p e c
differentiate P Int RationalFunction
sd)
sd' <- P Int RationalFunction -> Maybe (P Int IndexedPolynomial)
toPolyCoefficients P Int RationalFunction
sd
resultant' <- toPoly resultant
prs' <- toMaybeList $ map toPolyCoefficients prs :: Maybe [IndexedPolynomialWith IndexedPolynomial]
let qs = IndexedPolynomial -> [IndexedPolynomial]
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 IndexedPolynomial
resultant' :: [IndexedPolynomial]
let terms = (Int
-> IndexedPolynomial
-> (IndexedPolynomial, P Int IndexedPolynomial))
-> [Int]
-> [IndexedPolynomial]
-> [(IndexedPolynomial, P Int IndexedPolynomial)]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith (P Int IndexedPolynomial
-> [P Int IndexedPolynomial]
-> Int
-> IndexedPolynomial
-> (IndexedPolynomial, P Int IndexedPolynomial)
toTerm P Int IndexedPolynomial
sd' [P Int IndexedPolynomial]
prs') [Int
1 ..] [IndexedPolynomial]
qs
return $ filter ((/=) 1 . snd) terms
where
toTerm ::
IndexedPolynomialWith IndexedPolynomial ->
[IndexedPolynomialWith IndexedPolynomial] ->
Int ->
IndexedPolynomial ->
(IndexedPolynomial, IndexedPolynomialWith IndexedPolynomial)
toTerm :: P Int IndexedPolynomial
-> [P Int IndexedPolynomial]
-> Int
-> IndexedPolynomial
-> (IndexedPolynomial, P Int IndexedPolynomial)
toTerm P Int IndexedPolynomial
sd [P Int IndexedPolynomial]
prs Int
i IndexedPolynomial
q
| IndexedPolynomial -> Int
forall (p :: * -> * -> *) e c. Polynomial p e c => p e c -> e
degree IndexedPolynomial
q Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 = (IndexedPolynomial
q, P Int IndexedPolynomial
1)
| Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== IndexedPolynomial -> Int
forall (p :: * -> * -> *) e c. Polynomial p e c => p e c -> e
degree IndexedPolynomial
d = (IndexedPolynomial
q, P Int IndexedPolynomial
sd)
| (Just P Int IndexedPolynomial
r) <- (P Int IndexedPolynomial -> Bool)
-> [P Int IndexedPolynomial] -> Maybe (P Int IndexedPolynomial)
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find (Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
(==) Int
i (Int -> Bool)
-> (P Int IndexedPolynomial -> Int)
-> P Int IndexedPolynomial
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. P Int IndexedPolynomial -> Int
forall (p :: * -> * -> *) e c. Polynomial p e c => p e c -> e
degree) [P Int IndexedPolynomial]
prs = IndexedPolynomial
-> P Int IndexedPolynomial
-> (IndexedPolynomial, P Int IndexedPolynomial)
derive IndexedPolynomial
q P Int IndexedPolynomial
r
| Bool
otherwise = (IndexedPolynomial
q, P Int IndexedPolynomial
1)
derive ::
IndexedPolynomial ->
IndexedPolynomialWith IndexedPolynomial ->
(IndexedPolynomial, IndexedPolynomialWith IndexedPolynomial)
derive :: IndexedPolynomial
-> P Int IndexedPolynomial
-> (IndexedPolynomial, P Int IndexedPolynomial)
derive IndexedPolynomial
q P Int IndexedPolynomial
s = (IndexedPolynomial
q, P Int IndexedPolynomial
s')
where
as :: [IndexedPolynomial]
as = IndexedPolynomial -> [IndexedPolynomial]
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 (IndexedPolynomial -> [IndexedPolynomial])
-> IndexedPolynomial -> [IndexedPolynomial]
forall a b. (a -> b) -> a -> b
$ P Int IndexedPolynomial -> IndexedPolynomial
forall (p :: * -> * -> *) e c. Polynomial p e c => p e c -> c
leadingCoefficient P Int IndexedPolynomial
s
s' :: P Int IndexedPolynomial
s' = (P Int IndexedPolynomial
-> (Int, IndexedPolynomial) -> P Int IndexedPolynomial)
-> P Int IndexedPolynomial
-> [(Int, IndexedPolynomial)]
-> P Int IndexedPolynomial
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl P Int IndexedPolynomial
-> (Int, IndexedPolynomial) -> P Int IndexedPolynomial
forall {p :: * -> * -> *} {e} {p :: * -> * -> *} {b}.
(Polynomial p e IndexedPolynomial,
Polynomial p e IndexedPolynomial, Integral b,
Num (p e IndexedPolynomial)) =>
p e IndexedPolynomial
-> (b, IndexedPolynomial) -> p e IndexedPolynomial
scalePoly P Int IndexedPolynomial
s ([Int] -> [IndexedPolynomial] -> [(Int, IndexedPolynomial)]
forall a b. [a] -> [b] -> [(a, b)]
zip ([Int
1 ..] :: [Int]) [IndexedPolynomial]
as)
where
scalePoly :: p e IndexedPolynomial
-> (b, IndexedPolynomial) -> p e IndexedPolynomial
scalePoly p e IndexedPolynomial
x (b
j, IndexedPolynomial
u) =
Sum (p e IndexedPolynomial) -> p e IndexedPolynomial
forall a. Sum a -> a
getSum (Sum (p e IndexedPolynomial) -> p e IndexedPolynomial)
-> Sum (p e IndexedPolynomial) -> p e IndexedPolynomial
forall a b. (a -> b) -> a -> b
$ (e -> IndexedPolynomial -> Sum (p e IndexedPolynomial))
-> p e IndexedPolynomial -> Sum (p e IndexedPolynomial)
forall m.
Monoid m =>
(e -> IndexedPolynomial -> m) -> p e IndexedPolynomial -> m
forall (p :: * -> * -> *) e c m.
(Polynomial p e c, Monoid m) =>
(e -> c -> m) -> p e c -> m
foldTerms (IndexedPolynomial
-> e -> IndexedPolynomial -> Sum (p e IndexedPolynomial)
forall {p :: * -> * -> *} {e} {p :: * -> * -> *} {e} {c}.
(Polynomial p e (p e c), Polynomial p e c, Fractional c,
Eq (p e c)) =>
p e c -> e -> p e c -> Sum (p e (p e c))
reduceTerm (IndexedPolynomial -> IndexedPolynomial
forall (p :: * -> * -> *) e c.
(Polynomial p e c, Eq c, Fractional c) =>
p e c -> p e c
monic (IndexedPolynomial -> IndexedPolynomial)
-> IndexedPolynomial -> IndexedPolynomial
forall a b. (a -> b) -> a -> b
$ IndexedPolynomial -> IndexedPolynomial -> IndexedPolynomial
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 IndexedPolynomial
u IndexedPolynomial
q IndexedPolynomial -> b -> IndexedPolynomial
forall a b. (Num a, Integral b) => a -> b -> a
^ b
j)) p e IndexedPolynomial
x
reduceTerm :: p e c -> e -> p e c -> Sum (p e (p e c))
reduceTerm p e c
v e
e p e c
c = p e (p e c) -> Sum (p e (p e c))
forall a. a -> Sum a
Sum (p e (p e c) -> Sum (p e (p e c)))
-> p e (p e c) -> Sum (p e (p e c))
forall a b. (a -> b) -> a -> b
$ p e c -> p e (p e c) -> p e (p e c)
forall (p :: * -> * -> *) e c.
Polynomial p e c =>
c -> p e c -> p e c
scale (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
exactDivide p e c
c p e c
v) (p e (p e c) -> p e (p e c)) -> p e (p e c) -> p e (p e c)
forall a b. (a -> b) -> a -> b
$ e -> p e (p e c)
forall (p :: * -> * -> *) e c. Polynomial p e c => e -> p e c
power e
e
exactDivide :: p e c -> p e c -> p e c
exactDivide p e c
u p e c
v = p e c
r
where
(p e c
r, p e c
_) = p e c
u 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
v
complexLogTermToAtan ::
Text ->
IndexedPolynomial ->
IndexedPolynomial ->
Expression
complexLogTermToAtan :: Text -> IndexedPolynomial -> IndexedPolynomial -> Expression
complexLogTermToAtan Text
v IndexedPolynomial
a IndexedPolynomial
b
| IndexedPolynomial
r IndexedPolynomial -> IndexedPolynomial -> Bool
forall a. Eq a => a -> a -> Bool
== IndexedPolynomial
0 = Expression
2 Expression -> Expression -> Expression
forall a. Num a => a -> a -> a
* Expression -> Expression
forall a. Floating a => a -> a
atan (Expression
a' Expression -> Expression -> Expression
forall a. Fractional a => a -> a -> a
/ Expression
b')
| IndexedPolynomial -> Int
forall (p :: * -> * -> *) e c. Polynomial p e c => p e c -> e
degree IndexedPolynomial
a Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< IndexedPolynomial -> Int
forall (p :: * -> * -> *) e c. Polynomial p e c => p e c -> e
degree IndexedPolynomial
b = Text -> IndexedPolynomial -> IndexedPolynomial -> Expression
complexLogTermToAtan Text
v (-IndexedPolynomial
b) IndexedPolynomial
a
| Bool
otherwise = Expression
2 Expression -> Expression -> Expression
forall a. Num a => a -> a -> a
* Expression -> Expression
forall a. Floating a => a -> a
atan (Expression
s' Expression -> Expression -> Expression
forall a. Fractional a => a -> a -> a
/ Expression
g') Expression -> Expression -> Expression
forall a. Num a => a -> a -> a
+ Text -> IndexedPolynomial -> IndexedPolynomial -> Expression
complexLogTermToAtan Text
v IndexedPolynomial
d IndexedPolynomial
c
where
(IndexedPolynomial
_, IndexedPolynomial
r) = IndexedPolynomial
a IndexedPolynomial
-> IndexedPolynomial -> (IndexedPolynomial, IndexedPolynomial)
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` IndexedPolynomial
b
(IndexedPolynomial
d, IndexedPolynomial
c, IndexedPolynomial
g) = IndexedPolynomial
-> IndexedPolynomial
-> (IndexedPolynomial, IndexedPolynomial, IndexedPolynomial)
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 IndexedPolynomial
b (-IndexedPolynomial
a)
a' :: Expression
a' = Text -> (Rational -> Expression) -> IndexedPolynomial -> Expression
forall (p :: * -> * -> *) e c.
Polynomial p e c =>
Text -> (c -> Expression) -> p e c -> Expression
toExpression Text
v Rational -> Expression
forall c. Real c => c -> Expression
toRationalCoefficient IndexedPolynomial
a
b' :: Expression
b' = Text -> (Rational -> Expression) -> IndexedPolynomial -> Expression
forall (p :: * -> * -> *) e c.
Polynomial p e c =>
Text -> (c -> Expression) -> p e c -> Expression
toExpression Text
v Rational -> Expression
forall c. Real c => c -> Expression
toRationalCoefficient IndexedPolynomial
b
g' :: Expression
g' = Text -> (Rational -> Expression) -> IndexedPolynomial -> Expression
forall (p :: * -> * -> *) e c.
Polynomial p e c =>
Text -> (c -> Expression) -> p e c -> Expression
toExpression Text
v Rational -> Expression
forall c. Real c => c -> Expression
toRationalCoefficient IndexedPolynomial
g
s' :: Expression
s' = Text -> (Rational -> Expression) -> IndexedPolynomial -> Expression
forall (p :: * -> * -> *) e c.
Polynomial p e c =>
Text -> (c -> Expression) -> p e c -> Expression
toExpression Text
v Rational -> Expression
forall c. Real c => c -> Expression
toRationalCoefficient (IndexedPolynomial -> Expression)
-> IndexedPolynomial -> Expression
forall a b. (a -> b) -> a -> b
$ IndexedPolynomial
a IndexedPolynomial -> IndexedPolynomial -> IndexedPolynomial
forall a. Num a => a -> a -> a
* IndexedPolynomial
d IndexedPolynomial -> IndexedPolynomial -> IndexedPolynomial
forall a. Num a => a -> a -> a
+ IndexedPolynomial
b IndexedPolynomial -> IndexedPolynomial -> IndexedPolynomial
forall a. Num a => a -> a -> a
* IndexedPolynomial
c
complexLogTermToRealTerm ::
(IndexedPolynomial, IndexedPolynomialWith IndexedPolynomial) ->
( (IndexedPolynomialWith IndexedPolynomial, IndexedPolynomialWith IndexedPolynomial),
(IndexedPolynomialWith (IndexedPolynomialWith IndexedPolynomial), IndexedPolynomialWith (IndexedPolynomialWith IndexedPolynomial))
)
complexLogTermToRealTerm :: (IndexedPolynomial, P Int IndexedPolynomial)
-> ((P Int IndexedPolynomial, P Int IndexedPolynomial),
(IndexedPolynomialWith (P Int IndexedPolynomial),
IndexedPolynomialWith (P Int IndexedPolynomial)))
complexLogTermToRealTerm (IndexedPolynomial
q, P Int IndexedPolynomial
s) = ((P Int IndexedPolynomial
qp, P Int IndexedPolynomial
qq), (IndexedPolynomialWith (P Int IndexedPolynomial)
sp, IndexedPolynomialWith (P Int IndexedPolynomial)
sq))
where
q' :: IndexedPolynomialWith (P Int IndexedPolynomial)
q' = Sum (IndexedPolynomialWith (P Int IndexedPolynomial))
-> IndexedPolynomialWith (P Int IndexedPolynomial)
forall a. Sum a -> a
getSum (Sum (IndexedPolynomialWith (P Int IndexedPolynomial))
-> IndexedPolynomialWith (P Int IndexedPolynomial))
-> Sum (IndexedPolynomialWith (P Int IndexedPolynomial))
-> IndexedPolynomialWith (P Int IndexedPolynomial)
forall a b. (a -> b) -> a -> b
$ (Int
-> P Int IndexedPolynomial
-> Sum (IndexedPolynomialWith (P Int IndexedPolynomial)))
-> IndexedPolynomialWith (P Int IndexedPolynomial)
-> Sum (IndexedPolynomialWith (P Int IndexedPolynomial))
forall m.
Monoid m =>
(Int -> P Int IndexedPolynomial -> m)
-> IndexedPolynomialWith (P Int IndexedPolynomial) -> m
forall (p :: * -> * -> *) e c m.
(Polynomial p e c, Monoid m) =>
(e -> c -> m) -> p e c -> m
foldTerms Int
-> P Int IndexedPolynomial
-> Sum (IndexedPolynomialWith (P Int IndexedPolynomial))
forall a.
(Eq a, Num a) =>
Int -> a -> Sum (IndexedPolynomialWith a)
reduceImaginary (IndexedPolynomialWith (P Int IndexedPolynomial)
-> Sum (IndexedPolynomialWith (P Int IndexedPolynomial)))
-> IndexedPolynomialWith (P Int IndexedPolynomial)
-> Sum (IndexedPolynomialWith (P Int IndexedPolynomial))
forall a b. (a -> b) -> a -> b
$ Sum (IndexedPolynomialWith (P Int IndexedPolynomial))
-> IndexedPolynomialWith (P Int IndexedPolynomial)
forall a. Sum a -> a
getSum (Sum (IndexedPolynomialWith (P Int IndexedPolynomial))
-> IndexedPolynomialWith (P Int IndexedPolynomial))
-> Sum (IndexedPolynomialWith (P Int IndexedPolynomial))
-> IndexedPolynomialWith (P Int IndexedPolynomial)
forall a b. (a -> b) -> a -> b
$ (Int
-> Rational
-> Sum (IndexedPolynomialWith (P Int IndexedPolynomial)))
-> IndexedPolynomial
-> Sum (IndexedPolynomialWith (P Int IndexedPolynomial))
forall m.
Monoid m =>
(Int -> Rational -> m) -> IndexedPolynomial -> m
forall (p :: * -> * -> *) e c m.
(Polynomial p e c, Monoid m) =>
(e -> c -> m) -> p e c -> m
foldTerms Int
-> Rational
-> Sum (IndexedPolynomialWith (P Int IndexedPolynomial))
fromTerm IndexedPolynomial
q
where
fromTerm :: Int -> Rational -> Sum (IndexedPolynomialWith (IndexedPolynomialWith IndexedPolynomial))
fromTerm :: Int
-> Rational
-> Sum (IndexedPolynomialWith (P Int IndexedPolynomial))
fromTerm Int
e Rational
c = IndexedPolynomialWith (P Int IndexedPolynomial)
-> Sum (IndexedPolynomialWith (P Int IndexedPolynomial))
forall a. a -> Sum a
Sum (IndexedPolynomialWith (P Int IndexedPolynomial)
-> Sum (IndexedPolynomialWith (P Int IndexedPolynomial)))
-> IndexedPolynomialWith (P Int IndexedPolynomial)
-> Sum (IndexedPolynomialWith (P Int IndexedPolynomial))
forall a b. (a -> b) -> a -> b
$ IndexedPolynomialWith (P Int IndexedPolynomial)
c' IndexedPolynomialWith (P Int IndexedPolynomial)
-> IndexedPolynomialWith (P Int IndexedPolynomial)
-> IndexedPolynomialWith (P Int IndexedPolynomial)
forall a. Num a => a -> a -> a
* (IndexedPolynomialWith (P Int IndexedPolynomial)
u IndexedPolynomialWith (P Int IndexedPolynomial)
-> IndexedPolynomialWith (P Int IndexedPolynomial)
-> IndexedPolynomialWith (P Int IndexedPolynomial)
forall a. Num a => a -> a -> a
+ IndexedPolynomialWith (P Int IndexedPolynomial)
i IndexedPolynomialWith (P Int IndexedPolynomial)
-> IndexedPolynomialWith (P Int IndexedPolynomial)
-> IndexedPolynomialWith (P Int IndexedPolynomial)
forall a. Num a => a -> a -> a
* IndexedPolynomialWith (P Int IndexedPolynomial)
v) IndexedPolynomialWith (P Int IndexedPolynomial)
-> Int -> IndexedPolynomialWith (P Int IndexedPolynomial)
forall a b. (Num a, Integral b) => a -> b -> a
^ Int
e
where
c' :: IndexedPolynomialWith (P Int IndexedPolynomial)
c' = P Int IndexedPolynomial
-> IndexedPolynomialWith (P Int IndexedPolynomial)
-> IndexedPolynomialWith (P Int IndexedPolynomial)
forall (p :: * -> * -> *) e c.
Polynomial p e c =>
c -> p e c -> p e c
scale (IndexedPolynomial
-> P Int IndexedPolynomial -> P Int IndexedPolynomial
forall (p :: * -> * -> *) e c.
Polynomial p e c =>
c -> p e c -> p e c
scale (Rational -> IndexedPolynomial -> IndexedPolynomial
forall (p :: * -> * -> *) e c.
Polynomial p e c =>
c -> p e c -> p e c
scale Rational
c IndexedPolynomial
1) P Int IndexedPolynomial
1) IndexedPolynomialWith (P Int IndexedPolynomial)
1
i :: IndexedPolynomialWith (P Int IndexedPolynomial)
i = Int -> IndexedPolynomialWith (P Int IndexedPolynomial)
forall (p :: * -> * -> *) e c. Polynomial p e c => e -> p e c
power Int
1
u :: IndexedPolynomialWith (P Int IndexedPolynomial)
u = P Int IndexedPolynomial
-> IndexedPolynomialWith (P Int IndexedPolynomial)
-> IndexedPolynomialWith (P Int IndexedPolynomial)
forall (p :: * -> * -> *) e c.
Polynomial p e c =>
c -> p e c -> p e c
scale (Int -> P Int IndexedPolynomial
forall (p :: * -> * -> *) e c. Polynomial p e c => e -> p e c
power Int
1) IndexedPolynomialWith (P Int IndexedPolynomial)
1
v :: IndexedPolynomialWith (P Int IndexedPolynomial)
v = P Int IndexedPolynomial
-> IndexedPolynomialWith (P Int IndexedPolynomial)
-> IndexedPolynomialWith (P Int IndexedPolynomial)
forall (p :: * -> * -> *) e c.
Polynomial p e c =>
c -> p e c -> p e c
scale (IndexedPolynomial
-> P Int IndexedPolynomial -> P Int IndexedPolynomial
forall (p :: * -> * -> *) e c.
Polynomial p e c =>
c -> p e c -> p e c
scale (Int -> IndexedPolynomial
forall (p :: * -> * -> *) e c. Polynomial p e c => e -> p e c
power Int
1) P Int IndexedPolynomial
1) IndexedPolynomialWith (P Int IndexedPolynomial)
1
(P Int IndexedPolynomial
qp, P Int IndexedPolynomial
qq) = (IndexedPolynomialWith (P Int IndexedPolynomial)
-> Int -> P Int IndexedPolynomial
forall (p :: * -> * -> *) e c. Polynomial p e c => p e c -> e -> c
coefficient IndexedPolynomialWith (P Int IndexedPolynomial)
q' Int
0, IndexedPolynomialWith (P Int IndexedPolynomial)
-> Int -> P Int IndexedPolynomial
forall (p :: * -> * -> *) e c. Polynomial p e c => p e c -> e -> c
coefficient IndexedPolynomialWith (P Int IndexedPolynomial)
q' Int
1)
s' :: IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial))
s' = Sum
(IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial)))
-> IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial))
forall a. Sum a -> a
getSum (Sum
(IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial)))
-> IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial)))
-> Sum
(IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial)))
-> IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial))
forall a b. (a -> b) -> a -> b
$ (Int
-> IndexedPolynomialWith (P Int IndexedPolynomial)
-> Sum
(IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial))))
-> IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial))
-> Sum
(IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial)))
forall m.
Monoid m =>
(Int -> IndexedPolynomialWith (P Int IndexedPolynomial) -> m)
-> IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial))
-> m
forall (p :: * -> * -> *) e c m.
(Polynomial p e c, Monoid m) =>
(e -> c -> m) -> p e c -> m
foldTerms Int
-> IndexedPolynomialWith (P Int IndexedPolynomial)
-> Sum
(IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial)))
forall a.
(Eq a, Num a) =>
Int -> a -> Sum (IndexedPolynomialWith a)
reduceImaginary (IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial))
-> Sum
(IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial))))
-> IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial))
-> Sum
(IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial)))
forall a b. (a -> b) -> a -> b
$ Sum
(IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial)))
-> IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial))
forall a. Sum a -> a
getSum (Sum
(IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial)))
-> IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial)))
-> Sum
(IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial)))
-> IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial))
forall a b. (a -> b) -> a -> b
$ (Int
-> IndexedPolynomial
-> Sum
(IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial))))
-> P Int IndexedPolynomial
-> Sum
(IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial)))
forall m.
Monoid m =>
(Int -> IndexedPolynomial -> m) -> P Int IndexedPolynomial -> m
forall (p :: * -> * -> *) e c m.
(Polynomial p e c, Monoid m) =>
(e -> c -> m) -> p e c -> m
foldTerms Int
-> IndexedPolynomial
-> Sum
(IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial)))
fromTerm P Int IndexedPolynomial
s
where
fromTerm :: Int -> IndexedPolynomial -> Sum (IndexedPolynomialWith (IndexedPolynomialWith (IndexedPolynomialWith IndexedPolynomial)))
fromTerm :: Int
-> IndexedPolynomial
-> Sum
(IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial)))
fromTerm Int
e IndexedPolynomial
c = IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial))
-> Sum
(IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial)))
forall a. a -> Sum a
Sum (IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial))
-> Sum
(IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial))))
-> IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial))
-> Sum
(IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial)))
forall a b. (a -> b) -> a -> b
$ IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial))
c' IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial))
-> IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial))
-> IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial))
forall a. Num a => a -> a -> a
* IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial))
x IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial))
-> Int
-> IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial))
forall a b. (Num a, Integral b) => a -> b -> a
^ Int
e
where
c' :: IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial))
c' = Sum
(IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial)))
-> IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial))
forall a. Sum a -> a
getSum (Sum
(IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial)))
-> IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial)))
-> Sum
(IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial)))
-> IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial))
forall a b. (a -> b) -> a -> b
$ (Int
-> Rational
-> Sum
(IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial))))
-> IndexedPolynomial
-> Sum
(IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial)))
forall m.
Monoid m =>
(Int -> Rational -> m) -> IndexedPolynomial -> m
forall (p :: * -> * -> *) e c m.
(Polynomial p e c, Monoid m) =>
(e -> c -> m) -> p e c -> m
foldTerms Int
-> Rational
-> Sum
(IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial)))
forall {b}.
Integral b =>
b
-> Rational
-> Sum
(IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial)))
fromCoefficient IndexedPolynomial
c
fromCoefficient :: b
-> Rational
-> Sum
(IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial)))
fromCoefficient b
e' Rational
c'' = IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial))
-> Sum
(IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial)))
forall a. a -> Sum a
Sum (IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial))
-> Sum
(IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial))))
-> IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial))
-> Sum
(IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial)))
forall a b. (a -> b) -> a -> b
$ IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial))
c''' IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial))
-> IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial))
-> IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial))
forall a. Num a => a -> a -> a
* (IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial))
u IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial))
-> IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial))
-> IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial))
forall a. Num a => a -> a -> a
+ IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial))
i IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial))
-> IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial))
-> IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial))
forall a. Num a => a -> a -> a
* IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial))
v) IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial))
-> b
-> IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial))
forall a b. (Num a, Integral b) => a -> b -> a
^ b
e'
where
c''' :: IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial))
c''' = IndexedPolynomialWith (P Int IndexedPolynomial)
-> IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial))
-> IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial))
forall (p :: * -> * -> *) e c.
Polynomial p e c =>
c -> p e c -> p e c
scale (P Int IndexedPolynomial
-> IndexedPolynomialWith (P Int IndexedPolynomial)
-> IndexedPolynomialWith (P Int IndexedPolynomial)
forall (p :: * -> * -> *) e c.
Polynomial p e c =>
c -> p e c -> p e c
scale (IndexedPolynomial
-> P Int IndexedPolynomial -> P Int IndexedPolynomial
forall (p :: * -> * -> *) e c.
Polynomial p e c =>
c -> p e c -> p e c
scale (Rational -> IndexedPolynomial -> IndexedPolynomial
forall (p :: * -> * -> *) e c.
Polynomial p e c =>
c -> p e c -> p e c
scale Rational
c'' IndexedPolynomial
1) P Int IndexedPolynomial
1) IndexedPolynomialWith (P Int IndexedPolynomial)
1) IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial))
1
i :: IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial))
i = Int
-> IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial))
forall (p :: * -> * -> *) e c. Polynomial p e c => e -> p e c
power Int
1
x :: IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial))
x = IndexedPolynomialWith (P Int IndexedPolynomial)
-> IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial))
-> IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial))
forall (p :: * -> * -> *) e c.
Polynomial p e c =>
c -> p e c -> p e c
scale (Int -> IndexedPolynomialWith (P Int IndexedPolynomial)
forall (p :: * -> * -> *) e c. Polynomial p e c => e -> p e c
power Int
1) IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial))
1
u :: IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial))
u = IndexedPolynomialWith (P Int IndexedPolynomial)
-> IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial))
-> IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial))
forall (p :: * -> * -> *) e c.
Polynomial p e c =>
c -> p e c -> p e c
scale (P Int IndexedPolynomial
-> IndexedPolynomialWith (P Int IndexedPolynomial)
-> IndexedPolynomialWith (P Int IndexedPolynomial)
forall (p :: * -> * -> *) e c.
Polynomial p e c =>
c -> p e c -> p e c
scale (Int -> P Int IndexedPolynomial
forall (p :: * -> * -> *) e c. Polynomial p e c => e -> p e c
power Int
1) IndexedPolynomialWith (P Int IndexedPolynomial)
1) IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial))
1
v :: IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial))
v = IndexedPolynomialWith (P Int IndexedPolynomial)
-> IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial))
-> IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial))
forall (p :: * -> * -> *) e c.
Polynomial p e c =>
c -> p e c -> p e c
scale (P Int IndexedPolynomial
-> IndexedPolynomialWith (P Int IndexedPolynomial)
-> IndexedPolynomialWith (P Int IndexedPolynomial)
forall (p :: * -> * -> *) e c.
Polynomial p e c =>
c -> p e c -> p e c
scale (IndexedPolynomial
-> P Int IndexedPolynomial -> P Int IndexedPolynomial
forall (p :: * -> * -> *) e c.
Polynomial p e c =>
c -> p e c -> p e c
scale (Int -> IndexedPolynomial
forall (p :: * -> * -> *) e c. Polynomial p e c => e -> p e c
power Int
1) P Int IndexedPolynomial
1) IndexedPolynomialWith (P Int IndexedPolynomial)
1) IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial))
1
(IndexedPolynomialWith (P Int IndexedPolynomial)
sp, IndexedPolynomialWith (P Int IndexedPolynomial)
sq) = (IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial))
-> Int -> IndexedPolynomialWith (P Int IndexedPolynomial)
forall (p :: * -> * -> *) e c. Polynomial p e c => p e c -> e -> c
coefficient IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial))
s' Int
0, IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial))
-> Int -> IndexedPolynomialWith (P Int IndexedPolynomial)
forall (p :: * -> * -> *) e c. Polynomial p e c => p e c -> e -> c
coefficient IndexedPolynomialWith
(IndexedPolynomialWith (P Int IndexedPolynomial))
s' Int
1)
reduceImaginary :: (Eq a, Num a) => Int -> a -> Sum (IndexedPolynomialWith a)
reduceImaginary :: forall a.
(Eq a, Num a) =>
Int -> a -> Sum (IndexedPolynomialWith a)
reduceImaginary Int
e a
c = IndexedPolynomialWith a -> Sum (IndexedPolynomialWith a)
forall a. a -> Sum a
Sum (IndexedPolynomialWith a -> Sum (IndexedPolynomialWith a))
-> IndexedPolynomialWith a -> Sum (IndexedPolynomialWith a)
forall a b. (a -> b) -> a -> b
$ case Int
e Int -> Int -> Int
forall a. Integral a => a -> a -> a
`mod` Int
4 of
Int
0 -> IndexedPolynomialWith a
c'
Int
1 -> IndexedPolynomialWith a
c' IndexedPolynomialWith a
-> IndexedPolynomialWith a -> IndexedPolynomialWith a
forall a. Num a => a -> a -> a
* IndexedPolynomialWith a
i
Int
2 -> IndexedPolynomialWith a
c' IndexedPolynomialWith a
-> IndexedPolynomialWith a -> IndexedPolynomialWith a
forall a. Num a => a -> a -> a
* (-IndexedPolynomialWith a
1)
Int
3 -> IndexedPolynomialWith a
c' IndexedPolynomialWith a
-> IndexedPolynomialWith a -> IndexedPolynomialWith a
forall a. Num a => a -> a -> a
* (-IndexedPolynomialWith a
i)
Int
_ -> IndexedPolynomialWith a
0
where
i :: IndexedPolynomialWith a
i = Int -> IndexedPolynomialWith a
forall (p :: * -> * -> *) e c. Polynomial p e c => e -> p e c
power Int
1
c' :: IndexedPolynomialWith a
c' = a -> IndexedPolynomialWith a -> IndexedPolynomialWith a
forall (p :: * -> * -> *) e c.
Polynomial p e c =>
c -> p e c -> p e c
scale a
c IndexedPolynomialWith a
1
complexLogTermToRealExpression ::
Text ->
(IndexedPolynomial, IndexedPolynomialWith IndexedPolynomial) ->
Maybe Expression
complexLogTermToRealExpression :: Text
-> (IndexedPolynomial, P Int IndexedPolynomial) -> Maybe Expression
complexLogTermToRealExpression Text
v (IndexedPolynomial
r, P Int IndexedPolynomial
s)
| (Just [(Rational, Rational)]
xys) <- P Int IndexedPolynomial
-> P Int IndexedPolynomial -> Maybe [(Rational, Rational)]
solveBivariatePolynomials P Int IndexedPolynomial
p P Int IndexedPolynomial
q,
(Just [Expression]
h) <- [(Rational, Rational)] -> Maybe [Expression]
f [(Rational, Rational)]
xys,
(Just [Rational]
zs) <- Maybe [Expression] -> Maybe [Rational]
toRationalList (IndexedPolynomial -> Maybe [Expression]
solve IndexedPolynomial
r) =
Expression -> Maybe Expression
forall a. a -> Maybe a
Just (Expression -> Maybe Expression) -> Expression -> Maybe Expression
forall a b. (a -> b) -> a -> b
$ [Expression] -> Expression
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [Expression]
h Expression -> Expression -> Expression
forall a. Num a => a -> a -> a
+ [Rational] -> Expression
forall {t :: * -> *}.
(Foldable t, Monad t) =>
t Rational -> Expression
g [Rational]
zs
| Bool
otherwise = Maybe Expression
forall a. Maybe a
Nothing
where
((P Int IndexedPolynomial
p, P Int IndexedPolynomial
q), (IndexedPolynomialWith (P Int IndexedPolynomial)
a, IndexedPolynomialWith (P Int IndexedPolynomial)
b)) = (IndexedPolynomial, P Int IndexedPolynomial)
-> ((P Int IndexedPolynomial, P Int IndexedPolynomial),
(IndexedPolynomialWith (P Int IndexedPolynomial),
IndexedPolynomialWith (P Int IndexedPolynomial)))
complexLogTermToRealTerm (IndexedPolynomial
r, P Int IndexedPolynomial
s)
f :: [(Rational, Rational)] -> Maybe [Expression]
f :: [(Rational, Rational)] -> Maybe [Expression]
f [(Rational, Rational)]
xys = [Maybe Expression] -> Maybe [Expression]
forall a. [Maybe a] -> Maybe [a]
toMaybeList ([Maybe Expression] -> Maybe [Expression])
-> [Maybe Expression] -> Maybe [Expression]
forall a b. (a -> b) -> a -> b
$ do
(x, y) <- ((Rational, Rational) -> Bool)
-> [(Rational, Rational)] -> [(Rational, Rational)]
forall a. (a -> Bool) -> [a] -> [a]
filter ((Rational -> Rational -> Bool
forall a. Ord a => a -> a -> Bool
> Rational
0) (Rational -> Bool)
-> ((Rational, Rational) -> Rational)
-> (Rational, Rational)
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Rational, Rational) -> Rational
forall a b. (a, b) -> b
snd) [(Rational, Rational)]
xys
let flatten'' = (IndexedPolynomial -> Expression)
-> P Int IndexedPolynomial -> P Int Expression
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 (Expression
-> (Rational -> Expression) -> IndexedPolynomial -> Expression
forall {p :: * -> * -> *} {a} {t}.
Polynomial p a t =>
Expression -> (t -> Expression) -> p a t -> Expression
toExpr (Rational -> Expression
forall a. Fractional a => Rational -> a
fromRational Rational
y) Rational -> Expression
forall a. Fractional a => Rational -> a
fromRational)
let flatten' = (P Int IndexedPolynomial -> Expression)
-> IndexedPolynomialWith (P Int IndexedPolynomial)
-> P Int Expression
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 (Expression
-> (Expression -> Expression) -> P Int Expression -> Expression
forall {p :: * -> * -> *} {a} {t}.
Polynomial p a t =>
Expression -> (t -> Expression) -> p a t -> Expression
toExpr (Rational -> Expression
forall a. Fractional a => Rational -> a
fromRational Rational
x) Expression -> Expression
forall a. a -> a
id (P Int Expression -> Expression)
-> (P Int IndexedPolynomial -> P Int Expression)
-> P Int IndexedPolynomial
-> Expression
forall b c a. (b -> c) -> (a -> b) -> a -> c
. P Int IndexedPolynomial -> P Int Expression
flatten'')
let flatten = Expression
-> (Expression -> Expression) -> P Int Expression -> Expression
forall {p :: * -> * -> *} {a} {t}.
Polynomial p a t =>
Expression -> (t -> Expression) -> p a t -> Expression
toExpr (Text -> Expression
Symbol Text
v) Expression -> Expression
forall a. a -> a
id (P Int Expression -> Expression)
-> (IndexedPolynomialWith (P Int IndexedPolynomial)
-> P Int Expression)
-> IndexedPolynomialWith (P Int IndexedPolynomial)
-> Expression
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IndexedPolynomialWith (P Int IndexedPolynomial) -> P Int Expression
flatten'
let a' = IndexedPolynomialWith (P Int IndexedPolynomial) -> Expression
flatten IndexedPolynomialWith (P Int IndexedPolynomial)
a
let b' = IndexedPolynomialWith (P Int IndexedPolynomial) -> Expression
flatten IndexedPolynomialWith (P Int IndexedPolynomial)
b
return $ do
a'' <- convertCoefficients $ flatten' a
b'' <- convertCoefficients $ flatten' b
return $ fromRational x * log (a' * a' + b' * b') + fromRational y * complexLogTermToAtan v a'' b''
g :: t Rational -> Expression
g t Rational
zs = t Expression -> Expression
forall a. Num a => t a -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum (t Expression -> Expression) -> t Expression -> Expression
forall a b. (a -> b) -> a -> b
$ do
z <- t Rational
zs
let s' = (IndexedPolynomial -> Expression)
-> P Int IndexedPolynomial -> P Int Expression
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 (Expression
-> (Rational -> Expression) -> IndexedPolynomial -> Expression
forall {p :: * -> * -> *} {a} {t}.
Polynomial p a t =>
Expression -> (t -> Expression) -> p a t -> Expression
toExpr (Rational -> Expression
forall a. Fractional a => Rational -> a
fromRational Rational
z) Rational -> Expression
forall a. Fractional a => Rational -> a
fromRational) P Int IndexedPolynomial
s
return $ fromRational z * Log' (toExpression v toSymbolicCoefficient s')
toRationalList :: Maybe [Expression] -> Maybe [Rational]
toRationalList :: Maybe [Expression] -> Maybe [Rational]
toRationalList Maybe [Expression]
Nothing = Maybe [Rational]
forall a. Maybe a
Nothing
toRationalList (Just []) = [Rational] -> Maybe [Rational]
forall a. a -> Maybe a
Just []
toRationalList (Just (Expression
x : [Expression]
xs))
| (Just Rational
x'') <- Expression -> Maybe Rational
forall {a}. Fractional a => Expression -> Maybe a
convert (Expression -> Expression
simplify Expression
x'), (Just [Rational]
xs'') <- Maybe [Rational]
xs' = [Rational] -> Maybe [Rational]
forall a. a -> Maybe a
Just ([Rational] -> Maybe [Rational]) -> [Rational] -> Maybe [Rational]
forall a b. (a -> b) -> a -> b
$ Rational
x'' Rational -> [Rational] -> [Rational]
forall a. a -> [a] -> [a]
: [Rational]
xs''
| Bool
otherwise = Maybe [Rational]
forall a. Maybe a
Nothing
where
x' :: Expression
x' = Expression -> Expression
simplify Expression
x
xs' :: Maybe [Rational]
xs' = Maybe [Expression] -> Maybe [Rational]
toRationalList (Maybe [Expression] -> Maybe [Rational])
-> Maybe [Expression] -> Maybe [Rational]
forall a b. (a -> b) -> a -> b
$ [Expression] -> Maybe [Expression]
forall a. a -> Maybe a
Just [Expression]
xs
convert :: Expression -> Maybe a
convert (Number Integer
n) = a -> Maybe a
forall a. a -> Maybe a
Just (a -> Maybe a) -> a -> Maybe a
forall a b. (a -> b) -> a -> b
$ Integer -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
n
convert (Number Integer
n :/: Number Integer
m) = a -> Maybe a
forall a. a -> Maybe a
Just (a -> Maybe a) -> a -> Maybe a
forall a b. (a -> b) -> a -> b
$ Integer -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
n a -> a -> a
forall a. Fractional a => a -> a -> a
/ Integer -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
m
convert Expression
_ = Maybe a
forall a. Maybe a
Nothing
convertCoefficients :: IndexedPolynomialWith Expression -> Maybe IndexedPolynomial
convertCoefficients :: P Int Expression -> Maybe IndexedPolynomial
convertCoefficients P Int Expression
x = [IndexedPolynomial] -> IndexedPolynomial
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum ([IndexedPolynomial] -> IndexedPolynomial)
-> ([(Int, Rational)] -> [IndexedPolynomial])
-> [(Int, Rational)]
-> IndexedPolynomial
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Int, Rational) -> IndexedPolynomial)
-> [(Int, Rational)] -> [IndexedPolynomial]
forall a b. (a -> b) -> [a] -> [b]
map (\(Int
e, Rational
c) -> Rational -> IndexedPolynomial -> IndexedPolynomial
forall (p :: * -> * -> *) e c.
Polynomial p e c =>
c -> p e c -> p e c
scale Rational
c (Int -> IndexedPolynomial
forall (p :: * -> * -> *) e c. Polynomial p e c => e -> p e c
power Int
e)) ([(Int, Rational)] -> IndexedPolynomial)
-> Maybe [(Int, Rational)] -> Maybe IndexedPolynomial
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Maybe (Int, Rational)] -> Maybe [(Int, Rational)]
forall a. [Maybe a] -> Maybe [a]
toMaybeList ((Int -> Expression -> [Maybe (Int, Rational)])
-> P Int Expression -> [Maybe (Int, Rational)]
forall m.
Monoid m =>
(Int -> Expression -> m) -> P Int Expression -> m
forall (p :: * -> * -> *) e c m.
(Polynomial p e c, Monoid m) =>
(e -> c -> m) -> p e c -> m
foldTerms (\Int
e Expression
c -> [(Int
e,) (Rational -> (Int, Rational))
-> Maybe Rational -> Maybe (Int, Rational)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Expression -> Maybe Rational
forall {a}. Fractional a => Expression -> Maybe a
convert (Expression -> Expression
simplify Expression
c)]) P Int Expression
x)
toExpr :: Expression -> (t -> Expression) -> p a t -> Expression
toExpr Expression
x t -> Expression
h p a t
u = Sum Expression -> Expression
forall a. Sum a -> a
getSum (Sum Expression -> Expression) -> Sum Expression -> Expression
forall a b. (a -> b) -> a -> b
$ (a -> t -> Sum Expression) -> p a t -> Sum Expression
forall m. Monoid m => (a -> t -> m) -> p a t -> m
forall (p :: * -> * -> *) e c m.
(Polynomial p e c, Monoid m) =>
(e -> c -> m) -> p e c -> m
foldTerms (\a
e'' t
c -> Expression -> Sum Expression
forall a. a -> Sum a
Sum (Expression -> Sum Expression) -> Expression -> Sum Expression
forall a b. (a -> b) -> a -> b
$ t -> Expression
h t
c Expression -> Expression -> Expression
forall a. Num a => a -> a -> a
* (Expression
x Expression -> Expression -> Expression
forall a. Floating a => a -> a -> a
** Integer -> Expression
Number (a -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral a
e''))) p a t
u
solveBivariatePolynomials ::
IndexedPolynomialWith IndexedPolynomial ->
IndexedPolynomialWith IndexedPolynomial ->
Maybe [(Rational, Rational)]
solveBivariatePolynomials :: P Int IndexedPolynomial
-> P Int IndexedPolynomial -> Maybe [(Rational, Rational)]
solveBivariatePolynomials P Int IndexedPolynomial
p P Int IndexedPolynomial
q = do
let p' :: P Int RationalFunction
p' = P Int IndexedPolynomial -> P Int RationalFunction
toRationalFunctionCoefficients P Int IndexedPolynomial
p
let q' :: P Int RationalFunction
q' = P Int IndexedPolynomial -> P Int RationalFunction
toRationalFunctionCoefficients P Int IndexedPolynomial
q
resultant <- RationalFunction -> Maybe IndexedPolynomial
toPoly (RationalFunction -> Maybe IndexedPolynomial)
-> RationalFunction -> Maybe IndexedPolynomial
forall a b. (a -> b) -> a -> b
$ (RationalFunction, [P Int RationalFunction]) -> RationalFunction
forall a b. (a, b) -> a
fst ((RationalFunction, [P Int RationalFunction]) -> RationalFunction)
-> (RationalFunction, [P Int RationalFunction]) -> RationalFunction
forall a b. (a -> b) -> a -> b
$ P Int RationalFunction
-> P Int RationalFunction
-> (RationalFunction, [P Int RationalFunction])
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 Int RationalFunction
p' P Int RationalFunction
q'
vs' <- solve resultant
vs <- toMaybeList $ map (convert . simplify) vs'
concat <$> toMaybeList (map solveForU vs)
where
toRationalFunctionCoefficients :: P Int IndexedPolynomial -> P Int RationalFunction
toRationalFunctionCoefficients = (IndexedPolynomial -> RationalFunction)
-> P Int IndexedPolynomial -> P Int RationalFunction
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 (IndexedPolynomial -> IndexedPolynomial -> RationalFunction
`toRationalFunction` IndexedPolynomial
1)
solveForU :: Rational -> Maybe [(Rational, Rational)]
solveForU :: Rational -> Maybe [(Rational, Rational)]
solveForU Rational
v
| IndexedPolynomial
0 <- IndexedPolynomial
p' = do
u <- (Expression -> Maybe Rational) -> [Expression] -> [Maybe Rational]
forall a b. (a -> b) -> [a] -> [b]
map (Expression -> Maybe Rational
convert (Expression -> Maybe Rational)
-> (Expression -> Expression) -> Expression -> Maybe Rational
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Expression -> Expression
simplify) ([Expression] -> [Maybe Rational])
-> Maybe [Expression] -> Maybe [Maybe Rational]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IndexedPolynomial -> Maybe [Expression]
solve IndexedPolynomial
q'
map (,v) <$> toMaybeList u
| IndexedPolynomial
0 <- IndexedPolynomial
q' = do
u <- (Expression -> Maybe Rational) -> [Expression] -> [Maybe Rational]
forall a b. (a -> b) -> [a] -> [b]
map (Expression -> Maybe Rational
convert (Expression -> Maybe Rational)
-> (Expression -> Expression) -> Expression -> Maybe Rational
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Expression -> Expression
simplify) ([Expression] -> [Maybe Rational])
-> Maybe [Expression] -> Maybe [Maybe Rational]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IndexedPolynomial -> Maybe [Expression]
solve IndexedPolynomial
p'
map (,v) <$> toMaybeList u
| Bool
otherwise = do
up <- (Expression -> Maybe Rational) -> [Expression] -> [Maybe Rational]
forall a b. (a -> b) -> [a] -> [b]
map (Expression -> Maybe Rational
convert (Expression -> Maybe Rational)
-> (Expression -> Expression) -> Expression -> Maybe Rational
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Expression -> Expression
simplify) ([Expression] -> [Maybe Rational])
-> Maybe [Expression] -> Maybe [Maybe Rational]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IndexedPolynomial -> Maybe [Expression]
solve IndexedPolynomial
p'
uq <- map (convert . simplify) <$> solve q'
up' <- toMaybeList up
uq' <- toMaybeList uq
return $ map (,v) $ up' `intersect` uq'
where
p' :: IndexedPolynomial
p' = (IndexedPolynomial -> Rational)
-> P Int IndexedPolynomial -> IndexedPolynomial
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 (Sum Rational -> Rational
forall a. Sum a -> a
getSum (Sum Rational -> Rational)
-> (IndexedPolynomial -> Sum Rational)
-> IndexedPolynomial
-> Rational
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int -> Rational -> Sum Rational)
-> IndexedPolynomial -> Sum Rational
forall m.
Monoid m =>
(Int -> Rational -> m) -> IndexedPolynomial -> m
forall (p :: * -> * -> *) e c m.
(Polynomial p e c, Monoid m) =>
(e -> c -> m) -> p e c -> m
foldTerms (\Int
e Rational
c -> Rational -> Sum Rational
forall a. a -> Sum a
Sum (Rational -> Sum Rational) -> Rational -> Sum Rational
forall a b. (a -> b) -> a -> b
$ Rational
c Rational -> Rational -> Rational
forall a. Num a => a -> a -> a
* Rational
v Rational -> Int -> Rational
forall a b. (Num a, Integral b) => a -> b -> a
^ Int
e)) P Int IndexedPolynomial
p
q' :: IndexedPolynomial
q' = (IndexedPolynomial -> Rational)
-> P Int IndexedPolynomial -> IndexedPolynomial
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 (Sum Rational -> Rational
forall a. Sum a -> a
getSum (Sum Rational -> Rational)
-> (IndexedPolynomial -> Sum Rational)
-> IndexedPolynomial
-> Rational
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int -> Rational -> Sum Rational)
-> IndexedPolynomial -> Sum Rational
forall m.
Monoid m =>
(Int -> Rational -> m) -> IndexedPolynomial -> m
forall (p :: * -> * -> *) e c m.
(Polynomial p e c, Monoid m) =>
(e -> c -> m) -> p e c -> m
foldTerms (\Int
e Rational
c -> Rational -> Sum Rational
forall a. a -> Sum a
Sum (Rational -> Sum Rational) -> Rational -> Sum Rational
forall a b. (a -> b) -> a -> b
$ Rational
c Rational -> Rational -> Rational
forall a. Num a => a -> a -> a
* Rational
v Rational -> Int -> Rational
forall a b. (Num a, Integral b) => a -> b -> a
^ Int
e)) P Int IndexedPolynomial
q
convert :: Expression -> Maybe Rational
convert :: Expression -> Maybe Rational
convert (Number Integer
n) = Rational -> Maybe Rational
forall a. a -> Maybe a
Just (Rational -> Maybe Rational) -> Rational -> Maybe Rational
forall a b. (a -> b) -> a -> b
$ Integer -> Rational
forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
n
convert (Number Integer
n :/: Number Integer
m) = Rational -> Maybe Rational
forall a. a -> Maybe a
Just (Rational -> Maybe Rational) -> Rational -> Maybe Rational
forall a b. (a -> b) -> a -> b
$ Integer -> Rational
forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
n Rational -> Rational -> Rational
forall a. Fractional a => a -> a -> a
/ Integer -> Rational
forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
m
convert Expression
_ = Maybe Rational
forall a. Maybe a
Nothing
toPoly :: RationalFunction -> Maybe IndexedPolynomial
toPoly :: RationalFunction -> Maybe IndexedPolynomial
toPoly (RationalFunction IndexedPolynomial
p IndexedPolynomial
q)
| IndexedPolynomial -> Int
forall (p :: * -> * -> *) e c. Polynomial p e c => p e c -> e
degree IndexedPolynomial
q Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0, IndexedPolynomial
q IndexedPolynomial -> IndexedPolynomial -> Bool
forall a. Eq a => a -> a -> Bool
/= IndexedPolynomial
0 = IndexedPolynomial -> Maybe IndexedPolynomial
forall a. a -> Maybe a
Just IndexedPolynomial
p'
| Bool
otherwise = Maybe IndexedPolynomial
forall a. Maybe a
Nothing
where
p' :: IndexedPolynomial
p' = Rational -> IndexedPolynomial -> IndexedPolynomial
forall (p :: * -> * -> *) e c.
Polynomial p e c =>
c -> p e c -> p e c
scale (Rational
1 Rational -> Rational -> Rational
forall a. Fractional a => a -> a -> a
/ IndexedPolynomial -> Rational
forall (p :: * -> * -> *) e c. Polynomial p e c => p e c -> c
leadingCoefficient IndexedPolynomial
q) IndexedPolynomial
p
toPolyCoefficients ::
IndexedPolynomialWith RationalFunction ->
Maybe (IndexedPolynomialWith IndexedPolynomial)
toPolyCoefficients :: P Int RationalFunction -> Maybe (P Int IndexedPolynomial)
toPolyCoefficients P Int RationalFunction
p = [(Int, Maybe IndexedPolynomial)] -> Maybe (P Int IndexedPolynomial)
forall {p :: * -> * -> *} {e} {c}.
(Polynomial p e c, Num (p e c)) =>
[(e, Maybe c)] -> Maybe (p e c)
reconstruct [(Int, Maybe IndexedPolynomial)]
terms
where
terms :: [(Int, Maybe IndexedPolynomial)]
terms = (Int -> RationalFunction -> [(Int, Maybe IndexedPolynomial)])
-> P Int RationalFunction -> [(Int, Maybe IndexedPolynomial)]
forall m.
Monoid m =>
(Int -> RationalFunction -> m) -> P Int RationalFunction -> m
forall (p :: * -> * -> *) e c m.
(Polynomial p e c, Monoid m) =>
(e -> c -> m) -> p e c -> m
foldTerms (\Int
e RationalFunction
c -> [(Int
e, RationalFunction -> Maybe IndexedPolynomial
toPoly RationalFunction
c)]) P Int RationalFunction
p
reconstruct :: [(e, Maybe c)] -> Maybe (p e c)
reconstruct [] = p e c -> Maybe (p e c)
forall a. a -> Maybe a
Just p e c
0
reconstruct ((e
_, Maybe c
Nothing) : [(e, Maybe c)]
_) = Maybe (p e c)
forall a. Maybe a
Nothing
reconstruct ((e
e, Just c
c) : [(e, Maybe c)]
xs)
| (Just p e c
p') <- [(e, Maybe c)] -> Maybe (p e c)
reconstruct [(e, Maybe c)]
xs = p e c -> Maybe (p e c)
forall a. a -> Maybe a
Just (p e c -> Maybe (p e c)) -> p e c -> Maybe (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 (e -> p e c
forall (p :: * -> * -> *) e c. Polynomial p e c => e -> p e c
power e
e) p e c -> p e c -> p e c
forall a. Num a => a -> a -> a
+ p e c
p'
| Bool
otherwise = Maybe (p e c)
forall a. Maybe a
Nothing
toMaybeList :: [Maybe a] -> Maybe [a]
toMaybeList :: forall a. [Maybe a] -> Maybe [a]
toMaybeList [] = [a] -> Maybe [a]
forall a. a -> Maybe a
Just []
toMaybeList (Maybe a
Nothing : [Maybe a]
_) = Maybe [a]
forall a. Maybe a
Nothing
toMaybeList (Just a
x : [Maybe a]
xs)
| (Just [a]
xs') <- [Maybe a] -> Maybe [a]
forall a. [Maybe a] -> Maybe [a]
toMaybeList [Maybe a]
xs = [a] -> Maybe [a]
forall a. a -> Maybe a
Just (a
x a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a]
xs')
| Bool
otherwise = Maybe [a]
forall a. Maybe a
Nothing