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

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

ID: 3838076 • Letter: U

Question

Using python 3: 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

This is actually pretty simple. All you have to do is do the exact same calucaltions we do on paper via python. Here is a sample code. Read the input from stdin in different lines

import math

print("enter vector A")
a = [int(n) for n in raw_input().split()]
print("enter vector b")
b = [int(n) for n in raw_input().split()]
sum=[a[0]+b[0],a[1]+b[1],a[2]+b[2]]
print(sum)
product=a[0]*b[0]+a[1]*b[1]+a[2]*b[2]
print(product)
norm=math.sqrt(a[0]*a[0]+a[1]*a[1]+a[2]*a[2])
print(format(norm, '.2f'))

// to restrict the decimal points to 2

Let me know if you have any more questions.

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