Skip to content
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
3 changes: 2 additions & 1 deletion lib/ControlSystemsBase/src/types/SisoTfTypes/SisoZpk.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,12 @@ function minreal(sys::SisoZpk{T,TR}, eps::Real) where {T, TR}
distance, zidx = findmin(abs.(p .- newZ))

if distance < eps
if imag(p) == 0 && imag(newZ[zidx]) != 0
if imag(p) == 0 && imag(newZ[zidx]) != 0 && zidx < length(newZ)
newZ[zidx+1] = real(newZ[zidx+1])
end
if imag(newZ[zidx]) == 0 && imag(p) != 0
pidx += 1
pidx > length(sys.p) && (deleteat!(newZ, zidx); break)
p = real(sys.p[pidx])
deleteat!(newZ, zidx)
continue
Expand Down
8 changes: 8 additions & 0 deletions lib/ControlSystemsBase/test/test_zpk.jl
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,14 @@ k = 0.3
@test minreal(zpk([-1.0, -2.0], Float64[], 2.5)) == zpk([-1.0, -2.0], Float64[], 2.5)
@test minreal(zpk(Float64[], [-1.0, -2.0], 2.5)) == zpk(Float64[], [-1.0, -2.0], 2.5)

# Regression: bounds-checks in SisoZpk minreal. With complex-valued zpk the
# constructor does not enforce conjugate-pair ordering, so the cancellation
# loop can hit `newZ[zidx+1]` (line 83) with `zidx == length(newZ)`, or
# advance `pidx` past `length(sys.p)` before reading `sys.p[pidx]` (line 87).
# Both previously threw BoundsError; they should now return cleanly.
@test minreal(zpk([1.0+0.001im, 1.0-0.001im], [1.0, 1.0+0.001im], 1.0+0im), 0.01) == zpk(ComplexF64[], ComplexF64[], 1.0+0im)
@test minreal(zpk([1.0+0.001im, 1.0-0.001im], [1.0+0.001im, 1.0], 1.0+0im), 0.01) == zpk(ComplexF64[], ComplexF64[], 1.0+0im)

# Test type inference
@test eltype(fill(zpk("s"),2)) <: TransferFunction
@test eltype(fill(zpk([1],[1,1],1),2)) <: TransferFunction
Expand Down
Loading