Copyright | Copyright 2025 Yoo Chung |
---|---|
License | Apache-2.0 |
Maintainer | dev@chungyc.org |
Safe Haskell | None |
Language | GHC2021 |
Integrates rational functions. Rational functions are ratios of two polynomials, not functions of rational numbers. Only rational number coefficients are supported.
Synopsis
- integrate :: Text -> Expression -> Maybe Expression
- hermiteReduce :: RationalFunction -> ([RationalFunction], RationalFunction)
- rationalIntegralLogTerms :: RationalFunction -> Maybe [(IndexedPolynomial, IndexedPolynomialWith IndexedPolynomial)]
- complexLogTermToAtan :: Text -> IndexedPolynomial -> IndexedPolynomial -> Expression
- complexLogTermToRealTerm :: (IndexedPolynomial, IndexedPolynomialWith IndexedPolynomial) -> ((IndexedPolynomialWith IndexedPolynomial, IndexedPolynomialWith IndexedPolynomial), (IndexedPolynomialWith (IndexedPolynomialWith IndexedPolynomial), IndexedPolynomialWith (IndexedPolynomialWith IndexedPolynomial)))
- toRationalFunction :: IndexedPolynomial -> IndexedPolynomial -> RationalFunction
- data RationalFunction = RationalFunction IndexedPolynomial IndexedPolynomial
Integration
integrate :: Text -> Expression -> Maybe Expression Source #
Integrate a ratio of two polynomials with rational number coefficients.
For example,
>>>
let p = "x" ** 7 - 24 * "x" ** 4 - 4 * "x" ** 2 + 8 * "x" - 8
>>>
let q = "x" ** 8 + 6 * "x" ** 6 + 12 * "x" ** 4 + 8 * "x" ** 2
>>>
toHaskell . simplify <$> integrate "x" (p / q)
Just "3 / (2 + x ** 2) + (4 + 8 * x ** 2) / (4 * x + 4 * x ** 3 + x ** 5) + log x"
so that
\[\int \frac{x^7-24x^4-4x^2+8x-8}{x^8+6x^6+12x^4+8x^2} \, dx = \frac{3}{x^2+2} + \frac{8x^2+4}{x^5+4x^3+4x} + \log x\]
For another example,
>>>
let f = 36 / ("x" ** 5 - 2 * "x" ** 4 - 2 * "x" ** 3 + 4 * "x" ** 2 + "x" - 2)
>>>
toHaskell . simplify <$> integrate "x" f
Just "(-4) * log (8 + 8 * x) + 4 * log (16 + (-8) * x) + (6 + 12 * x) / ((-1) + x ** 2)"
so that
\[\int \frac{36}{x^5-2x^4-2x^3+4x^2+x-2} \, dx = \frac{12x+6}{x^2-1} + 4 \log \left( x - 2 \right) - 4 \log \left( x + 1 \right)\]
Algorithms
Algorithms used for integrating rational functions.
hermiteReduce :: RationalFunction -> ([RationalFunction], RationalFunction) Source #
Applies Hermite reduction to a rational function. Returns a list of rational functions whose sums add up to the integral and a rational function which remains to be integrated. Only rational functions with rational number coefficients and where the numerator and denominator are coprime are supported.
Specifically, for rational function \(f = \frac{A}{D}\),
where \(A\) and \(D\) are coprime polynomials, then for return value (gs, h)
,
the sum of gs
is equal to \(g\) and h
is equal to \(h\) in the following:
\[ \frac{A}{D} = \frac{dg}{dx} + h \]
This is equivalent to the following:
\[ \int \frac{A}{D} \, dx = g + \int h \, dx \]
If preconditions are satisfied, i.e., \(D \neq 0\) and \(A\) and \(D\) are coprime, then \(h\) will have a squarefree denominator.
For example,
>>>
let p = power 7 - 24 * power 4 - 4 * power 2 + 8 * power 1 - 8 :: IndexedPolynomial
>>>
let q = power 8 + 6 * power 6 + 12 * power 4 + 8 * power 2 :: IndexedPolynomial
>>>
hermiteReduce $ toRationalFunction p q
([(3) / (x^2 + 2),(8x^2 + 4) / (x^5 + 4x^3 + 4x)],(1) / (x))
so that
\[\int \frac{x^7-24x^4-4x^2+8x-8}{x^8+6x^6+12x^4+8x^2} \, dx = \frac{3}{x^2+2}+\frac{8x^2+4}{x^5+4x^3+4x}+\int \frac{1}{x} \, dx\]
\(g\) is returned as a list of rational functions which sum to \(g\) instead of a single rational function, because the former could sometimes be simpler to read.
rationalIntegralLogTerms :: RationalFunction -> Maybe [(IndexedPolynomial, IndexedPolynomialWith IndexedPolynomial)] Source #
For rational function \(\frac{A}{D}\), where \(\deg(A) < \deg(D)\), and \(D\) is non-zero, squarefree, and coprime with \(A\), returns the components which form the logarithmic terms of \(\int \frac{A}{D} \, dx\). Specifically, when a list of \((Q_i(t), S_i(t, x))\) is returned, where \(Q_i(t)\) are polynomials of \(t\) and \(S_i(t, x)\) are polynomials of \(x\) with coefficients formed from polynomials of \(t\), then
\[ \int \frac{A}{D} \, dx = \sum_{i=1}^n \sum_{a \in \{t \mid Q_i(t) = 0\}} a \log \left(S_i(a,x)\right) \]
For example,
>>>
let p = power 4 - 3 * power 2 + 6 :: IndexedPolynomial
>>>
let q = power 6 - 5 * power 4 + 5 * power 2 + 4 :: IndexedPolynomial
>>>
let f = toRationalFunction p q
>>>
let gs = rationalIntegralLogTerms f
>>>
length <$> gs
Just 1>>>
fst . head <$> gs
Just x^2 + (1 % 4)>>>
foldTerms (\e c -> show (e, c) <> " ") . snd . head <$> gs
Just "(0,792x^2 + (-16)) (1,(-2440)x^3 + 32x) (2,(-400)x^2 + 7) (3,800x^3 + (-14)x) "
so it is the case that
\[ \int \frac{x^4-3x^2+6}{x^6-5x^4+5x^2+4} \, dx = \sum_{a \mid a^2+\frac{1}{4} = 0} a \log \left( (800a^3-14a)x^3+(-400a^2+7)x^2+(-2440a^3+32a)x + 792a^2-16 \right) \]
It may return Nothing
if \(\frac{A}{D}\) is not in the expected form.
:: Text | Symbol for the variable. |
-> IndexedPolynomial | Polynomial \(A\). |
-> IndexedPolynomial | Polynomial \(B\). |
-> Expression | Sum \(f\) of inverse tangents. |
Given polynomials \(A\) and \(B\), return a sum \(f\) of inverse tangents such that the following is true.
\[ \frac{df}{dx} = \frac{d}{dx} i \log \left( \frac{A + iB}{A - iB} \right) \]
This allows integrals to be evaluated with only real-valued functions. It also avoids the discontinuities in real-valued indefinite integrals which may result when the integral uses logarithms with complex arguments.
For example,
>>>
toHaskell $ simplify $ complexLogTermToAtan "x" (power 3 - 3 * power 1) (power 2 - 2)
"2 * atan x + 2 * atan ((x + (-3) * x ** 3 + x ** 5) / 2) + 2 * atan (x ** 3)"
so it is the case that
\[ \frac{d}{dx} \left( i \log \left( \frac{(x^3-3x) + i(x^2-2)}{(x^3-3x) - i(x^2-2)} \right) \right) = \frac{d}{dx} \left( 2 \tan^{-1} \left(\frac{x^5-3x^3+x}{2}\right) + 2 \tan^{-1} \left(x^3\right) + 2 \tan^{-1} x \right) \]
complexLogTermToRealTerm :: (IndexedPolynomial, IndexedPolynomialWith IndexedPolynomial) -> ((IndexedPolynomialWith IndexedPolynomial, IndexedPolynomialWith IndexedPolynomial), (IndexedPolynomialWith (IndexedPolynomialWith IndexedPolynomial), IndexedPolynomialWith (IndexedPolynomialWith IndexedPolynomial))) Source #
For the ingredients of a complex logarithm, return the ingredients of an equivalent real function in terms of an indefinite integral.
Specifically, for polynomials \(\left(R(t), S(t,x)\right)\) such that
\[ \frac{df}{dx} = \frac{d}{dx} \sum_{\alpha \in \{ t \mid R(t) = 0 \}} \left( \alpha \log \left( S(\alpha,x) \right) \right) \]
then with return value \(\left( \left(P(u,v), Q(u,v)\right), \left(A(u,v,x), B(u,v,x)\right) \right)\),
and a return value \(g_{uv}\) from complexLogTermToAtan
for \(A(u,v)\) and \(B(u,v)\), the real function is
\[ \frac{df}{dx} = \frac{d}{dx} \left( \sum_{(a,b) \in \{(u,v) \in (\mathbb{R}, \mathbb{R}) \mid P(u,v)=Q(u,v)=0, b > 0\}} \left( a \log \left( A(a,b,x)^2 + B(a,b,x)^2 \right) + b g_{ab}(x) \right) + \sum_{a \in \{t \in \mathbb{R} \mid R(t)=0 \}} \left( a \log (S(a,x)) \right) \right) \]
The return value are polynomials \(\left( (P,Q), (A,B) \right)\), where
- \(P\) is a \(u\)-polynomial, i.e., a polynomial with variable \(u\), with coefficients which are \(v\)-polynomials.
- \(Q\) is a \(u\)-polynomial, with coefficients which are \(v\)-polynomials.
- \(A\) is an \(x\)-polynomial, with coefficients which are \(u\)-polynomials, which in turn have coefficients with \(v\)-polynomials.
- \(B\) is an \(x\)-polynomial, with coefficients which are \(u\)-polynomials, which in turn have coefficients with \(v\)-polynomials.
For example,
>>>
let r = 4 * power 2 + 1 :: IndexedPolynomial
>>>
let s = power 3 + scale (2 * power 1) (power 2) - 3 * power 1 - scale (4 * power 1) 1 :: IndexedPolynomialWith IndexedPolynomial
>>>
complexLogTermToRealTerm (r, s)
(([(0,(-4)x^2 + 1),(2,4)],[(1,8x)]),([(0,[(1,(-4))]),(1,[(0,(-3))]),(2,[(1,2)]),(3,[(0,1)])],[(0,[(0,(-4)x)]),(2,[(0,2x)])]))
While the return value may be hard to parse, this means:
\[ \begin{align*} P & = 4u^2 - 4v^2 + 1 \\ Q & = 8uv \\ A & = x^3 + 2ux^2 - 3x - 4u \\ B & = 2vx^2 - 4v \end{align*} \]
Support
Functions and types useful when integrating rational functions.
:: IndexedPolynomial | Numerator. |
-> IndexedPolynomial | Denominator. |
-> RationalFunction |
Form a rational function from two polynomials. The polynomials will be reduced so that the numerator and denominator are coprime.
data RationalFunction Source #
Represents the ratio of two polynomials with rational number coefficients.