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.Mon, 18 Jul 2016 06:58:36 -0500Composite Number Sum Errorhttps://ask.sagemath.org/question/34100/composite-number-sum-error/New to sage, although familiar with a few other coding langages. Trying to obtain a sum of alternating terms. Any idea on this one? Thanks in advance.
def a(n) : return [k for k in (1..n) if not is_prime(k)]
from mpmath import *
mp.dps = 15
mp.pretty = True
nsum(lambda n: (1)/(a(n)), [1,5000], method='alternating')
Error Received:
TypeError: unable to coerce <type 'sage.libs.mpmath.ext_main.mpf'>
to an integer
Fri, 15 Jul 2016 20:55:47 -0500https://ask.sagemath.org/question/34100/composite-number-sum-error/Comment by redbaron01 for <p>New to sage, although familiar with a few other coding langages. Trying to obtain a sum of alternating terms. Any idea on this one? Thanks in advance.</p>
<pre><code>def a(n) : return [k for k in (1..n) if not is_prime(k)]
from mpmath import *
mp.dps = 15
mp.pretty = True
nsum(lambda n: (1)/(a(n)), [1,5000], method='alternating')
</code></pre>
<p>Error Received: </p>
<pre><code>TypeError: unable to coerce <type 'sage.libs.mpmath.ext_main.mpf'>
to an integer
</code></pre>
https://ask.sagemath.org/question/34100/composite-number-sum-error/?comment=34108#post-id-34108OEIS A269229. The sum of ((-1)^n) / x(n) where x(n) is the next composite (non-prime) number.Sat, 16 Jul 2016 09:04:17 -0500https://ask.sagemath.org/question/34100/composite-number-sum-error/?comment=34108#post-id-34108Comment by slelievre for <p>New to sage, although familiar with a few other coding langages. Trying to obtain a sum of alternating terms. Any idea on this one? Thanks in advance.</p>
<pre><code>def a(n) : return [k for k in (1..n) if not is_prime(k)]
from mpmath import *
mp.dps = 15
mp.pretty = True
nsum(lambda n: (1)/(a(n)), [1,5000], method='alternating')
</code></pre>
<p>Error Received: </p>
<pre><code>TypeError: unable to coerce <type 'sage.libs.mpmath.ext_main.mpf'>
to an integer
</code></pre>
https://ask.sagemath.org/question/34100/composite-number-sum-error/?comment=34107#post-id-34107What sum are you trying to compute?Sat, 16 Jul 2016 08:59:42 -0500https://ask.sagemath.org/question/34100/composite-number-sum-error/?comment=34107#post-id-34107Answer by tmonteil for <p>New to sage, although familiar with a few other coding langages. Trying to obtain a sum of alternating terms. Any idea on this one? Thanks in advance.</p>
<pre><code>def a(n) : return [k for k in (1..n) if not is_prime(k)]
from mpmath import *
mp.dps = 15
mp.pretty = True
nsum(lambda n: (1)/(a(n)), [1,5000], method='alternating')
</code></pre>
<p>Error Received: </p>
<pre><code>TypeError: unable to coerce <type 'sage.libs.mpmath.ext_main.mpf'>
to an integer
</code></pre>
https://ask.sagemath.org/question/34100/composite-number-sum-error/?answer=34110#post-id-34110Let me try to mimic you approach in a Pythonic way:
First you want to create the list of composite numbers. Since the convergence of your series is very slow, storing enough composite numbers will eat all your memory. So you should write an *iterator* instead (search the web for this notion in Python), `NN` denotes the nonnegative numbers, it is an iterator too:
sage: composites = (i for i in NN if i>3 and not i.is_prime())
sage: composites.next()
4
sage: composites.next()
6
sage: composites.next()
8
sage: composites.next()
9
sage: composites.next()
10
sage: composites.next()
12
sage: composites.next()
14
sage: composites.next()
15
Then, you can add the elements of your sequence one by one, starting from zero:
sage: s = RDF(0)
sage: for i in xrange(0,1000000):
....: s += (-1)^i / composites.next()
....: print s
Note that if you restart the computation, you have to redefine `composites`, since otherwise it will continue to be exhausted from its state, not from the beginning.
Note that the convergence is very slow and the computation above is not exact, so some errors might add up. You could :
- use rational numbers and make exact computations (but their size will explode)
- increase the precision of the real field
- use interval arithmetic to have a guarantee of your errors
Also, note that the variable `i` is only here to make the sign oscilate, so it is not very useful to compute `(-1)^i`, instead you can introduce a `sign` variable and let it alternate at each loop. Also, you can compute them two by two:
sage: while True:
....: s += 1/composites.next() - 1/composites.next()
....: print s
**EDIT** Regarding the use of `RLF`, this is a kind of overlay to get numerical values of some other representation of real numbers. In this case, since you start from an integer 0 and only make integer divisions, the element `s` will be a rational number. You can check this by doing:
sage: s._value
<big fraction>
sage: s._value.parent()
Rational Field
The good is that s will always be exact, so you do not have to deal with the accumulation of rounding errors that might end-up in some loss of precision that the end of the loop. The bad is that, since your loop must be huge before giving enough digits to the searched number (remember that the confergence of this alternating non-absolutely convergent series is very slow), the rational number `s` will have bigger and bigger numerator and denominator, so the computations will become slower and slower and it will eat your memory. So, it depends on how far you plan to go with your loop.
Sat, 16 Jul 2016 13:11:50 -0500https://ask.sagemath.org/question/34100/composite-number-sum-error/?answer=34110#post-id-34110Comment by tmonteil for <p>Let me try to mimic you approach in a Pythonic way:</p>
<p>First you want to create the list of composite numbers. Since the convergence of your series is very slow, storing enough composite numbers will eat all your memory. So you should write an <em>iterator</em> instead (search the web for this notion in Python), <code>NN</code> denotes the nonnegative numbers, it is an iterator too:</p>
<pre><code>sage: composites = (i for i in NN if i>3 and not i.is_prime())
sage: composites.next()
4
sage: composites.next()
6
sage: composites.next()
8
sage: composites.next()
9
sage: composites.next()
10
sage: composites.next()
12
sage: composites.next()
14
sage: composites.next()
15
</code></pre>
<p>Then, you can add the elements of your sequence one by one, starting from zero:</p>
<pre><code>sage: s = RDF(0)
sage: for i in xrange(0,1000000):
....: s += (-1)^i / composites.next()
....: print s
</code></pre>
<p>Note that if you restart the computation, you have to redefine <code>composites</code>, since otherwise it will continue to be exhausted from its state, not from the beginning.</p>
<p>Note that the convergence is very slow and the computation above is not exact, so some errors might add up. You could :</p>
<ul>
<li>use rational numbers and make exact computations (but their size will explode)</li>
<li>increase the precision of the real field</li>
<li>use interval arithmetic to have a guarantee of your errors</li>
</ul>
<p>Also, note that the variable <code>i</code> is only here to make the sign oscilate, so it is not very useful to compute <code>(-1)^i</code>, instead you can introduce a <code>sign</code> variable and let it alternate at each loop. Also, you can compute them two by two:</p>
<pre><code>sage: while True:
....: s += 1/composites.next() - 1/composites.next()
....: print s
</code></pre>
<p><strong>EDIT</strong> Regarding the use of <code>RLF</code>, this is a kind of overlay to get numerical values of some other representation of real numbers. In this case, since you start from an integer 0 and only make integer divisions, the element <code>s</code> will be a rational number. You can check this by doing:</p>
<pre><code>sage: s._value
<big fraction>
sage: s._value.parent()
Rational Field
</code></pre>
<p>The good is that s will always be exact, so you do not have to deal with the accumulation of rounding errors that might end-up in some loss of precision that the end of the loop. The bad is that, since your loop must be huge before giving enough digits to the searched number (remember that the confergence of this alternating non-absolutely convergent series is very slow), the rational number <code>s</code> will have bigger and bigger numerator and denominator, so the computations will become slower and slower and it will eat your memory. So, it depends on how far you plan to go with your loop.</p>
https://ask.sagemath.org/question/34100/composite-number-sum-error/?comment=34124#post-id-34124Also, note that writing `RealField(300)(s)` is not changing `s`, it is just returning another object. Note also that `RLF` and `RealField(300)(s)` are very different objects.Mon, 18 Jul 2016 06:58:36 -0500https://ask.sagemath.org/question/34100/composite-number-sum-error/?comment=34124#post-id-34124Comment by tmonteil for <p>Let me try to mimic you approach in a Pythonic way:</p>
<p>First you want to create the list of composite numbers. Since the convergence of your series is very slow, storing enough composite numbers will eat all your memory. So you should write an <em>iterator</em> instead (search the web for this notion in Python), <code>NN</code> denotes the nonnegative numbers, it is an iterator too:</p>
<pre><code>sage: composites = (i for i in NN if i>3 and not i.is_prime())
sage: composites.next()
4
sage: composites.next()
6
sage: composites.next()
8
sage: composites.next()
9
sage: composites.next()
10
sage: composites.next()
12
sage: composites.next()
14
sage: composites.next()
15
</code></pre>
<p>Then, you can add the elements of your sequence one by one, starting from zero:</p>
<pre><code>sage: s = RDF(0)
sage: for i in xrange(0,1000000):
....: s += (-1)^i / composites.next()
....: print s
</code></pre>
<p>Note that if you restart the computation, you have to redefine <code>composites</code>, since otherwise it will continue to be exhausted from its state, not from the beginning.</p>
<p>Note that the convergence is very slow and the computation above is not exact, so some errors might add up. You could :</p>
<ul>
<li>use rational numbers and make exact computations (but their size will explode)</li>
<li>increase the precision of the real field</li>
<li>use interval arithmetic to have a guarantee of your errors</li>
</ul>
<p>Also, note that the variable <code>i</code> is only here to make the sign oscilate, so it is not very useful to compute <code>(-1)^i</code>, instead you can introduce a <code>sign</code> variable and let it alternate at each loop. Also, you can compute them two by two:</p>
<pre><code>sage: while True:
....: s += 1/composites.next() - 1/composites.next()
....: print s
</code></pre>
<p><strong>EDIT</strong> Regarding the use of <code>RLF</code>, this is a kind of overlay to get numerical values of some other representation of real numbers. In this case, since you start from an integer 0 and only make integer divisions, the element <code>s</code> will be a rational number. You can check this by doing:</p>
<pre><code>sage: s._value
<big fraction>
sage: s._value.parent()
Rational Field
</code></pre>
<p>The good is that s will always be exact, so you do not have to deal with the accumulation of rounding errors that might end-up in some loss of precision that the end of the loop. The bad is that, since your loop must be huge before giving enough digits to the searched number (remember that the confergence of this alternating non-absolutely convergent series is very slow), the rational number <code>s</code> will have bigger and bigger numerator and denominator, so the computations will become slower and slower and it will eat your memory. So, it depends on how far you plan to go with your loop.</p>
https://ask.sagemath.org/question/34100/composite-number-sum-error/?comment=34123#post-id-34123I edited my answer about RLF to have more space.Mon, 18 Jul 2016 06:55:45 -0500https://ask.sagemath.org/question/34100/composite-number-sum-error/?comment=34123#post-id-34123Comment by redbaron01 for <p>Let me try to mimic you approach in a Pythonic way:</p>
<p>First you want to create the list of composite numbers. Since the convergence of your series is very slow, storing enough composite numbers will eat all your memory. So you should write an <em>iterator</em> instead (search the web for this notion in Python), <code>NN</code> denotes the nonnegative numbers, it is an iterator too:</p>
<pre><code>sage: composites = (i for i in NN if i>3 and not i.is_prime())
sage: composites.next()
4
sage: composites.next()
6
sage: composites.next()
8
sage: composites.next()
9
sage: composites.next()
10
sage: composites.next()
12
sage: composites.next()
14
sage: composites.next()
15
</code></pre>
<p>Then, you can add the elements of your sequence one by one, starting from zero:</p>
<pre><code>sage: s = RDF(0)
sage: for i in xrange(0,1000000):
....: s += (-1)^i / composites.next()
....: print s
</code></pre>
<p>Note that if you restart the computation, you have to redefine <code>composites</code>, since otherwise it will continue to be exhausted from its state, not from the beginning.</p>
<p>Note that the convergence is very slow and the computation above is not exact, so some errors might add up. You could :</p>
<ul>
<li>use rational numbers and make exact computations (but their size will explode)</li>
<li>increase the precision of the real field</li>
<li>use interval arithmetic to have a guarantee of your errors</li>
</ul>
<p>Also, note that the variable <code>i</code> is only here to make the sign oscilate, so it is not very useful to compute <code>(-1)^i</code>, instead you can introduce a <code>sign</code> variable and let it alternate at each loop. Also, you can compute them two by two:</p>
<pre><code>sage: while True:
....: s += 1/composites.next() - 1/composites.next()
....: print s
</code></pre>
<p><strong>EDIT</strong> Regarding the use of <code>RLF</code>, this is a kind of overlay to get numerical values of some other representation of real numbers. In this case, since you start from an integer 0 and only make integer divisions, the element <code>s</code> will be a rational number. You can check this by doing:</p>
<pre><code>sage: s._value
<big fraction>
sage: s._value.parent()
Rational Field
</code></pre>
<p>The good is that s will always be exact, so you do not have to deal with the accumulation of rounding errors that might end-up in some loss of precision that the end of the loop. The bad is that, since your loop must be huge before giving enough digits to the searched number (remember that the confergence of this alternating non-absolutely convergent series is very slow), the rational number <code>s</code> will have bigger and bigger numerator and denominator, so the computations will become slower and slower and it will eat your memory. So, it depends on how far you plan to go with your loop.</p>
https://ask.sagemath.org/question/34100/composite-number-sum-error/?comment=34113#post-id-34113This is brilliant - thank you! Would you recommend switching from RDF to RLF and defining the precision of the real field in a way similar to the below method:
composites = (i for i in NN if i>3 and not i.is_prime())
s = RLF(0); s
RealField(300)(s)
for i in xrange(0,5000): s += (-1)^i / composites.next()
print sSat, 16 Jul 2016 18:47:16 -0500https://ask.sagemath.org/question/34100/composite-number-sum-error/?comment=34113#post-id-34113