Skip to content

Commit

Permalink
Fix struct_ctrb_obsv (#540)
Browse files Browse the repository at this point in the history
* Fix struct_ctrb_obsv, closes #409

* Update src/simplification.jl

Co-authored-by: Fredrik Bagge Carlson <baggepinnen@gmail.com>

* add to docstring

Co-authored-by: olof3 <olof3@users.noreply.github.com>
  • Loading branch information
baggepinnen and olof3 authored Jun 16, 2021
1 parent 1f6fcec commit 714bf85
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
30 changes: 21 additions & 9 deletions src/simplification.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,24 @@
Compute the structurally minimal realization of the state-space system `sys`. A
structurally minimal realization is one where only states that can be
determined to be uncontrollable and unobservable based on the location of 0s in
`sys` are removed."""
`sys` are removed.
Systems with numerical noise in the coefficients, e.g., noise on the order of `eps` require truncation to zero to be
affected by structural simplification, e.g.,
```julia
trunc_zero!(A) = A[abs.(A) .< 10eps(maximum(abs, A))] .= 0
trunc_zero!(sys.A); trunc_zero!(sys.B); trunc_zero!(sys.C)
sminreal(sys)
```
"""
function sminreal(sys::StateSpace)
A, B, C, inds = struct_ctrb_obsv(sys)
return StateSpace(A, B, C, sys.D, sys.timeevol)
end

# Determine the structurally controllable and observable realization for the system
struct_ctrb_obsv(sys::StateSpace) = struct_ctrb_obsv(sys.A, sys.B, sys.C)

function struct_ctrb_obsv(A::AbstractVecOrMat, B::AbstractVecOrMat, C::AbstractVecOrMat)
costates = struct_ctrb_states(A, B) .& struct_ctrb_states(A', C')
if !all(costates)
Expand All @@ -22,15 +32,17 @@ function struct_ctrb_obsv(A::AbstractVecOrMat, B::AbstractVecOrMat, C::AbstractV
end
end

# Compute a bit-vector, expressing whether a state of the pair (A, B) is
# structurally controllable.
"""
struct_ctrb_states(A::AbstractVecOrMat, B::AbstractVecOrMat)
Compute a bit-vector, expressing whether a state of the pair (A, B) is
structurally controllable based on the location of zeros in the matrices.
"""
function struct_ctrb_states(A::AbstractVecOrMat, B::AbstractVecOrMat)
bitA = A .!= 0
d_cvec = cvec = vec(any(B .!= 0, dims=2))
while any(d_cvec .!= 0)
Adcvec = vec(any(bitA[:, findall(d_cvec)], dims=2))
cvec = cvec .| Adcvec
d_cvec = Adcvec .& .~cvec
x = vec(any(B .!= 0, dims=2)) # index vector indicating states that have been affected by input
for i = 1:size(A, 1) # apply A nx times, similar to controllability matrix
x = (bitA * x) .!= 0
end
return cvec
x
end
3 changes: 3 additions & 0 deletions test/test_simplification.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ G = ss([-5 0 0 0; 0 -1 -2.5 0; 0 4 0 0; 0 0 0 -6], [2 0; 0 1; 0 0; 0 2],
@test sminreal(G[2, 1]) == ss([-5], [2], [-2], [1])
@test sminreal(G[2, 2]) == ss([-6], [2], [1], [0])

using ControlSystems.DemoSystems: resonant
R = resonant()*resonant()
@test sminreal(R) == R # https://github.com/JuliaControl/ControlSystems.jl/issues/409

## MINREAL ##

Expand Down

0 comments on commit 714bf85

Please sign in to comment.