rand_vector_at_angle {clugenr} | R Documentation |
Get a random unit vector at a given angle with another vector.
Description
Get a random unit vector which is at angle
radians of vector u
.
Note that u
is expected to be a unit vector itself.
Usage
rand_vector_at_angle(u, angle)
Arguments
u |
Unit vector with \(n\) components. |
angle |
Angle in radians. |
Value
Random unit vector with \(n\) components which is at angle
radians with vector u
.
Note
This function is stochastic. For reproducibility set a PRNG seed with set.seed.
Examples
u <- c(1.0, 0, 0.5, -0.5) # Define a 4D vector
u <- u / norm(u, "2") # Normalize the vector
v <- rand_vector_at_angle(u, pi / 4) # Get a vector at 45 degrees
arad <- acos((u %*% v) / norm(u,"2") * norm(v, "2")) # Get angle in radians
arad * 180 / pi # Convert to degrees, should be close to 45 degrees
[Package clugenr version 1.0.3 Index]