import numpy as np
import math
masterArray = []
alphabet = "xyz"
def main():
ans = 1
vectorDimension = input('What is the dimension of the vectors that you will input?\n*Note that all the vectors must have the same dimension.*\n')
while vectorDimension == '':
vectorDimension = input('Please enter a valid dimension...\n')
numOfVectors = input('How many vectors will be inputted?\n*Please do not input more than 9 vectors*\n')
while numOfVectors == '':
numOfVectors = input('Please enter a valid number of Vectors...\n')
getVectors(int(vectorDimension), int(numOfVectors))
while ans == 1:
menu()
ans = int(input('Would you like to do other functions?\n* 1: Yes 2: No *\n'))
exit(0)
def getVectors(vectorDimension, numOfVectors): # This is a function to get Vector details straight from User
count = 0
print("Please enter the following: \n***\nEx. vector (1,2,3) should be inputted as:\n1 2 3\n***")
for i in range(numOfVectors):
vectorArray = []
print("Vector {}".format(len(masterArray) + 1))
if vectorDimension == 2 or vectorDimension == 3:
for j in range(0, vectorDimension):
print("{}-component:".format(alphabet[j]))
x = list(map(float, input().split()))
elif vectorDimension > 3:
for j in range(0, vectorDimension):
print("component-{}".format(j+1))
x = list(map(float, input().split()))
if len(x) < vectorDimension:
print("Sorry inputted components was less that the dimension previously stated. Enter again")
getVectors(vectorDimension, numOfVectors)
elif len(x) > vectorDimension:
print("Inputted components was greater than the dimension previously stated. The first {} components were used".format(vectorDimension))
for i in range(0, vectorDimension):
vectorArray.append(x[i])
masterArray.append(vectorArray)
def printVectors():
for i in range(0, len(masterArray)):
print("{}: {}".format(i+1, masterArray[i]))
def makeNegative(selectedVectors):
array = []
for i in range(0, len(selectedVectors)):
array.append(selectedVectors[i] * -1)
return array
def menu():
count = 0
selectedVectors = []
print('''
1: Cross Product
2: Addition
3: Subtraction
4: Dot product
5: Distance
6: Add another vector
''')
ansInput = input('Please select the function you would like to do\n*Just type the number on the left and hit enter*\n')
while ansInput == '' or int(ansInput) < 1 or int(ansInput) > 6:
ansInput = input('Please enter a valid option...\n')
ans = int(ansInput)
printVectors()
if ans == 2:
userInput = input('Please choose what vectors you would like to add.\n*Ex if choices are 1,3,4,5:\n 1 3 4 5\nPlease include spaces*\n')
for i in range(0, len(userInput)):
if userInput[i] == ' ':
continue
else:
count += 1
selectedVectors.append(masterArray[int(userInput[i]) - 1])
elif ans == 6:
getVectors(len(masterArray[0]), 1)
menu()
elif ans == 3:
userInput = input('Please choose what vectors you would like to add.\n*ex if choices are 1,(-)3,4: "1 -3 4", please include spaces and dashes for negatives*\n')
for i in range(0, len(userInput)):
if userInput[i] == ' ':
continue
elif userInput[i] == '-':
i += 1
count += 1
selectedVectors.append(makeNegative(masterArray[int(userInput[i]) - 1]))
else:
if userInput[i-1] == '-':
continue
count += 1
selectedVectors.append(masterArray[int(userInput[i]) - 1])
else:
userInput1, userInput2 = input('You have chosen a function that only takes 2 vectors\nPlease input the first vector number, hit space and input the second\n').split()
selectedVectors.append(masterArray[int(userInput1) - 1])
count += 1
selectedVectors.append(masterArray[int(userInput2) - 1])
count += 1
if count >= 2:
print("Vectors selected: {}".format(selectedVectors))
if ans == 1:
print("Solution: {}".format(crossProduct(selectedVectors)))
elif ans == 2 or ans == 3:
print("Solution: {}".format(addVectors(selectedVectors)))
elif ans == 4:
print("Solution: {}".format(dotProduct(selectedVectors)))
else:
print("Solution: {}".format(distance(selectedVectors)))
else:
print("Not enough vectors selected")
def distance(selectedVectors):
selectedVectors[1] = makeNegative(selectedVectors[1])
h = addVectors(selectedVectors)
dist = np.dot(h, h)
dist = math.sqrt(dist)
return dist
def dotProduct(selectedVectors):
if len(selectedVectors) > 2:
print("Dot product can only be done between 2 vectors, I will not do the dot product of the first two arrays you selected")
x = selectedVectors[0]
y = selectedVectors[1]
return np.dot(x, y)
def addVectors(selectedVectors):
outputArray = []
for i in range(0, len(selectedVectors[0])):
added = 0
for j in range(0, len(selectedVectors)):
added += selectedVectors[j][i]
outputArray.append(added)
return outputArray
def crossProduct(selectedVectors):
if len(selectedVectors[0]) == 2:
return (selectedVectors[0][0] * selectedVectors[1][1]) - (selectedVectors[0][1] * selectedVectors[1][0])
elif len(selectedVectors[0]) == 3:
return np.cross(selectedVectors[0], selectedVectors[1])
else:
print("Cross Product can only be done in dimensions: 3 & 2(scalar)")
'''
Due to programs being top -> bottom I need to call main down here to iterate through everything.
since this call is at the bottom, it allows the main function to be able to use the functions below it
'''
main()
What is the dimension of the vectors that you will input? *Note that all the vectors must have the same dimension.* 3 How many vectors will be inputted? *Please do not input more than 9 vectors* 3 Please enter the following: *** Ex. vector (1,2,3) should be inputted as: 1 2 3 *** Vector 1 x-component: y-component: z-component: 1 2 3 Vector 2 x-component: y-component: z-component: 2 1 3 Vector 3 x-component: y-component: z-component: -1 -1 -1 1: Cross Product 2: Addition 3: Subtraction 4: Dot product 5: Distance 6: Add another vector Please select the function you would like to do *Just type the number on the left and hit enter* 1 1: [1.0, 2.0, 3.0] 2: [2.0, 1.0, 3.0] 3: [-1.0, -1.0, -1.0] You have chosen a function that only takes 2 vectors Please input the first vector number, hit space and input the second 1 3 Vectors selected: [[1.0, 2.0, 3.0], [-1.0, -1.0, -1.0]] Solution: [ 1. -2. 1.]