Skip to content

Commit 8e66c4e

Browse files
authored
Rational function (#335)
* add rational-function type; fix bug with ngcd; plot recipe * fix bugs with ngcd; add tests; rename to lowest_terms * use VERSION check for rational functions
1 parent e9f59d7 commit 8e66c4e

File tree

13 files changed

+1336
-5
lines changed

13 files changed

+1336
-5
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name = "Polynomials"
22
uuid = "f27b6e38-b328-58d1-80ce-0feddd5e7a45"
33
license = "MIT"
44
author = "JuliaMath"
5-
version = "2.0.8"
5+
version = "2.0.9"
66

77
[deps]
88
Intervals = "d8418881-c3e1-53bb-8760-2df7ec849ed5"

src/Polynomials.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ include("polynomials/multroot.jl")
2929

3030
include("polynomials/ChebyshevT.jl")
3131

32+
# Rational functions
33+
if VERSION >= v"1.2.0"
34+
include("rational-functions/common.jl")
35+
include("rational-functions/rational-function.jl")
36+
include("rational-functions/fit.jl")
37+
#include("rational-transfer-function.jl")
38+
include("rational-functions/plot-recipes.jl")
39+
end
40+
41+
3242
# compat; opt-in with `using Polynomials.PolyCompat`
3343
include("polynomials/Poly.jl")
3444

src/common.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,6 +1019,7 @@ function Base.isapprox(p1::AbstractPolynomial{T,X},
10191019
rtol::Real = (Base.rtoldefault(T, S, 0)),
10201020
atol::Real = 0,) where {T,X,S,Y}
10211021
assert_same_variable(p1, p2)
1022+
(hasnan(p1) || hasnan(p2)) && return false # NaN poisons comparisons
10221023
# copy over from abstractarray.jl
10231024
Δ = norm(p1-p2)
10241025
if isfinite(Δ)

src/polynomials/multroot.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ is misidentified.
7979
function multroot(p::Polynomials.StandardBasisPolynomial{T}; verbose=false,
8080
kwargs...) where {T}
8181

82+
# degenerate case, constant
83+
degree(p) == 0 && return (values=T[], multiplicities=Int[], κ=NaN, ϵ=NaN)
84+
8285
# degenerate case, all zeros
8386
if (nz = findfirst(!iszero, coeffs(p))) == length(coeffs(p))
8487
return (values=zeros(T,1), multiplicities=[nz-1], κ=NaN, ϵ=NaN)
@@ -202,7 +205,7 @@ function pejorative_root(p, zs::Vector{S}, ls::Vector{Int};
202205
The multiplicity count may be in error: the initial guess for the roots failed
203206
to converge to a pejorative root.
204207
""")
205-
return(zₘs)
208+
return(zₖs)
206209
end
207210

208211
end

src/polynomials/ngcd.jl

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ function ngcd(p::P, q::Q,
1919
a,b = divrem(p,q)
2020
return ngcd(q,b, args...; λ=100, kwargs...)
2121
end
22+
2223
# easy cases
2324
degree(p) < 0 && return (u=q, v=p, w=one(q), θ=NaN, κ=NaN)
2425
degree(p) == 0 && return (u=one(q), v=p, w=q, θ=NaN, κ=NaN)
@@ -28,15 +29,30 @@ function ngcd(p::P, q::Q,
2829
Polynomials.assert_same_variable(p,q)
2930

3031
R = promote_type(float(T), float(S))
32+
𝑷 = Polynomials.constructorof(promote_type(P,Q)){R,X}
33+
3134
ps = R[pᵢ for pᵢ coeffs(p)]
3235
qs = R[qᵢ for qᵢ coeffs(q)]
33-
p′ = PnPolynomial(ps)
34-
q′ = PnPolynomial(qs)
3536

37+
# cancel zeros
38+
nz = min(findfirst(!iszero, ps), findfirst(!iszero, qs))
39+
if nz == length(qs)
40+
x = variable(p)
41+
u = x^(nz-1)
42+
v,w = 𝑷(ps[nz:end]), 𝑷(qs[nz:end])
43+
return (u=u, v=v, w=w, Θ=NaN, κ=NaN)
44+
end
45+
46+
## call ngcd
47+
p′ = PnPolynomial{R,X}(ps[nz:end])
48+
q′ = PnPolynomial{R,X}(qs[nz:end])
3649
out = NGCD.ngcd(p′, q′, args...; kwargs...)
3750

38-
𝑷 = Polynomials.constructorof(promote_type(P,Q)){R,X}
51+
𝑷 = Polynomials.constructorof(promote_type(P,Q)){R,X}
3952
u,v,w = convert.(𝑷, (out.u,out.v,out.w))
53+
if nz > 1
54+
u *= variable(u)^(nz-1)
55+
end
4056
(u=u,v=v,w=w, Θ=out.Θ, κ = out.κ)
4157

4258
end

0 commit comments

Comments
 (0)