Skip to content

Commit 56f227f

Browse files
authored
Merge pull request #818 from bnavigator/flatsys-float-tests
Relax comparison of floats in tests
2 parents 2f343be + 5352222 commit 56f227f

File tree

3 files changed

+88
-78
lines changed

3 files changed

+88
-78
lines changed

control/tests/delay_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ def testTvalues(self, T, dendeg, numdeg, baseden, basenum):
5252
refnum /= refden[0]
5353
refden /= refden[0]
5454
num, den = pade(T, dendeg, numdeg)
55-
np.testing.assert_array_almost_equal_nulp(refden, den, nulp=2)
56-
np.testing.assert_array_almost_equal_nulp(refnum, num, nulp=2)
55+
np.testing.assert_array_almost_equal_nulp(refden, den, nulp=4)
56+
np.testing.assert_array_almost_equal_nulp(refnum, num, nulp=4)
5757

5858
def testErrors(self):
5959
"ValueError raised for invalid arguments"

control/tests/flatsys_test.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -693,9 +693,9 @@ def test_response(self, xf, uf, Tf):
693693

694694
# Recompute using response()
695695
response = traj.response(T, squeeze=False)
696-
np.testing.assert_equal(T, response.time)
697-
np.testing.assert_equal(u, response.inputs)
698-
np.testing.assert_equal(x, response.states)
696+
np.testing.assert_array_almost_equal(T, response.time)
697+
np.testing.assert_array_almost_equal(u, response.inputs)
698+
np.testing.assert_array_almost_equal(x, response.states)
699699

700700
@pytest.mark.parametrize(
701701
"basis",
@@ -713,7 +713,7 @@ def test_basis_class(self, basis):
713713
for j in range(basis.N):
714714
coefs = np.zeros(basis.N)
715715
coefs[j] = 1
716-
np.testing.assert_equal(
716+
np.testing.assert_array_almost_equal(
717717
basis.eval(coefs, timepts),
718718
basis.eval_deriv(j, 0, timepts))
719719
else:
@@ -722,7 +722,7 @@ def test_basis_class(self, basis):
722722
for j in range(basis.var_ncoefs(i)):
723723
coefs = np.zeros(basis.var_ncoefs(i))
724724
coefs[j] = 1
725-
np.testing.assert_equal(
725+
np.testing.assert_array_almost_equal(
726726
basis.eval(coefs, timepts, var=i),
727727
basis.eval_deriv(j, 0, timepts, var=i))
728728

@@ -732,7 +732,7 @@ def test_basis_class(self, basis):
732732
for j in range(basis.var_ncoefs(i)):
733733
coefs = np.zeros(basis.N)
734734
coefs[offset] = 1
735-
np.testing.assert_equal(
735+
np.testing.assert_array_almost_equal(
736736
basis.eval(coefs, timepts)[i],
737737
basis.eval_deriv(j, 0, timepts, var=i))
738738
offset += 1

control/tests/frd_test.py

Lines changed: 80 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -51,122 +51,126 @@ def testOperators(self):
5151
h1 = TransferFunction([1], [1, 2, 2])
5252
h2 = TransferFunction([1], [0.1, 1])
5353
omega = np.logspace(-1, 2, 10)
54+
chkpts = omega[::3]
5455
f1 = FRD(h1, omega)
5556
f2 = FRD(h2, omega)
5657

5758
np.testing.assert_array_almost_equal(
58-
(f1 + f2).frequency_response([0.1, 1.0, 10])[0],
59-
(h1 + h2).frequency_response([0.1, 1.0, 10])[0])
59+
(f1 + f2).frequency_response(chkpts)[0],
60+
(h1 + h2).frequency_response(chkpts)[0])
6061
np.testing.assert_array_almost_equal(
61-
(f1 + f2).frequency_response([0.1, 1.0, 10])[1],
62-
(h1 + h2).frequency_response([0.1, 1.0, 10])[1])
62+
(f1 + f2).frequency_response(chkpts)[1],
63+
(h1 + h2).frequency_response(chkpts)[1])
6364
np.testing.assert_array_almost_equal(
64-
(f1 - f2).frequency_response([0.1, 1.0, 10])[0],
65-
(h1 - h2).frequency_response([0.1, 1.0, 10])[0])
65+
(f1 - f2).frequency_response(chkpts)[0],
66+
(h1 - h2).frequency_response(chkpts)[0])
6667
np.testing.assert_array_almost_equal(
67-
(f1 - f2).frequency_response([0.1, 1.0, 10])[1],
68-
(h1 - h2).frequency_response([0.1, 1.0, 10])[1])
68+
(f1 - f2).frequency_response(chkpts)[1],
69+
(h1 - h2).frequency_response(chkpts)[1])
6970

7071
# multiplication and division
7172
np.testing.assert_array_almost_equal(
72-
(f1 * f2).frequency_response([0.1, 1.0, 10])[1],
73-
(h1 * h2).frequency_response([0.1, 1.0, 10])[1])
73+
(f1 * f2).frequency_response(chkpts)[1],
74+
(h1 * h2).frequency_response(chkpts)[1])
7475
np.testing.assert_array_almost_equal(
75-
(f1 / f2).frequency_response([0.1, 1.0, 10])[1],
76-
(h1 / h2).frequency_response([0.1, 1.0, 10])[1])
76+
(f1 / f2).frequency_response(chkpts)[1],
77+
(h1 / h2).frequency_response(chkpts)[1])
7778

7879
# with default conversion from scalar
7980
np.testing.assert_array_almost_equal(
80-
(f1 * 1.5).frequency_response([0.1, 1.0, 10])[1],
81-
(h1 * 1.5).frequency_response([0.1, 1.0, 10])[1])
81+
(f1 * 1.5).frequency_response(chkpts)[1],
82+
(h1 * 1.5).frequency_response(chkpts)[1])
8283
np.testing.assert_array_almost_equal(
83-
(f1 / 1.7).frequency_response([0.1, 1.0, 10])[1],
84-
(h1 / 1.7).frequency_response([0.1, 1.0, 10])[1])
84+
(f1 / 1.7).frequency_response(chkpts)[1],
85+
(h1 / 1.7).frequency_response(chkpts)[1])
8586
np.testing.assert_array_almost_equal(
86-
(2.2 * f2).frequency_response([0.1, 1.0, 10])[1],
87-
(2.2 * h2).frequency_response([0.1, 1.0, 10])[1])
87+
(2.2 * f2).frequency_response(chkpts)[1],
88+
(2.2 * h2).frequency_response(chkpts)[1])
8889
np.testing.assert_array_almost_equal(
89-
(1.3 / f2).frequency_response([0.1, 1.0, 10])[1],
90-
(1.3 / h2).frequency_response([0.1, 1.0, 10])[1])
90+
(1.3 / f2).frequency_response(chkpts)[1],
91+
(1.3 / h2).frequency_response(chkpts)[1])
9192

9293
def testOperatorsTf(self):
9394
# get two SISO transfer functions
9495
h1 = TransferFunction([1], [1, 2, 2])
9596
h2 = TransferFunction([1], [0.1, 1])
9697
omega = np.logspace(-1, 2, 10)
98+
chkpts = omega[::3]
9799
f1 = FRD(h1, omega)
98100
f2 = FRD(h2, omega)
99101
f2 # reference to avoid pyflakes error
100102

101103
np.testing.assert_array_almost_equal(
102-
(f1 + h2).frequency_response([0.1, 1.0, 10])[0],
103-
(h1 + h2).frequency_response([0.1, 1.0, 10])[0])
104+
(f1 + h2).frequency_response(chkpts)[0],
105+
(h1 + h2).frequency_response(chkpts)[0])
104106
np.testing.assert_array_almost_equal(
105-
(f1 + h2).frequency_response([0.1, 1.0, 10])[1],
106-
(h1 + h2).frequency_response([0.1, 1.0, 10])[1])
107+
(f1 + h2).frequency_response(chkpts)[1],
108+
(h1 + h2).frequency_response(chkpts)[1])
107109
np.testing.assert_array_almost_equal(
108-
(f1 - h2).frequency_response([0.1, 1.0, 10])[0],
109-
(h1 - h2).frequency_response([0.1, 1.0, 10])[0])
110+
(f1 - h2).frequency_response(chkpts)[0],
111+
(h1 - h2).frequency_response(chkpts)[0])
110112
np.testing.assert_array_almost_equal(
111-
(f1 - h2).frequency_response([0.1, 1.0, 10])[1],
112-
(h1 - h2).frequency_response([0.1, 1.0, 10])[1])
113+
(f1 - h2).frequency_response(chkpts)[1],
114+
(h1 - h2).frequency_response(chkpts)[1])
113115
# multiplication and division
114116
np.testing.assert_array_almost_equal(
115-
(f1 * h2).frequency_response([0.1, 1.0, 10])[1],
116-
(h1 * h2).frequency_response([0.1, 1.0, 10])[1])
117+
(f1 * h2).frequency_response(chkpts)[1],
118+
(h1 * h2).frequency_response(chkpts)[1])
117119
np.testing.assert_array_almost_equal(
118-
(f1 / h2).frequency_response([0.1, 1.0, 10])[1],
119-
(h1 / h2).frequency_response([0.1, 1.0, 10])[1])
120+
(f1 / h2).frequency_response(chkpts)[1],
121+
(h1 / h2).frequency_response(chkpts)[1])
120122
# the reverse does not work
121123

122124
def testbdalg(self):
123125
# get two SISO transfer functions
124126
h1 = TransferFunction([1], [1, 2, 2])
125127
h2 = TransferFunction([1], [0.1, 1])
126128
omega = np.logspace(-1, 2, 10)
129+
chkpts = omega[::3]
127130
f1 = FRD(h1, omega)
128131
f2 = FRD(h2, omega)
129132

130133
np.testing.assert_array_almost_equal(
131-
(bdalg.series(f1, f2)).frequency_response([0.1, 1.0, 10])[0],
132-
(bdalg.series(h1, h2)).frequency_response([0.1, 1.0, 10])[0])
134+
(bdalg.series(f1, f2)).frequency_response(chkpts)[0],
135+
(bdalg.series(h1, h2)).frequency_response(chkpts)[0])
133136

134137
np.testing.assert_array_almost_equal(
135-
(bdalg.parallel(f1, f2)).frequency_response([0.1, 1.0, 10])[0],
136-
(bdalg.parallel(h1, h2)).frequency_response([0.1, 1.0, 10])[0])
138+
(bdalg.parallel(f1, f2)).frequency_response(chkpts)[0],
139+
(bdalg.parallel(h1, h2)).frequency_response(chkpts)[0])
137140

138141
np.testing.assert_array_almost_equal(
139-
(bdalg.feedback(f1, f2)).frequency_response([0.1, 1.0, 10])[0],
140-
(bdalg.feedback(h1, h2)).frequency_response([0.1, 1.0, 10])[0])
142+
(bdalg.feedback(f1, f2)).frequency_response(chkpts)[0],
143+
(bdalg.feedback(h1, h2)).frequency_response(chkpts)[0])
141144

142145
np.testing.assert_array_almost_equal(
143-
(bdalg.negate(f1)).frequency_response([0.1, 1.0, 10])[0],
144-
(bdalg.negate(h1)).frequency_response([0.1, 1.0, 10])[0])
146+
(bdalg.negate(f1)).frequency_response(chkpts)[0],
147+
(bdalg.negate(h1)).frequency_response(chkpts)[0])
145148

146149
# append() and connect() not implemented for FRD objects
147150
# np.testing.assert_array_almost_equal(
148-
# (bdalg.append(f1, f2)).frequency_response([0.1, 1.0, 10])[0],
149-
# (bdalg.append(h1, h2)).frequency_response([0.1, 1.0, 10])[0])
151+
# (bdalg.append(f1, f2)).frequency_response(chkpts)[0],
152+
# (bdalg.append(h1, h2)).frequency_response(chkpts)[0])
150153
#
151154
# f3 = bdalg.append(f1, f2, f2)
152155
# h3 = bdalg.append(h1, h2, h2)
153156
# Q = np.mat([ [1, 2], [2, -1] ])
154157
# np.testing.assert_array_almost_equal(
155-
# (bdalg.connect(f3, Q, [2], [1])).frequency_response([0.1, 1.0, 10])[0],
156-
# (bdalg.connect(h3, Q, [2], [1])).frequency_response([0.1, 1.0, 10])[0])
158+
# (bdalg.connect(f3, Q, [2], [1])).frequency_response(chkpts)[0],
159+
# (bdalg.connect(h3, Q, [2], [1])).frequency_response(chkpts)[0])
157160

158161
def testFeedback(self):
159162
h1 = TransferFunction([1], [1, 2, 2])
160163
omega = np.logspace(-1, 2, 10)
164+
chkpts = omega[::3]
161165
f1 = FRD(h1, omega)
162166
np.testing.assert_array_almost_equal(
163-
f1.feedback(1).frequency_response([0.1, 1.0, 10])[0],
164-
h1.feedback(1).frequency_response([0.1, 1.0, 10])[0])
167+
f1.feedback(1).frequency_response(chkpts)[0],
168+
h1.feedback(1).frequency_response(chkpts)[0])
165169

166170
# Make sure default argument also works
167171
np.testing.assert_array_almost_equal(
168-
f1.feedback().frequency_response([0.1, 1.0, 10])[0],
169-
h1.feedback().frequency_response([0.1, 1.0, 10])[0])
172+
f1.feedback().frequency_response(chkpts)[0],
173+
h1.feedback().frequency_response(chkpts)[0])
170174

171175
def testFeedback2(self):
172176
h2 = StateSpace([[-1.0, 0], [0, -2.0]], [[0.4], [0.1]],
@@ -197,13 +201,14 @@ def testMIMO(self):
197201
[[1.0, 0.0], [0.0, 1.0]],
198202
[[0.0, 0.0], [0.0, 0.0]])
199203
omega = np.logspace(-1, 2, 10)
204+
chkpts = omega[::3]
200205
f1 = FRD(sys, omega)
201206
np.testing.assert_array_almost_equal(
202-
sys.frequency_response([0.1, 1.0, 10])[0],
203-
f1.frequency_response([0.1, 1.0, 10])[0])
207+
sys.frequency_response(chkpts)[0],
208+
f1.frequency_response(chkpts)[0])
204209
np.testing.assert_array_almost_equal(
205-
sys.frequency_response([0.1, 1.0, 10])[1],
206-
f1.frequency_response([0.1, 1.0, 10])[1])
210+
sys.frequency_response(chkpts)[1],
211+
f1.frequency_response(chkpts)[1])
207212

208213
@slycotonly
209214
def testMIMOfb(self):
@@ -212,14 +217,15 @@ def testMIMOfb(self):
212217
[[1.0, 0.0], [0.0, 1.0]],
213218
[[0.0, 0.0], [0.0, 0.0]])
214219
omega = np.logspace(-1, 2, 10)
220+
chkpts = omega[::3]
215221
f1 = FRD(sys, omega).feedback([[0.1, 0.3], [0.0, 1.0]])
216222
f2 = FRD(sys.feedback([[0.1, 0.3], [0.0, 1.0]]), omega)
217223
np.testing.assert_array_almost_equal(
218-
f1.frequency_response([0.1, 1.0, 10])[0],
219-
f2.frequency_response([0.1, 1.0, 10])[0])
224+
f1.frequency_response(chkpts)[0],
225+
f2.frequency_response(chkpts)[0])
220226
np.testing.assert_array_almost_equal(
221-
f1.frequency_response([0.1, 1.0, 10])[1],
222-
f2.frequency_response([0.1, 1.0, 10])[1])
227+
f1.frequency_response(chkpts)[1],
228+
f2.frequency_response(chkpts)[1])
223229

224230
@slycotonly
225231
def testMIMOfb2(self):
@@ -229,15 +235,16 @@ def testMIMOfb2(self):
229235
np.array([[1.0, 0], [0, 0], [0, 1]]),
230236
np.eye(3), np.zeros((3, 2)))
231237
omega = np.logspace(-1, 2, 10)
238+
chkpts = omega[::3]
232239
K = np.array([[1, 0.3, 0], [0.1, 0, 0]])
233240
f1 = FRD(sys, omega).feedback(K)
234241
f2 = FRD(sys.feedback(K), omega)
235242
np.testing.assert_array_almost_equal(
236-
f1.frequency_response([0.1, 1.0, 10])[0],
237-
f2.frequency_response([0.1, 1.0, 10])[0])
243+
f1.frequency_response(chkpts)[0],
244+
f2.frequency_response(chkpts)[0])
238245
np.testing.assert_array_almost_equal(
239-
f1.frequency_response([0.1, 1.0, 10])[1],
240-
f2.frequency_response([0.1, 1.0, 10])[1])
246+
f1.frequency_response(chkpts)[1],
247+
f2.frequency_response(chkpts)[1])
241248

242249
@slycotonly
243250
def testMIMOMult(self):
@@ -246,14 +253,15 @@ def testMIMOMult(self):
246253
[[1.0, 0.0], [0.0, 1.0]],
247254
[[0.0, 0.0], [0.0, 0.0]])
248255
omega = np.logspace(-1, 2, 10)
256+
chkpts = omega[::3]
249257
f1 = FRD(sys, omega)
250258
f2 = FRD(sys, omega)
251259
np.testing.assert_array_almost_equal(
252-
(f1*f2).frequency_response([0.1, 1.0, 10])[0],
253-
(sys*sys).frequency_response([0.1, 1.0, 10])[0])
260+
(f1*f2).frequency_response(chkpts)[0],
261+
(sys*sys).frequency_response(chkpts)[0])
254262
np.testing.assert_array_almost_equal(
255-
(f1*f2).frequency_response([0.1, 1.0, 10])[1],
256-
(sys*sys).frequency_response([0.1, 1.0, 10])[1])
263+
(f1*f2).frequency_response(chkpts)[1],
264+
(sys*sys).frequency_response(chkpts)[1])
257265

258266
@slycotonly
259267
def testMIMOSmooth(self):
@@ -263,17 +271,18 @@ def testMIMOSmooth(self):
263271
[[0.0, 0.0], [0.0, 0.0], [0.0, 0.0]])
264272
sys2 = np.array([[1, 0, 0], [0, 1, 0]]) * sys
265273
omega = np.logspace(-1, 2, 10)
274+
chkpts = omega[::3]
266275
f1 = FRD(sys, omega, smooth=True)
267276
f2 = FRD(sys2, omega, smooth=True)
268277
np.testing.assert_array_almost_equal(
269-
(f1*f2).frequency_response([0.1, 1.0, 10])[0],
270-
(sys*sys2).frequency_response([0.1, 1.0, 10])[0])
278+
(f1*f2).frequency_response(chkpts)[0],
279+
(sys*sys2).frequency_response(chkpts)[0])
271280
np.testing.assert_array_almost_equal(
272-
(f1*f2).frequency_response([0.1, 1.0, 10])[1],
273-
(sys*sys2).frequency_response([0.1, 1.0, 10])[1])
281+
(f1*f2).frequency_response(chkpts)[1],
282+
(sys*sys2).frequency_response(chkpts)[1])
274283
np.testing.assert_array_almost_equal(
275-
(f1*f2).frequency_response([0.1, 1.0, 10])[2],
276-
(sys*sys2).frequency_response([0.1, 1.0, 10])[2])
284+
(f1*f2).frequency_response(chkpts)[2],
285+
(sys*sys2).frequency_response(chkpts)[2])
277286

278287
def testAgainstOctave(self):
279288
# with data from octave:
@@ -284,6 +293,7 @@ def testAgainstOctave(self):
284293
np.array([[1.0, 0], [0, 0], [0, 1]]),
285294
np.eye(3), np.zeros((3, 2)))
286295
omega = np.logspace(-1, 2, 10)
296+
chkpts = omega[::3]
287297
f1 = FRD(sys, omega)
288298
np.testing.assert_array_almost_equal(
289299
(f1.frequency_response([1.0])[0] *

0 commit comments

Comments
 (0)