SoFunction
Updated on 2024-11-12

The way to find the angle between two vectors in python

python find the angle between two vectors

import numpy as np
x=([3,5])
y=([4,2])
# Two vectors
Lx=((x))
Ly=((y))
# Equivalent to the Pythagorean theorem, find the length of the diagonal line
cos_angle=(y)/(Lx*Ly)
# Find the value of cos_sita and reverse the calculation, multiplying the absolute length by the cos angle for the vector length, middle school knowledge.
print(cos_angle)
angle=(cos_angle)
angle2=angle*360/2/
# Turns into an angle
print(angle2)
#(y) =  y=∑(ai*bi)

Algorithm for matrix multiplication

>> a=[1,2,3;4,5,6;7,8,9];
>> b=[6,6,6;6,6,6;6,6,6];
>> dot(a,
ans =
    72    90   108
1*6+4*6+7*6=72
# Three add up
#dot is matrix multiplication
#print((x,x))
#34
# a=(x-y)
# print(a)
costheta=(y)/((x)*(y))
# Paradigm
# #[-1  3]
# a=(x-y)
# #[1,9]
# print((a))
# #10
# print((10))

Methods for finding point product, vector length, and vector angle

The methods for finding the dot product are

  • 1. (a*b)
  • 2. (a*b).sum()
  • 3. (a, b)
  • 4. (b)
  • 5. (a)

The methods for finding the length of a vector are

  • 1. ((a*a).sum()) 
  • 2. (a)

Find the angle between two vectors

By the definition of dot product, cosangle = (b)/((a) * (b))

In [25]: a = ([1,2])                                                    
In [26]: b = ([2, 1])                                                   
In [27]: dot = 0                                                                
In [28]: for e, f in zip(a, b): 
    ...:     dot += e*f 
    ...:                                                                        
In [29]: dot                                                                    
Out[29]: 4
In [30]: a*b                                                                    
Out[30]: array([2, 2])
In [31]: (a*b)                                                            
Out[31]: 4
In [32]: (a*b).sum()                                                            
Out[32]: 4
In [33]: (a,b)                                                            
Out[33]: 4
In [34]: (b)                                                               
Out[34]: 4
In [35]: (a)                                                               
Out[35]: 4
In [36]: amag = ((a*a).sum())                                            
In [37]: amag                                                                   
Out[37]: 2.23606797749979
In [38]: amag = (a)                                               
In [39]: amag                                                                   
Out[39]: 2.23606797749979
In [40]: cosangle = (b)/((a) * (b))            
In [41]: cosangle                                                               
Out[41]: 0.7999999999999998
In [42]: angle = (cosangle)                                            
In [43]: angle                                                                  
Out[43]: 0.6435011087932847

summarize

The above is a personal experience, I hope it can give you a reference, and I hope you can support me more.