ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 12 Jan 2016 13:45:29 -0600Integrating with constant integrandhttps://ask.sagemath.org/question/30185/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.Wed, 21 Oct 2015 13:20:59 -0500https://ask.sagemath.org/question/30185/integrating-with-constant-integrand/Answer by slelievre for <p>How can you specify an integral with a constant integrand? For example, I know that</p>
<pre><code>f = x
f.integrate(x,0,1)
</code></pre>
<p>works fine, but</p>
<pre><code>f = 1
f.integrate(x,0,1)
</code></pre>
<p>doesn't, since the Integer class has no integrate() method. (Should it?) I can get around this with something like</p>
<pre><code>f = x-x+1
f.integrate(x,0,1)
</code></pre>
<p>but that seems awfully kludgey.</p>
https://ask.sagemath.org/question/30185/integrating-with-constant-integrand/?answer=32175#post-id-32175A 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)
1
Tue, 12 Jan 2016 13:45:29 -0600https://ask.sagemath.org/question/30185/integrating-with-constant-integrand/?answer=32175#post-id-32175Answer by tmonteil for <p>How can you specify an integral with a constant integrand? For example, I know that</p>
<pre><code>f = x
f.integrate(x,0,1)
</code></pre>
<p>works fine, but</p>
<pre><code>f = 1
f.integrate(x,0,1)
</code></pre>
<p>doesn't, since the Integer class has no integrate() method. (Should it?) I can get around this with something like</p>
<pre><code>f = x-x+1
f.integrate(x,0,1)
</code></pre>
<p>but that seems awfully kludgey.</p>
https://ask.sagemath.org/question/30185/integrating-with-constant-integrand/?answer=30188#post-id-30188When 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
Wed, 21 Oct 2015 15:42:05 -0500https://ask.sagemath.org/question/30185/integrating-with-constant-integrand/?answer=30188#post-id-30188Comment by Jeremy Martin for <p>When you write:</p>
<pre><code>sage: f = 1
</code></pre>
<p>You define <code>f</code> as the integer <code>1</code>:</p>
<pre><code>sage: f.parent()
Integer Ring
</code></pre>
<p>So, it is unlikely that we will define a <code>.integrate()</code> method for this, otherwise there will be too much methods for such a universal object. See </p>
<pre><code>sage: f.<TAB>
</code></pre>
<p>to see how many methods there exist already for the integer <code>1</code>. So i guess there should not be a <code>.integrate()</code> method for integers (or Python should have a mechanism to understand that it should try the <code>integrate</code> function, or something like that).</p>
<p>Note that <code>x</code> is a symbolic expression (and an element of the symbolic ring), hence it has an <code>.integrate()</code> method:</p>
<pre><code>sage: x.parent()
Symbolic Ring
sage: type(x)
<type 'sage.symbolic.expression.Expression'>
</code></pre>
<p>Now, when you write:</p>
<pre><code>sage: f = 1 + x - x
</code></pre>
<p>There is a coercion mechanism that transforms the integer <code>1</code> as an element of the symbolic ring so that it can be added to <code>x</code>, hence here <code>f</code> is a symbolic expression, not an integer:</p>
<pre><code>sage: f.parent()
Symbolic Ring
</code></pre>
<p>This is why you can apply the <code>.integrate()</code> method.</p>
<p>So, there are two ways to integrate <code>1</code> easily. First, you can use the <code>integrate()</code> function:</p>
<pre><code>sage: integrate(1,x,0,1)
1
</code></pre>
<p>Second, you can transform the integer 1 as an element of the symbolic ring:</p>
<pre><code>sage: f = SR(1)
sage: f.integrate(x,0,1)
1
</code></pre>
https://ask.sagemath.org/question/30185/integrating-with-constant-integrand/?comment=30189#post-id-30189Thanks. 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.Wed, 21 Oct 2015 15:47:53 -0500https://ask.sagemath.org/question/30185/integrating-with-constant-integrand/?comment=30189#post-id-30189