vendredi 28 mai 2021

python printing vectors in different forms

Write a program called ‘vectormath.py’ to do basic vector calculations in 3 dimensions: addition, dot product and normalization.

hi there's an error on my mapping function

sample input:

Enter vector A:

1 3 2

Enter vector B:

2 3 0

sample output

A+B = [3, 6, 2]

A.B = 11

|A| = 3.74

|B| = 3.61

import math
def addition(a,b):
    return[a[0]+b[0] , a[1] + b[1] , a[2] + b[2]]

def  Dotproduct(a , b):
    return a[0]*b[0] + a[1]*b[1] + a[2]*b[2]

def norm(a):
    square = a[0]**2 + a[1]**2 + a[2]**2
    return math.sqrt(square)

def main():
    vector_A = input("Enter vector A:\n").split(" ")
    vector_A = map(int,vector_A)
    vector_B = input("Enter vector B:\n").split(" ")
    vector_B = map(int, vector_B)
    
    print(addition(vector_A,vector_B))
    print(addition(vector_A,vector_B))
    print(norm(vector_A))
    print(norm(vector_B))
    
if __name__ == "__main__":
    main()

Aucun commentaire:

Enregistrer un commentaire