From 3735889626e61435bdd148d333d30fe1f29fe5ef Mon Sep 17 00:00:00 2001 From: jverzani Date: Mon, 30 Jan 2023 18:04:05 -0500 Subject: [PATCH] clean up --- src/common.jl | 20 +++++++++++++++----- src/contrib.jl | 2 ++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/common.jl b/src/common.jl index 5de4f12f..e68cad31 100644 --- a/src/common.jl +++ b/src/common.jl @@ -208,9 +208,16 @@ vander(::Type{<:AbstractPolynomial}, x::AbstractVector, deg::Integer) """ - critical_points(p, I=domain(p); endpoints::Bool=true) + critical_points(p::AbstractPolynomial{<:Real}, I=domain(p); endpoints::Bool=true) + +Return the critical points of `p` (real zeros of the derivative) within `I` in sorted order. + +* `p`: a polynomial + +* `I`: a specification of a closed or infinite domain, defaulting to `Polynomials.domain(p)`. When specified, the values of `extrema(I)` are used with closed endpoints when finite. + +* `endpoints::Bool`: if `true`, return the endpoints of `I` along with the critical points -Returns critical points (sorted zeros of the derivative) or, if `endpoints=true` critical points in `I` where finite endpoints of `I` are included. `I` defaults to the `p`'s `domain`. Can be used in conjuction with `findmax`, `findmin`, `argmax`, `argmin`, `extrema`, etc. @@ -226,12 +233,15 @@ cps = Polynomials.critical_points(p, (0, 2)) extrema(p, cps) # (-2.0, 2.0) ``` """ -function critical_points(p::AbstractPolynomial{T}, I = domain(p); endpoints::Bool=true) where {T <: Real} - l, r = extrema(I) +function critical_points(p::AbstractPolynomial{T}, I = domain(p); + endpoints::Bool=true) where {T <: Real} + + I′ = Interval(I) + l, r = extrema(I′) q = Polynomials.ngcd(derivative(p), derivative(p,2)).v pts = sort(real.(filter(isreal, roots(q)))) - pts = filter(x -> l ≤ x ≤ r, pts) + pts = filter(in(I′), pts) !endpoints && return pts diff --git a/src/contrib.jl b/src/contrib.jl index e90be456..cf10f6ae 100644 --- a/src/contrib.jl +++ b/src/contrib.jl @@ -174,6 +174,8 @@ struct Interval{T, L <: Bound, R <: Bound} end Interval(f, l) = Interval{Closed, Closed}(f, l) end +Interval(I::Interval) = I +Interval(I) = Interval(extrema(I)...) bounds_types(x::Interval{T,L,R}) where {T,L,R} = (L, R)