Skip to content

Commit f8a70b1

Browse files
committed
Loosen a lot of unneeded type restrictions and unneeded boilerplate
1 parent 369a5e5 commit f8a70b1

7 files changed

Lines changed: 28 additions & 27 deletions

File tree

src/analysis.jl

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -192,13 +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::AbstractMatrix, B::AbstractMatrix, C::AbstractMatrix, D::AbstractMatrix)
201-
T = promote_type(eltype(A), eltype(B), eltype(C), eltype(D))
200+
function tzero(A::AbstractMatrix{T}, B::AbstractMatrix{T}, C::AbstractMatrix{T}, D::AbstractMatrix{T}) where {T <: Union{AbstractFloat,Complex{<:AbstractFloat}}#= For eps(T) =#}
202201
# Balance the system
203202
A, B, C = balance_statespace(A, B, C)
204203

@@ -233,8 +232,7 @@ function tzero(A::AbstractMatrix, B::AbstractMatrix, C::AbstractMatrix, D::Abstr
233232
return zs
234233
end
235234

236-
reduce_sys(A::AbstractMatrix, B::AbstractMatrix, C::AbstractMatrix, D::AbstractMatrix, meps::AbstractFloat) =
237-
reduce_sys(promote(A,B,C,D)..., meps)
235+
238236
"""
239237
Implements REDUCE in the Emami-Naeini & Van Dooren paper. Returns transformed
240238
A, B, C, D matrices. These are empty if there are no zeros.

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: 5 additions & 5 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},
190+
function ltitr(A::AbstractMatrix{T}, B::AbstractMatrix{T}, u::AbstractVecOrMat,
191191
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,
202+
function ltitr(A::AbstractMatrix{T}, B::AbstractMatrix{T}, u::Function, t,
203203
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)