Jason Merrill wrote: > On Sep 16, 11:45 pm, Jason Merrill <[EMAIL PROTECTED]> wrote: >> Can I ever get sage to print something like >> >> sage: (x - x).some_devious_trick() >> x - x >> >> If there is a way to do this, or if there could be a way to do this >> that wouldn't foul everything up, then extending it to operations like >> integrals, derivatives, sums, and products might be an interesting >> approach to answering the "how do I do a formal * in Sage?" question. > > Just wanted to develop this idea a little further. Right now, pretty > much everything has a ._repr_() that tells it how to be displayed. If > there's not already a .some_devious_trick(), why not call it .formal() > > sage: (x - x).formal() > x - x > > Currently, integral and diff are eager in that they call maxima as > soon as they get called and return a Sage representation of maxima's > answer. But what if they were lazy--that is, they just made an object > with their input stored, and a .formal() method, and then a few other > methods like ._repr_() that would actually trigger the maxima > evaluation. > > sage: m = (x^2).integral() # no call to maxima yet > sage: m.formal() > integral(x^2,x) > sage: latex(m.formal()) > \int x^2\,dx > # now maxima gets called when the _repr_ method > # of m is called. The result can be cached. > sage: m > x^3 > > If everything had a .formal() method, then these could cascade when > formal is called at higher levels > > sage: latex(integral(x - x,x).formal()) > \int x - x \,dx > sage: latex(integral(simplify(x - x),x).formal()) > \int 0 \,dx > > This one is rather nice, if you ask me: > > sage: m = integral(cos(x),x) > sage: latex(m.formal() == m) > \int cos(x)\,dx = sin(x) > > There was a discussion before about how Mathematica has a Hold[] > construct so you can do things like Hold[Integral[Cos[x],x]]. The > consensus was that python couldn't have this because it evaluates > function arguments before the function even gets to see them. But > with judicious use of laziness and remembering what was input, Sage > could sneak its way around that problem as suggested above. No need > for new preparsing, and no need for a distinction between Integral and > integral, nor integral(cos(x), evaluate=False) or any other unsavory > construction. > > I'm sure there's probably some snakes in the grass here, but on the > surface it all looks rather nice to me.
I believe this concept (lazy evaluation with Sage knowing what formal functions are involved) is what underlies the Function_* classes in calculus.py. Each of those seems to not evaluate itself, so you get a SymbolicComposition object, which does your recursion for you. At least, that is what things appear like on the surface as I've played around with it for a few minutes. Jason --~--~---------~--~----~------------~-------~--~----~ To post to this group, send email to sage-support@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-support URLs: http://www.sagemath.org -~----------~----~----~----~------~----~------~--~---