Skip to content

Commit 6f7cbfa

Browse files
committed
update transfer function repr() + signal/system names
1 parent 5076298 commit 6f7cbfa

2 files changed

Lines changed: 43 additions & 26 deletions

File tree

control/tests/xferfcn_test.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,24 +1086,34 @@ def test_latex_repr(self):
10861086
@pytest.mark.parametrize(
10871087
"Hargs, ref",
10881088
[(([-1., 4.], [1., 3., 5.]),
1089-
"TransferFunction(array([-1., 4.]), array([1., 3., 5.]))"),
1089+
"TransferFunction(\n"
1090+
"array([-1., 4.]),\n"
1091+
"array([1., 3., 5.]),\n"
1092+
"outputs=1, inputs=1)"),
10901093
(([2., 3., 0.], [1., -3., 4., 0], 2.0),
1091-
"TransferFunction(array([2., 3., 0.]),"
1092-
" array([ 1., -3., 4., 0.]), 2.0)"),
1093-
1094+
"TransferFunction(\n"
1095+
"array([2., 3., 0.]),\n"
1096+
"array([ 1., -3., 4., 0.]),\n"
1097+
"dt=2.0,\n"
1098+
"outputs=1, inputs=1)"),
10941099
(([[[0, 1], [2, 3]], [[4, 5], [6, 7]]],
10951100
[[[6, 7], [4, 5]], [[2, 3], [0, 1]]]),
1096-
"TransferFunction([[array([1]), array([2, 3])],"
1097-
" [array([4, 5]), array([6, 7])]],"
1098-
" [[array([6, 7]), array([4, 5])],"
1099-
" [array([2, 3]), array([1])]])"),
1101+
"TransferFunction(\n"
1102+
"[[array([1]), array([2, 3])],\n"
1103+
" [array([4, 5]), array([6, 7])]],\n"
1104+
"[[array([6, 7]), array([4, 5])],\n"
1105+
" [array([2, 3]), array([1])]],\n"
1106+
"outputs=2, inputs=2)"),
11001107
(([[[0, 1], [2, 3]], [[4, 5], [6, 7]]],
11011108
[[[6, 7], [4, 5]], [[2, 3], [0, 1]]],
11021109
0.5),
1103-
"TransferFunction([[array([1]), array([2, 3])],"
1104-
" [array([4, 5]), array([6, 7])]],"
1105-
" [[array([6, 7]), array([4, 5])],"
1106-
" [array([2, 3]), array([1])]], 0.5)")
1110+
"TransferFunction(\n"
1111+
"[[array([1]), array([2, 3])],\n"
1112+
" [array([4, 5]), array([6, 7])]],\n"
1113+
"[[array([6, 7]), array([4, 5])],\n"
1114+
" [array([2, 3]), array([1])]],\n"
1115+
"dt=0.5,\n"
1116+
"outputs=2, inputs=2)"),
11071117
])
11081118
def test_repr(self, Hargs, ref):
11091119
"""Test __repr__ printout."""

control/xferfcn.py

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ def __init__(self, *args, **kwargs):
194194
% type(args[0]))
195195
num = args[0].num
196196
den = args[0].den
197+
# TODO: copy over signal names
197198

198199
else:
199200
raise TypeError("Needs 1, 2 or 3 arguments; received %i."
@@ -491,28 +492,34 @@ def __str__(self, var=None):
491492
def __repr__(self):
492493
"""Print transfer function in loadable form."""
493494
if self.issiso():
494-
return "TransferFunction({num}, {den}{dt})".format(
495-
num=self.num_array[0, 0].__repr__(),
496-
den=self.den_array[0, 0].__repr__(),
497-
dt=', {}'.format(self.dt) if isdtime(self, strict=True)
498-
else '')
495+
out = "TransferFunction(\n{num},\n{den}".format(
496+
num=self.num[0][0].__repr__(), den=self.den[0][0].__repr__())
499497
else:
500-
out = "TransferFunction(["
498+
out = "TransferFunction(\n["
501499
for entry in [self.num_array, self.den_array]:
502500
for i in range(self.noutputs):
503-
out += "[" if i == 0 else " ["
501+
out += "[" if i == 0 else "\n ["
502+
linelen = 0
504503
for j in range(self.ninputs):
505504
out += ", " if j != 0 else ""
506505
numstr = np.array_repr(entry[i, j])
506+
if linelen + len(numstr) > 72:
507+
out += "\n "
508+
linelen = 0
507509
out += numstr
510+
linelen += len(numstr)
508511
out += "]," if i < self.noutputs - 1 else "]"
509-
out += "], [" if entry is self.num_array else "]"
512+
out += "],\n[" if entry is self.num_array else "]"
510513

511-
if config.defaults['control.default_dt'] != self.dt:
512-
out += ", {dt}".format(
513-
dt='None' if self.dt is None else self.dt)
514-
out += ")"
515-
return out
514+
if config.defaults['control.default_dt'] != self.dt:
515+
out += ",\ndt={dt}".format(
516+
dt='None' if self.dt is None else self.dt)
517+
518+
if len(labels := self._label_repr()) > 0:
519+
out += ",\n" + labels
520+
521+
out += ")"
522+
return out
516523

517524
def _repr_latex_(self, var=None):
518525
"""LaTeX representation of transfer function, for Jupyter notebook."""
@@ -777,7 +784,7 @@ def __getitem__(self, key):
777784
indices[0], self.output_labels, slice_to_list=True)
778785
inpdx, inputs = _process_subsys_index(
779786
indices[1], self.input_labels, slice_to_list=True)
780-
787+
781788
# Construct the transfer function for the subsyste
782789
num = _create_poly_array((len(outputs), len(inputs)))
783790
den = _create_poly_array(num.shape)

0 commit comments

Comments
 (0)