# How am I using definite_integral wrong?

I'm attempting to evaluate the definite integral of a symbolic function, and I'm getting a type error that I don't understand.

Here's my script:

from sage.symbolic.integration.integral import definite_integral

### random variables
xi = var('xi'); assume(xi >= 0)
tau, eta = var('tau', 'eta'); assume(tau > 0); assume(eta > 0)
p1 = var('p1'); assume(p1 >= 0); assume(p1 <=1)

### expressions
h(p1, alpha, beta) = p1^(1/3 - 1) * (1 - p1)^(1/3 - 1)
k(p1, xi) = (1 - exp(-p1 * xi)) * exp(-p1 * xi)
hk = h * k

### evaluate
print(hk)
print(type(hk))
definite_integral(hk, p1, 0, 1)


The call to print(type(hk)) returns <class 'sage.symbolic.expression.Expression'>, which is what I expect. However, the call to definite_integral(hk, p1, 0, 1) returns a lengthy error message featuring:

TypeError: cannot coerce arguments: no canonical coercion from Callable function ring with arguments (p1, alpha, beta, xi) to Symbolic Ring


I'm not sure what's going on with the types here, and I'd like to understand that so I can get this to work and also avoid making such mistakes in the future.

edit retag close merge delete

Sort by » oldest newest most voted

After executing the start of your code :

sage: h
(p1, alpha, beta) |--> 1/(p1^(2/3)*(-p1 + 1)^(2/3))
sage: kk
(p1, xi) |--> -(e^(-p1*xi) - 1)*e^(-p1*xi)
sage: hk
(p1, alpha, beta, xi) |--> -(e^(-p1*xi) - 1)*e^(-p1*xi)/(p1^(2/3)*(-p1 + 1)^(2/3))


Both h and k are symbolic functions (more precisely callable symbolic expressions needing respectively three and four arguments. But so is hk which needs four arguments.

iuntegrate works on symbolic expressions, not on such functions. What you intended is probably :

sage: definite_integral(hk(p1, alpha, beta, xi), p1, 0, 1)
integrate((e^(p1*xi) - 1)*e^(-2*p1*xi)/(p1^(2/3)*(-p1 + 1)^(2/3)), p1, 0, 1)


This integral turns out to be undoable by the four built-in integrators ; mathematica_free fails to return anything. However, Mathematica boasts :

sage: mathematica.Integrate(hk(p1, alpha, beta, xi), (p1, 0, 1)).sage()
sqrt(pi)*(bessel_I(-1/6, 1/2*xi)*e^(1/2*xi) - 2^(1/6)*bessel_I(-1/6, xi))*xi^(1/6)*e^(-xi)*gamma(1/3)


Note, however, that Mathematica fails to integrate the indefinite integral :

sage: mathematica.Integrate(hk(p1, alpha, beta, xi), p1)
-Integrate[(-1 + E^(-(p1*xi)))/(E^(p1*xi)*(1 - p1)^(2/3)*p1^(2/3)), p1]


which renders impossible the verification by re-differentiation. The only possible "raincheck" would be to plot the numerical value of the Mathematica ex^ression againts the numerical integration for various value of xi, and to check that these value do not depend on alpha nor beta...

Summary : Here may be tygers...

HTH,

more