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 b  
    13031303    """
    13041304    return cmp(repr(x), repr(y))
    13051305
    1306 def function(s, *args):
     1306def function(s, *args, **kwds):
    13071307    """
    13081308    Create a formal symbolic function with the name *s*.
    13091309   
     
    13441344        return s
    13451345
    13461346    if len(args) > 0:
    1347         return function(s)(*args)
     1347        return function(s, **kwds)(*args)
    13481348
    13491349    s = str(s)
    13501350    if ',' in s:
    1351         return tuple([function(x.strip()) for x in s.split(',')])
     1351        return tuple([function(x.strip(), **kwds) for x in s.split(',')])
    13521352    elif ' ' in s:
    1353         return tuple([function(x.strip()) for x in s.split()])
     1353        return tuple([function(x.strip(), **kwds) for x in s.split()])
    13541354    try:
    13551355        return symbol_table['functions'][s]
    13561356    except KeyError:
    13571357        pass
    1358     f = SFunction(s)
     1358    f = SFunction(s, **kwds)
    13591359    symbol_table['functions'][s] = f
    13601360    return f
    13611361
  • sage/calculus/var.pyx

    diff -r 58abaa4df2e2 -r a810ab7f1442 sage/calculus/var.pyx
    a b  
    7272        G[repr(v)] = v
    7373    return v
    7474
    75 def function(s, *args):
    76     """
     75def function(s, *args, **kwds):
     76    r"""
    7777    Create a formal symbolic function with the name *s*.
    7878
    7979    INPUT:
     
    8181    - ``s`` - a string, either a single variable name, or a space or
    8282      comma separated list of variable names.
    8383
     84    - ``**kwds`` - keyword arguments. Either one of the following two
     85        keywords can be used to customize latex representation of
     86        symbolic functions:
     87
     88            (1) latex_name=LaTeX   
     89                where ``LaTeX`` is any valid latex expression.
     90                Ex: f = function('f', x, latex_name="\\mathcal{F}")
     91                See EXAMPLES for more.
     92
     93            (2) print_latex_func=my_latex_print
     94                where ``my_latex_print`` is any callable function
     95                that returns a valid latex expression.
     96                Ex: f = function('f', x, print_latex_func=my_latex_print)
     97                See EXAMPLES for an explicit usage.
     98 
    8499    .. note::
    85100
    86101       The new function is both returned and automatically injected
     
    112127        sage: k = g.diff(x); k
    113128        (x, y) |--> 2*supersin(x)*D[0](supersin)(x)
    114129
     130    Custom typesetting of symbolic functions in LaTeX::
     131
     132    (1) Either using latex_name keyword::
     133
     134        sage: riemann(x) = function('riemann', x, latex_name="\\mathcal{R}")
     135        sage: latex(riemann(x))
     136        \mathcal{R}\left(x\right)
     137
     138    (2) Or passing a custom callable function that returns a
     139        latex expression::
     140
     141        sage: mu,nu = var('mu,nu')
     142        sage: def my_latex_print(*args): return "\\psi_{%s}"%(', '.join(map(latex, args)))
     143        sage: psi(mu,nu) = function('psi', mu, nu, print_latex_func=my_latex_print)
     144        sage: latex(psi(mu,nu))
     145        \psi_{\mu, \nu}
     146
    115147    In Sage 4.0, you must now use the :meth:`substitute_function`
    116148    method to replace functions::
    117149   
     
    119151        2*sin(x)*cos(x)
    120152    """
    121153    if len(args) > 0:
    122         return function(s)(*args)
     154        return function(s, **kwds)(*args)
    123155   
    124156    G = globals()  # this is the reason the code must be in Cython.
    125     v = calculus.function(s)
     157    v = calculus.function(s, **kwds)
    126158    if isinstance(v, tuple):
    127159        for x in v:
    128160            G[repr(x)] = x