Is there a way to access the unsimplified form of a symbolic expression?

20 views
Skip to first unread message

Jason Merrill

unread,
Sep 16, 2008, 11:45:56 PM9/16/08
to sage-support
The documentation for simplify explains:

Expressions always print simplified; a simplified expression is
distinguished because the way it prints agrees with its underlyilng
representation.

sage: x - x
0
sage: type(x - x)
<class 'sage.calculus.calculus.SymbolicArithmetic'>
sage: type(simplify(x - x))
<class 'sage.calculus.calculus.SymbolicConstant'>

So until hit with an explicit simplify command, symbolic expressions
seem retain at least some information about how they were input. Is
there a way to get the input form of an expression back out? Can I
ever get sage to print something like

sage: (x - x).some_devious_trick()
x - x

I'm not as interested in trickery involving strings.

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.

Regards,

JM

Mike Hansen

unread,
Sep 16, 2008, 11:49:11 PM9/16/08
to sage-s...@googlegroups.com
Hi Jason,

> So until hit with an explicit simplify command, symbolic expressions
> seem retain at least some information about how they were input. Is
> there a way to get the input form of an expression back out? Can I
> ever get sage to print something like
>
> sage: (x - x).some_devious_trick()
> x - x

Not quite so devious:

sage: a = x - x
sage: a._operator
<built-in function sub>
sage: a._operands
[x, x]

--Mike

Jason Merrill

unread,
Sep 17, 2008, 12:31:51 AM9/17/08
to sage-support
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

Jason Grout

unread,
Sep 17, 2008, 1:13:40 AM9/17/08
to sage-s...@googlegroups.com

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

Jason Merrill

unread,
Sep 17, 2008, 2:23:20 AM9/17/08
to sage-support
On Sep 17, 12:31 am, Jason Merrill <jwmerr...@gmail.com> wrote:
> 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
>
> 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

I've got a toy implementation of this. Seemed like time to move it
over to sage-devel:

http://groups.google.com/group/sage-devel/browse_thread/thread/51aa3a53ae155dd3

JM
Reply all
Reply to author
Forward
0 new messages