ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 21 Apr 2016 17:26:22 +0200rational numbers with cythonhttps://ask.sagemath.org/question/33154/rational-numbers-with-cython/ I want to work efficiently with rational numbers under the context %cython but I don`t know how. Can anyone suggest any ideas? Thank you.
An example:
%cython
def rational_partitions(n):
sol = [i/n for i in range(n)]
for a in sol[0:-1]:
for b in sol[1:]:
k=2
while abs(b-a)/k>1/n:
sol.append(abs(b-a)/k)
k += 1
return sol
rational_partitions(10)Wed, 20 Apr 2016 17:07:20 +0200https://ask.sagemath.org/question/33154/rational-numbers-with-cython/Comment by slelievre for <p>I want to work efficiently with rational numbers under the context %cython but I don`t know how. Can anyone suggest any ideas? Thank you.</p>
<p>An example:</p>
<pre><code> %cython
def rational_partitions(n):
sol = [i/n for i in range(n)]
for a in sol[0:-1]:
for b in sol[1:]:
k=2
while abs(b-a)/k>1/n:
sol.append(abs(b-a)/k)
k += 1
return sol
rational_partitions(10)
</code></pre>
https://ask.sagemath.org/question/33154/rational-numbers-with-cython/?comment=33155#post-id-33155Can you say a bit more about what you want to do? Or give some Sage code that you would like to cythonize?Wed, 20 Apr 2016 19:11:11 +0200https://ask.sagemath.org/question/33154/rational-numbers-with-cython/?comment=33155#post-id-33155Comment by rafarob32 for <p>I want to work efficiently with rational numbers under the context %cython but I don`t know how. Can anyone suggest any ideas? Thank you.</p>
<p>An example:</p>
<pre><code> %cython
def rational_partitions(n):
sol = [i/n for i in range(n)]
for a in sol[0:-1]:
for b in sol[1:]:
k=2
while abs(b-a)/k>1/n:
sol.append(abs(b-a)/k)
k += 1
return sol
rational_partitions(10)
</code></pre>
https://ask.sagemath.org/question/33154/rational-numbers-with-cython/?comment=33157#post-id-33157I re-edited with an example. Thanks.Wed, 20 Apr 2016 19:41:05 +0200https://ask.sagemath.org/question/33154/rational-numbers-with-cython/?comment=33157#post-id-33157Comment by nbruin for <p>I want to work efficiently with rational numbers under the context %cython but I don`t know how. Can anyone suggest any ideas? Thank you.</p>
<p>An example:</p>
<pre><code> %cython
def rational_partitions(n):
sol = [i/n for i in range(n)]
for a in sol[0:-1]:
for b in sol[1:]:
k=2
while abs(b-a)/k>1/n:
sol.append(abs(b-a)/k)
k += 1
return sol
rational_partitions(10)
</code></pre>
https://ask.sagemath.org/question/33154/rational-numbers-with-cython/?comment=33163#post-id-33163Note that cython does provide a limited "machine integer" type in the form of C int or C long, which behave different from python ints and sage Integers in that they silently overflow rather than use multiprecision arithmetic. Cython does NOT provide such a type for rational arithmetic because C does not provide it. When you do arithmetic with rational numbers, overflow happens remarkably easily, so having fixed bitlength rational numbers would probably not have much use anyway.Thu, 21 Apr 2016 00:51:37 +0200https://ask.sagemath.org/question/33154/rational-numbers-with-cython/?comment=33163#post-id-33163Comment by nbruin for <p>I want to work efficiently with rational numbers under the context %cython but I don`t know how. Can anyone suggest any ideas? Thank you.</p>
<p>An example:</p>
<pre><code> %cython
def rational_partitions(n):
sol = [i/n for i in range(n)]
for a in sol[0:-1]:
for b in sol[1:]:
k=2
while abs(b-a)/k>1/n:
sol.append(abs(b-a)/k)
k += 1
return sol
rational_partitions(10)
</code></pre>
https://ask.sagemath.org/question/33154/rational-numbers-with-cython/?comment=33164#post-id-33164If you're finding that the python wrappers are causing undue speed loss, you can look in `sage/rings/rational.pyx` and `sage/rings/rational.pyx` and see how much can be gained from interfacing at the C-level. Surprisingly few routines are mentioned in the `pxd`, but the `cdef mpq_t value` attribute allows you to use the super-optimized gmp `mpq` routines to compute with rational numbers.Thu, 21 Apr 2016 00:52:30 +0200https://ask.sagemath.org/question/33154/rational-numbers-with-cython/?comment=33164#post-id-33164Comment by slelievre for <p>I want to work efficiently with rational numbers under the context %cython but I don`t know how. Can anyone suggest any ideas? Thank you.</p>
<p>An example:</p>
<pre><code> %cython
def rational_partitions(n):
sol = [i/n for i in range(n)]
for a in sol[0:-1]:
for b in sol[1:]:
k=2
while abs(b-a)/k>1/n:
sol.append(abs(b-a)/k)
k += 1
return sol
rational_partitions(10)
</code></pre>
https://ask.sagemath.org/question/33154/rational-numbers-with-cython/?comment=33167#post-id-33167@nbruin: you could turn both of your comments into an answer.Thu, 21 Apr 2016 11:14:19 +0200https://ask.sagemath.org/question/33154/rational-numbers-with-cython/?comment=33167#post-id-33167Comment by rafarob32 for <p>I want to work efficiently with rational numbers under the context %cython but I don`t know how. Can anyone suggest any ideas? Thank you.</p>
<p>An example:</p>
<pre><code> %cython
def rational_partitions(n):
sol = [i/n for i in range(n)]
for a in sol[0:-1]:
for b in sol[1:]:
k=2
while abs(b-a)/k>1/n:
sol.append(abs(b-a)/k)
k += 1
return sol
rational_partitions(10)
</code></pre>
https://ask.sagemath.org/question/33154/rational-numbers-with-cython/?comment=33172#post-id-33172Sorry, I don't know how to use mpq_t attribute. Should I import some library for it? How do it? Thanks in advance.Thu, 21 Apr 2016 12:08:15 +0200https://ask.sagemath.org/question/33154/rational-numbers-with-cython/?comment=33172#post-id-33172Comment by nbruin for <p>I want to work efficiently with rational numbers under the context %cython but I don`t know how. Can anyone suggest any ideas? Thank you.</p>
<p>An example:</p>
<pre><code> %cython
def rational_partitions(n):
sol = [i/n for i in range(n)]
for a in sol[0:-1]:
for b in sol[1:]:
k=2
while abs(b-a)/k>1/n:
sol.append(abs(b-a)/k)
k += 1
return sol
rational_partitions(10)
</code></pre>
https://ask.sagemath.org/question/33154/rational-numbers-with-cython/?comment=33175#post-id-33175`sage/rings/rational.pyx` is cython code and is implemented to use gmp's `mpq` family of routines. Look there for examples. The things you can save on by doing so are
- Python method lookup and function call (probably biggest saving)
- intermediate python object creation
- some memory allocation savings if you can find ways to reuse temporary variables during your computation.
It's going to be quite a bit of work and I'm not sure it will make a big difference for the code you present. You'll learn how to use gmp directly, though.Thu, 21 Apr 2016 17:26:22 +0200https://ask.sagemath.org/question/33154/rational-numbers-with-cython/?comment=33175#post-id-33175