From c7e1e1040d8a29332ab36ab0fdc95591c03584c5 Mon Sep 17 00:00:00 2001 From: Carl Sandrock Date: Sun, 24 Feb 2019 17:48:33 +0200 Subject: [PATCH 1/3] Add _repr_latex_ for Jupyter notebooks --- control/xferfcn.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/control/xferfcn.py b/control/xferfcn.py index 44a038773..2c298e7b7 100644 --- a/control/xferfcn.py +++ b/control/xferfcn.py @@ -268,6 +268,43 @@ def __str__(self, var=None): # represent as string, makes display work for IPython __repr__ = __str__ + def _repr_latex_(self, var=None): + """LaTeX representation of the transfer function, for Jupyter notebook""" + + mimo = self.inputs > 1 or self.outputs > 1 + if var is None: + # ! TODO: replace with standard calls to lti functions + var = 's' if self.dt is None or self.dt == 0 else 'z' + + if mimo: + outstr = r"$$\begin{bmatrix}" + else: + outstr = "$$" + + for i in range(self.inputs): + for j in range(self.outputs): + # Convert the numerator and denominator polynomials to strings. + numstr = _tf_polynomial_to_string(self.num[j][i], var=var) + denstr = _tf_polynomial_to_string(self.den[j][i], var=var) + + + outstr += r"\frac{" + numstr + "}{" + denstr + "}" + if mimo and j < self.outputs - 1: + outstr += "&" + if mimo: + outstr += r"\\ " + + if mimo: + outstr += r"\end{bmatrix}" + + # See if this is a discrete time system with specific sampling time + if not (self.dt is None) and type(self.dt) != bool and self.dt > 0: + outstr += "\quad dt = " + self.dt.__str__() + + outstr += "$$" + + return outstr + def __neg__(self): """Negate a transfer function.""" From e6dccbffc9fd08006d207abbb14af23846e3a183 Mon Sep 17 00:00:00 2001 From: Carl Sandrock Date: Mon, 25 Feb 2019 05:18:39 +0200 Subject: [PATCH 2/3] Swap rows and columns in _repr_latex_ --- control/xferfcn.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/control/xferfcn.py b/control/xferfcn.py index 2c298e7b7..da615fadb 100644 --- a/control/xferfcn.py +++ b/control/xferfcn.py @@ -281,11 +281,11 @@ def _repr_latex_(self, var=None): else: outstr = "$$" - for i in range(self.inputs): - for j in range(self.outputs): + for i in range(self.outputs): + for j in range(self.inputs): # Convert the numerator and denominator polynomials to strings. - numstr = _tf_polynomial_to_string(self.num[j][i], var=var) - denstr = _tf_polynomial_to_string(self.den[j][i], var=var) + numstr = _tf_polynomial_to_string(self.num[i][j], var=var) + denstr = _tf_polynomial_to_string(self.den[i][j], var=var) outstr += r"\frac{" + numstr + "}{" + denstr + "}" From 6cbe0660c95d5d44aaf08ce9bf086ba059d450d1 Mon Sep 17 00:00:00 2001 From: Carl Sandrock Date: Thu, 28 Mar 2019 08:10:41 +0200 Subject: [PATCH 3/3] Build list instead of concatenating strings --- control/xferfcn.py | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/control/xferfcn.py b/control/xferfcn.py index da615fadb..1ef0661a5 100644 --- a/control/xferfcn.py +++ b/control/xferfcn.py @@ -272,14 +272,15 @@ def _repr_latex_(self, var=None): """LaTeX representation of the transfer function, for Jupyter notebook""" mimo = self.inputs > 1 or self.outputs > 1 + if var is None: # ! TODO: replace with standard calls to lti functions var = 's' if self.dt is None or self.dt == 0 else 'z' + out = ['$$'] + if mimo: - outstr = r"$$\begin{bmatrix}" - else: - outstr = "$$" + out.append(r"\begin{bmatrix}") for i in range(self.outputs): for j in range(self.inputs): @@ -287,23 +288,24 @@ def _repr_latex_(self, var=None): numstr = _tf_polynomial_to_string(self.num[i][j], var=var) denstr = _tf_polynomial_to_string(self.den[i][j], var=var) + out += [r"\frac{", numstr, "}{", denstr, "}"] - outstr += r"\frac{" + numstr + "}{" + denstr + "}" if mimo and j < self.outputs - 1: - outstr += "&" + out.append("&") + if mimo: - outstr += r"\\ " + out.append(r"\\") if mimo: - outstr += r"\end{bmatrix}" + out.append(r" \end{bmatrix}") # See if this is a discrete time system with specific sampling time if not (self.dt is None) and type(self.dt) != bool and self.dt > 0: - outstr += "\quad dt = " + self.dt.__str__() + out += ["\quad dt = ", str(self.dt)] - outstr += "$$" + out.append("$$") - return outstr + return ''.join(out) def __neg__(self): """Negate a transfer function."""