Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
jverzani committed Jan 30, 2023
1 parent 0995da3 commit 3735889
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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

Expand Down
2 changes: 2 additions & 0 deletions src/contrib.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 3735889

Please sign in to comment.