On Sep 17, 2008, at 9:37 AM, William Stein wrote: > > On Wed, Sep 17, 2008 at 8:02 AM, Jason Merrill > <[EMAIL PROTECTED]> wrote: >> >> On Sep 17, 2:20 am, Jason Merrill <[EMAIL PROTECTED]> wrote: >>> Here's a monkeypatch that makes this work(ish) >>> >>> from sage.calculus.calculus import SymbolicExpression >>> class FormalSymbolicExpression(SymbolicExpression): >>> def __init__(self,expr): >>> self.expr = expr >>> # just delegate everything to the input expression >>> for m in dir(expr): >>> self.m = m >>> def _repr_(self,simplify=False): >>> return self.expr._repr_(simplify=False) >>> # implement a _latex_ method >>> >>> SymbolicExpression.formal = lambda self: >>> FormalSymbolicExpression(self) >>> >>> def formal(expr): >>> try: >>> return expr.formal() >>> except AttributeError: >>> return expr >> >> Right... so that delegation bit doesn't actually do anything. This >> was leading to >> >> sage: simplify(formal(exp(I*pi))) >> e^(pi*I) >> >> and presumably horrendous amounts of other strangeness. Here's >> another try. It doesn't actually inherit from SymbolicExpression >> anymore, which will probably cause some difficulty with isinstance >> calls, but it's maybe a little closer >> >> from sage.calculus.calculus import SymbolicExpression >> class FormalSymbolicExpression(): >> def __init__(self,expr): >> self.expr = expr >> >> def __getattr__(self,attrib): >> return getattr(self.expr,attrib) >> >> def __repr__(self,simplify=False): >> return self.expr._repr_(simplify=False) >> >> SymbolicExpression.formal = \ >> lambda self: FormalSymbolicExpression(self) >> >> def formal(expr): >> try: >> return expr.formal() >> except AttributeError: >> return expr >> >> And now >> >> sage: simplify(formal(exp(I*pi))) >> -1 > > Jason, > > Just a heads up -- your code above is going to become pointless > when we switch > to using Ginac as a backend for symbolic manipulation, since Sage > will no longer > keep its own expression tree.
Hopefully there will still a way to query the tree, but I don't see how you'll avoid simplifications like 2+2 -> 4 or 1+x+3+x -> 4+x via this method. This would also be really useful for, e.g. plotting (where one could delay the evaluation at the symbolic "x" until one wants to plot, which seems to trip a lot of people up.) - Robert --~--~---------~--~----~------------~-------~--~----~ To post to this group, send email to sage-devel@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sage-devel URLs: http://www.sagemath.org -~----------~----~----~----~------~----~------~--~---