import numpy as np
#initialize the vectors a and b
a = np.array(input('Enter the first vector: '))
b = np.array(input('Enter the second vector: '))
#Evaluate the dot product using numpy
a_dot_b = 0
for i in range(3):
a_dot_b += a[i] * b[i]
if a_dot_b == 0:
print("The vectors are orthogonal")
else:
print("Dot product = ", a_dot_b)
I'm trying to write a program that tells the user whether two vectors are orthogonal. When I try to run this, it saysIndexError: too many indices for array
I don't know if it's something wrong with my loop or if it's something I'm doing wrong with inputting the vectors.
in NumPy, you should determine the dimension of the array using [], here you give a string with zero dimension to NumPy and try to iterate, so you got the error. you can change your code into this :
# get the first vector from user
firstVector = [int(i) for i in input('Enter the first vector: ').split()]
# convert into numpy array
a = np.array(firstVector)
# get the second vector from user
secondVector = [int(i) for i in input('Enter the second vector: ').split()]
# convert into numpy array
b = np.array(secondVector)
the rest of code is the same.
An alternative solution for the second part of your code: in NumPy, you can find the dot product of two vectors only with the below codes:
1) sum(a * b)
2) np.dot(a,b)