# check whether object is matrix

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()
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?

edit retag close merge delete

Sort by » oldest newest most voted

Here we go.

sage: m1=matrix()
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.

more

Can you let us know how you figured that out? I would like to be able to do the same for point on an elliptic curve in a finite field. I am using what I get for the type sage.schemes.elliptic_curves.ell_point.EllipticCurvePoint_finite_field.

I would like to know how to figure these sorts of things out for myself.

You should be able to get the "type" of something by using type(A) where A is your Sage object. Then you can use the exact same syntax I provided to check this. I will point out that sometimes you get something too detailed with that. For instance, the first one will give <class 'sage.matrix.matrix_integer_dense.Matrix_integer_dense'> which is more than a matrix. So sometimes you have to scull through the code itself to see if there is a more generic class that the others inherit from. Sorry.