integral of exp(-x^2)*cos(x) from -pi to pi is complex?

13 views
Skip to first unread message

Dan Drake

unread,
Oct 8, 2011, 1:03:45 AM10/8/11
to sage-s...@googlegroups.com
This is strange:

sage: (integrate(exp(-x^2)*cos(x),(x,-pi,pi))/pi).n()

complains that "Unable to convert 3.14159265358979 - 0.500000000000000*I
to float". Which is odd, given that I'm integrating a real-valued
function. But it gets weirder: if I try to use .abs(), it *still* complains:

sage: (integrate(exp(-x^2)*cos(x),(x,-pi,pi))/pi).abs().n()

...same error.

Meanwhile:

sage: numerical_integral(exp(-x^2)*cos(x), (-pi,pi))
(1.3804038617166086, 1.5567746861731315e-14)

I'm guessing this is a Maxima problem. Any ideas what's going on?

Dan

--
--- Dan Drake
----- http://mathsci.kaist.ac.kr/~drake
-------

signature.asc

achrzesz

unread,
Oct 8, 2011, 4:05:31 AM10/8/11
to sage-support
The exact integral

1/2*(erf(pi - 1/2*I) + erf(pi + 1/2*I))*sqrt(pi)*e^(-1/4)

contains *real* expression

(erf(pi - 1/2*I) + erf(pi + 1/2*I))

but maxima/sage can't check that it is real

In WolframAlpha one can check that

Im[Erf[Pi-1/2*I]+Erf[Pi+1/2*I]]

iz zero

Andrzej Chrzeszczyk

Dan Drake

unread,
Oct 8, 2011, 11:26:46 PM10/8/11
to sage-s...@googlegroups.com

Hrm...but I get:

(erf(pi - 1/2*I) + erf(pi + 1/2*I)).n()

resulting in "TypeError: Unable to convert 3.14159265358979 -
0.500000000000000*I", which, as I've figured out, comes from the first
erf(). It seems like .n() should return CC values when necessary, but
the obvious workaround doesn't, well, work around the problem:

erf(pi - 1/2*I).real_part().n()

gives the same TypeError -- and suggests I use real_part()! Neither

CC(erf(pi - 1/2*I))

nor

CC(erf(pi - 1/2*I).real_part(), erf(pi - 1/2*I).imag_part())

work. We need to work on the numerical approximation stuff for the error
function!

signature.asc

achrzesz

unread,
Oct 9, 2011, 2:38:37 AM10/9/11
to sage-support

>
> We need to work on the numerical approximation stuff for the error
> function!
>
> Dan



More generally
in my opinion n() in complex domain needs improvement
For example (as I have mentioned)

sage: ((-1)^(1/3)).n()
0.500000000000000 + 0.866025403784439*I
leads to wrong solution
sage: s=solve(3*x^3-9*x+10,x,solution_dict=True)
sage: ns=[n(s[k][x]) for k in range(3)]
sage: ns
[1.06780542232902 - 1.84949324407141*I, 0.0277635108030695 +
1.24902476648341*I, -1.09556893313209 + 0.600468477588001*I] # no real
solution, no conjugate solutions


Andrzej Chrzeszczyk

achrzesz

unread,
Oct 23, 2011, 7:42:19 AM10/23/11
to sage-support
Fortunately I was wrong:

sage: w=integrate(exp(-x^2)*cos(x),(x,-pi,pi))/pi
sage: w.imag().full_simplify()
0

Jeroen Demeyer

unread,
Oct 24, 2011, 12:52:42 PM10/24/11
to sage-s...@googlegroups.com
On 2011-10-09 05:26, Dan Drake wrote:
> We need to work on the numerical approximation stuff for the error
> function!

Ticket #11948:
http://trac.sagemath.org/sage_trac/ticket/11948

(I have a preliminary but not properly tested patch)

Reply all
Reply to author
Forward
0 new messages