Ask Your Question

check whether object is matrix

asked 2013-02-01 07:01:04 +0200

twch gravatar image

updated 2013-02-01 07:07:48 +0200


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:

print type(m1)
print isinstance(m1, sage.matrix.matrix_generic_dense.Matrix_generic_dense)
print type(m1)
isinstance(m1, sage.matrix.matrix_generic_dense.Matrix_generic_dense)


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

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


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

answered 2013-02-01 08:59:13 +0200

kcrisman gravatar image

updated 2013-02-01 08:59:53 +0200

Here we go.

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

I agree that this should be easier to figure out.

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

jpgoldberg gravatar imagejpgoldberg ( 2022-03-26 21:39:03 +0200 )edit

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.

kcrisman gravatar imagekcrisman ( 2022-03-30 17:05:32 +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


Asked: 2013-02-01 07:01:04 +0200

Seen: 1,250 times

Last updated: Feb 01 '13