Ask Your Question
0

Cannot convert int to sage.rings.integer.Integer

asked 2011-05-02 14:23:12 +0200

paragon gravatar image

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 flag offensive close merge delete

Comments

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.

benjaminfjones gravatar imagebenjaminfjones ( 2011-05-02 15:00:29 +0200 )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.

kcrisman gravatar imagekcrisman ( 2011-05-02 15:50:51 +0200 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2011-05-02 16:01:15 +0200

kcrisman gravatar image

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)
edit flag offensive delete link more

Comments

This is now #11328.

kcrisman gravatar imagekcrisman ( 2011-05-11 14:19:18 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2011-05-02 14:23:12 +0200

Seen: 3,637 times

Last updated: May 02 '11