Skip to content

Instantly share code, notes, and snippets.

@ronan-mch
Created November 9, 2011 21:56
Show Gist options
  • Save ronan-mch/1353221 to your computer and use it in GitHub Desktop.
Save ronan-mch/1353221 to your computer and use it in GitHub Desktop.
This is a script which performs several vector functions
# this function adds two vectors together to produce a new vector, vector3.
# first vector 3 is defined as an array. then, an if sentence tests
#which vector is longer. this means that vectors of unequal sizes can be added
# together. - except that this doesn't actually work... loop stops
# the for loop adds each element of the first vector to that of its counterpart
# the output is then appended to vector3, the new output.
def vectorAdd(vector1,vector2):
vector3 = []
if len(vector1)<= len(vector2):
vectorSize = len(vector1)
else: vectorSize = len(vector2)
for i in range (vectorSize):
vector3.append(vector1[i] + vector2[i])
print vector3
# this function returns the DotProduct of two Vectors
# the lengthbased looping is retained from vector addition,
# but the result is now created by incrementing the product variable
# with the product of each row multiplication.
def DotProduct(vector1,vector2):
product = 0
if len(vector1) >= len(vector2):
vectorSize = len(vector1)
else: vectorSize = len(vector2)
for i in range(vectorSize):
product+= (vector1[i] * vector2[i])
print product
# this function is used to create vectors
def createVector(newVector):
length = int(raw_input('Enter the number of variables in your vector: '))
for i in range(length):
i +=1
newVector.append(int(raw_input('Enter variable %s: ' %i)))
print newVector
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment