On Sep 24, 4:17 pm, Julius <juliuslul...@gmail.com> wrote:
> Hi list,
>
> I wonder if there is any command similar to subsop in maple
> (www.maplesoft.com/support/help/Maple/view.aspx?path=subsop).
> There is an operands(), but I don't see how substitutions can be made
> from it.
>
> Antonio

Hi,
here is a try to mimic subsop:

{{{

def subsop(e, d=None):
    """
    Some sort of subsop, inspired by maple...

    EXAMPLES::
        sage: var('a, x, y, z, f, g, h, w')
        (a, x, y, z, f, g, h, w)
        sage: p = x^7 + 8*x^6 + x^2 - 9
        sage: subsop(p, {1: y})
        x^7 + x^2 + y - 9
        sage: subsop(p, {0: 0})
        8*x^6 + x^2 - 9
        sage: subsop(x*y*z, {0: 1})
        y*z
        sage: subsop([x, y, z], {0: None, 1: z, 2: y})
        [None, z, y]
        sage: p = function('f', x, function('g', x, y, z), x)
        sage: subsop(p, {(1,2): w})
        f(x, g(x, y, w), x)
        sage: subsop(p, {(1,-1): function('h', x, y, z), (1,2): w, 2:
a})
        f(x, h(x, y, w), a)
        sage: subsop(p)
        f(x, g(x, y, z), x)
    """
    if not d:
        return e
    if isinstance(e, list):
        operands_ = copy(e)
        operator_ = list
    elif isinstance(e, tuple):
        operands_ = copy(e)
        operator_ = tuple
    else:
        operands_ = e.operands()
        operator_ = e.operator()
    for i, di in d.iteritems():
        if isinstance(i, tuple):
            if len(i) > 1:
                di = subsop(operands_[i[0]], {i[1:]: di})
            i = i[0]
        if i == -1:
            operator_ = di
        else:
            operands_[i] = di
    # does this operator accepts lists?
    try:
        r = operator_(*operands_)
    except TypeError:
        # maybe we can reduce then ?
        try:
            r = reduce(operator_, operands_)
        except TypeError:
            # let's try lists...
            r = operator_(operands_)
    return r

}}}

It's zero-based, and the substitutions are given with a dict for
consistency with Sage.
The case of function is slightly different and the way I did it you
get a deprecation warning.

I hope this still gives you some insight.

       Yann

-- 
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to 
sage-support+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sage-support
URL: http://www.sagemath.org

Reply via email to