Ticket #6344: trac_6344-mma_style_attempt.patch

File trac_6344-mma_style_attempt.patch, 4.2 KB (added by burcin, 10 years ago)

an attempt at implementing the MMA style, for testing only

  • sage/symbolic/pynac.pyx

    # HG changeset patch
    # User Burcin Erocal <burcin@erocal.org>
    # Date 1245879621 -7200
    # Node ID fa723d4ca05291f630cf651ae85f39fc4fae297c
    # Parent  93983390d6669e95588069d40a3adcda67280a71
    Change typesetting of derivatives of symbolic functions to something more sensible. #6344
    
    diff --git a/sage/symbolic/pynac.pyx b/sage/symbolic/pynac.pyx
    a b  
    274274    print(ostr.c_str())
    275275    stdstring_delete(ostr)
    276276
    277 def py_print_function_pystring(id, args, fname_paren=False):
     277def dparams_to_str(params):
     278    if len(params) == 1:
     279        if params[0] == 1:
     280            return "'"
     281        elif params[0] == 2:
     282            return "''"
     283    return ''.join(['^{(', ','.join(map(str, params)), ')}'])
     284
     285def py_print_function_pystring(id, args, dparams=None):
    278286    """
    279287    Return a string with the representation of the symbolic function specified
    280288    by the given id applied to args.
    281289   
    282     INPUT:
     290    INPUT::
    283291
    284292        id --   serial number of the corresponding symbolic function
    285         params -- Set of parameter numbers with respect to which to take
    286                     the derivative.
    287293        args -- arguments of the function.
     294        dparams -- list of integers indicating how many times the function is
     295            derivated w.r.t. each argument
    288296
    289297    EXAMPLES::
    290298
     
    325333        res = func._print_(*args)
    326334        # make sure the output is a string
    327335        if res is None:
    328             return ""
     336            raise ValueError, "custom print function for symbolic function returned None"
    329337        if not isinstance(res, str):
    330             return str(res)
     338            res = str(res)
     339        if dparams:
     340            res = ''.join(['D^{', dparams_to_str(dparams), '}', res])
    331341        return res
    332342
    333343    # otherwise use default output
    334     if fname_paren:
    335         olist = ['(', func._name, ')']
     344    if dparams:
     345        olist = [func._name, dparams_to_str(dparams)]
    336346    else:
    337347        olist = [func._name]
    338348    olist.extend(['(', ', '.join(map(repr, args)), ')'])
     
    341351cdef public stdstring* py_print_function(unsigned id, object args) except +:
    342352    return string_from_pystr(py_print_function_pystring(id, args))
    343353
    344 def py_latex_function_pystring(id, args, fname_paren=False):
     354def py_latex_function_pystring(id, args, dparams=None):
    345355    """
    346356    Return a string with the latex representation of the symbolic function
    347357    specified by the given id applied to args.
     
    403413        res = func._print_latex_(*args)
    404414        # make sure the output is a string
    405415        if res is None:
    406             return ""
     416            raise ValueError, "custom print function for symbolic function returned None"
    407417        if not isinstance(res, str):
    408             return str(res)
     418            res = str(res)
     419        if dparams:
     420            res = ''.join(['D^{', dparams_to_str(dparams), '}', res])
    409421        return res
    410422
    411423    # otherwise, use the latex name if defined
     
    416428        # latex_variable_name with "is_fname=True" flag
    417429        from sage.misc.latex import latex_variable_name
    418430        name = latex_variable_name(func._name, is_fname=True)
    419     if fname_paren:
    420         olist = [r'\left(', name, r'\right)']
     431    if dparams:
     432        olist = [name, dparams_to_str(dparams)]
    421433    else:
    422434        olist = [name]
    423435    # print the arguments
     
    443455
    444456
    445457    """
    446     ostr = ''.join(['D[', ', '.join([repr(int(x)) for x in params]), ']'])
    447     fstr = py_print_function_pystring(id, args, True)
    448     py_res = ostr + fstr
     458    dparams = [0]*len(args)
     459    for i in params:
     460        dparams[i] += 1
     461    py_res = py_print_function_pystring(id, args, dparams)
    449462    return string_from_pystr(py_res)
    450463
    451464def py_print_fderivative_for_doctests(id, params, args):
     
    493506    See documentation of py_print_fderivative for more information.
    494507
    495508    """
    496     ostr = ''.join(['D[', ', '.join([repr(int(x)) for x in params]), ']'])
    497     fstr = py_latex_function_pystring(id, args, True)
    498     py_res = ostr + fstr
     509    dparams = [0]*len(args)
     510    for i in params:
     511        dparams[i] += 1
     512    py_res = py_latex_function_pystring(id, args, dparams)
    499513    return string_from_pystr(py_res)
    500514
    501515def py_latex_fderivative_for_doctests(id, params, args):