Skip to content

Commit 4c4c205

Browse files
authored
type stability fixes and tests for freqresp (#642)
* use getproperty instead of getfield for LQG * merge * merge * type stability fixes and tests for freqresp
1 parent f745e38 commit 4c4c205

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

src/freqresp.jl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ _freq(w, te::Discrete) = cis(w*te.Ts)
3939
@autovec () function freqresp(sys::AbstractStateSpace, w_vec::AbstractVector{W}) where W <: Real
4040
ny, nu = size(sys)
4141
T = promote_type(Complex{real(eltype(sys.A))}, Complex{W})
42+
PDT = PermutedDimsArray{T,3,(3,1,2),(2,3,1),Array{T,3}}
4243
if sys.nx == 0 # Only D-matrix
43-
return PermutedDimsArray(repeat(T.(sys.D), 1, 1, length(w_vec)), (3,1,2))
44+
return PDT(repeat(T.(sys.D), 1, 1, length(w_vec)))
4445
end
4546
local F, Q
4647
try
@@ -68,7 +69,7 @@ _freq(w, te::Discrete) = cis(w*te.Ts)
6869
ldiv!(A, Bc, shift = w) # B += (A - w*I)\B # solve (A-wI)X = B, storing result in B
6970
mul!(Ri, C, Bc, -1, 1) # use of 5-arg mul to subtract from D already in Ri. - rather than + since (A - w*I) instead of (w*I - A)
7071
end
71-
PermutedDimsArray(R, (3,1,2)) # PermutedDimsArray doesn't allocate to perform the permutation
72+
PDT(R) # PermutedDimsArray doesn't allocate to perform the permutation
7273
end
7374

7475
"""
@@ -82,8 +83,9 @@ freqresp_nohess
8283
ny, nu = size(sys)
8384
nx = sys.nx
8485
T = promote_type(Complex{real(eltype(sys.A))}, Complex{W})
86+
PDT = PermutedDimsArray{T,3,(3,1,2),(2,3,1),Array{T,3}}
8587
if nx == 0 # Only D-matrix
86-
return PermutedDimsArray(repeat(T.(sys.D), 1, 1, length(w_vec)), (3,1,2))
88+
return PDT(repeat(T.(sys.D), 1, 1, length(w_vec)))
8789
end
8890
A,B,C,D = ssdata(sys)
8991
te = sys.timeevol
@@ -100,7 +102,7 @@ freqresp_nohess
100102
Bc = Ac \ B # Bc = (A - w*I)\B # avoid inplace to handle sparse matrices etc.
101103
mul!(Ri, C, Bc, -1, 1) # use of 5-arg mul to subtract from D already in Ri. - rather than + since (A - w*I) instead of (w*I - A)
102104
end
103-
PermutedDimsArray(R, (3,1,2)) # PermutedDimsArray doesn't allocate to perform the permutation
105+
PDT(R) # PermutedDimsArray doesn't allocate to perform the permutation
104106
end
105107

106108

test/test_freqresp.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ 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],
1010
@test evalfr(H, -5) == [0.0 -0.5; Inf 2.0]
1111
@test evalfr(H, -1) == [0.0 -0.3; 0.0 0.4]
1212
@test evalfr(H, 0) [0.0 0.0; 0.2 1/3]
13+
@inferred evalfr(H, 0)
1314

1415
@test (@test_logs (:warn, "Got exception SingularException(4), returning Inf") evalfr(G, -6)) == [Inf Inf; Inf Inf]
1516
@test (@test_logs (:warn, "Got exception SingularException(1), returning Inf") evalfr(G, -5)) == [Inf Inf; Inf Inf]
@@ -33,6 +34,9 @@ resp1 = ones(ComplexF64, length(w), 1, 1)
3334
@test freqresp(sys1s, w) == resp1
3435
@test freqresp(G1, w) == resp1
3536
@test freqresp(H1, w) == resp1
37+
@inferred freqresp(sys1, w)
38+
@inferred freqresp(G1, w)
39+
@inferred freqresp(H1, w)
3640

3741
for G in [2, 2I, 2I(2), randn(2,2)]
3842
@test dcgain(G) == G
@@ -58,6 +62,11 @@ resp2 = reshape((im*w .+ 2)./(im*w .+ 1), length(w), 1, 1)
5862
@test freqresp(G2, w) == resp2
5963
@test freqresp(H2, w) == resp2
6064

65+
@inferred freqresp(sys2, w)
66+
@inferred freqresp(sys2s, w)
67+
@inferred freqresp(G2, w)
68+
@inferred freqresp(H2, w)
69+
6170

6271
## Complex-coefficient system
6372
sys3 = ss(-1+im, 1, (1-im), (1-im))

0 commit comments

Comments
 (0)