Ask Your Question
1

check whether object is matrix

asked 2013-02-01 00:01:04 -0600

twch gravatar image

updated 2013-02-01 00:07:48 -0600

Hi,

I would like to check whether an object is a sage matrix. I thought that "isinstance" might help, however isinstance is a little bit too sensitive for my purpose as the following example shows:

m1=matrix([1.])
print type(m1)
print isinstance(m1, sage.matrix.matrix_generic_dense.Matrix_generic_dense)
m1=matrix([1])
print type(m1)
isinstance(m1, sage.matrix.matrix_generic_dense.Matrix_generic_dense)

returns

<type 'sage.matrix.matrix_generic_dense.Matrix_generic_dense'>
True
<type 'sage.matrix.matrix_integer_dense.Matrix_integer_dense'>
False

I would however like to obtain a function which recognizes a matrix regardless to the types of its entries.

isinstance(m1,matrix)

doesn't work either but gives the error: "isinstance() arg 2 must be a class, type, or tuple of classes and types"

So what else can I do?

Thanks for your answers, Tobias

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted
2

answered 2013-02-01 01:59:13 -0600

kcrisman gravatar image

updated 2013-02-01 01:59:53 -0600

Here we go.

sage: m1=matrix([1])
sage: isinstance(m1,sage.matrix.matrix.Matrix)
True
sage: m1=matrix([1.])
sage: isinstance(m1,sage.matrix.matrix.Matrix)
True

I agree that this should be easier to figure out.

edit flag offensive delete link more

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: 2013-02-01 00:01:04 -0600

Seen: 219 times

Last updated: Feb 01 '13