Simplification / Latex question

44 views
Skip to first unread message

Mike Witt

unread,
Jun 22, 2010, 2:56:52 PM6/22/10
to sage-s...@googlegroups.com
sage: version()
'Sage Version 4.4.3, Release Date: 2010-06-04'
sage: f=e^(i*x*pi-i*2*pi)
sage: f.simplify_full()
e^(I*pi*x)

# So far, so good

sage: n=var('n')
sage: f=e^(i*x*pi*n-i*2*pi*n)
sage: f.simplify_full()
e^(I*pi*n*x - 2*I*pi*n)

# Is there a way I can get this to simplify?

sage: latex(f)
e^{\left(I \, \pi n x + \left(-2 I\right) \, \pi n\right)}

# Why the "extra" parentheses around -2I ?

--Mike

kcrisman

unread,
Jun 22, 2010, 3:41:17 PM6/22/10
to sage-support

> sage: n=var('n')
> sage: f=e^(i*x*pi*n-i*2*pi*n)
> sage: f.simplify_full()
> e^(I*pi*n*x - 2*I*pi*n)
>
> # Is there a way I can get this to simplify?

This apparently isn't even that easy in Maxima.

Maxima 5.21.1 http://maxima.sourceforge.net
using Lisp ECL 10.4.1
Distributed under the GNU Public License. See the file COPYING.
Dedicated to the memory of William Schelter.
The function bug_report() provides bug reporting information.
(%i5) radcan(%e^(%pi*n-2*%pi));
%pi n - 2 %pi
(%o5) %e
(%i6) expand(%e^(%pi*n-2*%pi));
%pi n - 2 %pi
(%o6) %e

There are several Maxima experts on the list, though, who may know
about a flag to set in such a case to factor the exponent first. I
couldn't find one in the simplification documentation for Maxima, but
it may be elsewhere.

Of course, you can do this ahead of time:

sage: e^((n*pi-pi*2).factor())
e^((n - 2)*pi)

but this is probably not what you want.

> sage: latex(f)
> e^{\left(I \, \pi n x + \left(-2 I\right) \, \pi n\right)}
>
> # Why the "extra" parentheses around -2I ?

No idea. Pynac usually handles these sorts of things; I'm not sure
whether I would call it a bug, though it does seem strange. Perhaps
Pynac represents this as a complex internally and so this happens?
Burcin will know :)

- kcrisman

Mike Witt

unread,
Jun 23, 2010, 5:55:41 PM6/23/10
to sage-s...@googlegroups.com
On 06/22/2010 12:41:17 PM, kcrisman wrote:
>
> > sage: n=var('n')
> > sage: f=e^(i*x*pi*n-i*2*pi*n)
> > sage: f.simplify_full()
> > e^(I*pi*n*x - 2*I*pi*n)
> >
> > # Is there a way I can get this to simplify?
>
> This apparently isn't even that easy in Maxima.
>
> Maxima 5.21.1 http://maxima.sourceforge.net
> using Lisp ECL 10.4.1
> Distributed under the GNU Public License. See the file COPYING.
> Dedicated to the memory of William Schelter.
> The function bug_report() provides bug reporting information.
> (%i5) radcan(%e^(%pi*n-2*%pi));
> %pi n - 2 %pi
> (%o5) %e
> (%i6) expand(%e^(%pi*n-2*%pi));
> %pi n - 2 %pi
> (%o6) %e
>
> There are several Maxima experts on the list, though, who may know
> about a flag to set in such a case to factor the exponent first. I
> couldn't find one in the simplification documentation for Maxima, but
> it may be elsewhere.

Well, I hope to hear from one of these Maxima experts!

> Of course, you can do this ahead of time:
>
> sage: e^((n*pi-pi*2).factor())
> e^((n - 2)*pi)
>
> but this is probably not what you want.

Right. This crops up in the middle of a more complicated
expression. If I could figure out how to break the expression
up in the right way, then I guess I could search for parts
that are exponential functions, take the log of those, and
then simplify the logs. I know how to ultimately find all
the pieces of the function with .operands(), but I don't
then know any way to put them back together with the
proper operators. Maybe there's a way to access the parsed
tree of the expression? But of course that's crazy.
There must be a "normal" way to simplify it!

> > sage: latex(f)
> > e^{\left(I \, \pi n x + \left(-2 I\right) \, \pi n\right)}
> >
> > # Why the "extra" parentheses around -2I ?
>
> No idea. Pynac usually handles these sorts of things; I'm not sure
> whether I would call it a bug, though it does seem strange. Perhaps
> Pynac represents this as a complex internally and so this happens?
> Burcin will know :)
>
> - kcrisman
>

> --
> To post to this group, send email to sage-s...@googlegroups.com
> To unsubscribe from this group, send email to
> sage-support...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/sage-support
> URL: http://www.sagemath.org
>
>
>
>
>
>


kcrisman

unread,
Jun 24, 2010, 9:15:52 AM6/24/10
to sage-support


> > > sage: n=var('n')
> > > sage: f=e^(i*x*pi*n-i*2*pi*n)
> > > sage: f.simplify_full()
> > > e^(I*pi*n*x - 2*I*pi*n)
>
> > > # Is there a way I can get this to simplify?
>
> > This apparently isn't even that easy in Maxima.
>
> > Maxima 5.21.1http://maxima.sourceforge.net
I believe there is, but I can't figure out how to do it without going
through fast_callable, which doesn't seem right. This information is
in Pynac, but I can't find a method or underscore method that accesses
it. This is now http://trac.sagemath.org/sage_trac/ticket/9329 .

> tree of the expression? But of course that's crazy.
> There must be a "normal" way to simplify it!
>

I don't know about that. Many other discussions about 'obvious'
simplifications have led me to agree that this is a much harder
problem than one thinks.

On the other hand, it can be hard to find references to additional
packages in Maxima that might do this; it turns out that lots of
things one wants to do are not automatically available. Try
http://maxima.sourceforge.net/docs/manual/en/maxima_71.html#SEC298 for
ways you might be able to do this directly in Maxima, though I
couldn't see for sure if that is part of its functionality.

sage: maxima_console()

(%i4) demo("facexp");

Annoyingly, it continues this thing of asking whether 2*%pi is an
integer which one often sees...

I hope this helps.

- kcrisman

Mike Witt

unread,
Jun 24, 2010, 11:59:09 AM6/24/10
to sage-s...@googlegroups.com

This is all good information, thanks. It helps to at least know
that I'm not missing something obvious. It's the combination with
that latex issue that results in some really ugly output.

I've noticed too about how maxima continues to ask things that
(it would seem) you have already told it. I guess it would be
in my best interests to learn more about maxima.

Thanks again,

-Mike


kcrisman

unread,
Jun 24, 2010, 12:48:25 PM6/24/10
to sage-support

> I've noticed too about how maxima continues to ask things that
> (it would seem) you have already told it. I guess it would be
> in my best interests to learn more about maxima.
>

If you are serious about doing symbolic manipulation that you can
control from within Sage, yes. That said, various people will note
that things it would seem you told it are often things that are
provably undecidable or something like that.

Good luck!

- kcrisman

kcrisman

unread,
Jun 25, 2010, 9:07:02 AM6/25/10
to sage-support
Dear Mike,

Just to follow up:

There is further discussion at http://trac.sagemath.org/sage_trac/ticket/9329
if you are interested in saying exactly what sort of data structure
would enable you to perform the simplifications you would like to
without having to create a custom Maxima simplification routine.

- kcrisman

Mike Witt

unread,
Jun 25, 2010, 10:53:00 AM6/25/10
to sage-s...@googlegroups.com

Well ... I can see how one might work one's way
through the expression, using the operator() and operands()
functions. And, I suppose, I can see how one could then build
up the equivalent expression, having modified one of the
operands in a certain way. So, I don't suppose that there is
actually any need for a custom data structure to do this.

I guess it's really just a question of whether the way
these expressions sometimes display in latex bothers me
enough to do something about it, other than just complain.

Although ... I guess I'm still a bit confused as to why
this happens, even given the form of the exponential.

sage: f = e^(2*I*pi*n*x - 2*I*pi*n)
sage: latex(f)
e^{\left(\left(2 I\right) \, \pi n x + \left(-2 I\right) \, \pi
n\right)}

Still, I shouldn't really get +(−2i) right? I think you
mentioned something about Pynac (another program I know
nothing about). It seems like trying to fix this just
involves learning too much of a learning curve for me
to contemplate.

-Mike

Jason Grout

unread,
Jun 26, 2010, 6:26:06 PM6/26/10
to sage-s...@googlegroups.com
On 6/24/10 6:15 AM, kcrisman wrote:
>>
>> Right. This crops up in the middle of a more complicated
>> expression. If I could figure out how to break the expression
>> up in the right way, then I guess I could search for parts
>> that are exponential functions, take the log of those, and
>> then simplify the logs. I know how to ultimately find all
>> the pieces of the function with .operands(), but I don't
>> then know any way to put them back together with the
>> proper operators. Maybe there's a way to access the parsed
>
> I believe there is, but I can't figure out how to do it without going
> through fast_callable, which doesn't seem right. This information is
> in Pynac, but I can't find a method or underscore method that accesses
> it. This is now http://trac.sagemath.org/sage_trac/ticket/9329 .


See http://sagenb.org/home/pub/1760/ for an example of creating an
expression tree (in that worksheet, the expression tree is used to make
a mathematica expression...)

Thanks,

Jason

Mike Witt

unread,
Jun 26, 2010, 6:44:59 PM6/26/10
to sage-s...@googlegroups.com

Hey, that's really cool. Thanks!

BTW, at this point, after the further Latex discussion, I realize
that my original problem was really more related to Ticket #9314
than to the expression itself (at least, that's what I'm currently
speculating). But, especially with that example,
it's pretty clear to me what to do if I need to parse an expression.
I believe that Ticket #9329 was generated in response to my original
post, before I understood that there was a Latex issue involved.
I believe that Ticket #9329 should be deleted (closed or whatever).

-Mike

kcrisman

unread,
Jun 26, 2010, 8:21:21 PM6/26/10
to sage-support

> I believe that Ticket #9329 was generated in response to my original
> post, before I understood that there was a Latex issue involved.
> I believe that Ticket #9329 should be deleted (closed or whatever).

But part of your question was also to try to simplify more complicated
expressions, and it does seem reasonable that we could provide a full
nested expression tree for symbolic expressions (rather than having to
iterate something by hand), since we do so for fast_callable (I
think?). Just because we might not do it anytime soon doesn't mean we
can't have a ticket for it! We usually only close tickets it is clear
are duplicates or things we won't fix or are too vague; things which
no one is motivated to provide just stay that way until someone shows
up (and you'd be surprised how many stay open for > 1 year and all of
a sudden get someone working on them).

I do hope the LaTeX issue gets resolved for you soon, though.

- kcrisman

Mike Witt

unread,
Jun 26, 2010, 8:32:28 PM6/26/10
to sage-s...@googlegroups.com

OK, sure, that makes sense.

> I do hope the LaTeX issue gets resolved for you soon, though.

My current theory is that it will be resolved if Ticket #9314
gets fixed. But I don't really know that for sure :-)

Burcin Erocal

unread,
Jun 30, 2010, 8:06:19 AM6/30/10
to sage-s...@googlegroups.com
On Fri, 25 Jun 2010 07:53:00 -0700
Mike Witt <msg...@gmail.com> wrote:

> On 06/25/2010 06:07:02 AM, kcrisman wrote:
> > Dear Mike,
> >
> > Just to follow up:
> >
> > There is further discussion at
> > http://trac.sagemath.org/sage_trac/ticket/9329
> > if you are interested in saying exactly what sort of data structure
> > would enable you to perform the simplifications you would like to
> > without having to create a custom Maxima simplification routine.
> >
> > - kcrisman
>
> Well ... I can see how one might work one's way
> through the expression, using the operator() and operands()
> functions. And, I suppose, I can see how one could then build
> up the equivalent expression, having modified one of the
> operands in a certain way. So, I don't suppose that there is
> actually any need for a custom data structure to do this.

As far as I understand from your previous comments, a way to extract the
exponential functions from the expression is all you need. You don't
really need to walk through the tree. Here is one way to do this:

sage: t = exp(x+y)*(x-y)*(exp(y)+exp(z-y))
sage: t
(e^(-y + z) + e^y)*(x - y)*e^(x + y)
sage: w = SR.wild()
sage: t.find(exp(w))
[e^(-y + z), e^(x + y), e^y]

You can then change the expressions in the given array and substitute
new values for them:

sage: t.subs({res[1]: sin(res[1].operands()[0])})
(e^(-y + z) + e^y)*(x - y)*sin(x + y)

The .operands()[0] syntax is really cumbersome. We need a shortcut for
this. I thought .op(0) worked for pynac expressions before we switched
from the maxima backend.

> Although ... I guess I'm still a bit confused as to why
> this happens, even given the form of the exponential.
>
> sage: f = e^(2*I*pi*n*x - 2*I*pi*n)
> sage: latex(f)
> e^{\left(\left(2 I\right) \, \pi n x + \left(-2 I\right) \, \pi
> n\right)}
>

> Still, I shouldn't really get +(-2i) right?

This is a bug:

http://trac.sagemath.org/sage_trac/ticket/9394

I'll fix this when I have time to work on pynac again.


Cheers,
Burcin

kcrisman

unread,
Jun 30, 2010, 9:09:41 AM6/30/10
to sage-support

> As far as I understand from your previous comments, a way to extract the
> exponential functions from the expression is all you need. You don't
> really need to walk through the tree. Here is one way to do this:
>
> sage: t = exp(x+y)*(x-y)*(exp(y)+exp(z-y))
> sage: t
> (e^(-y + z) + e^y)*(x - y)*e^(x + y)
> sage: w = SR.wild()
> sage: t.find(exp(w))
> [e^(-y + z), e^(x + y), e^y]
>
> You can then change the expressions in the given array and substitute
> new values for them:

Did you define res in between? Maybe this? Just a guess.

sage: res = t.find(exp(w))

Mike Witt

unread,
Jun 30, 2010, 10:57:18 AM6/30/10
to sage-s...@googlegroups.com
> --
> To post to this group, send email to sage-s...@googlegroups.com
> To unsubscribe from this group, send email to
> sage-support...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/sage-support
> URL: http://www.sagemath.org
>
>
>

Thanks. That's a good idea. I'll try that.

-Mike

Reply all
Reply to author
Forward
0 new messages