Skip to content

Relax comparison of floats in tests #818

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions control/tests/delay_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ def testTvalues(self, T, dendeg, numdeg, baseden, basenum):
refnum /= refden[0]
refden /= refden[0]
num, den = pade(T, dendeg, numdeg)
np.testing.assert_array_almost_equal_nulp(refden, den, nulp=2)
np.testing.assert_array_almost_equal_nulp(refnum, num, nulp=2)
np.testing.assert_array_almost_equal_nulp(refden, den, nulp=4)
np.testing.assert_array_almost_equal_nulp(refnum, num, nulp=4)

def testErrors(self):
"ValueError raised for invalid arguments"
Expand Down
12 changes: 6 additions & 6 deletions control/tests/flatsys_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -693,9 +693,9 @@ def test_response(self, xf, uf, Tf):

# Recompute using response()
response = traj.response(T, squeeze=False)
np.testing.assert_equal(T, response.time)
np.testing.assert_equal(u, response.inputs)
np.testing.assert_equal(x, response.states)
np.testing.assert_array_almost_equal(T, response.time)
np.testing.assert_array_almost_equal(u, response.inputs)
np.testing.assert_array_almost_equal(x, response.states)

@pytest.mark.parametrize(
"basis",
Expand All @@ -713,7 +713,7 @@ def test_basis_class(self, basis):
for j in range(basis.N):
coefs = np.zeros(basis.N)
coefs[j] = 1
np.testing.assert_equal(
np.testing.assert_array_almost_equal(
basis.eval(coefs, timepts),
basis.eval_deriv(j, 0, timepts))
else:
Expand All @@ -722,7 +722,7 @@ def test_basis_class(self, basis):
for j in range(basis.var_ncoefs(i)):
coefs = np.zeros(basis.var_ncoefs(i))
coefs[j] = 1
np.testing.assert_equal(
np.testing.assert_array_almost_equal(
basis.eval(coefs, timepts, var=i),
basis.eval_deriv(j, 0, timepts, var=i))

Expand All @@ -732,7 +732,7 @@ def test_basis_class(self, basis):
for j in range(basis.var_ncoefs(i)):
coefs = np.zeros(basis.N)
coefs[offset] = 1
np.testing.assert_equal(
np.testing.assert_array_almost_equal(
basis.eval(coefs, timepts)[i],
basis.eval_deriv(j, 0, timepts, var=i))
offset += 1
150 changes: 80 additions & 70 deletions control/tests/frd_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,122 +51,126 @@ def testOperators(self):
h1 = TransferFunction([1], [1, 2, 2])
h2 = TransferFunction([1], [0.1, 1])
omega = np.logspace(-1, 2, 10)
chkpts = omega[::3]
f1 = FRD(h1, omega)
f2 = FRD(h2, omega)

np.testing.assert_array_almost_equal(
(f1 + f2).frequency_response([0.1, 1.0, 10])[0],
(h1 + h2).frequency_response([0.1, 1.0, 10])[0])
(f1 + f2).frequency_response(chkpts)[0],
(h1 + h2).frequency_response(chkpts)[0])
np.testing.assert_array_almost_equal(
(f1 + f2).frequency_response([0.1, 1.0, 10])[1],
(h1 + h2).frequency_response([0.1, 1.0, 10])[1])
(f1 + f2).frequency_response(chkpts)[1],
(h1 + h2).frequency_response(chkpts)[1])
np.testing.assert_array_almost_equal(
(f1 - f2).frequency_response([0.1, 1.0, 10])[0],
(h1 - h2).frequency_response([0.1, 1.0, 10])[0])
(f1 - f2).frequency_response(chkpts)[0],
(h1 - h2).frequency_response(chkpts)[0])
np.testing.assert_array_almost_equal(
(f1 - f2).frequency_response([0.1, 1.0, 10])[1],
(h1 - h2).frequency_response([0.1, 1.0, 10])[1])
(f1 - f2).frequency_response(chkpts)[1],
(h1 - h2).frequency_response(chkpts)[1])

# multiplication and division
np.testing.assert_array_almost_equal(
(f1 * f2).frequency_response([0.1, 1.0, 10])[1],
(h1 * h2).frequency_response([0.1, 1.0, 10])[1])
(f1 * f2).frequency_response(chkpts)[1],
(h1 * h2).frequency_response(chkpts)[1])
np.testing.assert_array_almost_equal(
(f1 / f2).frequency_response([0.1, 1.0, 10])[1],
(h1 / h2).frequency_response([0.1, 1.0, 10])[1])
(f1 / f2).frequency_response(chkpts)[1],
(h1 / h2).frequency_response(chkpts)[1])

# with default conversion from scalar
np.testing.assert_array_almost_equal(
(f1 * 1.5).frequency_response([0.1, 1.0, 10])[1],
(h1 * 1.5).frequency_response([0.1, 1.0, 10])[1])
(f1 * 1.5).frequency_response(chkpts)[1],
(h1 * 1.5).frequency_response(chkpts)[1])
np.testing.assert_array_almost_equal(
(f1 / 1.7).frequency_response([0.1, 1.0, 10])[1],
(h1 / 1.7).frequency_response([0.1, 1.0, 10])[1])
(f1 / 1.7).frequency_response(chkpts)[1],
(h1 / 1.7).frequency_response(chkpts)[1])
np.testing.assert_array_almost_equal(
(2.2 * f2).frequency_response([0.1, 1.0, 10])[1],
(2.2 * h2).frequency_response([0.1, 1.0, 10])[1])
(2.2 * f2).frequency_response(chkpts)[1],
(2.2 * h2).frequency_response(chkpts)[1])
np.testing.assert_array_almost_equal(
(1.3 / f2).frequency_response([0.1, 1.0, 10])[1],
(1.3 / h2).frequency_response([0.1, 1.0, 10])[1])
(1.3 / f2).frequency_response(chkpts)[1],
(1.3 / h2).frequency_response(chkpts)[1])

def testOperatorsTf(self):
# get two SISO transfer functions
h1 = TransferFunction([1], [1, 2, 2])
h2 = TransferFunction([1], [0.1, 1])
omega = np.logspace(-1, 2, 10)
chkpts = omega[::3]
f1 = FRD(h1, omega)
f2 = FRD(h2, omega)
f2 # reference to avoid pyflakes error

np.testing.assert_array_almost_equal(
(f1 + h2).frequency_response([0.1, 1.0, 10])[0],
(h1 + h2).frequency_response([0.1, 1.0, 10])[0])
(f1 + h2).frequency_response(chkpts)[0],
(h1 + h2).frequency_response(chkpts)[0])
np.testing.assert_array_almost_equal(
(f1 + h2).frequency_response([0.1, 1.0, 10])[1],
(h1 + h2).frequency_response([0.1, 1.0, 10])[1])
(f1 + h2).frequency_response(chkpts)[1],
(h1 + h2).frequency_response(chkpts)[1])
np.testing.assert_array_almost_equal(
(f1 - h2).frequency_response([0.1, 1.0, 10])[0],
(h1 - h2).frequency_response([0.1, 1.0, 10])[0])
(f1 - h2).frequency_response(chkpts)[0],
(h1 - h2).frequency_response(chkpts)[0])
np.testing.assert_array_almost_equal(
(f1 - h2).frequency_response([0.1, 1.0, 10])[1],
(h1 - h2).frequency_response([0.1, 1.0, 10])[1])
(f1 - h2).frequency_response(chkpts)[1],
(h1 - h2).frequency_response(chkpts)[1])
# multiplication and division
np.testing.assert_array_almost_equal(
(f1 * h2).frequency_response([0.1, 1.0, 10])[1],
(h1 * h2).frequency_response([0.1, 1.0, 10])[1])
(f1 * h2).frequency_response(chkpts)[1],
(h1 * h2).frequency_response(chkpts)[1])
np.testing.assert_array_almost_equal(
(f1 / h2).frequency_response([0.1, 1.0, 10])[1],
(h1 / h2).frequency_response([0.1, 1.0, 10])[1])
(f1 / h2).frequency_response(chkpts)[1],
(h1 / h2).frequency_response(chkpts)[1])
# the reverse does not work

def testbdalg(self):
# get two SISO transfer functions
h1 = TransferFunction([1], [1, 2, 2])
h2 = TransferFunction([1], [0.1, 1])
omega = np.logspace(-1, 2, 10)
chkpts = omega[::3]
f1 = FRD(h1, omega)
f2 = FRD(h2, omega)

np.testing.assert_array_almost_equal(
(bdalg.series(f1, f2)).frequency_response([0.1, 1.0, 10])[0],
(bdalg.series(h1, h2)).frequency_response([0.1, 1.0, 10])[0])
(bdalg.series(f1, f2)).frequency_response(chkpts)[0],
(bdalg.series(h1, h2)).frequency_response(chkpts)[0])

np.testing.assert_array_almost_equal(
(bdalg.parallel(f1, f2)).frequency_response([0.1, 1.0, 10])[0],
(bdalg.parallel(h1, h2)).frequency_response([0.1, 1.0, 10])[0])
(bdalg.parallel(f1, f2)).frequency_response(chkpts)[0],
(bdalg.parallel(h1, h2)).frequency_response(chkpts)[0])

np.testing.assert_array_almost_equal(
(bdalg.feedback(f1, f2)).frequency_response([0.1, 1.0, 10])[0],
(bdalg.feedback(h1, h2)).frequency_response([0.1, 1.0, 10])[0])
(bdalg.feedback(f1, f2)).frequency_response(chkpts)[0],
(bdalg.feedback(h1, h2)).frequency_response(chkpts)[0])

np.testing.assert_array_almost_equal(
(bdalg.negate(f1)).frequency_response([0.1, 1.0, 10])[0],
(bdalg.negate(h1)).frequency_response([0.1, 1.0, 10])[0])
(bdalg.negate(f1)).frequency_response(chkpts)[0],
(bdalg.negate(h1)).frequency_response(chkpts)[0])

# append() and connect() not implemented for FRD objects
# np.testing.assert_array_almost_equal(
# (bdalg.append(f1, f2)).frequency_response([0.1, 1.0, 10])[0],
# (bdalg.append(h1, h2)).frequency_response([0.1, 1.0, 10])[0])
# (bdalg.append(f1, f2)).frequency_response(chkpts)[0],
# (bdalg.append(h1, h2)).frequency_response(chkpts)[0])
#
# f3 = bdalg.append(f1, f2, f2)
# h3 = bdalg.append(h1, h2, h2)
# Q = np.mat([ [1, 2], [2, -1] ])
# np.testing.assert_array_almost_equal(
# (bdalg.connect(f3, Q, [2], [1])).frequency_response([0.1, 1.0, 10])[0],
# (bdalg.connect(h3, Q, [2], [1])).frequency_response([0.1, 1.0, 10])[0])
# (bdalg.connect(f3, Q, [2], [1])).frequency_response(chkpts)[0],
# (bdalg.connect(h3, Q, [2], [1])).frequency_response(chkpts)[0])

def testFeedback(self):
h1 = TransferFunction([1], [1, 2, 2])
omega = np.logspace(-1, 2, 10)
chkpts = omega[::3]
f1 = FRD(h1, omega)
np.testing.assert_array_almost_equal(
f1.feedback(1).frequency_response([0.1, 1.0, 10])[0],
h1.feedback(1).frequency_response([0.1, 1.0, 10])[0])
f1.feedback(1).frequency_response(chkpts)[0],
h1.feedback(1).frequency_response(chkpts)[0])

# Make sure default argument also works
np.testing.assert_array_almost_equal(
f1.feedback().frequency_response([0.1, 1.0, 10])[0],
h1.feedback().frequency_response([0.1, 1.0, 10])[0])
f1.feedback().frequency_response(chkpts)[0],
h1.feedback().frequency_response(chkpts)[0])

def testFeedback2(self):
h2 = StateSpace([[-1.0, 0], [0, -2.0]], [[0.4], [0.1]],
Expand Down Expand Up @@ -197,13 +201,14 @@ def testMIMO(self):
[[1.0, 0.0], [0.0, 1.0]],
[[0.0, 0.0], [0.0, 0.0]])
omega = np.logspace(-1, 2, 10)
chkpts = omega[::3]
f1 = FRD(sys, omega)
np.testing.assert_array_almost_equal(
sys.frequency_response([0.1, 1.0, 10])[0],
f1.frequency_response([0.1, 1.0, 10])[0])
sys.frequency_response(chkpts)[0],
f1.frequency_response(chkpts)[0])
np.testing.assert_array_almost_equal(
sys.frequency_response([0.1, 1.0, 10])[1],
f1.frequency_response([0.1, 1.0, 10])[1])
sys.frequency_response(chkpts)[1],
f1.frequency_response(chkpts)[1])

@slycotonly
def testMIMOfb(self):
Expand All @@ -212,14 +217,15 @@ def testMIMOfb(self):
[[1.0, 0.0], [0.0, 1.0]],
[[0.0, 0.0], [0.0, 0.0]])
omega = np.logspace(-1, 2, 10)
chkpts = omega[::3]
f1 = FRD(sys, omega).feedback([[0.1, 0.3], [0.0, 1.0]])
f2 = FRD(sys.feedback([[0.1, 0.3], [0.0, 1.0]]), omega)
np.testing.assert_array_almost_equal(
f1.frequency_response([0.1, 1.0, 10])[0],
f2.frequency_response([0.1, 1.0, 10])[0])
f1.frequency_response(chkpts)[0],
f2.frequency_response(chkpts)[0])
np.testing.assert_array_almost_equal(
f1.frequency_response([0.1, 1.0, 10])[1],
f2.frequency_response([0.1, 1.0, 10])[1])
f1.frequency_response(chkpts)[1],
f2.frequency_response(chkpts)[1])

@slycotonly
def testMIMOfb2(self):
Expand All @@ -229,15 +235,16 @@ def testMIMOfb2(self):
np.array([[1.0, 0], [0, 0], [0, 1]]),
np.eye(3), np.zeros((3, 2)))
omega = np.logspace(-1, 2, 10)
chkpts = omega[::3]
K = np.array([[1, 0.3, 0], [0.1, 0, 0]])
f1 = FRD(sys, omega).feedback(K)
f2 = FRD(sys.feedback(K), omega)
np.testing.assert_array_almost_equal(
f1.frequency_response([0.1, 1.0, 10])[0],
f2.frequency_response([0.1, 1.0, 10])[0])
f1.frequency_response(chkpts)[0],
f2.frequency_response(chkpts)[0])
np.testing.assert_array_almost_equal(
f1.frequency_response([0.1, 1.0, 10])[1],
f2.frequency_response([0.1, 1.0, 10])[1])
f1.frequency_response(chkpts)[1],
f2.frequency_response(chkpts)[1])

@slycotonly
def testMIMOMult(self):
Expand All @@ -246,14 +253,15 @@ def testMIMOMult(self):
[[1.0, 0.0], [0.0, 1.0]],
[[0.0, 0.0], [0.0, 0.0]])
omega = np.logspace(-1, 2, 10)
chkpts = omega[::3]
f1 = FRD(sys, omega)
f2 = FRD(sys, omega)
np.testing.assert_array_almost_equal(
(f1*f2).frequency_response([0.1, 1.0, 10])[0],
(sys*sys).frequency_response([0.1, 1.0, 10])[0])
(f1*f2).frequency_response(chkpts)[0],
(sys*sys).frequency_response(chkpts)[0])
np.testing.assert_array_almost_equal(
(f1*f2).frequency_response([0.1, 1.0, 10])[1],
(sys*sys).frequency_response([0.1, 1.0, 10])[1])
(f1*f2).frequency_response(chkpts)[1],
(sys*sys).frequency_response(chkpts)[1])

@slycotonly
def testMIMOSmooth(self):
Expand All @@ -263,17 +271,18 @@ def testMIMOSmooth(self):
[[0.0, 0.0], [0.0, 0.0], [0.0, 0.0]])
sys2 = np.array([[1, 0, 0], [0, 1, 0]]) * sys
omega = np.logspace(-1, 2, 10)
chkpts = omega[::3]
f1 = FRD(sys, omega, smooth=True)
f2 = FRD(sys2, omega, smooth=True)
np.testing.assert_array_almost_equal(
(f1*f2).frequency_response([0.1, 1.0, 10])[0],
(sys*sys2).frequency_response([0.1, 1.0, 10])[0])
(f1*f2).frequency_response(chkpts)[0],
(sys*sys2).frequency_response(chkpts)[0])
np.testing.assert_array_almost_equal(
(f1*f2).frequency_response([0.1, 1.0, 10])[1],
(sys*sys2).frequency_response([0.1, 1.0, 10])[1])
(f1*f2).frequency_response(chkpts)[1],
(sys*sys2).frequency_response(chkpts)[1])
np.testing.assert_array_almost_equal(
(f1*f2).frequency_response([0.1, 1.0, 10])[2],
(sys*sys2).frequency_response([0.1, 1.0, 10])[2])
(f1*f2).frequency_response(chkpts)[2],
(sys*sys2).frequency_response(chkpts)[2])

def testAgainstOctave(self):
# with data from octave:
Expand All @@ -284,6 +293,7 @@ def testAgainstOctave(self):
np.array([[1.0, 0], [0, 0], [0, 1]]),
np.eye(3), np.zeros((3, 2)))
omega = np.logspace(-1, 2, 10)
chkpts = omega[::3]
f1 = FRD(sys, omega)
np.testing.assert_array_almost_equal(
(f1.frequency_response([1.0])[0] *
Expand Down