Skip to content

Commit c7e1e10

Browse files
committed
Add _repr_latex_ for Jupyter notebooks
1 parent e4e7a0d commit c7e1e10

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

control/xferfcn.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,43 @@ 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+
if var is None:
276+
# ! TODO: replace with standard calls to lti functions
277+
var = 's' if self.dt is None or self.dt == 0 else 'z'
278+
279+
if mimo:
280+
outstr = r"$$\begin{bmatrix}"
281+
else:
282+
outstr = "$$"
283+
284+
for i in range(self.inputs):
285+
for j in range(self.outputs):
286+
# Convert the numerator and denominator polynomials to strings.
287+
numstr = _tf_polynomial_to_string(self.num[j][i], var=var)
288+
denstr = _tf_polynomial_to_string(self.den[j][i], var=var)
289+
290+
291+
outstr += r"\frac{" + numstr + "}{" + denstr + "}"
292+
if mimo and j < self.outputs - 1:
293+
outstr += "&"
294+
if mimo:
295+
outstr += r"\\ "
296+
297+
if mimo:
298+
outstr += r"\end{bmatrix}"
299+
300+
# See if this is a discrete time system with specific sampling time
301+
if not (self.dt is None) and type(self.dt) != bool and self.dt > 0:
302+
outstr += "\quad dt = " + self.dt.__str__()
303+
304+
outstr += "$$"
305+
306+
return outstr
307+
271308
def __neg__(self):
272309
"""Negate a transfer function."""
273310

0 commit comments

Comments
 (0)