Skip to content

Commit 9ca6833

Browse files
committed
add Parameters to NonlinearIOSystem.__str__() + avoid double empty lines
1 parent 964c3aa commit 9ca6833

6 files changed

Lines changed: 23 additions & 37 deletions

File tree

control/frdata.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ def __str__(self):
399399
"""String representation of the transfer function."""
400400

401401
mimo = self.ninputs > 1 or self.noutputs > 1
402-
outstr = [f"{InputOutputSystem.__str__(self)}"]
402+
outstr = [f"{InputOutputSystem.__str__(self)}", ""]
403403

404404
for i in range(self.ninputs):
405405
for j in range(self.noutputs):

control/iosys.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -276,12 +276,12 @@ def repr_format(self, value):
276276

277277
def __str__(self):
278278
"""String representation of an input/output object"""
279-
str = f"<{self.__class__.__name__}>: {self.name}\n"
280-
str += f"Inputs ({self.ninputs}): {self.input_labels}\n"
281-
str += f"Outputs ({self.noutputs}): {self.output_labels}\n"
279+
out = f"<{self.__class__.__name__}>: {self.name}"
280+
out += f"\nInputs ({self.ninputs}): {self.input_labels}"
281+
out += f"\nOutputs ({self.noutputs}): {self.output_labels}"
282282
if self.nstates is not None:
283-
str += f"States ({self.nstates}): {self.state_labels}"
284-
return str
283+
out += f"\nStates ({self.nstates}): {self.state_labels}"
284+
return out
285285

286286
def _label_repr(self, show_count=True):
287287
out, count = "", 0

control/nlsys.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,13 @@ def __init__(self, updfcn, outfcn=None, params=None, **kwargs):
154154
self._current_params = {} if params is None else params.copy()
155155

156156
def __str__(self):
157-
return f"{InputOutputSystem.__str__(self)}\n\n" + \
157+
out = f"{InputOutputSystem.__str__(self)}"
158+
if len(self.params) > 1:
159+
out += f"\nParameters: {[p for p in self.params.keys()]}"
160+
out += "\n\n" + \
158161
f"Update: {self.updfcn}\n" + \
159162
f"Output: {self.outfcn}"
163+
return out
160164

161165
# Return the value of a static nonlinear system
162166
def __call__(sys, u, params=None, squeeze=None):
@@ -1368,7 +1372,7 @@ def nlsys(updfcn, outfcn=None, **kwargs):
13681372
Examples
13691373
--------
13701374
>>> def kincar_update(t, x, u, params):
1371-
... l = params.get('l', 1) # wheelbase
1375+
... l = params['l'] # wheelbase
13721376
... return np.array([
13731377
... np.cos(x[2]) * u[0], # x velocity
13741378
... np.sin(x[2]) * u[0], # y velocity
@@ -1379,7 +1383,8 @@ def nlsys(updfcn, outfcn=None, **kwargs):
13791383
... return x[0:2] # x, y position
13801384
>>>
13811385
>>> kincar = ct.nlsys(
1382-
... kincar_update, kincar_output, states=3, inputs=2, outputs=2)
1386+
... kincar_update, kincar_output, states=3, inputs=2, outputs=2,
1387+
... params={'l': 1})
13831388
>>>
13841389
>>> timepts = np.linspace(0, 10)
13851390
>>> response = ct.input_output_response(

control/statesp.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1596,11 +1596,6 @@ def ss(*args, **kwargs):
15961596
System name (used for specifying signals). If unspecified, a generic
15971597
name <sys[id]> is generated with a unique integer id.
15981598
1599-
Returns
1600-
-------
1601-
out: StateSpace
1602-
Linear input/output system.
1603-
16041599
Raises
16051600
------
16061601
ValueError

control/tests/nlsys_test.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
# Basic test of nlsys()
2020
def test_nlsys_basic():
2121
def kincar_update(t, x, u, params):
22-
l = params.get('l', 1) # wheelbase
22+
l = params['l'] # wheelbase
2323
return np.array([
2424
np.cos(x[2]) * u[0], # x velocity
2525
np.sin(x[2]) * u[0], # y velocity
@@ -33,10 +33,11 @@ def kincar_output(t, x, u, params):
3333
kincar_update, kincar_output,
3434
states=['x', 'y', 'theta'],
3535
inputs=2, input_prefix='U',
36-
outputs=2)
36+
outputs=2, params={'l': 1})
3737
assert kincar.input_labels == ['U[0]', 'U[1]']
3838
assert kincar.output_labels == ['y[0]', 'y[1]']
3939
assert kincar.state_labels == ['x', 'y', 'theta']
40+
assert kincar.params == {'l': 1}
4041

4142

4243
# Test nonlinear initial, step, and forced response

control/tests/xferfcn_test.py

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -886,9 +886,9 @@ def test_printing(self):
886886

887887
@pytest.mark.parametrize(
888888
"args, output",
889-
[(([0], [1]), "\n0\n-\n1\n"),
890-
(([1.0001], [-1.1111]), "\n 1\n------\n-1.111\n"),
891-
(([0, 1], [0, 1.]), "\n1\n-\n1\n"),
889+
[(([0], [1]), "0\n-\n1\n"),
890+
(([1.0001], [-1.1111]), " 1\n------\n-1.111\n"),
891+
(([0, 1], [0, 1.]), "1\n-\n1\n"),
892892
])
893893
def test_printing_polynomial_const(self, args, output):
894894
"""Test _tf_polynomial_to_string for constant systems"""
@@ -897,9 +897,9 @@ def test_printing_polynomial_const(self, args, output):
897897
@pytest.mark.parametrize(
898898
"args, outputfmt",
899899
[(([1, 0], [2, 1]),
900-
"\n {var}\n-------\n2 {var} + 1\n{dtstring}"),
900+
" {var}\n-------\n2 {var} + 1\n{dtstring}"),
901901
(([2, 0, -1], [1, 0, 0, 1.2]),
902-
"\n2 {var}^2 - 1\n---------\n{var}^3 + 1.2\n{dtstring}")])
902+
"2 {var}^2 - 1\n---------\n{var}^3 + 1.2\n{dtstring}")])
903903
@pytest.mark.parametrize("var, dt, dtstring",
904904
[("s", None, ''),
905905
("z", True, ''),
@@ -919,57 +919,46 @@ def test_printing_mimo(self):
919919
@pytest.mark.parametrize(
920920
"zeros, poles, gain, output",
921921
[([0], [-1], 1,
922-
'\n'
923922
' s\n'
924923
'-----\n'
925924
's + 1\n'),
926925
([-1], [-1], 1,
927-
'\n'
928926
's + 1\n'
929927
'-----\n'
930928
's + 1\n'),
931929
([-1], [1], 1,
932-
'\n'
933930
's + 1\n'
934931
'-----\n'
935932
's - 1\n'),
936933
([1], [-1], 1,
937-
'\n'
938934
's - 1\n'
939935
'-----\n'
940936
's + 1\n'),
941937
([-1], [-1], 2,
942-
'\n'
943938
'2 (s + 1)\n'
944939
'---------\n'
945940
' s + 1\n'),
946941
([-1], [-1], 0,
947-
'\n'
948942
'0\n'
949943
'-\n'
950944
'1\n'),
951945
([-1], [1j, -1j], 1,
952-
'\n'
953946
' s + 1\n'
954947
'-----------------\n'
955948
'(s - 1j) (s + 1j)\n'),
956949
([4j, -4j], [2j, -2j], 2,
957-
'\n'
958950
'2 (s - 4j) (s + 4j)\n'
959951
'-------------------\n'
960952
' (s - 2j) (s + 2j)\n'),
961953
([1j, -1j], [-1, -4], 2,
962-
'\n'
963954
'2 (s - 1j) (s + 1j)\n'
964955
'-------------------\n'
965956
' (s + 1) (s + 4)\n'),
966957
([1], [-1 + 1j, -1 - 1j], 1,
967-
'\n'
968958
' s - 1\n'
969959
'-------------------------\n'
970960
'(s + (1-1j)) (s + (1+1j))\n'),
971961
([1], [1 + 1j, 1 - 1j], 1,
972-
'\n'
973962
' s - 1\n'
974963
'-------------------------\n'
975964
'(s - (1+1j)) (s - (1-1j))\n'),
@@ -983,17 +972,14 @@ def test_printing_zpk(self, zeros, poles, gain, output):
983972
@pytest.mark.parametrize(
984973
"zeros, poles, gain, format, output",
985974
[([1], [1 + 1j, 1 - 1j], 1, ".2f",
986-
'\n'
987975
' 1.00\n'
988976
'-------------------------------------\n'
989977
'(s + (1.00-1.41j)) (s + (1.00+1.41j))\n'),
990978
([1], [1 + 1j, 1 - 1j], 1, ".3f",
991-
'\n'
992979
' 1.000\n'
993980
'-----------------------------------------\n'
994981
'(s + (1.000-1.414j)) (s + (1.000+1.414j))\n'),
995982
([1], [1 + 1j, 1 - 1j], 1, ".6g",
996-
'\n'
997983
' 1\n'
998984
'-------------------------------------\n'
999985
'(s + (1-1.41421j)) (s + (1+1.41421j))\n')
@@ -1012,8 +998,7 @@ def test_printing_zpk_format(self, zeros, poles, gain, format, output):
1012998
"num, den, output",
1013999
[([[[11], [21]], [[12], [22]]],
10141000
[[[1, -3, 2], [1, 1, -6]], [[1, 0, 1], [1, -1, -20]]],
1015-
('\n'
1016-
'Input 1 to output 1:\n'
1001+
('Input 1 to output 1:\n'
10171002
' 11\n'
10181003
'---------------\n'
10191004
'(s - 2) (s - 1)\n'

0 commit comments

Comments
 (0)