Tell us what’s happening:
I do not understand how to implement this. I know it is something like (self[i]*other[i],self[i + 1] * other[i + 1])
or this (self[0]*other[0],self[1]*other[1]
problem with the second is that it will only work on 2D. None of the two is working.
Your code so far
class R2Vector:
def __init__(self, *, x, y):
self.x = x
self.y = y
def norm(self):
return sum(val**2 for val in vars(self).values())**0.5
def __str__(self):
return str(tuple(getattr(self, i) for i in vars(self)))
def __repr__(self):
arg_list = [f'{key}={val}' for key, val in vars(self).items()]
args = ', '.join(arg_list)
return f'{self.__class__.__name__}({args})'
def __add__(self, other):
if type(self) != type(other):
return NotImplemented
kwargs = {i: getattr(self, i) + getattr(other, i) for i in vars(self)}
return self.__class__(**kwargs)
def __sub__(self, other):
if type(self) != type(other):
return NotImplemented
kwargs = {i: getattr(self, i) - getattr(other, i) for i in vars(self)}
return self.__class__(**kwargs)
# User Editable Region
def __mul__(self, other):
if type(other) in (int, float):
kwargs = {i: getattr(self, i) * other for i in vars(self)}
return self.__class__(**kwargs)
elif type(self) == type(other):
pass
# User Editable Region
class R3Vector(R2Vector):
def __init__(self, *, x, y, z):
super().__init__(x=x, y=y)
self.z = z
v1 = R2Vector(x=2, y=3)
v2 = R2Vector(x=0.5, y=1.25)
print(f'v1 = {v1}')
print(f'v2 = {v2}')
v3 = v1 + v2
print(f'v1 + v2 = {v3}')
v4 = v1 - v2
print(f'v1 - v2 = {v4}')
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36
Challenge Information:
Learn Special Methods by Building a Vector Space - Step 54
To generalize, I suggest you to take a look at what you have already done to calculate the result of other operations.
I took a couple of days and back but more confused. Example on how to write this scalar multiplication would have been helpful. I am stuck here.
Have you tried to write a comprehension? or at least a loop to iterate over the components?
multiply [1,2]
and [3,4]
and scalar wise will result in [7,14]
Plainly speaking I do not know how to implement this
To be frank I am surprised no one has asked question on this Step. Everyone understood this?
It’s new material maybe not that many people have been through it yet.
It’s also in beta so maybe the instructions will change and be refined depending on how people get through it.
As @Dario_DC mentioned, look at how you’ve done every other method. You are using a comprehension, iterating over vars(self)
and using getattr()
to perform operations on self
and other
.
Use the exact same format.
I understand that this step can be hard. I’ll try to make a numeric example.
If vector a = (2, 3)
and vector b = (4, 5)
, the result of a⋅b
should be
2⋅4 + 3⋅5 = 8 + 15 = 23
.
If the vectors are three-dimensional:
a = (2, 3, 4)
, b=(5, 6, 7)
a⋅b = 2⋅5 + 3⋅6 + 4⋅7 = 10 + 18 + 28 = 56
To make it work for n-dimensional vectors, you need to loop over the components of a vector and multiply each by the corresponding component of the other vector (remember that you have them in the same order because of how are stored in the object). Then, you want to add all those products together.
Here the implementation is free. The only thing that matters is the final result.
As @pkdvalis said instructions are going to change probably, and this feedback is fundamental to do it. That said, last additions to the curriculum have been made tougher on purpose.
help
a1⋅b1+a2⋅b2+…+an⋅bn…
looping thru (x=0.5, y=1.25) *(x=2, y=3) = (1.0, 3.75)
the sum of x+ y = 4.75.
not sure how implement the scalar product calculation a but this code does not pass
if type(other) in (int, float):
kwargs = {i: getattr(self, i) * other for i in vars(self)}
return self.__class__(**kwargs)
elif type(self) == type(other):
kwargs ={}
for l in vars(self):
if l in kwargs:
kwargs[l] = kwargs[l] + getattr(self, l) * getattr(other, l)
else:
kwargs[l] = getattr(self, l) * getattr(other, l)
print(kwargs)
return self.__class__(**kwargs)
can anyone help?
kwargs = {‘x’: 1.0, ‘y’: 3.75}
1 = R2Vector(x=2, y=3)
v2 = R2Vector(x=0.5, y=1.25)
v3 = v1 * v2
print(f’_mul v1 + v2 = {v3}')
_mul v1 + v2 = (1.0, 3.75)
Please open a new topic.
If you have a question about a specific challenge as it relates to your written code for that challenge and need some help, click the Ask for Help button located on the challenge (it looks like a question mark). This button only appears if you have tried to submit an answer at least three times.
The Ask for Help button will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.
Thank you.