0

The problem is to implement scalar and inner product in the vector class in Python. Here is the code:

class Point(object):
    def __init__(self, x, y):
        self.x = x
        self.y = y
    def __repr__(self):
        return 'Point(%s, %s)' % (self.x, self.y)
    def __mul__(self,other):
        x, y = self.x*other.x, self.y*other.y
        return self.__class__(x,y)
    def __rmul__(self,other):
        x,y = other*self.x,other*self.y
        return self.__class__(x,y)
    def __add__(self,other):
        x,y = self.x + other.x, self.y + other.y
        return self.__class__(x, y)
    def __sub__(self,other):
        x,y = self.x - other.x, self.y - other.y
        return self.__class__(x, y)

With inner product it works great, but with scalar multiplication(like if I call Point(3,2)*2) it gives the following error: AttributeError: 'int' object has no attribute 'x'.

How do I fix this?

2
  • 1
    You could check the type of other in __mul__. Commented Nov 30, 2019 at 9:36
  • What kind of vector multiplication are you doing? A dot product between vectors is a number, not a vector. Commented Nov 30, 2019 at 9:49

2 Answers 2

1

Look carefully at your __mul__ method:

    def __mul__(self,other):
        x, y = self.x*other.x, self.y*other.y
        return self.__class__(x,y)

When you're trying to multiply a Point and an int, you're passing the int as the second argument (to the other parameter) to Point.__mul__. Then your method will try to access other.x and other.y, and an int doesn't have these attributes:

You can manually check for the type of other and decide whether you should be doing scalar or vector product:

    def __mul__(self,other):
        if isinstance(other, Point):
            x, y = self.x*other.x, self.y*other.y
            return self.__class__(x,y)
        elif isinstance(other, (int, float, complex)):
            x, y = other * self.x, other * self.y
            return self.__class__(x, y)
        else:
            raise TypeError

    def __rmul__(self, other):
        return self * other

Also, the way you're doing multiplication is strange. A dot product of two vectors should be a number, not a vector. https://en.wikipedia.org/wiki/Dot_product

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, the answer is very helpful. Yes, I messed up with definition a little bit, it should be like x1*x2 +y1*y2. Thanks again :)
1

You need to add a check for the type of other, it can be a Point instance or other

Here the code for __mul__

def __mul__(self, other):
    if isinstance(other, Point):
        return Point(self.x * other.x, self.y * other.y)  # Point * Point
    return Point(self.x * other, self.y * other)          # Point * othertype

def __rmul__(self, other):
    return Point(self.x * other, self.y * other)          # othertype * Point

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.