First time here? Check out the FAQ!

Ask Your Question
1

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

asked 4 years ago

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?

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
3

answered 4 years ago

rburing gravatar image

updated 4 years ago

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.

Preview: (hide)
link

Comments

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

more_weight gravatar imagemore_weight ( 4 years ago )

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: 4 years ago

Seen: 566 times

Last updated: Oct 14 '20