Skip to content

Commit eca0250

Browse files
committed
companion working for ChebyshevT
1 parent 223b800 commit eca0250

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

src/polynomials/ChebyshevT.jl

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -116,19 +116,21 @@ end
116116
# end
117117

118118
##
119-
# function companion(p::ChebyshevT{T}) where T
120-
# d = length(p) - 1
121-
# d < 1 && error("Series must have degree greater than 1")
122-
# d == 1 && return diagm(0 => [-p[0] / p[1]])
123-
# R = eltype(one(T) / p.coeffs[end])
124-
125-
# comp = diagm(-1 => ones(d - 1),
126-
# 1 => ones(d - 1))
127-
# comp[2, 1] = 2
128-
# monics = p.coeffs ./ p.coeffs[end]
129-
# comp[:, end] .= -monics[1:d]
130-
# return R.(comp ./ 2)
131-
# end
119+
function companion(p::ChebyshevT{T}) where T
120+
d = length(p) - 1
121+
d < 1 && error("Series must have degree greater than 1")
122+
d == 1 && return diagm(0 => [-p[0] / p[1]])
123+
R = eltype(one(T) / one(T))
124+
125+
scl = append!([1.0], 5 .* ones(d - 1))
126+
127+
diag = append!([5], 0.5 .* ones(d - 2))
128+
comp = diagm(-1 => diag,
129+
1 => diag)
130+
monics = p.coeffs ./ p.coeffs[end]
131+
comp[:, end] .-= monics[1:d] .* scl ./ scl[end]
132+
return R.(comp ./ 2)
133+
end
132134

133135
function Base.:+(p1::ChebyshevT, p2::ChebyshevT)
134136
p1.var != p2.var && error("Polynomials must have same variable")

test/ChebyshevT.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,20 @@ end
7171

7272
end
7373

74+
@testset "Companion" begin
75+
c_null = ChebyshevT(Int[])
76+
c_1 = ChebyshevT([1])
77+
@test_throws ErrorException companion(c_null)
78+
@test_throws ErrorException companion(c_1)
79+
for i in 1:5
80+
coef = push!(zeros(i), 1)
81+
c = ChebyshevT(coef)
82+
@test size(companion(c)) == (i, i)
83+
end
84+
c = ChebyshevT([1, 2])
85+
@test companion(c)[1, 1] == -0.5
86+
end
87+
7488
@testset "Arithmetic $i, $j" for i in 1:5, j in 1:5
7589
# multiplication
7690
target = zeros(i + j + 1)

0 commit comments

Comments
 (0)