ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 11 Nov 2021 22:17:27 +0100How am I using definite_integral wrong?https://ask.sagemath.org/question/59606/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.
Thanks in advance,
A beginnerSat, 06 Nov 2021 01:34:31 +0100https://ask.sagemath.org/question/59606/how-am-i-using-definite_integral-wrong/Answer by Emmanuel Charpentier for <p>I'm attempting to evaluate the definite integral of a symbolic function, and I'm getting a type error that I don't understand.</p>
<p>Here's my script:</p>
<pre><code>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)
</code></pre>
<p>The call to <code>print(type(hk))</code> returns <code><class 'sage.symbolic.expression.Expression'></code>, which is what I expect. However, the call to <code>definite_integral(hk, p1, 0, 1)</code> returns a lengthy error message featuring: </p>
<pre><code>TypeError: cannot coerce arguments: no canonical coercion from Callable function ring with arguments (p1, alpha, beta, xi) to Symbolic Ring
</code></pre>
<p>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.</p>
<p>Thanks in advance,
A beginner</p>
https://ask.sagemath.org/question/59606/how-am-i-using-definite_integral-wrong/?answer=59698#post-id-59698After 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...*
HTH,Thu, 11 Nov 2021 22:17:27 +0100https://ask.sagemath.org/question/59606/how-am-i-using-definite_integral-wrong/?answer=59698#post-id-59698