Ask Your Question
1

Is there a reliable way to check if an object is a vector?

asked 2020-10-14 17:57:18 +0100

more_weight gravatar image

I'm want to check if a given sage object obj is a vector or not. I'd like to do this as efficiently as possible. Currently my best idea is:

sage_input(obj).__repr__().startswith('vector')

This works, assuming that sage_input is smart enough to identify every vector without false positives. However, this solution feels inefficient (and also inelegant!). Is there a better way?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2020-10-14 18:06:58 +0100

rburing gravatar image

updated 2020-10-14 18:11:18 +0100

You want is_Vector:

sage: from sage.structure.element import is_Vector
sage: v = vector([1,2,3])
sage: is_Vector(v)
True
sage: is_Vector([1,2,3])
False

It checks whether the input is an instance of the abstract element class Vector.

edit flag offensive delete link more

Comments

Ha. Had no idea this was built in. Thanks!

more_weight gravatar imagemore_weight ( 2020-10-14 20:37:10 +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

1 follower

Stats

Asked: 2020-10-14 17:57:18 +0100

Seen: 466 times

Last updated: Oct 14 '20