Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix type stability in siso_tf_to_ss #656

Merged
merged 1 commit into from
Feb 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions src/types/conversion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -137,24 +137,24 @@ function siso_tf_to_ss(T::Type, f::SisoRational)
N = length(den) - 1 # The order of the rational function f

# Get numerator coefficient of the same order as the denominator
bN = length(num) == N+1 ? num[1] : 0
bN = length(num) == N+1 ? num[1] : zero(num[1])

if N == 0 #|| num == zero(Polynomial{T})
A = zeros(T, (0, 0))
B = zeros(T, (0, 1))
C = zeros(T, (1, 0))
@views if N == 0 #|| num == zero(Polynomial{T})
A = zeros(T, 0, 0)
B = zeros(T, 0, 1)
C = zeros(T, 1, 0)
else
A = diagm(1 => ones(T, N-1))
A[end, :] .= -reverse(den)[1:end-1]
A[end, :] .= .-reverse(den)[1:end-1]

B = zeros(T, (N, 1))
B = zeros(T, N, 1)
B[end] = one(T)

C = zeros(T, (1, N))
C = zeros(T, 1, N)
C[1:min(N, length(num))] = reverse(num)[1:min(N, length(num))]
C[:] -= bN * reverse(den)[1:end-1] # Can index into polynomials at greater inddices than their length
C[:] .-= bN .* reverse(den)[1:end-1] # Can index into polynomials at greater inddices than their length
end
D = fill(bN, (1, 1))
D = fill(bN, 1, 1)

return A, B, C, D
end
Expand Down
5 changes: 5 additions & 0 deletions test/test_conversion.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
@testset "test_conversion" begin

G = tf(1.0,[1,1])
H = zpk([0.0], [1.0], 1.0)
@inferred ControlSystems.siso_tf_to_ss(Float64, G.matrix[1,1])
@inferred ControlSystems.siso_tf_to_ss(Float64, H.matrix[1,1])

# Easy second order system
sys1 = ss([-1 0;1 1],[1;0],[1 1],0)
G1 = tf([1,0],[1,0,-1])
Expand Down