Re: automatically assigning names to indexes

2005-07-13 Thread Skip Montanaro
George What 'magic' ? The (x,y,z) notation is used only for 3D George vectors. (x,y) is also common for 2D and perhaps (t,x,y,z) for George 4D, with t for time. Don't forget (w,x,y,z) for quaternions... Skip -- http://mail.python.org/mailman/listinfo/python-list

automatically assigning names to indexes

2005-07-12 Thread simonwittber
I know its been done before, but I'm hacking away on a simple Vector class. class Vector(tuple): def __add__(self, b): return Vector([x+y for x,y in zip(self, b)]) def __sub__(self, b): return Vector([x-y for x,y in zip(self, b)]) def __div__(self, b): return

Re: automatically assigning names to indexes

2005-07-12 Thread simonwittber
class Vector(tuple): ... x = property(lambda self: self[0]) ... y = property(lambda self: self[1]) ... z = property(lambda self: self[2]) ... Vector(abc) ('a', 'b', 'c') Vector(abc).z 'c' Vector(abc)[2] 'c' Aha! You have simultaneously proposed a neat solution, and

Re: automatically assigning names to indexes

2005-07-12 Thread George Sakkis
[EMAIL PROTECTED] wrote: I know its been done before, but I'm hacking away on a simple Vector class. class Vector(tuple): def __add__(self, b): return Vector([x+y for x,y in zip(self, b)]) def __sub__(self, b): return Vector([x-y for x,y in zip(self, b)]) def

Re: automatically assigning names to indexes

2005-07-12 Thread François Pinard
[EMAIL PROTECTED] I know its been done before, but I'm hacking away on a simple Vector class. [...] However, I'd like to add attribute access (magically), so I can do this: [...] Has anyone got any ideas on how this might be done? I needed something this last week, while toying with

Re: automatically assigning names to indexes

2005-07-12 Thread simonwittber
And what should happen for vectors of size != 3 ? I don't think that a general purpose vector class should allow it; a Vector3D subclass would be more natural for this. That's the 'magic' good idea I'm looking for. I think a unified Vector class for all size vectors is a worthy goal! --

Re: automatically assigning names to indexes

2005-07-12 Thread George Sakkis
[EMAIL PROTECTED] wrote And what should happen for vectors of size != 3 ? I don't think that a general purpose vector class should allow it; a Vector3D subclass would be more natural for this. That's the 'magic' good idea I'm looking for. I think a unified Vector class for all size

Re: automatically assigning names to indexes

2005-07-12 Thread Devan L
import math class Vector: def __init__(self, coordinates): self.coordinates = coordinates self.magnitude = sum([c**2 for c in coordinates])**0.5 self.direction = getangle(Vector([1]+[0 for i in range(len(coordinates)-1)])) def dotproduct(self, vector):