automatically assigning names to indexes

Devan L devlai at gmail.com
Tue Jul 12 22:27:55 CEST 2005


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*b for a,b in zip(self.coordinates,vector.coordinates)])
    def crossproduct(self, vector, pvector):
        return
pvector*self.magnitude*vector.magnitude*math.sin(self.getangle(vector))
    def getangle(self, vector):
        return
math.acos(self.dotproduct(vector)/(self.magnitude*vector.magnitude))
    def __mul__(self, scalar):
        return Vector([c*scalar for c in self.coordinates])
    def __add__(self, vector):
        return Vector([c+d for c,d in
zip(self.coordinates,vector.coordinates)])
    def __sub__(self, vector):
        return Vector([c-d for c,d in
zip(self.coordinates,vector.coordinates)])

What about this?




More information about the Python-list mailing list