Opened 11 years ago

Closed 8 years ago

#12807 closed defect (fixed)

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

Reported by: Iñaki García Etxebarria Owned by: Burcin Erocal
Priority: major Milestone: sage-6.7
Component: symbolics Keywords:
Cc: Merged in:
Authors: Ralf Stephan Reviewers: Travis Scrimshaw
Report Upstream: Fixed upstream, in a later stable release. Work issues:
Branch: 410bab7 (Commits, GitHub, GitLab) Commit: 410bab7e7016f06220e1a34335abbaa8f5e3e876
Dependencies: #18362 Stopgaps:

Status badges


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()

Doing the computation numerically gives the right result:

sage: N(c).real()

and asking maxima directly also works:

sage: (c.maxima_methods().rectform()).real()

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

Change History (11)

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.11sage-5.12

comment:3 Changed 9 years ago by For batch modifications

Milestone: sage-6.1sage-6.2

comment:4 Changed 9 years ago by For batch modifications

Milestone: sage-6.2sage-6.3

comment:5 Changed 8 years ago by For batch modifications

Milestone: sage-6.3sage-6.4

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

This gets really weird, actually.

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

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
Milestone: sage-6.4sage-6.7
Report Upstream: N/AFixed 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()
sage: (6*exp(i*pi/3)-6*exp(i*2*pi/3)).real_part()

This is evident in the GiNaC source, this line(

    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
Commit: 410bab7e7016f06220e1a34335abbaa8f5e3e876
Dependencies: pynac-0.3.7#18362
Status: newneeds_review

New commits:

23892cb18362: upgrade to pynac-0.3.7; fix one changed doctest result
410bab712807: doctest

comment:10 Changed 8 years ago by Travis Scrimshaw

Reviewers: Travis Scrimshaw
Status: needs_reviewpositive_review


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_result410bab7e7016f06220e1a34335abbaa8f5e3e876
Resolution: fixed
Status: positive_reviewclosed
Note: See TracTickets for help on using tickets.