I am trying to create a recursive function that will do matrix multiplication n times.
My code is the following:
R <- function(P, n){
R(P, n-1) %*% P
}
I would expect when this function is called with n = 3 to perform
(P %*% P) %*% P.
Using an example:
> P
[,1] [,2] [,3]
[1,] 0.6 0.1 0.3
[2,] 0.2 0.7 0.1
[3,] 0.3 0.3 0.4
However when I call the function I get an error message.
Error: evaluation nested too deeply: infinite recursion / options(expressions=)?
Error during wrapup: evaluation nested too deeply: infinite recursion / options(expressions=)?
Could you explain to me why the function does not work and how the code should be amended?