# Ticket #6290: allow-keyword-arguments-such-as-latex_name-in-symbolic-functions.patch

File allow-keyword-arguments-such-as-latex_name-in-symbolic-functions.patch, 3.8 KB (added by gmhossain, 11 years ago)
• ## sage/calculus/calculus.py

# HG changeset patch
# User Golam Mortuza Hossain <gmhossain@gmail.com>
# Date 1245096292 10800
# Node ID a810ab7f1442ee2a3a198e506f30f68f9683fd1b
# Parent  58abaa4df2e26c9d272e262845af6e025a4d8fa0
Allow keyword arguments such as latex_name=LaTeX while defining symbolic functions

diff -r 58abaa4df2e2 -r a810ab7f1442 sage/calculus/calculus.py
 a """ return cmp(repr(x), repr(y)) def function(s, *args): def function(s, *args, **kwds): """ Create a formal symbolic function with the name *s*. return s if len(args) > 0: return function(s)(*args) return function(s, **kwds)(*args) s = str(s) if ',' in s: return tuple([function(x.strip()) for x in s.split(',')]) return tuple([function(x.strip(), **kwds) for x in s.split(',')]) elif ' ' in s: return tuple([function(x.strip()) for x in s.split()]) return tuple([function(x.strip(), **kwds) for x in s.split()]) try: return symbol_table['functions'][s] except KeyError: pass f = SFunction(s) f = SFunction(s, **kwds) symbol_table['functions'][s] = f return f
• ## sage/calculus/var.pyx

diff -r 58abaa4df2e2 -r a810ab7f1442 sage/calculus/var.pyx
 a G[repr(v)] = v return v def function(s, *args): """ def function(s, *args, **kwds): r""" Create a formal symbolic function with the name *s*. INPUT: - s - a string, either a single variable name, or a space or comma separated list of variable names. - **kwds - keyword arguments. Either one of the following two keywords can be used to customize latex representation of symbolic functions: (1) latex_name=LaTeX where LaTeX is any valid latex expression. Ex: f = function('f', x, latex_name="\\mathcal{F}") See EXAMPLES for more. (2) print_latex_func=my_latex_print where my_latex_print is any callable function that returns a valid latex expression. Ex: f = function('f', x, print_latex_func=my_latex_print) See EXAMPLES for an explicit usage. .. note:: The new function is both returned and automatically injected sage: k = g.diff(x); k (x, y) |--> 2*supersin(x)*D[0](supersin)(x) Custom typesetting of symbolic functions in LaTeX:: (1) Either using latex_name keyword:: sage: riemann(x) = function('riemann', x, latex_name="\\mathcal{R}") sage: latex(riemann(x)) \mathcal{R}\left(x\right) (2) Or passing a custom callable function that returns a latex expression:: sage: mu,nu = var('mu,nu') sage: def my_latex_print(*args): return "\\psi_{%s}"%(', '.join(map(latex, args))) sage: psi(mu,nu) = function('psi', mu, nu, print_latex_func=my_latex_print) sage: latex(psi(mu,nu)) \psi_{\mu, \nu} In Sage 4.0, you must now use the :meth:substitute_function method to replace functions:: 2*sin(x)*cos(x) """ if len(args) > 0: return function(s)(*args) return function(s, **kwds)(*args) G = globals()  # this is the reason the code must be in Cython. v = calculus.function(s) v = calculus.function(s, **kwds) if isinstance(v, tuple): for x in v: G[repr(x)] = x