Skip to content

Commit

Permalink
use VERSION check for rational functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jverzani committed Apr 13, 2021
1 parent 371200c commit fa950a6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 19 deletions.
12 changes: 7 additions & 5 deletions src/Polynomials.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ include("polynomials/multroot.jl")
include("polynomials/ChebyshevT.jl")

# Rational functions
include("rational-functions/common.jl")
include("rational-functions/rational-function.jl")
include("rational-functions/fit.jl")
#include("rational-transfer-function.jl")
include("rational-functions/plot-recipes.jl")
if VERSION >= v"1.2.0"
include("rational-functions/common.jl")
include("rational-functions/rational-function.jl")
include("rational-functions/fit.jl")
#include("rational-transfer-function.jl")
include("rational-functions/plot-recipes.jl")
end


# compat; opt-in with `using Polynomials.PolyCompat`
Expand Down
28 changes: 15 additions & 13 deletions src/rational-functions/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ Abstract type for holding ratios of polynomials of type `P{T,X}`.
Default methods for basic arithmetic operations are provided.
Numeric methods to cancel common factors, compute the poles, and return the residues are provided.
!!! Note:
Requires `VERSION >= v"1.2.0"`
"""
abstract type AbstractRationalFunction{T,X,P} end

Expand Down Expand Up @@ -360,9 +363,6 @@ function integrate(pq::P) where {P <: AbstractRationalFunction}
end

## ----
# :numerical only works for v1.2 or later
# drop once new LTS Julia is released
const default_gcd_method = VERSION >= v"1.2" ? :numerical : :euclidean

"""
divrem(pq::AbstractRationalFunction; method=:numerical, kargs...)
Expand All @@ -372,7 +372,7 @@ Return `d,r` with `p/q = d + r/q` where `degree(numerator(r)) < degree(denominat
* `method`: passed to `gcd`
* `kwargs...`: passed to `gcd`
"""
function Base.divrem(pq::PQ; method=default_gcd_method, kwargs...) where {PQ <: AbstractRationalFunction}
function Base.divrem(pq::PQ; method=:numerical, kwargs...) where {PQ <: AbstractRationalFunction}
p,q = pqs(pq)
degree(p) < degree(q) && return (zero(p), pq)

Expand All @@ -384,12 +384,14 @@ end

# like Base.divgcd in rational.jl
# divide p,q by u
function _divgcd(v::Val{:euclidean}, pq; kwargs...)
u = gcd(v, pqs(pq)...; kwargs...)
function _divgcd(V::Val{:euclidean}, pq; kwargs...)
p, q = pqs(pq)
u = gcd(V,p,q; kwargs...)
p÷u, q÷u
end
function _divgcd(v::Val{:noda_sasaki}, pq; kwargs...)
u = gcd(v, pqs(pq)...; kwargs...)
function _divgcd(V::Val{:noda_sasaki}, pq; kwargs...)
p, q = pqs(pq)
u = gcd(V,p,q; kwargs...)
p÷u, q÷u
end
function _divgcd(v::Val{:numerical}, pq; kwargs...)
Expand All @@ -409,7 +411,7 @@ Find GCD of `(p,q)`, `u`, and return `(p÷u)//(q÷u)`. Commonly referred to as l
By default, `AbstractRationalFunction` types do not cancel common factors. This method will numerically cancel common factors, returning the normal form, canonicalized here by `q[end]=1`. The result and original may be considered equivalent as rational expressions, but different when seen as functions of the indeterminate.
"""
function lowest_terms(pq::PQ; method=default_gcd_method, kwargs...) where {T,X,
function lowest_terms(pq::PQ; method=:numerical, kwargs...) where {T,X,
P<:StandardBasisPolynomial{T,X},
PQ<:AbstractRationalFunction{T,X,P}}
v,w = _divgcd(Val(method), pq; kwargs...)
Expand All @@ -423,7 +425,7 @@ end
For a rational function `p/q`, first reduces to normal form, then finds the roots and multiplicities of the resulting denominator.
"""
function poles(pq::AbstractRationalFunction; method=default_gcd_method, kwargs...)
function poles(pq::AbstractRationalFunction; method=:numerical, kwargs...)
pq′ = lowest_terms(pq; method=method, kwargs...)
den = denominator(pq′)
mr = Multroot.multroot(den)
Expand All @@ -436,7 +438,7 @@ end
Return the `zeros` of the rational function (after cancelling commong factors, the `zeros` are the roots of the numerator.
"""
function roots(pq::AbstractRationalFunction; method=default_gcd_method, kwargs...)
function roots(pq::AbstractRationalFunction; method=:numerical, kwargs...)
pq′ = lowest_terms(pq; method=method, kwargs...)
den = numerator(pq′)
mr = Multroot.multroot(den)
Expand Down Expand Up @@ -498,7 +500,7 @@ true
There are several areas where numerical issues can arise. The `divrem`, the identification of multiple roots (`multroot`), the evaluation of the derivatives, ...
"""
function residues(pq::AbstractRationalFunction; method=default_gcd_method, kwargs...)
function residues(pq::AbstractRationalFunction; method=:numerical, kwargs...)


d,r′ = divrem(pq)
Expand Down Expand Up @@ -541,7 +543,7 @@ Should be if `p/q` is in normal form and `d,r=partial_fraction(p//q)` that
`d + sum(r) - p//q ≈ 0`
"""
function partial_fraction(pq::AbstractRationalFunction; method=default_gcd_method, kwargs...)
function partial_fraction(pq::AbstractRationalFunction; method=:numerical, kwargs...)
d,r = residues(pq; method=method, kwargs...)
s = variable(pq)
d, partial_fraction(Val(:residue), r, s)
Expand Down
4 changes: 3 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ using OffsetArrays

@testset "Standard basis" begin include("StandardBasis.jl") end
@testset "ChebyshevT" begin include("ChebyshevT.jl") end
@testset "Rational functions" begin include("rational-functions.jl") end
if VERSION >= v"1.2.0"
@testset "Rational functions" begin include("rational-functions.jl") end
end
@testset "Poly, Pade (compatability)" begin include("Poly.jl") end

0 comments on commit fa950a6

Please sign in to comment.