Repository navigation
Add slicing access for state-space models with tests #1012
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
Changes from 1 commit
6b61ed0
4dff649
c6ef9b4
a0fc6bc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -463,24 +463,38 @@ def test_append_tf(self): | |
| np.testing.assert_array_almost_equal(sys3c.A[:3, 3:], np.zeros((3, 2))) | ||
| np.testing.assert_array_almost_equal(sys3c.A[3:, :3], np.zeros((2, 3))) | ||
|
|
||
| def test_array_access_ss(self): | ||
|
|
||
| def test_array_access_ss_failure(self): | ||
| sys1 = StateSpace( | ||
| [[1., 2.], [3., 4.]], | ||
| [[5., 6.], [6., 8.]], | ||
| [[9., 10.], [11., 12.]], | ||
| [[13., 14.], [15., 16.]], 1, | ||
| inputs=['u0', 'u1'], outputs=['y0', 'y1']) | ||
| with pytest.raises(IOError): | ||
| sys1[0] | ||
|
|
||
| @pytest.mark.parametrize("outdx, inpdx", | ||
| [(0, 1), | ||
| (slice(0, 1, 1), 1), | ||
| (0, slice(1, 2, 1)), | ||
| (slice(0, 1, 1), slice(1, 2, 1))]) | ||
|
Comment on lines
+477
to
+486
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be nice to add some more general tests. For example, how about
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have added some more general tests in the new commit. I also found a bug in the StateSpace init function which I have corrected. The init function doesn't check if the dimensions of the D matrix are correct if the input/output names are given. |
||
| def test_array_access_ss(self, outdx, inpdx): | ||
| sys1 = StateSpace( | ||
| [[1., 2.], [3., 4.]], | ||
| [[5., 6.], [6., 8.]], | ||
| [[9., 10.], [11., 12.]], | ||
| [[13., 14.], [15., 16.]], 1, | ||
| inputs=['u0', 'u1'], outputs=['y0', 'y1']) | ||
|
|
||
| sys1_01 = sys1[0, 1] | ||
| sys1_01 = sys1[outdx, inpdx] | ||
| np.testing.assert_array_almost_equal(sys1_01.A, | ||
| sys1.A) | ||
| np.testing.assert_array_almost_equal(sys1_01.B, | ||
| sys1.B[:, 1:2]) | ||
| np.testing.assert_array_almost_equal(sys1_01.C, | ||
| sys1.C[0:1, :]) | ||
| np.testing.assert_array_almost_equal(sys1_01.D, | ||
| sys1.D[0, 1]) | ||
| sys1.D[0:1, 1:2]) | ||
|
|
||
| assert sys1.dt == sys1_01.dt | ||
| assert sys1_01.input_labels == ['u1'] | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,6 +47,8 @@ | |
|
|
||
| """ | ||
|
|
||
| from collections.abc import Iterable | ||
|
|
||
| # External function declarations | ||
| import numpy as np | ||
| from numpy import angle, array, empty, finfo, ndarray, ones, \ | ||
|
|
@@ -758,7 +760,12 @@ def __pow__(self, other): | |
| return (TransferFunction([1], [1]) / self) * (self**(other + 1)) | ||
|
|
||
| def __getitem__(self, key): | ||
| if not isinstance(key, Iterable) or len(key) != 2: | ||
| raise IOError('must provide indices of length 2 for state space') | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Change "state space" to "transfer functions".
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have updated in the latest commit. |
||
|
|
||
| key1, key2 = key | ||
| if not isinstance(key1, (int, slice)) or not isinstance(key2, (int, slice)): | ||
| raise TypeError(f"system indices must be integers or slices") | ||
|
|
||
| # pre-process | ||
| if isinstance(key1, int): | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was there a reason for this change? It looks like the updated final line goes past column 79, which we try not to do.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The change was needed since
sliceis not iterable. I have pushed a new commit with a maximum column length of 79.