RosettaCodeData/Task/Dot-product/R/dot-product.r
2023-07-01 13:44:08 -04:00

16 lines
276 B
R

x <- c(1, 3, -5)
y <- c(4, -2, -1)
sum(x*y) # compute products, then do the sum
x %*% y # inner product
# loop implementation
dotp <- function(x, y) {
n <- length(x)
if(length(y) != n) stop("invalid argument")
s <- 0
for(i in 1:n) s <- s + x[i]*y[i]
s
}
dotp(x, y)