def Format(Fmode=True, Dmode=True, dop=1): """ Set modes for latex printer - Fmode: Suppress function arguments (True) Use sympy latex for functions (False) Dmode: Use compact form of derivatives (True) Use sympy latex for derivatives (False) ipy: Do not redirect ouput for ipython (True) Redirect output for latex post processing (False) and redirects printer output so that latex compiler can capture it. """ GaLatexPrinter.Dmode = Dmode GaLatexPrinter.Fmode = Fmode if metric.in_ipynb(): GaLatexPrinter.ipy = True else: GaLatexPrinter.ipy = False GaLatexPrinter.dop = dop GaLatexPrinter.latex_flg = True GaLatexPrinter.redirect() Basic.__str__ = lambda self: GaLatexPrinter().doprint(self) Matrix.__str__ = lambda self: GaLatexPrinter().doprint(self) Basic.__repr_ = lambda self: GaLatexPrinter().doprint(self) return
def Fmt(self, fmt=1, title=None): self.fmt = fmt if metric.in_ipynb(): return self latex_str = printer.GaLatexPrinter.latex(self) """ if printer.GaLatexPrinter.ipy: if title is None: if r'\begin{align*}' not in latex_str: latex_str = r'\begin{equation*} ' + latex_str + r' \end{equation*}' else: if r'\begin{align*}' not in latex_str: latex_str = r'\begin{equation*} ' + title + ' = ' + latex_str + r' \end{equation*}' else: latex_str = latex_str.replace(r'\begin{align*}', r'\begin{align*} ' + title) latex_str = latex_str.replace('&', '=&', 1) from IPython.core.display import display, Math display(Math(latex_str)) else: if title is not None: return title + ' = ' + latex_str else: return latex_str """ if title is not None: return title + ' = ' + latex_str else: return latex_str
def Fmt(self, fmt=1, title=None): if metric.in_ipynb(): return self latex_str = printer.GaLatexPrinter.latex(self) """ if printer.GaLatexPrinter.ipy: if title is None: if r'\begin{align*}' not in latex_str: latex_str = r'\begin{equation*} ' + latex_str + r' \end{equation*}' else: if r'\begin{align*}' not in latex_str: latex_str = r'\begin{equation*} ' + title + ' = ' + latex_str + r' \end{equation*}' else: latex_str = latex_str.replace(r'\begin{align*}', r'\begin{align*} ' + title) latex_str = latex_str.replace('&', '=&', 1) from IPython.core.display import display, Math display(Math(latex_str)) else: if title is not None: return title + ' = ' + latex_str else: return latex_str """ if title is not None: return title + ' = ' + latex_str else: return latex_str
def Fmt(obj,fmt=0): if isinstance(obj,(list,tuple)): n = len(obj) if isinstance(obj,list): ldelim = '[' rdelim = ']' else: ldelim = '(' rdelim = ')' if fmt == 1: latex_str = r' \left ' + ldelim + r' \begin{array}{' + n*'c' + '} ' for cell in obj: title = cell.title cell.title = None latex_cell = latex(cell) latex_cell = latex_cell.replace('\n', ' ') latex_cell= latex_cell.replace(r'\begin{equation*}', ' ') latex_cell= latex_cell.replace(r'\end{equation*}', ' ') if cell.fmt != 1: latex_cell= latex_cell.replace(r'\begin{align*}', r'\begin{array}{c} ') latex_cell= latex_cell.replace('&','') latex_cell= latex_cell.replace(r'\end{align*}', r'\\ \end{array} ') latex_str += latex_cell + ', & ' cell.title = title latex_str = latex_str[:-4] latex_str += r'\\ \end{array} \right ' + rdelim + ' \n' else: latex_str = '' i = 1 for cell in obj: title = cell.title cell.title = None latex_cell = latex(cell) latex_cell = latex_cell.replace('\n', ' ') latex_cell= latex_cell.replace(r'\begin{equation*}', ' ') latex_cell= latex_cell.replace(r'\end{equation*}', ' ') if cell.fmt != 1: latex_cell= latex_cell.replace(r'\begin{align*}', r'\begin{array}{c} ') latex_cell= latex_cell.replace('&','') latex_cell= latex_cell.replace(r'\end{align*}', r'\\ \end{array} ') cell.title = title if i == 1: latex_str += r'\begin{array}{c} \left ' + ldelim + r' ' + latex_cell + r', \right. \\ ' elif i == n: latex_str += r' \left. ' + latex_cell + r'\right ' + rdelim + r' \\ \end{array}' else: latex_str += r' ' + latex_cell + r', \\' i += 1 if metric.in_ipynb(): if r'\begin{align*}' not in latex_str: latex_str = r'\begin{equation*} ' + latex_str + r'\end{equation*}' return Latex(latex_str) else: return latex_str return obj
def Fmt(obj, fmt=0): if isinstance(obj, (list, tuple, dict)): n = len(obj) if isinstance(obj, list): ldelim = '[' rdelim = ']' elif isinstance(obj, dict): ldelim = r'\{' rdelim = r'\}' else: ldelim = '(' rdelim = ')' if fmt == 1: latex_str = r' \left ' + ldelim + r' \begin{array}{' + n * 'c' + '} ' for cell in obj: if isinstance(ojb, dict): cell.title = None latex_cell = latex(cell) + ' : ' + latex(obj[cell]) else: title = cell.title cell.title = None latex_cell = latex(cell) latex_cell = latex_cell.replace('\n', ' ') latex_cell = latex_cell.replace(r'\begin{equation*}', ' ') latex_cell = latex_cell.replace(r'\end{equation*}', ' ') if cell.fmt != 1: latex_cell = latex_cell.replace(r'\begin{align*}', r'\begin{array}{c} ') latex_cell = latex_cell.replace('&', '') latex_cell = latex_cell.replace(r'\end{align*}', r'\\ \end{array} ') latex_str += latex_cell + ', & ' cell.title = title latex_str = latex_str[:-4] latex_str += r'\\ \end{array} \right ' + rdelim + ' \n' else: latex_str = '' i = 1 for cell in obj: title = cell.title cell.title = None latex_cell = latex(cell) latex_cell = latex_cell.replace('\n', ' ') latex_cell = latex_cell.replace(r'\begin{equation*}', ' ') latex_cell = latex_cell.replace(r'\end{equation*}', ' ') if cell.fmt != 1: latex_cell = latex_cell.replace(r'\begin{align*}', r'\begin{array}{c} ') latex_cell = latex_cell.replace('&', '') latex_cell = latex_cell.replace(r'\end{align*}', r'\\ \end{array} ') cell.title = title if i == 1: latex_str += r'\begin{array}{c} \left ' + ldelim + r' ' + latex_cell + r', \right. \\ ' elif i == n: latex_str += r' \left. ' + latex_cell + r'\right ' + rdelim + r' \\ \end{array}' else: latex_str += r' ' + latex_cell + r', \\' i += 1 if metric.in_ipynb(): # For Ipython notebook if r'\begin{align*}' not in latex_str: latex_str = r'\begin{equation*} ' + latex_str + r'\end{equation*}' return Latex(latex_str) else: return latex_str return obj
import os import sys import StringIO import re from sympy import Matrix, Basic, S, C, Symbol, Function, Derivative from itertools import islice from sympy.printing.str import StrPrinter from sympy.printing.latex import LatexPrinter, accepted_latex_functions from sympy.core.function import _coeff_isneg from inspect import getouterframes, currentframe import ga import mv import lt import metric if metric.in_ipynb(): from IPython.display import Latex ip_cmds = \ """ $\\DeclareMathOperator{\Tr}{Tr} \\DeclareMathOperator{\Adj}{Adj} \\newcommand{\\bfrac}[2]{\\displaystyle\\frac{#1}{#2}} \\newcommand{\\lp}{\\left (} \\newcommand{\\rp}{\\right )} \\newcommand{\\paren}[1]{\\lp {#1} \\rp} \\newcommand{\\half}{\\frac{1}{2}} \\newcommand{\\llt}{\\left <} \\newcommand{\\rgt}{\\right >} \\newcommand{\\abs}[1]{\\left |{#1}\\right | } \\newcommand{\\pdiff}[2]{\\bfrac{\\partial {#1}}{\\partial {#2}}}
def Fmt(obj,fmt=0): if isinstance(obj,(list,tuple,dict)): n = len(obj) if isinstance(obj,list): ldelim = '[' rdelim = ']' elif isinstance(obj,dict): ldelim = r'\{' rdelim = r'\}' else: ldelim = '(' rdelim = ')' if fmt == 1: latex_str = r' \left ' + ldelim + r' \begin{array}{' + n*'c' + '} ' for cell in obj: if isinstance(obj,dict): #cell.title = None latex_cell = latex(cell) + ' : '+ latex(obj[cell]) else: #title = cell.title #cell.title = None latex_cell = latex(cell) latex_cell = latex_cell.replace('\n', ' ') latex_cell= latex_cell.replace(r'\begin{equation*}', ' ') latex_cell= latex_cell.replace(r'\end{equation*}', ' ') if cell.fmt != 1: latex_cell= latex_cell.replace(r'\begin{align*}', r'\begin{array}{c} ') latex_cell= latex_cell.replace('&','') latex_cell= latex_cell.replace(r'\end{align*}', r'\\ \end{array} ') latex_str += latex_cell + ', & ' #cell.title = title latex_str = latex_str[:-4] latex_str += r'\\ \end{array} \right ' + rdelim + ' \n' else: latex_str = '' i = 1 for cell in obj: #title = cell.title #cell.title = None latex_cell = latex(cell) latex_cell = latex_cell.replace('\n', ' ') latex_cell= latex_cell.replace(r'\begin{equation*}', ' ') latex_cell= latex_cell.replace(r'\end{equation*}', ' ') if GaLatexPrinter.fmt != 1: latex_cell= latex_cell.replace(r'\begin{align*}', r'\begin{array}{c} ') latex_cell= latex_cell.replace('&','') latex_cell= latex_cell.replace(r'\end{align*}', r'\\ \end{array} ') #cell.title = title if i == 1: latex_str += r'\begin{array}{c} \left ' + ldelim + r' ' + latex_cell + r', \right. \\ ' elif i == n: latex_str += r' \left. ' + latex_cell + r'\right ' + rdelim + r' \\ \end{array}' else: latex_str += r' ' + latex_cell + r', \\' i += 1 if metric.in_ipynb(): # For Ipython notebook if r'\begin{align*}' not in latex_str: latex_str = r'\begin{equation*} ' + latex_str + r'\end{equation*}' return Latex('$$ '+latex_str+' $$') else: return latex_str elif isinstance(obj,int): GaLatexPrinter.prev_fmt = GaLatexPrinter.fmt GaLatexPrinter.fmt = obj return else: raise TypeError(str(type(obj)) + ' not allowed arg type in Fmt')
from itertools import islice from sympy.printing.str import StrPrinter from sympy.printing.latex import LatexPrinter, accepted_latex_functions from sympy.core.function import _coeff_isneg from sympy.core.operations import AssocOp from sympy import init_printing from IPython.display import display, Latex, Math from sympy.interactive import printing from inspect import getouterframes, currentframe import ga import mv import lt import metric if metric.in_ipynb(): from IPython.display import Latex Format_cnt = 0 ip_cmds = \ """ $\\DeclareMathOperator{\Tr}{Tr} \\DeclareMathOperator{\Adj}{Adj} \\newcommand{\\bfrac}[2]{\\displaystyle\\frac{#1}{#2}} \\newcommand{\\lp}{\\left (} \\newcommand{\\rp}{\\right )} \\newcommand{\\paren}[1]{\\lp {#1} \\rp} \\newcommand{\\half}{\\frac{1}{2}} \\newcommand{\\llt}{\\left <} \\newcommand{\\rgt}{\\right >}