|
8 | 8 | import operator |
9 | 9 |
|
10 | 10 | import control as ct |
11 | | -from control import StateSpace, TransferFunction, rss, ss2tf, evalfr |
| 11 | +from control import StateSpace, TransferFunction, rss, evalfr |
| 12 | +from control import ss, ss2tf, tf, tf2ss |
12 | 13 | from control import isctime, isdtime, sample_system, defaults |
13 | 14 | from control.statesp import _convert_to_statespace |
14 | 15 | from control.xferfcn import _convert_to_transfer_function |
@@ -986,7 +987,7 @@ def test_repr(self, Hargs, ref): |
986 | 987 | np.testing.assert_array_almost_equal(H.num[p][m], H2.num[p][m]) |
987 | 988 | np.testing.assert_array_almost_equal(H.den[p][m], H2.den[p][m]) |
988 | 989 | assert H.dt == H2.dt |
989 | | - |
| 990 | + |
990 | 991 | def test_sample_named_signals(self): |
991 | 992 | sysc = ct.TransferFunction(1.1, (1, 2), inputs='u', outputs='y') |
992 | 993 |
|
@@ -1073,3 +1074,72 @@ def test_xferfcn_ndarray_precedence(op, tf, arr): |
1073 | 1074 | # Apply the operator to the array and transfer function |
1074 | 1075 | result = op(arr, tf) |
1075 | 1076 | assert isinstance(result, ct.TransferFunction) |
| 1077 | + |
| 1078 | + |
| 1079 | +@pytest.mark.parametrize( |
| 1080 | + "zeros, poles, gain, args, kwargs", [ |
| 1081 | + ([], [-1], 1, [], {}), |
| 1082 | + ([1, 2], [-1, -2, -3], 5, [], {}), |
| 1083 | + ([1, 2], [-1, -2, -3], 5, [], {'name': "sys"}), |
| 1084 | + ([1, 2], [-1, -2, -3], 5, [], {'inputs': ["in"], 'outputs': ["out"]}), |
| 1085 | + ([1, 2], [-1, -2, -3], 5, [0.1], {}), |
| 1086 | + (np.array([1, 2]), np.array([-1, -2, -3]), 5, [], {}), |
| 1087 | +]) |
| 1088 | +def test_zpk(zeros, poles, gain, args, kwargs): |
| 1089 | + # Create the transfer function |
| 1090 | + sys = ct.zpk(zeros, poles, gain, *args, **kwargs) |
| 1091 | + |
| 1092 | + # Make sure the poles and zeros match |
| 1093 | + np.testing.assert_equal(sys.zeros().sort(), zeros.sort()) |
| 1094 | + np.testing.assert_equal(sys.poles().sort(), poles.sort()) |
| 1095 | + |
| 1096 | + # Check to make sure the gain is OK |
| 1097 | + np.testing.assert_almost_equal( |
| 1098 | + gain, sys(0) * np.prod(-sys.poles()) / np.prod(-sys.zeros())) |
| 1099 | + |
| 1100 | + # Check time base |
| 1101 | + if args: |
| 1102 | + assert sys.dt == args[0] |
| 1103 | + |
| 1104 | + # Check inputs, outputs, name |
| 1105 | + input_labels = kwargs.get('inputs', []) |
| 1106 | + for i, label in enumerate(input_labels): |
| 1107 | + assert sys.input_labels[i] == label |
| 1108 | + |
| 1109 | + output_labels = kwargs.get('outputs', []) |
| 1110 | + for i, label in enumerate(output_labels): |
| 1111 | + assert sys.output_labels[i] == label |
| 1112 | + |
| 1113 | + if kwargs.get('name'): |
| 1114 | + assert sys.name == kwargs.get('name') |
| 1115 | + |
| 1116 | +@pytest.mark.parametrize("create, args, kwargs, convert", [ |
| 1117 | + (StateSpace, ([-1], [1], [1], [0]), {}, ss2tf), |
| 1118 | + (StateSpace, ([-1], [1], [1], [0]), {}, ss), |
| 1119 | + (StateSpace, ([-1], [1], [1], [0]), {}, tf), |
| 1120 | + (StateSpace, ([-1], [1], [1], [0]), dict(inputs='i', outputs='o'), ss2tf), |
| 1121 | + (StateSpace, ([-1], [1], [1], [0]), dict(inputs=1, outputs=1), ss2tf), |
| 1122 | + (StateSpace, ([-1], [1], [1], [0]), dict(inputs='i', outputs='o'), ss), |
| 1123 | + (StateSpace, ([-1], [1], [1], [0]), dict(inputs='i', outputs='o'), tf), |
| 1124 | + (TransferFunction, ([1], [1, 1]), {}, tf2ss), |
| 1125 | + (TransferFunction, ([1], [1, 1]), {}, tf), |
| 1126 | + (TransferFunction, ([1], [1, 1]), {}, ss), |
| 1127 | + (TransferFunction, ([1], [1, 1]), dict(inputs='i', outputs='o'), tf2ss), |
| 1128 | + (TransferFunction, ([1], [1, 1]), dict(inputs=1, outputs=1), tf2ss), |
| 1129 | + (TransferFunction, ([1], [1, 1]), dict(inputs='i', outputs='o'), tf), |
| 1130 | + (TransferFunction, ([1], [1, 1]), dict(inputs='i', outputs='o'), ss), |
| 1131 | +]) |
| 1132 | +def test_copy_names(create, args, kwargs, convert): |
| 1133 | + # Convert a system with no renaming |
| 1134 | + sys = create(*args, **kwargs) |
| 1135 | + cpy = convert(sys) |
| 1136 | + |
| 1137 | + assert cpy.input_labels == sys.input_labels |
| 1138 | + assert cpy.input_labels == sys.input_labels |
| 1139 | + if cpy.nstates is not None and sys.nstates is not None: |
| 1140 | + assert cpy.state_labels == sys.state_labels |
| 1141 | + |
| 1142 | + # Relabel inputs and outputs |
| 1143 | + cpy = convert(sys, inputs='myin', outputs='myout') |
| 1144 | + assert cpy.input_labels == ['myin'] |
| 1145 | + assert cpy.output_labels == ['myout'] |
0 commit comments