1 year ago

#381144

test-img

12666727b9

3D vector class and assert that does not work

I'm trying creating a 3D class Vector and I have prepared the following code:

 class Vector:
    
    def __init__(self, x, y, z):   
        self.x = x
        self.y = y
        self.z = z
        
          #return string 'Vector(x,y,z)'
        def __repr__(self): 
            return ("Vector(x,y,z)")
            
            # or alternatively
            #def __str__(self):
                #return str("Vector3({0.x},{0.y},{0.z})".format(self))
            
            # v == w
        def __eq__(self, other):
            self.v = Vector(self.x,self.y,self.z)
            other.w = Vector(other.x, other.y, other.z)
            if self.v() == self.w(): 
                return True
            else: 
                return False
            #def __eq__(self, other):   # poly1 == poly2
            #    return (self - other).is_zero()   # it works when the substraction is defined

            
            # v != w
        def __ne__(self, other): 
            self.v = Vector(self.x,self.y,self.z)
            other.w = Vector(other.x, other.y, other.z)
            if self.v() != self.w(): 
                return True
            else: 
                return False
                
                
            #def __ne__(self, other):   # poly1 != poly2
            #   return not self == other       
            
            # v + w
        def __add__(self, other):  
            if other == 0:
                return self
            else:
                return vector(self.x+other.x, self.y+other.y, self.z+other.z)   
                
            # v - w
        def __sub__(self, other):
            self.x -= other.x
            self.y -= other.y
            self.z -= other.z
    
            # return the dot product (number)
        def __mul__(self, other): 
            self.v = Vector(self.x + self.y + self.z)
            self.w = Vector(other.x + other.y + other.z)
            return Vector(self.x*other.x,self.y*other.y, self.z*other.z)

                
            #cross product
        def __pow__(self, other):
            return Vector(self.y*other.z - self.z*other.y, 
                          self.z*other.x - self.x*other.z,
                          self.z*other.y - self.y*other.x)
           
            # the length of the vector
        def length(self):
            return len(self)
                
            
            # we assume that vectors are immutable
        def __hash__(self):   
            return hash((self.x, self.y, self.z))

However no one of the following assert tests seem to work:

import math
v = Vector(1, 2, 3)
w = Vector(2, -3, 2)
assert v != w
assert v + w == Vector(3, -1, 5)
assert v - w == Vector(-1, 5, 1)
assert v * w == 2
assert v.cross(w) == Vector(13, 4, -7)

If someone tries running the following assert code, some errors pop up but I am not able to figure out what the problem is. Moreover, if it is possible I would lie to ask for a possible way to return the hash, length and string representation. Thanks

python

vector

3d

assert

definition

0 Answers

Your Answer

Accepted video resources