Skip to content

Commit 0bafe29

Browse files
authored
Close issue 326 (#327)
* Allow ChebyshevT polynomials to be evaluated outside their domain through evalpoly
1 parent e156428 commit 0bafe29

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
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.2"
5+
version = "2.0.3"
66

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

src/polynomials/ChebyshevT.jl

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,24 @@ terms of the given variable `var`, which can be a character, symbol, or string.
1717
```jldoctest ChebyshevT
1818
julia> using Polynomials
1919
20-
julia> ChebyshevT([1, 0, 3, 4])
20+
julia> p = ChebyshevT([1, 0, 3, 4])
2121
ChebyshevT(1⋅T_0(x) + 3⋅T_2(x) + 4⋅T_3(x))
2222
2323
julia> ChebyshevT([1, 2, 3, 0], :s)
2424
ChebyshevT(1⋅T_0(s) + 2⋅T_1(s) + 3⋅T_2(s))
2525
2626
julia> one(ChebyshevT)
2727
ChebyshevT(1.0⋅T_0(x))
28+
29+
julia> p(0.5)
30+
-4.5
31+
32+
julia> Polynomials.evalpoly(5.0, p, false) # bypasses the domain check done in p(5.0)
33+
2088.0
2834
```
35+
36+
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.
37+
2938
"""
3039
struct ChebyshevT{T <: Number, X} <: AbstractPolynomial{T, X}
3140
coeffs::Vector{T}
@@ -103,6 +112,11 @@ julia> c.(-1:0.5:1)
103112
"""
104113
function evalpoly(x::S, ch::ChebyshevT{T}) where {T,S}
105114
x domain(ch) && throw(ArgumentError("$x outside of domain"))
115+
evalpoly(x, ch, false)
116+
end
117+
118+
# no checking, so can be called directly through any third argument
119+
function evalpoly(x::S, ch::ChebyshevT{T}, checked) where {T,S}
106120
R = promote_type(T, S)
107121
length(ch) == 0 && return zero(R)
108122
length(ch) == 1 && return R(ch[0])

test/ChebyshevT.jl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,16 @@ end
157157
@test d.coeffs [0, 2]
158158
@test r.coeffs [-2, -4]
159159

160-
160+
# evaluation
161+
c1 = ChebyshevT([0,0,1,1])
162+
fn = x -> (2x^2-1) + (4x^3 - 3x)
163+
for xᵢ range(-0.9, stop=0.9, length=5)
164+
@test c1(xᵢ) fn(xᵢ)
165+
end
166+
# issue 326 evaluate outside of domain
167+
@test Polynomials.evalpoly(2, c1, false) fn(2)
168+
@test Polynomials.evalpoly(3, c1, false) fn(3)
169+
161170
# GCD
162171
c1 = ChebyshevT([1, 2, 3])
163172
c2 = ChebyshevT([3, 2, 1])

0 commit comments

Comments
 (0)