Skip to content

Commit 0ebf9ff

Browse files
committed
implemented chebyshev from roots
1 parent 3f630f9 commit 0ebf9ff

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

src/polynomials/ChebyshevT.jl

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,21 @@ function (ch::ChebyshevT{T})(x::S) where {T,S}
5959
return R(c0 + c1 * x)
6060
end
6161

62-
##
63-
# function fromroots(P::Type{<:ChebyshevT}, r::AbstractVector{T}; var::SymbolLike = :x) where {T <: Number}
64-
# n = length(r)
65-
# c = zeros(T, n + 1)
66-
# c[1] = one(T)
67-
# @inbounds for j = 2:n, i = j:-1:1
68-
# c[i + 1] = c[i + 1] - r[j] * c[i]
69-
# end
70-
# return ChebyshevT(reverse(c), var)
71-
# end
62+
function fromroots(P::Type{<:ChebyshevT}, roots::AbstractVector{T}; var::SymbolLike = :x) where {T <: Number}
63+
p = [P([-r, 1]) for r in roots]
64+
n = length(p)
65+
while n > 1
66+
m, r = divrem(n, 2)
67+
tmp = [p[i] * p[i + m] for i in 1:m]
68+
if r > 0
69+
tmp[1] *= p[end]
70+
end
71+
p = tmp
72+
n = m
73+
end
74+
# return truncate!(reduce(*, p))
75+
return truncate!(p[1])
76+
end
7277

7378
function vander(P::Type{<:ChebyshevT}, x::AbstractVector{T}, n::Integer) where {T <: Number}
7479
A = Matrix{T}(undef, length(x), n + 1)

test/ChebyshevT.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,17 @@ end
4545
@test degree(p0) == -1
4646
end
4747

48+
@testset "Roots" begin
49+
# from roots
50+
for i in 1:5
51+
roots = cos.(range(-π, 0, length = 2i + 1)[2:2:end])
52+
target = ChebyshevT(append!(zeros(i), [1]))
53+
res = fromroots(ChebyshevT, roots) .* 2^(i-1)
54+
@test res == target
55+
end
56+
@test fromroots(ChebyshevT, [-1, 0, 1]) == ChebyshevT([0, -0.25, 0, 0.25])
57+
@test fromroots(ChebyshevT, [-1im, 1im]) ChebyshevT([1.5 + 0im, 0 + 0im, 0.5 + 0im])
58+
end
4859

4960
@testset "Values" begin
5061
c1 = ChebyshevT([2.5, 2.0, 1.5]) #1 + 2x + 3x^2

0 commit comments

Comments
 (0)