On Sep 16, 11:45 pm, Jason Merrill <
jwmerr...@gmail.com> 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.
Regards,
JM