Home > Net >  Generating two vectors with a given angle between them
Generating two vectors with a given angle between them

Time:05-26

I am trying to generate two vectors with a given cosine similarity. Input would be the degree of cosine similarity (or angle as it depends on it anyway) and the number of dimensions (D) in the vectors, and output would be two vectors of D dimensions with that given similarity between them Now, I know how to use the cosine similarity function to calculate the similarity but I'm lost when trying it the other way around. Is there such a procedure or algorithm and how is it called?

CodePudding user response:

For a given starting vector u and cosine similarity c:

  • Generate 2 points in n-D space; call them a and b
  • Project b onto the plane orthogonal to u and containing a
  • Subtract a from the result to obtain a vector orthogonal to u; call it h
  • Use [u,h] as a basis and basic trigonometry to generate the desired vector v

The above method is dimension-agnostic as it only uses dot products. The resultant vectors {v} are of unit length and uniformly distributed around u.

enter image description here

enter image description here

  • Related