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.Wed, 11 May 2011 07:19:18 -0500Cannot convert int to sage.rings.integer.Integerhttps://ask.sagemath.org/question/8099/cannot-convert-int-to-sageringsintegerinteger/I created a minimal example in the file bad.py
from sage.all import *
j = 0
m = matrix(3,3)
m.insert_row(j, [1,1,1])
Then from sage:
sage: load bad.py
TypeError: Cannot convert int to sage.rings.integer.Integer
I think I can see an easy workaround, I can wrap all the 1's in Integer() (or change the name of the file to .sage so the preparser does it), but why is that necessary? Is this a bug? Why would sage not be able to convert int to Integer? Isn't that a pretty obvious conversion?
Mon, 02 May 2011 07:23:12 -0500https://ask.sagemath.org/question/8099/cannot-convert-int-to-sageringsintegerinteger/Comment by benjaminfjones for <p>I created a minimal example in the file bad.py</p>
<pre><code>from sage.all import *
j = 0
m = matrix(3,3)
m.insert_row(j, [1,1,1])
</code></pre>
<p>Then from sage:</p>
<pre><code>sage: load bad.py
TypeError: Cannot convert int to sage.rings.integer.Integer
</code></pre>
<p>I think I can see an easy workaround, I can wrap all the 1's in Integer() (or change the name of the file to .sage so the preparser does it), but why is that necessary? Is this a bug? Why would sage not be able to convert int to Integer? Isn't that a pretty obvious conversion?</p>
https://ask.sagemath.org/question/8099/cannot-convert-int-to-sageringsintegerinteger/?comment=21767#post-id-21767It's important to have a way of using Python types like int without having the preparser rewrite them, that's why the load and attach behavior of `.py` files and `.sage` files is different. But, I agree, it should be trivial for the matrix code to coerce `int` to `Integer`. It could be that the coercion is not done for a specific reason. I'm also curious why if you declare `m = matrix(QQ, 3, 3)` that `m` no longer has an `insert_row` method.Mon, 02 May 2011 08:00:29 -0500https://ask.sagemath.org/question/8099/cannot-convert-int-to-sageringsintegerinteger/?comment=21767#post-id-21767Comment by kcrisman for <p>I created a minimal example in the file bad.py</p>
<pre><code>from sage.all import *
j = 0
m = matrix(3,3)
m.insert_row(j, [1,1,1])
</code></pre>
<p>Then from sage:</p>
<pre><code>sage: load bad.py
TypeError: Cannot convert int to sage.rings.integer.Integer
</code></pre>
<p>I think I can see an easy workaround, I can wrap all the 1's in Integer() (or change the name of the file to .sage so the preparser does it), but why is that necessary? Is this a bug? Why would sage not be able to convert int to Integer? Isn't that a pretty obvious conversion?</p>
https://ask.sagemath.org/question/8099/cannot-convert-int-to-sageringsintegerinteger/?comment=21766#post-id-21766Yeah, insert_row seems to only be defined for dense integer matrices - check out sage: search_def('insert_row'). Presumably at least some of this could be recycled. Sounds like another Trac ticket to file! Or search for.Mon, 02 May 2011 08:50:51 -0500https://ask.sagemath.org/question/8099/cannot-convert-int-to-sageringsintegerinteger/?comment=21766#post-id-21766Answer by kcrisman for <p>I created a minimal example in the file bad.py</p>
<pre><code>from sage.all import *
j = 0
m = matrix(3,3)
m.insert_row(j, [1,1,1])
</code></pre>
<p>Then from sage:</p>
<pre><code>sage: load bad.py
TypeError: Cannot convert int to sage.rings.integer.Integer
</code></pre>
<p>I think I can see an easy workaround, I can wrap all the 1's in Integer() (or change the name of the file to .sage so the preparser does it), but why is that necessary? Is this a bug? Why would sage not be able to convert int to Integer? Isn't that a pretty obvious conversion?</p>
https://ask.sagemath.org/question/8099/cannot-convert-int-to-sageringsintegerinteger/?answer=12330#post-id-12330I think the problem might be that
cdef Integer z
is part of the code here, and then there is no coercion to the integers, it's just assumed that it is one. Trying
z = Integer(row[k])
seems to fix it, though this is probably inelegant and inefficient.
Here is a minimal example inside Sage itself, for reference.
sage: m = matrix([[int(1),int(1)],[int(1),int(1)]])
sage: m.insert_row(1,[int(1),int(1)])
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Mon, 02 May 2011 09:01:15 -0500https://ask.sagemath.org/question/8099/cannot-convert-int-to-sageringsintegerinteger/?answer=12330#post-id-12330Comment by kcrisman for <p>I think the problem might be that </p>
<pre><code>cdef Integer z
</code></pre>
<p>is part of the code here, and then there is no coercion to the integers, it's just assumed that it is one. Trying </p>
<pre><code> z = Integer(row[k])
</code></pre>
<p>seems to fix it, though this is probably inelegant and inefficient.</p>
<p>Here is a minimal example inside Sage itself, for reference.</p>
<pre><code>sage: m = matrix([[int(1),int(1)],[int(1),int(1)]])
sage: m.insert_row(1,[int(1),int(1)])
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
</code></pre>
https://ask.sagemath.org/question/8099/cannot-convert-int-to-sageringsintegerinteger/?comment=21751#post-id-21751This is now #11328.Wed, 11 May 2011 07:19:18 -0500https://ask.sagemath.org/question/8099/cannot-convert-int-to-sageringsintegerinteger/?comment=21751#post-id-21751