Learn Special Methods by Building a Vector Space - Step 54

Hi there,

Please, could someone give me some hints on how to implement the prompt of the step 54.? Thank you for your time.

Step 54
The scalar product between two vectors aa and bb is indicated as:

a⋅b=a1⋅b1+a2⋅b2+…+an⋅bn=∑ni=1ai⋅bia⋅b=a1⋅b1+a2⋅b2+…+an⋅bn=∑i=1nai⋅bi

Where each component of aa is multiplied by the correspondent component of bb, and all the products are summed together, resulting in a number.

Within the elif clause, implement the above formula to compute the result of a scalar product and return the result. Remember that your implementation must be valid for any vector, independently from the number of components, when the method is inherited.

My implementation which does not work (the elif part):

    elif type(self) == type(other):
        kwargs = {i: getattr(self, i) * getattr(other, i) for i in vars(self)}
        return self.__class__(**kwargs)

each component of a is multiplied by the correspondent component of b, and all the products are summed together, resulting in a number.

It seems you are doing something a bit different. You want to return a number, not a new instance of the class.

Thanks @Dario_DC , I am figuring out each of the two parts of the prompt you have in your message:

  1. each component of a is multiplied by the correspondent component of b
  2. all the products are summed together, resulting in a number .
    I am getting right up to now! I am really stuck! I will appreciate more clues!

That formula is telling you to take the first component of vector a and multiply it by the first component of vector b and add it to the product of the second component of a times the second component of b and so on, up to the last component.

So you need to store all those products and return their sum, which is a number, not a vector.

The way you wrote kwargs works, but in the end you don’t need a dictionary to do that because you don’t care of the component once you compute the product.

Anyway, kwargs might be fine but the main problem is what you are returning.

2 Likes

Hi @Dario_DC , I am not able to fix my mistake follwoing your indications. Thanks heaps!

Please, try to explain what is unclear and I’ll try to help you.

Sorry @Dario_DC I was meant to write :
"Hi @Dario_DC , I was able to fix my mistake follwoing your indications. Thanks heaps! All good now!

1 Like

Great, glad it helped :+1:

1 Like

Sorry @Dario_DC I was meant to write :
"Hi @Dario_DC , I was able to fix my mistake following your indications. Thanks heaps! All good now!