Using Python tuples as vectors

Posted by Etaoin on Stack Overflow See other posts from Stack Overflow or by Etaoin
Published on 2010-04-04T23:39:43Z Indexed on 2010/04/04 23:43 UTC
Read the original article Hit count: 287

Filed under:
|

I need to represent immutable vectors in Python ("vectors" as in linear algebra, not as in programming). The tuple seems like an obvious choice.

The trouble is when I need to implement things like addition and scalar multiplication. If a and b are vectors, and c is a number, the best I can think of is this:

tuple(map(lambda x,y: x + y, a, b)) # add vectors 'a' and 'b'
tuple(map(lambda x: x * c, a))      # multiply vector 'a' by scalar 'c'

which seems inelegant; there should be a clearer, simpler way to get this done -- not to mention avoiding the call to tuple, since map returns a list.

Is there a better option?

© Stack Overflow or respective owner

Related posts about python

Related posts about tuple