RosettaCodeData/Task/Dot-product/R/dot-product.r
Ingy döt Net 764da6cbbb CDE
2013-04-10 16:57:12 -07: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)