Projection of 2 vectors in R

Viewed 315

I want to calculate the projection u⟶v

Definition: proj(u⟶v)=u⋅v|v|2v

# my 2 vectors 
u<-c(1.5,sqrt(3)/2)
v<-c(2,0)

The answer should look as the following :

[1] 1.5 0.0
2 Answers

Not entirely sure what you are looking for.

u <- c(1.5,sqrt(3)/2)
v <- c(2,0)

as.vector( (u %*% v) / (v %*% v) ) * v

yields

[1] 1.5 0.0

You can wrap it into a function

proj <- function(u, v) {
 return(as.vector( (u %*% v) / (v %*% v) ) * v)
}

An alternative approach:

library(mosaic)
u <- c(1.5,sqrt(3)/2)
v <- c(2,0)
project(u, v)
# 1.5 0.0

vlength(u)
# 1.732051

vlength(v)
# 2
Related