Processing math: 100%
Ask Your Question
1

Function defined with an integral

asked 1 year ago

Sotto gravatar image

updated 1 year ago

FrédéricC gravatar image

Dear all, I'm puzzled by this answer from Sage:

sage: def f(x):
....:     return integral(exp(t^2)/sqrt(t),t,1,x^2)
sage: f(4).numerical_approx()
1.18428615262607e109 - 1.28184667602042*I

Why the answer is a complex number? How can I define f as a real function? Thanks.

Preview: (hide)

Comments

This just means that approximation works in complex numbers here. Note that the imaginary part is negligible as compared to the real part, and so there is nothing wrong. Since you know that the result is real, you can use f(4).numerical_approx().real() to get the appropriation in real numbers.

Max Alekseyev gravatar imageMax Alekseyev ( 1 year ago )

This can be a workaround:

def f(x):
    var('t')
    return real(integral(exp(t^2)/sqrt(t),t,1,x^2))

f(4).numerical_approx()
tolga gravatar imagetolga ( 1 year ago )

Also try this:

def f(x):
    var('t')
    return numerical_integral(exp(t^2)/sqrt(t),1,x^2)

f(4)

As you can see from the result (1.1842861526260861e+109, 4.7486208201754434e+95), the error bound is quite large. You can plot your integrand to see its behavior in the range of your interest.

tolga gravatar imagetolga ( 1 year ago )

[ Comment reposted as an answer... ]

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 1 year ago )

Thank you. So nothing wrong with this function… But why I receive an error message if I try to differentiate it?!? I understand that this is not the original question though… :-)

Sotto gravatar imageSotto ( 1 year ago )

1 Answer

Sort by » oldest newest most voted
1

answered 1 year ago

Emmanuel Charpentier gravatar image

updated 1 year ago

Complement to Max's and Tolga's comments :

sage: var("t")
t
sage: with assuming(x>1): (e^(t^2)/sqrt(t)).integrate(t, 1, x^2)
-1/2*(-1)^(3/4)*gamma(1/4, -1) + 1/2*(-x^4)^(3/4)*gamma(1/4, -x^4)/x^3

12(1)34Γ(14,1)+(x4)34Γ(14,x4)2x3

As you can see, this expression involves different inherently non-real complex factors (fractional powers of negative numbers, Γ with negative arguments), whose sums happens to be real. The computation of the numerical value is inexact, so a small inexactitude is enough to result in a numerically non-zero imaginary part.

Showing that this imaginary part is zero seems out of reach of current CASses...

EDIT : To illistrate Max's comment about derivation :

sage: with assuming(x>1): foo=(e^(t^2)/sqrt(t)).integrate(t, 1, x^2) ; foo
-1/2*(-1)^(3/4)*gamma(1/4, -1) + 1/2*(-x^4)^(3/4)*gamma(1/4, -x^4)/x^3
sage: foo.diff(x)
-2*(-x^4)^(3/4)*D[1](gamma)(1/4, -x^4) - 3/2*gamma(1/4, -x^4)/(-x^4)^(1/4) - 3/2*(-x^4)^(3/4)*gamma(1/4, -x^4)/x^4

2(x4)34D1(Γ)(14,x4)3Γ(14,x4)2(x4)143(x4)34Γ(14,x4)2x4

One notes that Sage does not explicites a derivative of Γ wrt one of its arguments...

Suspicious :

sage: giac.diff(*map(giac, (foo, x)))._sage_()
-3/2*gamma(1/4, -x^4)/(-x^4)^(1/4) - 3/2*(-x^4)^(3/4)*gamma(1/4, -x^4)/x^4 + 2*e^(x^4)

3Γ(14,x4)2(x4)143(x4)34Γ(14,x4)2x4+2e(x4)

Highly suspicious :

sage: fricas.D(*map(fricas, (foo, x)))._sage_()
2*e^(x^4)

and, BTW :

sage: foo._sympy_().diff(x._sympy_())._sage_()
2*e^(x^4)
sage: foo._mathematica_().D(x).FullSimplify().sage()
2*e^(x^4)

But these expressions tun out to be equal :

sage: (fricas.D(*map(fricas, (foo, x)))._sage_()-giac.diff(*map(giac, (foo, x)))._sage_())
3/2*gamma(1/4, -x^4)/(-x^4)^(1/4) + 3/2*(-x^4)^(3/4)*gamma(1/4, -x^4)/x^4
sage: (fricas.D(*map(fricas, (foo, x)))._sage_()-giac.diff(*map(giac, (foo, x)))._sage_()).factor()
0

and comparing the Fricas result to a (rough) numerical estimate of the derivative of the function shows a very good agreement : their quotient is 1 up to 106 on the range (1, 4) :

sage: plot(lambda u:((Foo(u+1e-8)-Foo(u))/1e-8).real()/(2*e^(u^4))-1, (1, 4))
Launched png viewer for Graphics object consisting of 1 graphics primitive

image description

Mathematica's, Fricas' and Giac's derivations seem to be exact. The Sage's Γ implementation may be perfected...

HTH,

Preview: (hide)
link

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 1 year ago

Seen: 208 times

Last updated: Nov 19 '23