Opened 11 years ago

Closed 8 years ago

# Taking the real part of a sum of exponentials with imaginary exponents gives wrong result

Reported by: Owned by: Iñaki García Etxebarria Burcin Erocal major sage-6.7 symbolics Ralf Stephan Travis Scrimshaw Fixed upstream, in a later stable release. 410bab7 410bab7e7016f06220e1a34335abbaa8f5e3e876 #18362

### Description

In sage 4.8 the following computation gives 1/2, while the right result is -3/2:

sage: a = exp(i*2*pi/3)
sage: b = exp(i*pi/3)
sage: c = 2*a-b
sage: c.real()
1/2

Doing the computation numerically gives the right result:

sage: N(c).real()
-1.50000000000000

and asking maxima directly also works:

sage: (c.maxima_methods().rectform()).real()
-3/2

This is sage 4.8 running on Fedora 16, linux 3.1.0-7.fc16.x86_64 SMP.

### comment:1 Changed 10 years ago by Burcin Erocal

Thanks for the report! This is a bug in Pynac. I opened a ticket here:

### comment:2 Changed 9 years ago by Jeroen Demeyer

Milestone: sage-5.11 → sage-5.12

### comment:3 Changed 9 years ago by For batch modifications

Milestone: sage-6.1 → sage-6.2

### comment:4 Changed 9 years ago by For batch modifications

Milestone: sage-6.2 → sage-6.3

### comment:5 Changed 8 years ago by For batch modifications

Milestone: sage-6.3 → sage-6.4

### comment:6 Changed 8 years ago by Karl-Dieter Crisman

This gets really weird, actually.

sage: a.real()
-1/2
sage: b.real()
1/2
sage: (a-b).real() # good
-1
sage: (a-2*b).real() # good
-3/2
sage: (a-3*b).real()  # ho-hum
-2
sage: (2*a-b).real() # yikes
1/2
sage: (3*a-b).real()  # what?
3/4
sage: (4*a-b).real()  # consistent...
1
sage: (5*a-b).real()  # ?!?!?
5/4
sage: (-a-b).real()  # seeing but not believing
-1/4
sage: (-2*a-b).real()
-1/2
sage: (-3*a-b).real()
-3/4
sage: (2*a).real()  # okay
-1
sage: (2*a+b).real()  #  okay
-1/2
sage: (3*a+b).real()
-1
sage: (4*a+b).real()
-3/2

This is consistent with Pynac somehow thinking that a.real()==1/4 when preceded by a positive constant and -1/4 when given a negative constant. But only when part of a sum including -b, with +b it's fine.

I tried to figure out what it was but so far no luck.

### comment:7 Changed 8 years ago by Ralf Stephan

Dependencies: → pynac-0.3.7 sage-6.4 → sage-6.7 N/A → Fixed upstream, in a later stable release.

Actually and surprisingly what's happening is that the simplified values of the exp expressions (1/2 and -1/2) and their coefficients are all multiplied together on final evaluation:

sage: (4*exp(i*pi/3)-4*exp(i*2*pi/3)).real_part()
4
sage: (6*exp(i*pi/3)-6*exp(i*2*pi/3)).real_part()
9

This is evident in the GiNaC source, this line(https://github.com/pynac/pynac/blob/master/ginac/add.cpp#L432):

oc = oc.mul(ex_to<numeric>(j->rest)).mul(ex_to<numeric>(j->coeff));

is so rarely executed that e.g. only three doctests in symbolic/ touch it but don't trigger the bug because the expressions are too short.

The fix will be in Pynac-0.3.7.

### comment:8 Changed 8 years ago by Ralf Stephan

Branch: → u/rws/taking_the_real_part_of_a_sum_of_exponentials_with_imaginary_exponents_gives_wrong_result

### comment:9 Changed 8 years ago by Ralf Stephan

Authors: → Ralf Stephan → 410bab7e7016f06220e1a34335abbaa8f5e3e876 pynac-0.3.7 → #18362 new → needs_review

New commits:

 ​23892cb 18362: upgrade to pynac-0.3.7; fix one changed doctest result ​410bab7 12807: doctest

### comment:10 Changed 8 years ago by Travis Scrimshaw

Reviewers: → Travis Scrimshaw needs_review → positive_review

LGTM.

### comment:11 Changed 8 years ago by Volker Braun

Branch: u/rws/taking_the_real_part_of_a_sum_of_exponentials_with_imaginary_exponents_gives_wrong_result → 410bab7e7016f06220e1a34335abbaa8f5e3e876 → fixed positive_review → closed
Note: See TracTickets for help on using tickets.