I am trying to use R to triple sum over a function with three indices.
I could easily do this in Mathematica with the following code:
out = Sum[B[G[[k]]] * A[G[[k]], G[[j]], G[[i]]] * prod[G[[j]],G[[i]]],
{k,1,Length[G]},{j,1,Length[G]},{i,1,Length[G]}]
where Gis the matrix, and B[.], A[.]and prod[.]are all predefined functions.
Mathematica Sum[f,{k,k_min, k_max}, {j,j_min, j_max}, {i, i_min, i_max}]will calculate the triple sum, Sum (k = k_min, k_max) [Sum (j = j_min, j_max) [Sum (i = i_min, i_max) [f]]], where fis some function.
Now I am trying to do this in R, and I have a lot of difficulties. I tried applying sapplywith the sumfollowing, but it does not work either.
guts.i.j.k <- function(i,j,k) B.k(k) * A.i.j.k(i, j, k) * prod.i.j(i, j)
innermostSum <- function(j,k) sum(sapply(1:length(G), FUN=guts.i.j.k, j=j, k=k ))
middleSum <- function(k) sum(sapply(1:length(G), FUN=innermostSum, k=k ))
outsideSum <- sum(sapply(1:length(G), FUN=middleSum))
return(outsideSum)
Any help would be greatly appreciated.