Ask Your Question

Integrating with constant integrand

asked 2015-10-21 20:20:59 +0200

Jeremy Martin gravatar image

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

f = x

works fine, but

f = 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

but that seems awfully kludgey.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2015-10-21 22:42:05 +0200

tmonteil gravatar image

updated 2015-10-21 22:44:27 +0200

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)

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

sage: f = SR(1)
sage: f.integrate(x,0,1)
edit flag offensive delete link 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.

Jeremy Martin gravatar imageJeremy Martin ( 2015-10-21 22:47:53 +0200 )edit

answered 2016-01-12 20:45:29 +0200

slelievre gravatar image

A complement to @tmonteil's answer.

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)
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: 2015-10-21 20:20:59 +0200

Seen: 612 times

Last updated: Jan 12 '16