Vector classes

M

Mizipzor

During my coding Ive found two vector classes on the internet. Ive
modified them both a little but the do both have advantages and
disadvantages.

vector1: http://rafb.net/p/4FVdh699.html
vector2: http://rafb.net/p/0KShGu30.html

With 1, I can typ vec.x and vec.y, very nice. With 2, I need to do
vec.vals[0] and vec.vals[1], which makes my eyes bleed.

But with 2, I can create a vector by supplying a list of numbers as
arguments. And I can also do maths with numbers (ints/floats) and not
just vectors (something 1 forces me to do).

Is there any way to combine the two? The ultimate would be if I
somehow could take vector2 and hook the member self.vals[0] to self.x
or something.
 
M

Mark Dickinson

With 1, I can typ vec.x and vec.y, very nice. With 2, I need to do
vec.vals[0] and vec.vals[1], which makes my eyes bleed.

If you add a __getitem__ method to the second vector class:

def __getitem__(self, i):
return self.vals

then you can use vecs[0] instead of vec.vals[0]. But this vector
class has some other problems that you probably want to fix. For
example, the __add__ method modifies the second argument:
a = Vector([1, 2, 3]) [1, 2, 3]
b = Vector([4, 5, 6]) [4, 5, 6]
c = a + b
print b.vals
[5, 7, 9]

Something like

def __add__(self, other):
return Vector(x + y for x, y in zip(self.vals,
other.vals))

might work better. Similarly for the __sub__ method.

Have you considered using numpy?

Mark
 
W

Will McGugan

Mizipzor said:
During my coding Ive found two vector classes on the internet. Ive
modified them both a little but the do both have advantages and
disadvantages.
I'm working on a vector class at the moment, in my 'gameobjects'
library. It's not really ready for public consumption, but feel free to
take a look...

http://www.willmcgugan.com/game-objects/
 
E

Erik Max Francis

Mizipzor said:
During my coding Ive found two vector classes on the internet. Ive
modified them both a little but the do both have advantages and
disadvantages.

vector1: http://rafb.net/p/4FVdh699.html
vector2: http://rafb.net/p/0KShGu30.html

With 1, I can typ vec.x and vec.y, very nice. With 2, I need to do
vec.vals[0] and vec.vals[1], which makes my eyes bleed.

But with 2, I can create a vector by supplying a list of numbers as
arguments. And I can also do maths with numbers (ints/floats) and not
just vectors (something 1 forces me to do).

Is there any way to combine the two? The ultimate would be if I
somehow could take vector2 and hook the member self.vals[0] to self.x
or something.

There are also full-featured Vector and Matrix classes in the la.py
module of ZOE:

http://www.alcyone.com/software/zoe/
 
G

Gabriel Genellina

During my coding Ive found two vector classes on the internet. Ive
modified them both a little but the do both have advantages and
disadvantages.

vector1: http://rafb.net/p/4FVdh699.html
vector2: http://rafb.net/p/0KShGu30.html

With 1, I can typ vec.x and vec.y, very nice. With 2, I need to do
vec.vals[0] and vec.vals[1], which makes my eyes bleed.

[2] modifies the 2nd argument to __add__ and __sub__, not a good thing...
The only advantage I can see over [1], is that it handles dimensions
higher than 2.
But with 2, I can create a vector by supplying a list of numbers as
arguments.

Same with the first one:

coords = (1.2, 3.4)
v1 = vector(*coords)

There is even the "list" static method (not a good name!):

coords = (1.2, 3.4)
v1 = vector.list(coords)

Anyway I'd remove the __ne__ and __lt__ methods (if you don't require it)
and replace __hash__ with hash((self.x,self.y))
Is there any way to combine the two? The ultimate would be if I
somehow could take vector2 and hook the member self.vals[0] to self.x
or something.

You should try the other suggested classes, but if I had to choose among
these two, I'd use the first.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top