Skip to content
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

FIX: transfer_to_state tripping over numerical noise #52

Merged
merged 1 commit into from
Apr 25, 2019
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
8 changes: 7 additions & 1 deletion harold/_classes.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import numpy as np
import warnings
from numpy import zeros_like, kron, ndarray, zeros, exp, convolve
from numpy import zeros_like, kron, ndarray, zeros, exp, convolve, spacing
from numpy.random import rand, choice
from numpy.linalg.linalg import _assertNdSquareness
from scipy.linalg import (eigvals, svdvals, block_diag, qz, norm, solve, expm,
Expand Down Expand Up @@ -2898,6 +2898,9 @@ def transfer_to_state(G, output='system'):
else:
# Watch out for full cancellation !!
NumOrEmpty, datanum = haroldpolydiv(num, den)
# Clean up the tiny entries
datanum[np.abs(datanum) < spacing(100.)] = 0.

# If all cancelled datanum is returned empty
if datanum.size == 0:
A = None
Expand Down Expand Up @@ -2934,6 +2937,9 @@ def transfer_to_state(G, output='system'):
pass # D[x,y] is already 0.
else:
NumOrEmpty, datanum = haroldpolydiv(datanum, dataden)
# Clean up the tiny entries
datanum[np.abs(datanum) < spacing(100.)] = 0.

# Case 3: If all cancelled datanum is returned empty
if np.count_nonzero(datanum) == 0:
D[x, y] = NumOrEmpty
Expand Down
20 changes: 17 additions & 3 deletions harold/tests/test_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
from numpy.testing import (assert_,
assert_equal,
assert_array_equal,
assert_raises,
assert_almost_equal,
assert_array_almost_equal)
from pytest import raises as assert_raises


def test_concatenate_state_matrices():
Expand Down Expand Up @@ -759,8 +759,8 @@ def test_random_state_model():
assert not G._isgain
assert not G._isSISO

G = random_state_model(5, 2, 4, stable=False)
assert np.any(G.poles.real > 0)
G = random_state_model(5, 2, 4, stable=True)
assert not (G.poles.real > 0).any()
G = random_state_model(11, stable=False, prob_dist=[0, 0, 0.5, 0.5])
assert_array_almost_equal(np.abs(G.poles.real), np.zeros(11))
assert np.any(G.poles.imag)
Expand Down Expand Up @@ -834,6 +834,20 @@ def test_transfer_to_state():
assert_array_almost_equal(Gss.c, des_c)
assert_array_almost_equal(Gss.d, np.zeros([2, 2]))

# reported in gh-#50
num = [[[61.79732492202783, 36.24988430260625, 0.7301196233698941],
[0.0377840674057878, 0.9974993795127982, 21.763622825733773]]]
den = [[[84.64, 18.4, 1.0], [1.0, 7.2, 144.0]]]

TF = transfer_to_state((num, den))
assert_array_almost_equal([-3.6-1.14472704e+01j,
-3.6+1.14472704e+01j,
-0.10869565-1.74405237e-07j,
-0.10869565+1.74405237e-07j,
],
np.sort(TF.poles))
assert TF.zeros.size == 0


def test_state_to_transfer():
G = State(-2*np.eye(2), np.eye(2), [[1, -3], [0, 0]], [[0, 1], [-1, 0]])
Expand Down