Skip to content

Commit 8deea9e

Browse files
authored
Merge pull request #657 from JuliaControl/ss2tftypestab
fix type stability in ss2tf conversion
2 parents 172f839 + f103c63 commit 8deea9e

File tree

3 files changed

+13
-8
lines changed

3 files changed

+13
-8
lines changed

src/types/conversion.jl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -300,10 +300,9 @@ end
300300

301301
# TODO: Could perhaps be made more accurate. See: An accurate and efficient
302302
# algorithm for the computation of the # characteristic polynomial of a general square matrix.
303-
function charpoly(A::AbstractMatrix{<:Number})
304-
Λ = eigvalsnosort(A)
305-
306-
return prod(roots2poly_factors(Λ)) # Compute the polynomial factors directly?
303+
function charpoly(A::AbstractMatrix{T}) where T
304+
Λ::Vector{T} = eigvalsnosort(A)
305+
return prod(roots2poly_factors(Λ))::Polynomial{T,:x} # Compute the polynomial factors directly?
307306
end
308307
function charpoly(A::AbstractMatrix{<:Real})
309308
Λ = eigvalsnosort(A)

src/utilities.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ printpolyfun(var) = (io, p, mimetype = MIME"text/plain"()) -> Polynomials.printp
5454
# returned by LAPACK routines for eigenvalues.
5555
function roots2real_poly_factors(roots::Vector{cT}) where cT <: Number
5656
T = real(cT)
57-
poly_factors = Vector{Polynomial{T}}()
57+
poly_factors = Vector{Polynomial{T,:x}}()
5858
for k=1:length(roots)
5959
r = roots[k]
6060

6161
if isreal(r)
62-
push!(poly_factors,Polynomial{T}([-real(r),1]))
62+
push!(poly_factors,Polynomial{T,:x}([-real(r),1]))
6363
else
6464
if imag(r) < 0 # This roots was handled in the previous iteration # TODO: Fix better error handling
6565
continue
@@ -69,7 +69,7 @@ function roots2real_poly_factors(roots::Vector{cT}) where cT <: Number
6969
throw(ArgumentError("Found pole without matching conjugate."))
7070
end
7171

72-
push!(poly_factors,Polynomial{T}([real(r)^2+imag(r)^2, -2*real(r), 1]))
72+
push!(poly_factors,Polynomial{T,:x}([real(r)^2+imag(r)^2, -2*real(r), 1]))
7373
# k += 1 # Skip one iteration in the loop
7474
end
7575
end
@@ -78,7 +78,7 @@ function roots2real_poly_factors(roots::Vector{cT}) where cT <: Number
7878
end
7979
# This function should hande both Complex as well as symbolic types
8080
function roots2poly_factors(roots::Vector{T}) where T <: Number
81-
return Polynomial{T}[Polynomial{T}([-r, 1]) for r in roots]
81+
return Polynomial{T,:x}[Polynomial{T,:x}([-r, 1]) for r in roots]
8282
end
8383

8484

test/test_conversion.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
@testset "test_conversion" begin
22

3+
A = randn(3,3)
4+
@inferred ControlSystems.charpoly(A)
5+
6+
A = randn(ComplexF64,3,3)
7+
@inferred ControlSystems.charpoly(A)
8+
39
G = tf(1.0,[1,1])
410
H = zpk([0.0], [1.0], 1.0)
511
@inferred ControlSystems.siso_tf_to_ss(Float64, G.matrix[1,1])

0 commit comments

Comments
 (0)