Ask Your Question

How am I using definite_integral wrong?

asked 2021-11-08 17:05:26 +0200

corvax gravatar image

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
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.

Thanks in advance, A beginner

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2021-11-11 22:17:27 +0200

Emmanuel Charpentier gravatar image

After executing the start of your code :

sage: h
(p1, alpha, beta) |--> 1/(p1^(2/3)*(-p1 + 1)^(2/3))
sage: k`k`
(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...


edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools

1 follower


Asked: 2021-11-06 01:34:31 +0200

Seen: 249 times

Last updated: Nov 11 '21