Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using python 3.5: Write a program called ‘vectormath.py’ to do basic vector calc

ID: 3835366 • Letter: U

Question

Using python 3.5:

Write a program called ‘vectormath.py’ to do basic vector calculations in 3 dimensions: addition, dot product and normalization. A vector has 3 component values, such as (1, 3, 2) and is naturally storable as an array. Addition of vectors requires addition of the corresponding elements. A dot product is the sum of the products of corresponding elements.

The norm of a single vector is the square root of the sum of the squares of the elements. Suppose that we have 2 vectors: A=(1, 3, 2) and B=(2, 3, 0): Addition: A+B = (1+2, 3+3, 2+0) = (3, 6, 2) Dot product: A.B = 1.2 + 3.3 + 2.0 = 2 + 9 = 11 Norm (of A): |A| = Sqrt(1^2 + 3^2 + 2^2) = Sqrt(1+9+4) = Sqrt(14) = 3.74 Norm (of B): |B| = Sqrt(2^2 + 3^2 + 0^2) = Sqrt(4+9+0) = Sqrt(13) = 3.61 For the norms, print your answer to 2 decimal positions.

Explanation / Answer

import math
def addition(vectorA, vectorB):
   return [i + j for i, j in zip(vectorA, vectorB)]

def dotProduct(vectorA, vectorB):
   return sum([i* j for i, j in zip(vectorA, vectorB)])

def getNorm(vectorA):
   norm = math.sqrt(sum(map(lambda x:x*x,vectorA)))
   return format(norm, '.2f')


A = raw_input("Enter vector A:")
A = map(int, A.split())
B= raw_input("Enter vector B:")
B= map(int, B.split())
print("A+B ="+str(addition(A,B)))
print("A.B ="+str(dotProduct(A,B)))
print("|A| ="+str(getNorm(A)))
print("|A| ="+str(getNorm(B)))

NewCode

import math
def addition(vectorA, vectorB):
return [i + j for i, j in zip(vectorA, vectorB)]

def dotProduct(vectorA, vectorB):
return [i* j for i, j in zip(vectorA, vectorB)]

def getNorm(vectorA):
norm = math.sqrt(sum(map(lambda x:x*x,vectorA)))
return format(norm, '.2f')

A = input("Enter vector A:")
#A1 = map(int, A.split(" "))
B= input("Enter vector B:")
#B1= map(int, B.split())
print("A+B ="+str(addition(map(int, A.split(" ")),map(int, B.split()))))
#print(list(B1))
print("A.B ="+str(dotProduct(map(int, A.split(" ")),map(int, B.split()))))
print("|A| ="+str(getNorm(map(int, A.split(" ")))))
print("|A| ="+str(getNorm(map(int, B.split()))))

input A will be like "1 3 2"

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote