Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Hello, @Nasser. This behavior is related to the code of the integrate function itself. If you execute integrate?? in your Sage session, you will be able to see the docstring of the subroutine, and its source code. We are interested in the source code right now:

if hasattr(x, 'integral'):
    return x.integral(*args, **kwds)
else:
    from sage.symbolic.ring import SR
    return SR(x).integral(*args, **kwds)

This basically verifies if the object you passed as argument (x in this code) has the integral attribute, which is nothing more than a subroutine that specifies how x should be integrated. If x doesn't have such an attribute defined, then Sage converts it into an element of the Symbolic Ring (SR).

In the case of your example, the string "x" does not have an integral attribute (since it's a string), so Sage calls SR('x'), which converts your string into the identity function, i.e. $x\mapsto x$, whose integral is $1/2x^2$.

As particular examples, you can try:

f(x) = SR("x")
f(2)

This will return 2, as expected, showing you that you have actually define a function from its string representation. Now try:

(f(x)).integral(x)

which is equivalent to integrate(f(x), x) or integrate("x", x) of (SR("x")).integral(x). Finally, try:

f(x) = SR("sin(x)")
f(pi)
(f(x)).integral(x)
integrate("sin(x)", x)

I hope this helps!

Hello, @Nasser. This behavior is related to the code of the integrate function itself. If you execute integrate?? in your Sage session, you will be able to see the docstring of the subroutine, and its source code. We are interested in the source code right now:

if hasattr(x, 'integral'):
    return x.integral(*args, **kwds)
else:
    from sage.symbolic.ring import SR
    return SR(x).integral(*args, **kwds)

This basically verifies if the object you passed as argument (x in this code) has the integral attribute, which is nothing more than a subroutine that specifies how x should be integrated. If x doesn't have such an attribute defined, then Sage converts it into an element of the Symbolic Ring (SR).

In the case of your example, the string "x" does not have an integral attribute (since it's a string), so Sage calls SR('x'), which converts your string into the identity function, i.e. $x\mapsto x$, whose integral is $1/2x^2$.

As particular examples, you can try:

f(x) = SR("x")
f(2)

This will return 2, as expected, showing you that you have actually define a function from its string representation. Now try:

(f(x)).integral(x)

which is equivalent to integrate(f(x), x) or integrate("x", x) of (SR("x")).integral(x). Finally, try:

f(x) = SR("sin(x)")
f(pi)
(f(x)).integral(x)
integrate("sin(x)", x)

EDIT Based on the answer to this question, I must point out that the use of strings as functions, by means of the Symbolic Ring SR, is discourage and should be avoided, because it has unpleasant consequences. This kind of use of strings seems not to be documented for that reason.

I hope this helps!