Formal Symbolic Expressions

3 views
Skip to first unread message

Jason Merrill

unread,
Sep 17, 2008, 2:20:53 AM9/17/08
to sage-devel
I was musing about formal symbolic expressions on sage support:

http://groups.google.com/group/sage-support/browse_thread/thread/78dc8d2d476acc46

but thought it was time to move it over to devel. The motivation is
to have a nice notation for formal integrals and derivatives:

sage: integral(sin(x),x).formal()
integral(sin(x),x)

If this is a good idea, then maybe it's a good idea to have a formal
method for all symbolic expressions.

sage: (x - x).formal()
x - x

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

Let's see it in action

sage: x - x
0
sage: (x - x).formal()
x - x
sage: formal(x - x)
x - x
sage: simplify(formal(x - x))
0

sage: exp(i*pi)
-1
sage: formal(exp(i*pi))
exp(I*pi)

This won't work on integral or derivative yet because they aren't lazy
enough. But it seems like they could be made to behave like the
Function_* classes, for instance Function_exp above.

Cheers,

JM

Jason Merrill

unread,
Sep 17, 2008, 11:02:04 AM9/17/08
to sage-devel
On Sep 17, 2:20 am, Jason Merrill <jwmerr...@gmail.com> 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

Regards,

JM

William Stein

unread,
Sep 17, 2008, 12:37:44 PM9/17/08
to sage-...@googlegroups.com

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.

William

Jason Grout

unread,
Sep 17, 2008, 12:46:10 PM9/17/08
to sage-...@googlegroups.com


I looked at your ginac/pynac symbolic stuff last night. It appears that
you just call a Gex_to_str function (or something like that) to print
out the expression, which presumably is a ginac thing. Calling latex()
on any expression just returns \text{printed representation}. If Sage
will not maintain the expression tree, will it be able to access it to
print out a latex form? Does Ginac output latex versions of expressions?

Thanks,

Jason

Robert Bradshaw

unread,
Sep 17, 2008, 12:50:07 PM9/17/08
to sage-...@googlegroups.com

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


William Stein

unread,
Sep 17, 2008, 1:03:55 PM9/17/08
to sage-...@googlegroups.com

Yes.

> Does Ginac output latex versions of expressions?

Yes.

William

Jason Merrill

unread,
Sep 17, 2008, 1:38:10 PM9/17/08
to sage-devel
On Sep 17, 12:37 pm, "William Stein" <wst...@gmail.com> wrote:
>
> 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.

Thanks for the heads up. The code above would be "pointless" even if
Ginac weren't in the pipeline--it's surely full of holes. The point
was just to try to get a working version of the idea across. Once the
Ginac code lands, will there be a way to represent formal
integration? Could there be? Would it look like this, or would you
want it to?

Regards,

JM

Burcin Erocal

unread,
Sep 18, 2008, 8:01:03 AM9/18/08
to sage-...@googlegroups.com

There will be a way to represent formal integration and summation.
I believe avoiding automatic simplification of arbitrary expressions
requires more work (I haven't checked this yet.), but even that
shouldn't be too complicated. This is on my todo list though.

Did you manage to build the pynac package? If not, I can post a
modified version which should build in your environment so you can
start playing with the new code.


Cheers,

Burcin

Jason Merrill

unread,
Sep 18, 2008, 9:57:20 AM9/18/08
to sage-devel
On Sep 18, 8:01 am, Burcin Erocal <bur...@erocal.org> wrote:
>
> There will be a way to represent formal integration and summation.
> I believe avoiding automatic simplification of arbitrary expressions
> requires more work (I haven't checked this yet.), but even that
> shouldn't be too complicated. This is on my todo list though.

Great, sounds interesting.

> Did you manage to build the pynac package? If not, I can post a
> modified version which should build in your environment so you can
> start playing with the new code.

I am now able to install the spkg if I take macports off my path
(which I also had to do to build 3.1.2.rc2). Trying to apply the
patch at http://trac.sagemath.org/sage_trac/ticket/3872, though,
causes a conflict. Any chance there could be an updated patch
sometime soon that takes into account all the changes that went into
3.1.2?

Best,

JM

Burcin Erocal

unread,
Sep 18, 2008, 2:09:33 PM9/18/08
to sage-...@googlegroups.com
On Thu, 18 Sep 2008 06:57:20 -0700 (PDT)
Jason Merrill <jwme...@gmail.com> wrote:

>
> On Sep 18, 8:01 am, Burcin Erocal <bur...@erocal.org> wrote:

<snip>


> > Did you manage to build the pynac package? If not, I can post a
> > modified version which should build in your environment so you can
> > start playing with the new code.
>
> I am now able to install the spkg if I take macports off my path
> (which I also had to do to build 3.1.2.rc2). Trying to apply the
> patch at http://trac.sagemath.org/sage_trac/ticket/3872, though,
> causes a conflict. Any chance there could be an updated patch
> sometime soon that takes into account all the changes that went into
> 3.1.2?

You can use the bundle here:

http://www.risc.jku.at/people/berocal/sage/pynac.hg


Cheers,

Burcin

Jason Merrill

unread,
Sep 19, 2008, 9:10:00 PM9/19/08
to sage-devel
On Sep 18, 2:09 pm, Burcin Erocal <bur...@erocal.org> wrote:
> On Thu, 18 Sep 2008 06:57:20 -0700 (PDT)
>
> Jason Merrill <jwmerr...@gmail.com> wrote:
>
> > On Sep 18, 8:01 am, Burcin Erocal <bur...@erocal.org> wrote:
> <snip>
> > > Did you manage to build the pynac package? If not, I can post a
> > > modified version which should build in your environment so you can
> > > start playing with the new code.
>
> > I am now able to install the spkg if I take macports off my path
> > (which I also had to do to build 3.1.2.rc2).  Trying to apply the
> > patch athttp://trac.sagemath.org/sage_trac/ticket/3872, though,
> > causes a conflict.  Any chance there could be an updated patch
> > sometime soon that takes into account all the changes that went into
> > 3.1.2?
>
> You can use the bundle here:
>
> http://www.risc.jku.at/people/berocal/sage/pynac.hg

Hi Burcin,

I was able to apply the above patch cleanly, and I now have a working
version of the new symbolic ring to play with. Thanks!

I'd like to have a better understanding of the status of pynac, and
what it means for working on Sage in the coming weeks. Now that I can
play with pynac, who should I tell about my experience, and what sorts
of things are useful to share? Pynac as it stands seems far from
feature complete, but I'm sure you already know that. For instance,

sage: x = var('x',ns=1)

sage: type(x)
<type 'sage.symbolic.expression.Expression'>

# No plotting, I assume most other numerical operations
# don't work for similar reasons
sage: plot(x^3,-3,3)
verbose 0 (3585: plot.py, _plot) WARNING: When plotting, failed to
evaluate function at 400 points.
verbose 0 (3585: plot.py, _plot) Last error message:
''sage.symbolic.expression.Expression' object is not callable'

# Making a callable symbolic function involving x actually
# changes x back to the old symbolic ring
sage: g(x) = x^3
sage: type(x)
<class 'sage.calculus.calculus.SymbolicVariable'>

Somehow I doubt a flood of bug reports like these would be useful
right now. But at the same time, William suggests that writing code
specific to the current calculus module is now pointless. So what is
the best use of time currently? Waiting? For how long?

I'm really grateful to Burcin and William for taking the time and
initiative to write this new code. Because of your leadership and
hard work, symbolics in Sage looks to have a bright future. But at
least to me, it has a hazy present.

Regards,

JM

William Stein

unread,
Sep 19, 2008, 10:26:10 PM9/19/08
to sage-...@googlegroups.com

Yes. It is very very very far from "feature complete". In fact, we view
it as an absolutely minimal implementation of just enough to be useful
to start a second stage of development.

> For instance,
>
> sage: x = var('x',ns=1)
>
> sage: type(x)
> <type 'sage.symbolic.expression.Expression'>
>
> # No plotting, I assume most other numerical operations
> # don't work for similar reasons
> sage: plot(x^3,-3,3)
> verbose 0 (3585: plot.py, _plot) WARNING: When plotting, failed to
> evaluate function at 400 points.
> verbose 0 (3585: plot.py, _plot) Last error message:
> ''sage.symbolic.expression.Expression' object is not callable'
>
> # Making a callable symbolic function involving x actually
> # changes x back to the old symbolic ring
> sage: g(x) = x^3
> sage: type(x)
> <class 'sage.calculus.calculus.SymbolicVariable'>
>
> Somehow I doubt a flood of bug reports like these would be useful
> right now.

Yes, that would be useless.

> But at the same time, William suggests that writing code
> specific to the current calculus module is now pointless. So what is
> the best use of time currently? Waiting? For how long?

Yes. I'm sorry, but unfortunately this is how software development
sometimes works. It's painful. It has been very bad already because
another developer put a "lock" on symbolic calculus stuff in January 2008, and
in August had made worrisome progress. Yes, this is happening again
now, but hopefully Burcin/my progress will be much faster.

You could also help with the work to make the pynac based symbolic
calculus in sage feature complete.

> I'm really grateful to Burcin and William for taking the time and
> initiative to write this new code. Because of your leadership and
> hard work, symbolics in Sage looks to have a bright future. But at
> least to me, it has a hazy present.

I hope the above statements clarify the situation. You could also
look at the todo file in the
SAGE_ROOT/devel/sage/sage/symbolic
directory.

William

Reply all
Reply to author
Forward
0 new messages