Teaching simplify to do atan (issue 636)

38 views
Skip to first unread message

Priit Laes

unread,
Feb 26, 2009, 11:52:23 AM2/26/09
to sympy
Yesterday I ran into some trouble when trying to integrate a simple
integral that should return atan(x). I also found an open bug about
this topic: http://code.google.com/p/sympy/issues/detail?id=636&q=atan
.

So, I took a peek into sympy/simplify/simplify.py's trigsimp function.
So I tried to add the specific form with imaginary units into matchers
list under ifelse expr.is_Add but it doesn't seem to work :(

diff --git a/sympy/simplify/simplify.py b/sympy/simplify/simplify.py
index b2816f3..916e608 100644
--- a/sympy/simplify/simplify.py
+++ b/sympy/simplify/simplify.py
@@ -762,11 +762,16 @@ def trigsimp(expr, deep=False):
log(2)
"""
from sympy.core.basic import S
- sin, cos, tan, cot = C.sin, C.cos, C.tan, C.cot
+ from sympy import log
+ I = S.ImaginaryUnit
+ sin, cos, tan, cot, atan = C.sin, C.cos, C.tan, C.cot, C.atan

#XXX this stopped working:
if expr == 1/cos(Symbol("x"))**2 - 1:
return tan(Symbol("x"))**2
+# This works :)
+# if expr == (((I*log(Symbol("x") + I))/Rational(2) - (I*log(Symbol
("x") - I))/Rational(2))):
+# return atan(Symbol("x"))

if expr.is_Function:
if deep:
@@ -786,7 +791,8 @@ def trigsimp(expr, deep=False):
matchers = (
(a*sin(b)**2, a - a*cos(b)**2),
(a*tan(b)**2, a*(1/cos(b))**2 - a),
- (a*cot(b)**2, a*(1/sin(b))**2 - a)
+ (a*cot(b)**2, a*(1/sin(b))**2 - a),
+ (((I*log(a)+I)/Rational(2))-((I*log(a)-I)/Rational(2)),
atan(a)),
)

# Scan for the terms we need

Ondrej Certik

unread,
Feb 26, 2009, 11:57:15 AM2/26/09
to sy...@googlegroups.com
On Thu, Feb 26, 2009 at 8:52 AM, Priit Laes <plae...@gmail.com> wrote:
>
> Yesterday I ran into some trouble when trying to integrate a simple
> integral that should return atan(x). I also found an open bug about
> this topic: http://code.google.com/p/sympy/issues/detail?id=636&q=atan
> .
>
> So, I took a peek into sympy/simplify/simplify.py's trigsimp function.
> So I tried to add the specific form with imaginary units into matchers
> list under ifelse expr.is_Add but it doesn't seem to work :(

Thanks for trying --- this function needs some polishing, I'll try to
have a look at it if you don't manage to fix it yourself.

ONdrej

Priit Laes

unread,
Feb 26, 2009, 12:29:42 PM2/26/09
to sympy
On Feb 26, 6:57 pm, Ondrej Certik <ond...@certik.cz> wrote:
Ugh, there's currently a big bug in my code, but even if I correct it
there's no way it would work as currently simplifications are made
using single terms (so it wont look something "term1 + term2")

Ondrej Certik

unread,
Feb 26, 2009, 12:40:13 PM2/26/09
to sy...@googlegroups.com

Right. Do you have some ideas how to fix it so that it works with
"term1 + term2"?

Ondrej

Akshay Srinivasan

unread,
Feb 28, 2009, 5:31:54 AM2/28/09
to sympy
I had this problem with the simplification of Boolean expressions.
There's this algorithm by Quine-McCluskey where the expression is
reduced to a "standard form" and then simplified. I guess there's an
analogue for algebraic expressions as well. I'm yet to implement the
QM algorithm in sympy.

On Feb 26, 10:40 pm, Ondrej Certik <ond...@certik.cz> wrote:

Akshay Srinivasan

unread,
Mar 21, 2009, 1:05:35 AM3/21/09
to sympy
Here is a nice paper on Trignometric Simplification -
http://vv.cn/d/d.aspx?Id=21987_1.0.42119

On Feb 28, 3:31 pm, Akshay Srinivasan <akshaysriniva...@gmail.com>
wrote:

Ondrej Certik

unread,
Mar 21, 2009, 3:03:10 AM3/21/09
to sy...@googlegroups.com
On Fri, Mar 20, 2009 at 10:05 PM, Akshay Srinivasan
<akshaysr...@gmail.com> wrote:
>
> Here is a nice paper on Trignometric Simplification -
> http://vv.cn/d/d.aspx?Id=21987_1.0.42119

Very cool! Thanks a lot for sharing it. Let's implement it in sympy.

Ondrej

Vinzent Steinberg

unread,
Mar 21, 2009, 8:58:35 AM3/21/09
to sympy
What is the best way to replace tan(x) with sin(x)/cos(x)?

*.match() does not seem to be suited, because I don't care where tan
(x) occurs, I just want to replace it.
WildFunction() seems to be broken.

Vinzent

On 21 Mrz., 08:03, Ondrej Certik <ond...@certik.cz> wrote:
> On Fri, Mar 20, 2009 at 10:05 PM, Akshay Srinivasan
>

Ondrej Certik

unread,
Mar 21, 2009, 12:01:45 PM3/21/09
to sy...@googlegroups.com
On Sat, Mar 21, 2009 at 5:58 AM, Vinzent Steinberg
<vinzent....@googlemail.com> wrote:
>
> What is the best way to replace tan(x) with sin(x)/cos(x)?
>
> *.match() does not seem to be suited, because I don't care where tan
> (x) occurs, I just want to replace it.
> WildFunction() seems to be broken.

That's a good question. You need to know the argument of tan(x). If
it's just x, then:
I would use:

e.subs(tan(x), sin(x)/cos(x))

I think we should maybe enhance our .match() or introduce .find() so
that one can do e.find(tan(Wild("a"))) and it would return tan(x**2)
or whatever is in the expression. Then you can use .subs() to
substitute it.

Ondrej

Vinzent Steinberg

unread,
Mar 22, 2009, 6:39:23 AM3/22/09
to sympy
On Mar 21, 5:01 pm, Ondrej Certik <ond...@certik.cz> wrote:
> On Sat, Mar 21, 2009 at 5:58 AM, Vinzent Steinberg
>
> <vinzent.steinb...@googlemail.com> wrote:
>
> > What is the best way to replace tan(x) with sin(x)/cos(x)?
>
> > *.match() does not seem to be suited, because I don't care where tan
> > (x) occurs, I just want to replace it.
> > WildFunction() seems to be broken.
>
> That's a good question. You need to know the argument of tan(x). If
> it's just x, then:
> I would use:
>
> e.subs(tan(x), sin(x)/cos(x))

This is not general enough. Please note that you already can do

e.subs(tan, sin)

It would be great to improve this to use

a = Wild('a')
e.subs(tan(a), sin(a)/cos(a))

for replacing.

>
> I think we should maybe enhance our .match() or introduce .find() so
> that one can do e.find(tan(Wild("a"))) and it would return tan(x**2)
> or whatever is in the expression. Then you can use .subs() to
> substitute it.
>
> Ondrej

e.find() would be really useful, often you just want to find a term
and don't care whether it's nested in some other expressions.
It should be relatively easy to implement.

Vinzent

Vinzent Steinberg

unread,
Mar 22, 2009, 7:39:56 AM3/22/09
to sympy
On 22 Mrz., 11:39, Vinzent Steinberg
<vinzent.steinb...@googlemail.com> wrote:
> e.find() would be really useful, often you just want to find a term
> and don't care whether it's nested in some other expressions.
> It should be relatively easy to implement.
>

See http://code.google.com/p/sympy/issues/detail?id=1337 for a basic
implementation.

Vinzent

Ondrej Certik

unread,
Mar 24, 2009, 1:28:17 AM3/24/09
to sy...@googlegroups.com

Awesome, thanks for the work. If you had time to get it into a shape
and posted a patch, that'd help me a lot.

Thanks,
Ondrej

Vinzent Steinberg

unread,
Mar 24, 2009, 1:14:36 PM3/24/09
to sympy
On Mar 24, 6:28 am, Ondrej Certik <ond...@certik.cz> wrote:
> On Sun, Mar 22, 2009 at 4:39 AM, Vinzent Steinberg
>
> <vinzent.steinb...@googlemail.com> wrote:
>
> > On 22 Mrz., 11:39, Vinzent Steinberg
> > <vinzent.steinb...@googlemail.com> wrote:
> >> e.find() would be really useful, often you just want to find a term
> >> and don't care whether it's nested in some other expressions.
> >> It should be relatively easy to implement.
>
> > Seehttp://code.google.com/p/sympy/issues/detail?id=1337for a basic
> > implementation.
>
> Awesome, thanks for the work. If you had time to get it into a shape
> and posted a patch, that'd help me a lot.
>
> Thanks,
> Ondrej

I don't know whether I'll have time before the weekend, but definitely
I'm going post a patch.

Actually I implemented it because I wanted to play with the
trigonometric simplification of the paper kindly mentioned by
Akshay. :) For this, I need this functionality.

Vinzent
Reply all
Reply to author
Forward
0 new messages