Ask Your Question
3

check whether object is matrix

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

twch gravatar image

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

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
3

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

kcrisman gravatar image

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

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

Comments

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 +0100 )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 +0100 )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: 2013-02-01 07:01:04 +0100

Seen: 1,328 times

Last updated: Feb 01 '13