Currently I have a program that finds the dot product of two XYZ coordinates, how would I put this into a loop, so that it goes down a list of coordinates and finds the dot prod
import numpy
v0 = (6,7,6)
vectors = numpy.array([[1,2,3],[4,5,6],[7,8,9]])
dot_products = vectors.dot(v0)
is by far the easiest way to do what you are trying to do imho
and it should be much faster if you have "a lot" of vectors in your vectors list...(for some definition of a lot)
import operator
vector1 = (1, 2, 3)
# get a list of vectors
vectors = [
(4, 5, 6),
(7, 8, 9)
]
# for loop through the vectors,
# assignig the current vector to vector2 in every iteration
for vector2 in vectors:
dotProduct = reduce(operator.add, map(operator.mul, vector1, vector2))
print dotProduct
Using your l
, nat
and a
variables:
vector1 = (int(l[0][0]), int(l[0][1]), int(l[0][2]))
for a in range(1, nat):
vector2 = (int(l[a][0]), int(l[a][1]), int(l[a][2]))
dotProduct = reduce(operator.add, map(operator.mul, vector1, vector2))
print(dotProduct)