I have successfully implemented the inner product like this (its an excercise - some things might beeing imported):
class Point(object):
def __init__(self, x, y):
self.x = x
self.y = y
def __repr__(self):
return 'Point({}, {})'.format(self.x, self.y)
def __add__(self, point):
return Point(self.x + point.x, self.y + point.y)
def __sub__(self, point):
return Point(self.x - point.x, self.y - point.y)
def __mul__(self, second):
if isinstance(second, Point):
return (self.x * second.x + self.y * second.y)
elif isinstance(second, int):
return Point(self.x * second, self.y * second)
elif isinstance(second, float):
return Point(self.x * second, self.y * second)
def __mul__(first, self):
if isinstance(first, int) and isinstance(self, Point):
return Point(self.x * first, self.y * first)
elif isinstance(first, float) and isinstance(self, Point):
return Point(self.x * first, self.y * first)
I get an error if I execute say
p=Point(3.7,4.8)
print(p*3)
Without the last block (scalar mult. from the left) the outout is correct. Surely there is an error in the last block cause
p=Point(3.7,4.8)
print(3*p)
(which the last block is for) also is not working.
Hope someone can help - thanks!