-2
Hello, I’m trying to do this operation without using the Numpy library, which in itself is already a tragedy.
I get from the user two vectors of equal sizes, and need to do the scalar product operation between them. Remembering which scalar product is a. b = a1.B1 + a2.B2 .... + an.bn
I think there’s something missing that I don’t fully understand...
For example, if I put vector A as [1, 2, 3] and vector B as [3, 2, 1], the result of multiplication between them should vector C = [3, 4, 3]. But the way this code was written what I have is: [3, 2, 1, 6, 4, 2, 9, 6, 3] Since I’m not able to multiply the elements of the vectors, it doesn’t even make sense to try to add the elements of the vector C to get the final product. follows the code:
vetorA = []
vetorB = []
numA = int(input('Digite o tamanho do vetor do A: '))
while len(vetorA) < numA:
vetorA.append(int(input('Digite um numero para preencher o vetor A: ')))
numB = int(input('Digite o tamanho do vetor do B: '))
while len(vetorB) < numB:
vetorB.append(int(input('Digite um numero para preencher o vetor B: ')))
if len(vetorA) == len(vetorB):
vetorC = []
for itemA in vetorA:
for itemB in vetorB:
new = itemA * itemB
vetorC.append(new)
print(vetorC)
It is worth remembering that the suggested solutions (both below and in the duplicate suggested in the blue block above) only work if the lists have the same size, because
zip
ends as soon as the smallest of them ends. If you are processing lists of different sizes, you would have to useitertools.zip_longest
– hkotsubo