vecan.py

Created by cesphoto

Created on May 29, 2024

2.02 KB

Vector Analysis program. Input vectors U and V. Calculate dot product, cross product, scalar projections, vector projections, the angle between vectors, norm of vectors, and normalize vectors, along with switching vectors U and V.


import numpy as np
from math import *

def rndV(vec):
  for i in range(vec.size):
    vec[i] = round(vec[i],4)
  return vec

def projuv(u,v):
  puv = v*((np.dot(u,v)/np.dot(v,v)))
  puv = rndV(puv)
  print("\nProj of U onto V:")
  print("=",puv.tolist(),"\n")
  input("press [enter] to continue")
  vecan()

def compuv(u,v):
  cuv = ((np.dot(u,v)/normv(v)))
  print("\nComp of U onto V:")
  print("=",round(cuv,5),"\n")
  input("press [enter] to continue")
  vecan()

def normv(v):
  return sqrt(sum((v**2)))

def angleUV(u,v):
  print("\nThe angle between U and V:")
  print("=",round(acos(np.dot(u,v)/(normv(u)*normv(v)))*180/pi,5),"deg\n")
  input("press [enter] to continue")
  vecan()
  
def inputUV():
  global a, b
  a = np.array(eval(input("Vector U: ")))
  b = np.array(eval(input("Vector V: ")))
  vecan()

def switch(u, v):
  global a, b
  a = v
  b = u
  print("U=",a.tolist())
  print("V=",b.tolist())
  input("\npress [enter] to continue")
  vecan()

def normalize(u, v):
  global a,b
  a = rndV(u/normv(u))
  b = rndV(v/normv(v))
  print("unit_U=\n"," ",a.tolist())
  print("unit_V=\n"," ",b.tolist(),"\n")
  input("press [enter] to continue")
  vecan()

def vecan():
  k = 0
  print("\n========Vector Analysis=======")
  print("1: Input Vectors")
  print("2: U dot V")
  print("3: U cross V")
  print("4: Normalize U and V")
  print("5: Proj U on V")
  print("6: Comp U on V")
  print("7: Angle between U and V")
  print("8: Switch U and V")
  print("9: Quit")
  k = int(input("[1-9]?"))
  print()
  
  if k == 1:
    inputUV() 

  elif k == 2:
    print("\nU dot V:")
    print("=",np.dot(a, b))
    input("\npress [enter] to continue")
    vecan()

  elif k == 3:
    print("\nU cross V:")
    ucv = np.cross(a, b)
    ucv = rndV(ucv)
    print("=",ucv.tolist())
    input("\npress [enter] to continue")
    vecan()

  elif k == 4:
    normalize(a, b)

  elif k == 5:
    projuv(a, b)

  elif k == 6:
    compuv(a, b)
  
  elif k == 7:
    angleUV(a, b)
  
  elif k == 8:
    switch(a, b)

  else:
    print("The End")
      

  
  

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.