# Integrating with constant integrand

How can you specify an integral with a constant integrand? For example, I know that

f = x
f.integrate(x,0,1)


works fine, but

f = 1
f.integrate(x,0,1)


doesn't, since the Integer class has no integrate() method. (Should it?) I can get around this with something like

f = x-x+1
f.integrate(x,0,1)


but that seems awfully kludgey.

edit retag close merge delete

Sort by » oldest newest most voted

When you write:

sage: f = 1


You define f as the integer 1:

sage: f.parent()
Integer Ring


So, it is unlikely that we will define a .integrate() method for this, otherwise there will be too much methods for such a universal object. See

sage: f.<TAB>


to see how many methods there exist already for the integer 1. So i guess there should not be a .integrate() method for integers (or Python should have a mechanism to understand that it should try the integrate function, or something like that).

Note that x is a symbolic expression (and an element of the symbolic ring), hence it has an .integrate() method:

sage: x.parent()
Symbolic Ring
sage: type(x)
<type 'sage.symbolic.expression.Expression'>


Now, when you write:

sage: f = 1 + x - x


There is a coercion mechanism that transforms the integer 1 as an element of the symbolic ring so that it can be added to x, hence here f is a symbolic expression, not an integer:

sage: f.parent()
Symbolic Ring


This is why you can apply the .integrate() method.

So, there are two ways to integrate 1 easily. First, you can use the integrate() function:

sage: integrate(1,x,0,1)
1


Second, you can transform the integer 1 as an element of the symbolic ring:

sage: f = SR(1)
sage: f.integrate(x,0,1)
1

more

Thanks. I knew this was what was going on but did not know the systematic way to handle it. Your last suggestion (cast it to the symbolic ring) is exactly what I was looking for.

( 2015-10-21 15:47:53 -0600 )edit

Another straightforward way to define the constant function equal to one and to integrate it is:

sage: f(x) = 1
sage: f.integrate(x,0,1)
1

more