Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
sagetrac-inaki mannequin opened this issue Apr 4, 2012 · 13 comments
Closed

Comments

@sagetrac-inaki
Copy link
Mannequin

sagetrac-inaki mannequin commented Apr 4, 2012

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.

Depends on #18362

Upstream: Fixed upstream, in a later stable release.

Component: symbolics

Author: Ralf Stephan

Branch/Commit: 410bab7

Reviewer: Travis Scrimshaw

Issue created by migration from https://trac.sagemath.org/ticket/12807

@sagetrac-inaki sagetrac-inaki mannequin added this to the sage-5.11 milestone Apr 4, 2012
@sagetrac-inaki sagetrac-inaki mannequin assigned burcin Apr 4, 2012
@burcin
Copy link

burcin commented Nov 22, 2012

comment:1

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

http://hg.pynac.org/pynac/issue/15/wrong-result-from-real-part

@jdemeyer jdemeyer modified the milestones: sage-5.11, sage-5.12 Aug 13, 2013
@sagetrac-vbraun-spam sagetrac-vbraun-spam mannequin modified the milestones: sage-6.1, sage-6.2 Jan 30, 2014
@sagetrac-vbraun-spam sagetrac-vbraun-spam mannequin modified the milestones: sage-6.2, sage-6.3 May 6, 2014
@sagetrac-vbraun-spam sagetrac-vbraun-spam mannequin modified the milestones: sage-6.3, sage-6.4 Aug 10, 2014
@kcrisman
Copy link
Member

comment:6

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.

@rwst
Copy link

rwst commented May 3, 2015

comment:7

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.

@rwst
Copy link

rwst commented May 3, 2015

Dependencies: pynac-0.3.7

@rwst
Copy link

rwst commented May 3, 2015

Upstream: Fixed upstream, in a later stable release.

@rwst rwst modified the milestones: sage-6.4, sage-6.7 May 3, 2015
@rwst
Copy link

rwst commented May 4, 2015

@rwst
Copy link

rwst commented May 4, 2015

New commits:

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

@rwst
Copy link

rwst commented May 4, 2015

Commit: 410bab7

@rwst
Copy link

rwst commented May 4, 2015

Author: Ralf Stephan

@rwst
Copy link

rwst commented May 4, 2015

Changed dependencies from pynac-0.3.7 to #18362

@tscrim
Copy link
Collaborator

tscrim commented May 10, 2015

comment:10

LGTM.

@tscrim
Copy link
Collaborator

tscrim commented May 10, 2015

Reviewer: Travis Scrimshaw

@vbraun
Copy link
Member

vbraun commented May 14, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants