Skip to content

Commit

Permalink
Close issue 326 (#327)
Browse files Browse the repository at this point in the history
* Allow ChebyshevT polynomials to be evaluated outside their domain through evalpoly
  • Loading branch information
jverzani authored Mar 27, 2021
1 parent e156428 commit 0bafe29
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name = "Polynomials"
uuid = "f27b6e38-b328-58d1-80ce-0feddd5e7a45"
license = "MIT"
author = "JuliaMath"
version = "2.0.2"
version = "2.0.3"

[deps]
Intervals = "d8418881-c3e1-53bb-8760-2df7ec849ed5"
Expand Down
16 changes: 15 additions & 1 deletion src/polynomials/ChebyshevT.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,24 @@ terms of the given variable `var`, which can be a character, symbol, or string.
```jldoctest ChebyshevT
julia> using Polynomials
julia> ChebyshevT([1, 0, 3, 4])
julia> p = ChebyshevT([1, 0, 3, 4])
ChebyshevT(1⋅T_0(x) + 3⋅T_2(x) + 4⋅T_3(x))
julia> ChebyshevT([1, 2, 3, 0], :s)
ChebyshevT(1⋅T_0(s) + 2⋅T_1(s) + 3⋅T_2(s))
julia> one(ChebyshevT)
ChebyshevT(1.0⋅T_0(x))
julia> p(0.5)
-4.5
julia> Polynomials.evalpoly(5.0, p, false) # bypasses the domain check done in p(5.0)
2088.0
```
The latter shows how to evaluate a `ChebyshevT` polynomial outside of its domain, which is `[-1,1]`. (For newer versions of `Julia`, `evalpoly` is an exported function from Base with methods extended in this package, so the module qualification is unnecessary.
"""
struct ChebyshevT{T <: Number, X} <: AbstractPolynomial{T, X}
coeffs::Vector{T}
Expand Down Expand Up @@ -103,6 +112,11 @@ julia> c.(-1:0.5:1)
"""
function evalpoly(x::S, ch::ChebyshevT{T}) where {T,S}
x domain(ch) && throw(ArgumentError("$x outside of domain"))
evalpoly(x, ch, false)
end

# no checking, so can be called directly through any third argument
function evalpoly(x::S, ch::ChebyshevT{T}, checked) where {T,S}
R = promote_type(T, S)
length(ch) == 0 && return zero(R)
length(ch) == 1 && return R(ch[0])
Expand Down
11 changes: 10 additions & 1 deletion test/ChebyshevT.jl
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,16 @@ end
@test d.coeffs [0, 2]
@test r.coeffs [-2, -4]


# evaluation
c1 = ChebyshevT([0,0,1,1])
fn = x -> (2x^2-1) + (4x^3 - 3x)
for xᵢ range(-0.9, stop=0.9, length=5)
@test c1(xᵢ) fn(xᵢ)
end
# issue 326 evaluate outside of domain
@test Polynomials.evalpoly(2, c1, false) fn(2)
@test Polynomials.evalpoly(3, c1, false) fn(3)

# GCD
c1 = ChebyshevT([1, 2, 3])
c2 = ChebyshevT([3, 2, 1])
Expand Down

2 comments on commit 0bafe29

@jverzani
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/32940

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v2.0.3 -m "<description of version>" 0bafe29f756b58d4ff9b463b14519687179fc4bc
git push origin v2.0.3

Please sign in to comment.