# Cannot convert int to sage.rings.integer.Integer

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?

edit retag close merge delete

It'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.

( 2011-05-02 08:00:29 -0600 )edit

Yeah, 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.

( 2011-05-02 08:50:51 -0600 )edit

Sort by ยป oldest newest most voted

I 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)

more

This is now #11328.

( 2011-05-11 07:19:18 -0600 )edit