diff --git a/Slides/math_helper.py b/Slides/math_helper.py index c96ff9c..e0131ec 100644 --- a/Slides/math_helper.py +++ b/Slides/math_helper.py @@ -1,116 +1,132 @@ #!/usr/bin/env pyton ################################################################ import numpy as np import presentation_helper as ph from sympy import latex from IPython.display import display, Math ################################################################ def print_latex(sstr, *argv): args = [] # print argv for a in argv: try: ff = a._repr_latex_() ff = ff[2:-2] except Exception: # print(e) ff = latex(a) args.append(ff) sstr = sstr.format(*args) # print (sstr) display(Math(latex(sstr))) ################################################################ class ColoredMatrix: color_def = { 'b': 'blue', 'g': 'green', 'r': 'red', 'p': 'purple' } def __init__(self, mat): if isinstance(mat, ColoredMatrix): self.mat = mat.mat else: self.mat = mat - self.colors = np.zeros(self.mat.shape, dtype=str) + self.colors = np.zeros(self.mat.shape, dtype='S10') + self.alternative = np.zeros(self.mat.shape, dtype='S10') self.sym = False def __len__(self): return self.mat.__len__() def dot(self, mat): return self.mat.dot(mat) + def __mul__(self, n): + return self.mat.__mul__(n) + + def __rmul__(self, n): + return n*self.mat + def evalf(self, *args): mat_eval = self.mat.evalf(*args) new_mat = ColoredMatrix(mat_eval) new_mat.colors = self.colors.copy() return new_mat def __getitem__(self, index): return self.mat.__getitem__(index) def __setitem__(self, index, value): return self.mat.__setitem__(index, value) + def _get_coeff(self, i, j=0): + if self.alternative[i, j] != '': + return self.alternative[i, j] + else: + return latex(self.mat[i, j]) + def _colored_coeff(self, i, j=0): if self.sym is True: if i == self.mat.shape[0]-1 and j == 0: return 'Sym.' elif i > j: return '' if self.colors[i, j] in self.color_def: color = self.color_def[self.colors[i, j]] else: color = 'black' - return (r'{\color{' + color + '}{' + - latex(self.mat[i, j]) - + '}}') + + coeff = self._get_coeff(i, j) + if coeff == '': + return '' + + return (r'{\color{' + color + '}{' + coeff + '}}') def _repr_latex_(self): m = self.mat.shape[0] if len(self.mat.shape) > 1 and self.mat.shape[1] > 1: n = self.mat.shape[1] result = '' for i in range(0, m): row = [] for j in range(0, n): row.append(self._colored_coeff(i, j)) result += ' & '.join(row) if i < m-1: result += r'\\' result = (r'\left[\begin{array}{' + 'c'*n + '}' + result + r'\end{array}\right]') else: rows = [] for i in range(0, m): rows.append(self._colored_coeff(i)) result = r'\\'.join(rows) result = (r'\left\{\begin{matrix}' + result + r'\end{matrix}\right\}') return '$$' + result + '$$' ################################################################ def init_printing(): ph.registerFormatter(ColoredMatrix, 'image/png', ph._print_latex_png) ################################################################ init_printing() diff --git a/Slides/templates/reveal.tpl b/Slides/templates/reveal.tpl index 41ecb11..9ac517a 100644 --- a/Slides/templates/reveal.tpl +++ b/Slides/templates/reveal.tpl @@ -1,100 +1,100 @@ {% from 'mathjax.tpl' import mathjax %} {%- macro init_reveal(reveal_prefix) -%} {{ mathjax() }} {%- endmacro %} {%- macro load_reveal(reveal_prefix) -%} {%- endmacro %}