Skip to content

Commit c30746a

Browse files
authored
Merge pull request #276 from alchemyst/master
Add _repr_latex_ for Jupyter notebooks
2 parents 1dd0ca2 + 6cbe066 commit c30746a

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

control/xferfcn.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,45 @@ def __str__(self, var=None):
268268
# represent as string, makes display work for IPython
269269
__repr__ = __str__
270270

271+
def _repr_latex_(self, var=None):
272+
"""LaTeX representation of the transfer function, for Jupyter notebook"""
273+
274+
mimo = self.inputs > 1 or self.outputs > 1
275+
276+
if var is None:
277+
# ! TODO: replace with standard calls to lti functions
278+
var = 's' if self.dt is None or self.dt == 0 else 'z'
279+
280+
out = ['$$']
281+
282+
if mimo:
283+
out.append(r"\begin{bmatrix}")
284+
285+
for i in range(self.outputs):
286+
for j in range(self.inputs):
287+
# Convert the numerator and denominator polynomials to strings.
288+
numstr = _tf_polynomial_to_string(self.num[i][j], var=var)
289+
denstr = _tf_polynomial_to_string(self.den[i][j], var=var)
290+
291+
out += [r"\frac{", numstr, "}{", denstr, "}"]
292+
293+
if mimo and j < self.outputs - 1:
294+
out.append("&")
295+
296+
if mimo:
297+
out.append(r"\\")
298+
299+
if mimo:
300+
out.append(r" \end{bmatrix}")
301+
302+
# See if this is a discrete time system with specific sampling time
303+
if not (self.dt is None) and type(self.dt) != bool and self.dt > 0:
304+
out += ["\quad dt = ", str(self.dt)]
305+
306+
out.append("$$")
307+
308+
return ''.join(out)
309+
271310
def __neg__(self):
272311
"""Negate a transfer function."""
273312

0 commit comments

Comments
 (0)