On 07/13/2011 04:29 PM, robin wrote:
> Hello Michael
> 
> thanks for this.
> 
> Mathematica allows you to specify the metric by which you measure
> simplicity (actually, it's called "ComplexityFunction" and IIRC there
> are five or six builtins).
> 
> As for sage, how about just counting
> the number of calls to psi()?  This would just be an integer, with
>  "small" being Good. This would make sense because the psi
> function is difficult to evaluate.
> 
> Is this crazy enough for you?  ;-)
> 
> best wishes; keep me posted!

So, this code is crap, probably reinvents several wheels, doesn't check
that the variables are complex, fails on some inputs, might disengage
the locks on the dynamite cages, etc. But, it simplifies the expression
you gave as an example, and might not be a bad start with some tests and
the appropriate safeguards thrown in.

Watch out for word wrap.


from sage.all import *
from types import ListType


def is_list(obj):
    if isinstance(obj, ListType):
        return True

def flatten_list(l):
    result = []

    for item in l:
        if is_list(item):
            result += flatten_list(item)
        else:
            result += [item]

    return result


def flatten_operands(operands):
    return operands + [flatten_operands(g.operands()) for g in operands]


def count_psis(expr):
    count = 0
    operands = flatten_list(flatten_operands(expr.operands()))
    for o in operands:
        if "psi" in str(o):
            count += 1

    return count


def simplify_psi(expr):
    simple_expr = expr

    for v in expr.variables():
        simple_expr = simple_expr.subs({psi(1-v): pi/tan(pi*v) + psi(v)})

    simple_expr = simple_expr.full_simplify()

    if count_psis(simple_expr) < count_psis(expr):
        return simple_expr
    else:
        return expr


z = var('z')
f = (psi(1-z)- psi(z)-pi/tan(pi*z))

print simplify_psi(f)

-- 
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