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
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):
sum([a
<[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
> 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!
--
h
[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 rotat
<[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)])
> >>> 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 prop
[EMAIL PROTECTED] wrote:
> However, I'd like to add attribute access (magically), so I can do
> this:
>
> v = Vector((1,2,3))
> print v.x
> print v.y
> print v.z
>
> as well as:
>
> print v[0]
> print v[1]
> print v[2]
>
> Has anyone got any ideas on how this might be done?
>>> class Vector(
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 Ve