Skip to content

Commit 9d3edd4

Browse files
authored
Merge pull request #204 from JuliaControl/loosen_types
Remove unnecessary type restrictions
2 parents b94a759 + f8a70b1 commit 9d3edd4

7 files changed

Lines changed: 36 additions & 33 deletions

File tree

src/analysis.jl

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pole(sys::SisoTf) = error("pole is not implemented for type $(typeof(sys))")
88
# converting to zpk before works better in the cases I have tested.
99
pole(sys::TransferFunction) = pole(zpk(sys))
1010

11-
function pole(sys::TransferFunction{SisoZpk{T,TR}}) where {T<:Number, TR<:Number}
11+
function pole(sys::TransferFunction{SisoZpk{T,TR}}) where {T, TR}
1212
# With right TR, this code works for any SisoTf
1313

1414
# Calculate least common denominator of the minors,
@@ -70,7 +70,7 @@ end
7070
7171
Compute the determinant of the Matrix `sys` of SisoTf systems, returns a SisoTf system."""
7272
# TODO: improve this implementation, should be more efficient ones
73-
function det(sys::Matrix{S}) where {T<:Number, TR, S<:SisoZpk}
73+
function det(sys::Matrix{S}) where {S<:SisoZpk}
7474
ny, nu = size(sys)
7575
@assert ny == nu "Matrix is not square"
7676
if ny == 1
@@ -192,12 +192,12 @@ end
192192
# Note that this returns either Vector{ComplexF32} or Vector{Float64}
193193
tzero(sys::StateSpace) = tzero(sys.A, sys.B, sys.C, sys.D)
194194
# Make sure everything is BlasFloat
195-
function tzero(A::Matrix{<:Number}, B::Matrix{<:Number}, C::Matrix{<:Number}, D::Matrix{<:Number})
195+
function tzero(A::AbstractMatrix, B::AbstractMatrix, C::AbstractMatrix, D::AbstractMatrix)
196196
T = promote_type(eltype(A), eltype(B), eltype(C), eltype(D))
197-
A2, B2, C2, D2 = promote(A,B,C,D, fill(zero(T)/one(T),0,0)) # If Int, we get Float64
197+
A2, B2, C2, D2, _ = promote(A,B,C,D, fill(zero(T)/one(T),0,0)) # If Int, we get Float64
198198
tzero(A2, B2, C2, D2)
199199
end
200-
function tzero(A::Matrix{T}, B::Matrix{T}, C::Matrix{T}, D::Matrix{T}) where T<:BlasFloat
200+
function tzero(A::AbstractMatrix{T}, B::AbstractMatrix{T}, C::AbstractMatrix{T}, D::AbstractMatrix{T}) where {T <: Union{AbstractFloat,Complex{<:AbstractFloat}}#= For eps(T) =#}
201201
# Balance the system
202202
A, B, C = balance_statespace(A, B, C)
203203

@@ -232,13 +232,13 @@ function tzero(A::Matrix{T}, B::Matrix{T}, C::Matrix{T}, D::Matrix{T}) where T<:
232232
return zs
233233
end
234234

235-
reduce_sys(A::AbstractMatrix{<:BlasFloat}, B::AbstractMatrix{<:BlasFloat}, C::AbstractMatrix{<:BlasFloat}, D::AbstractMatrix{<:BlasFloat}, meps::BlasFloat) =
236-
reduce_sys(promote(A,B,C,D)..., meps)
235+
237236
"""
238237
Implements REDUCE in the Emami-Naeini & Van Dooren paper. Returns transformed
239238
A, B, C, D matrices. These are empty if there are no zeros.
240239
"""
241-
function reduce_sys(A::AbstractMatrix{T}, B::AbstractMatrix{T}, C::AbstractMatrix{T}, D::AbstractMatrix{T}, meps::BlasFloat) where T <: BlasFloat
240+
function reduce_sys(A::AbstractMatrix, B::AbstractMatrix, C::AbstractMatrix, D::AbstractMatrix, meps::AbstractFloat)
241+
T = promote_type(eltype(A), eltype(B), eltype(C), eltype(D))
242242
Cbar, Dbar = C, D
243243
if isempty(A)
244244
return A, B, C, D
@@ -313,7 +313,7 @@ If `!allMargins`, return only the smallest margin
313313
If `full` return also `fullPhase`
314314
315315
"""
316-
function margin(sys::LTISystem, w::AbstractVector{S}; full=false, allMargins=false) where S<:Real
316+
function margin(sys::LTISystem, w::AbstractVector{<:Real}; full=false, allMargins=false)
317317
ny, nu = size(sys)
318318

319319
if allMargins
@@ -346,7 +346,7 @@ end
346346
347347
returns frequencies for gain margins, gain margins, frequencies for phase margins, phase margins
348348
"""
349-
function sisomargin(sys::LTISystem, w::AbstractVector{S}; full=false, allMargins=false) where S<:Real
349+
function sisomargin(sys::LTISystem, w::AbstractVector{<:Real}; full=false, allMargins=false)
350350
ny, nu = size(sys)
351351
if ny !=1 || nu != 1
352352
error("System must be SISO, use `margin` instead")

src/connections.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,9 @@ Base.typed_hcat(::Type{T}, X::Number...) where {T<:LTISystem, N} = hcat(convert.
162162
# end
163163

164164

165-
blockdiag(mats::Matrix...) = blockdiag(promote(mats...)...)
165+
blockdiag(mats::AbstractMatrix...) = blockdiag(promote(mats...)...)
166166

167-
function blockdiag(mats::Matrix{T}...) where T
167+
function blockdiag(mats::AbstractMatrix{T}...) where T
168168
rows = Int[size(m, 1) for m in mats]
169169
cols = Int[size(m, 2) for m in mats]
170170
res = zeros(T, sum(rows), sum(cols))

src/discrete.jl

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ function c2d_poly2poly(p,h)
188188
end
189189

190190

191-
function c2d(G::TransferFunction{S}, h;kwargs...) where {S}
191+
function c2d(G::TransferFunction, h;kwargs...)
192192
@assert iscontinuous(G)
193193
ny, nu = size(G)
194194
@assert (ny + nu == 2) "c2d(G::TransferFunction, h) not implemented for MIMO systems"
@@ -198,16 +198,17 @@ function c2d(G::TransferFunction{S}, h;kwargs...) where {S}
198198
end
199199

200200

201-
"""`[y, t, x] = lsima(sys, t[, x0, method])`
201+
"""`[y, t, x] = lsima(sys, t, r, controller, state[, x0, method])`
202202
203203
Calculate the time response of adaptive controller. If `x0` is ommitted,
204204
a zero vector is used.
205205
206+
`controller` is a function `u[i],state = controller(state, y[1:i], u[1:i-1], r[1:i])`
206207
Continuous time systems are discretized before simulation. By default, the
207208
method is chosen based on the smoothness of the input signal. Optionally, the
208209
`method` parameter can be specified as either `:zoh` or `:foh`."""
209-
function lsima(sys::StateSpace, t::AbstractVector, r::AbstractVector{T}, control_signal::Function,state,
210-
x0::VecOrMat=zeros(sys.nx, 1), method::Symbol=:zoh) where T
210+
function lsima(sys::StateSpace, t::AbstractVector, r::AbstractVector, control_signal::Function,state,
211+
x0::VecOrMat=zeros(sys.nx, 1), method::Symbol=:zoh)
211212
ny, nu = size(sys)
212213

213214
nx = sys.nx
@@ -230,9 +231,9 @@ function lsima(sys::StateSpace, t::AbstractVector, r::AbstractVector{T}, control
230231
dsys, x0map = c2d(sys, dt, :foh)
231232
end
232233
n = size(t, 1)
233-
x = Array{T}(undef, size(sys.A, 1), n)
234-
u = Array{T}(undef, n)
235-
y = Array{T}(undef, n)
234+
x = similar(r, size(sys.A, 1), n)
235+
u = similar(r, n)
236+
y = similar(r, n)
236237
for i=1:n
237238
x[:,i] = x0
238239
y[i] = (sys.C*x0 + sys.D*u[i])[1]

src/freqresp.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Evaluate the frequency response of a linear system
55
`w -> C*((iw*im -A)^-1)*B + D`
66
77
of system `sys` over the frequency vector `w`."""
8-
function freqresp(sys::LTISystem, w_vec::AbstractVector{S}) where {S<:Real}
8+
function freqresp(sys::LTISystem, w_vec::AbstractVector{<:Real})
99
# Create imaginary freq vector s
1010
if !iscontinuous(sys)
1111
Ts = sys.Ts == -1 ? 1.0 : sys.Ts
@@ -62,7 +62,7 @@ function evalfr(sys::StateSpace{T0}, s::Number) where {T0}
6262
end
6363
end
6464

65-
function evalfr(G::TransferFunction{<:SisoTf{T0}}, s::Number) where {T0}
65+
function evalfr(G::TransferFunction{<:SisoTf}, s::Number)
6666
map(m -> evalfr(m,s), G.matrix)
6767
end
6868

@@ -133,7 +133,7 @@ function sigma(sys::LTISystem, w::AbstractVector)
133133
end
134134
sigma(sys::LTISystem) = sigma(sys, _default_freq_vector(sys, Val{:sigma}()))
135135

136-
function _default_freq_vector(systems::Vector{T}, plot) where T<:LTISystem
136+
function _default_freq_vector(systems::Vector{<:LTISystem}, plot)
137137
min_pt_per_dec = 60
138138
min_pt_total = 200
139139
bounds = map(sys -> _bounds_and_features(sys, plot)[1], systems)

src/matrix_comps.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ end
7272
Compute the solution `X` to the discrete Lyapunov equation
7373
`AXA' - X + Q = 0`.
7474
"""
75-
function dlyap(A::AbstractMatrix{T}, Q) where T
75+
function dlyap(A::AbstractMatrix, Q)
7676
lhs = kron(A, conj(A))
7777
lhs = I - lhs
7878
x = lhs\reshape(Q, prod(size(Q)), 1)
@@ -107,7 +107,7 @@ Compute the observability matrix for the system described by `(A, C)` or `sys`.
107107
Note that checking for observability by computing the rank from `obsv` is
108108
not the most numerically accurate way, a better method is checking if
109109
`gram(sys, :o)` is positive definite."""
110-
function obsv(A::AbstractMatrix{T}, C::AbstractMatrix{T}) where {T <: Number}
110+
function obsv(A::AbstractMatrix{T}, C::AbstractMatrix) where T
111111
n = size(A, 1)
112112
ny = size(C, 1)
113113
if n != size(C, 2)
@@ -422,7 +422,7 @@ function balance(A, perm::Bool=true)
422422
return S, P, B
423423
end
424424

425-
function cswap!(i::Integer, j::Integer, X::StridedMatrix{T}) where T<:Number
425+
function cswap!(i::Integer, j::Integer, X::StridedMatrix)
426426
for k = 1:size(X,1)
427427
X[i, k], X[j, k] = X[j, k], X[i, k]
428428
end

src/timeresp.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,10 @@ If `x0` is not provided, a zero-vector is used.
187187
188188
If `u` is a function, then `u(x,i)` is called to calculate the control signal every iteration. This can be used to provide a control law such as state feedback `u=-Lx` calculated by `lqr`. In this case, an integrer `iters` must be provided that indicates the number of iterations.
189189
"""
190-
function ltitr(A::Matrix{T}, B::Matrix{T}, u::AbstractVecOrMat{T},
191-
x0::VecOrMat{T}=zeros(T, size(A, 1))) where T
190+
function ltitr(A::AbstractMatrix{T}, B::AbstractMatrix{T}, u::AbstractVecOrMat,
191+
x0::VecOrMat=zeros(T, size(A, 1))) where T
192192
n = size(u, 1)
193-
x = Array{T}(undef, size(A, 1), n)
193+
x = similar(A, size(A, 1), n)
194194
for i=1:n
195195
x[:,i] = x0
196196
x0 = A * x0 + B * u[i,:]
@@ -199,11 +199,11 @@ function ltitr(A::Matrix{T}, B::Matrix{T}, u::AbstractVecOrMat{T},
199199
end
200200

201201

202-
function ltitr(A::Matrix{T}, B::Matrix{T}, u::Function, t,
203-
x0::VecOrMat{T}=zeros(T, size(A, 1))) where T
202+
function ltitr(A::AbstractMatrix{T}, B::AbstractMatrix{T}, u::Function, t,
203+
x0::VecOrMat=zeros(T, size(A, 1))) where T
204204
iters = length(t)
205-
x = Array{T}(undef, size(A, 1), iters)
206-
uout = Array{T}(undef, size(B, 2), iters)
205+
x = similar(A, size(A, 1), iters)
206+
uout = similar(A, size(B, 2), iters)
207207

208208
for i=1:iters
209209
x[:,i] = x0

src/utilities.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ to_matrix(T, A::Adjoint{R, MT}) where {R<:Number, MT<:AbstractMatrix} = to_matri
2727
function roots2real_poly_factors(roots::Vector{cT}) where cT <: Number
2828
T = real(cT)
2929
poly_factors = Vector{Poly{T}}()
30-
30+
@static if VERSION > v"1.2.0-DEV.0" # Sort one more time to handle GenericLinearAlgebra not being updated
31+
sort!(roots, by=LinearAlgebra.eigsortby)
32+
end
3133
for k=1:length(roots)
3234
r = roots[k]
3335

0 commit comments

Comments
 (0)