Ticket #5711: trac_5711-enhanced-symbolic-typesetting-rebased_to_4.0.2.patch

File trac_5711-enhanced-symbolic-typesetting-rebased_to_4.0.2.patch, 10.0 KB (added by gmhossain, 11 years ago)
• sage/calculus/calculus.py

# HG changeset patch
# User Golam Mortuza Hossain <gmhossain@gmail.com>
# Date 1245756426 10800
# Node ID 75e9c68c3e93ab7b6593c00519cbabd3f281e6be
# Parent  2e793d2a0e123293b73eed40715e43185fd9ccfe
Enhances typsetting of Symbolic functions within Sage (rebased to Sage-4.0.2)

diff -r 2e793d2a0e12 -r 75e9c68c3e93 sage/calculus/calculus.py
 a if not isinstance(f, PrimitiveFunction): del functions[name] _limit = SFunction('limit') def dummy_limit(*args): """ This function is called to create formal wrappers of limits that args[i] = Integer(args[i]) return f.diff(*args) def dummy_integrate(*args): """ This function is called to create formal wrappers of integrals that Maxima can't compute: EXAMPLES:: sage: from sage.calculus.calculus import dummy_integrate sage: f(x) = function('f',x) sage: dummy_integrate(f(x), x) integrate(f(x), x) sage: a,b = var('a,b') sage: dummy_integrate(f(x), x, a, b) integrate(f(x), x, a, b) """ if len(args) == 4: return _integrate(args[0], var(repr(args[1])), SR(args[2]), SR(args[3])) else: return _integrate(args[0], var(repr(args[1]))) def dummy_laplace(*args): """ This function is called to create formal wrappers of laplace transforms that Maxima can't compute: EXAMPLES:: sage: from sage.calculus.calculus import dummy_laplace sage: s,t = var('s,t') sage: f(t) = function('f',t) sage: dummy_laplace(f(t),t,s) laplace(f(t), t, s) """ return _laplace(args[0], var(repr(args[1])), var(repr(args[2]))) def dummy_inverse_laplace(*args): """ This function is called to create formal wrappers of inverse laplace transforms that Maxima can't compute: EXAMPLES:: sage: from sage.calculus.calculus import dummy_inverse_laplace sage: s,t = var('s,t') sage: F(s) = function('F',s) sage: dummy_inverse_laplace(F(s),s,t) ilt(F(s), s, t) """ return _inverse_laplace(args[0], var(repr(args[1])), var(repr(args[2]))) ####################################################### # # Helper functions for printing latex expression # ####################################################### def _limit_latex_(*args): r""" Return latex expression for limit of a symbolic function. EXAMPLES:: sage: from sage.calculus.calculus import _limit_latex_ sage: var('x,a') (x, a) sage: f(x) = function('f',x) sage: _limit_latex_(f(x), x, a) '\\lim_{x \\to a}\\, f\\left(x\\right)' AUTHORS: - Golam Mortuza Hossain (2009-06-15) """ # Read f,x,a from arguments f = args[0] x = args[1] a = args[2] return "\\lim_{%s \\to %s}\\, %s"%(latex(x), latex(a), latex(f)) def _integrate_latex_(*args): r""" Return LaTeX expression for integration of a symbolic function. EXAMPLES:: sage: from sage.calculus.calculus import _integrate_latex_ sage: var('x,a,b') (x, a, b) sage: f(x) = function('f',x) sage: _integrate_latex_(f(x),x) '\\int f\\left(x\\right)\\,{d x}' sage: _integrate_latex_(f(x),x,a,b) '\\int_{a}^{b} f\\left(x\\right)\\,{d x}' AUTHORS: - Golam Mortuza Hossain (2009-06-22) """ f = args[0] x = args[1] # Check whether its a definite integral if len(args) == 4: a = args[2] b = args[3] return "\\int_{%s}^{%s} %s\\,{d %s}"%(latex(a), latex(b), latex(f), latex(x)) # Typeset as indefinite integral return "\\int %s\\,{d %s}"%(latex(f), latex(x)) def _laplace_latex_(*args): r""" Return LaTeX expression for Laplace transform of a symbolic function. EXAMPLES:: sage: from sage.calculus.calculus import _laplace_latex_ sage: var('s,t') (s, t) sage: f(t) = function('f',t) sage: _laplace_latex_(f(t),t,s) '\\mathcal{L}\\left(f\\left(t\\right), t, s\\right)' AUTHORS: - Golam Mortuza Hossain (2009-06-22) """ return "\\mathcal{L}\\left(%s\\right)"%(', '.join([latex(x) for x in args])) def _inverse_laplace_latex_(*args): r""" Return LaTeX expression for inverse Laplace transform of a symbolic function. EXAMPLES:: sage: from sage.calculus.calculus import _inverse_laplace_latex_ sage: var('s,t') (s, t) sage: F(s) = function('F',s) sage: _inverse_laplace_latex_(F(s),s,t) '\\mathcal{L}^{-1}\\left(F\\left(s\\right), s, t\\right)' AUTHORS: - Golam Mortuza Hossain (2009-06-22) """ return "\\mathcal{L}^{-1}\\left(%s\\right)"%(', '.join([latex(x) for x in args])) # Return un-evaluated expression as instances of SFunction class _limit = SFunction('limit', print_latex_func=_limit_latex_) _integrate = SFunction('integrate', print_latex_func=_integrate_latex_) _laplace = SFunction('laplace', print_latex_func=_laplace_latex_) _inverse_laplace = SFunction('ilt', print_latex_func=_inverse_laplace_latex_) ######################################i################ # have to do this here, otherwise maxima_tick catches it syms['limit'] = dummy_limit syms['diff'] = dummy_diff syms['integrate'] = dummy_integrate syms['laplace'] = dummy_laplace syms['ilt'] = dummy_inverse_laplace global is_simplified try:
• sage/misc/latex.py

diff -r 2e793d2a0e12 -r 75e9c68c3e93 sage/misc/latex.py
 a 'omega', 'Omega'] def latex_varify(a): def latex_varify(a, is_fname=False): r""" Convert a string a to a LaTeX string: if it's an element of common_varnames, then prepend a backslash.  If a consists of a single letter, then return it.  Otherwise, return "\\mbox{a}". either "{\\rm a}" or "\\mbox{a}" if "is_fname" flag is True or False. INPUT: 'w' sage: latex_varify('aleph') '\\mbox{aleph}' sage: latex_varify('aleph', is_fname=True) '{\\rm aleph}' sage: latex_varify('alpha') '\\alpha' """ return "\\" + a elif len(a) == 1: return a elif is_fname is True: return '{\\rm %s}'%a else: return '\\mbox{%s}'%a def latex_variable_name(x): def latex_variable_name(x, is_fname=False): r""" Return latex version of a variable name. '\\sigma_{\\alpha}' sage: latex_variable_name('nothing1') '\\mbox{nothing}_{1}' sage: latex_variable_name('nothing1', is_fname=True) '{\\rm nothing}_{1}' sage: latex_variable_name('nothing_abc') '\\mbox{nothing}_{\\mbox{abc}}' sage: latex_variable_name('nothing_abc', is_fname=True) '{\\rm nothing}_{{\\rm abc}}' sage: latex_variable_name('alpha_beta_gamma12') '\\alpha_{\\beta_{\\gamma_{12}}}' # handle the suffix specially because it very well might be numeric # I use strip to avoid using regex's -- It makes it a bit faster (and the code is more comprehensible to non-regex'ed people) if suffix.strip("1234567890")!="": suffix = latex_variable_name(suffix) # recurse to deal with recursive subscripts return '%s_{%s}'%(latex_varify(prefix), suffix) suffix = latex_variable_name(suffix, is_fname) # recurse to deal with recursive subscripts return '%s_{%s}'%(latex_varify(prefix, is_fname), suffix) else: return latex_varify(prefix) return latex_varify(prefix, is_fname) class LatexExamples(): r"""
• sage/symbolic/function.pyx

diff -r 2e793d2a0e12 -r 75e9c68c3e93 sage/symbolic/function.pyx
 a my args are: x, y^z sage: latex(foo(x,y^z)) \mbox{t}\left(x, y^{z}\right) t\left(x, y^{z}\right) sage: foo = nfunction('t', 2, print_latex_func=my_print) sage: foo(x,y^z) t(x, y^z)
• sage/symbolic/pynac.pyx

diff -r 2e793d2a0e12 -r 75e9c68c3e93 sage/symbolic/pynac.pyx
 a sage: get_sfunction_from_serial(i) == foo True sage: py_latex_function_pystring(i, (x,y^z)) '\\mbox{foo}\\left(x, y^{z}\\right)' '{\\rm foo}\\left(x, y^{z}\\right)' sage: py_latex_function_pystring(i, (x,y^z), True) '\\left(\\mbox{foo}\\right)\\left(x, y^{z}\\right)' '\\left({\\rm foo}\\right)\\left(x, y^{z}\\right)' Test latex_name:: if func._latex_name: name = func._latex_name else: # if latex_name is not defined, use default name name = ''.join([r'\mbox{', func._name, '}']) # if latex_name is not defined, then call # latex_variable_name with "is_fname=True" flag from sage.misc.latex import latex_variable_name name = latex_variable_name(func._name, is_fname=True) if fname_paren: olist = [r'\left(', name, r'\right)'] else: sage: get_sfunction_from_serial(i) == foo True sage: py_latex_fderivative(i, (0, 1, 0, 1), (x, y^z)) D[0, 1, 0, 1]\left(\mbox{foo}\right)\left(x, y^{z}\right) D[0, 1, 0, 1]\left({\rm foo}\right)\left(x, y^{z}\right) Test latex_name::