title: R - Vectorized implementation of ternary function
body: <p>I have three vectors <code>X</code>, <code>Y</code> and <code>Z</code> of equal length <code>n</code>. I need to create an <code>n x n x n</code> array of a function <code>f(X[i],Y[j],Z[k])</code>. The straightforward way to do this is to sequentially loop through each element of each of the 3 vectors. However, the time required to compute the array grows exponentially with <code>n</code>. Is there a way to implement this using vectorized operations?</p>
  
  <p>EDIT: As mentioned in the comments, I have added a simple example of what's needed.</p>
  
  <pre><code>set.seed(1)
  X = rnorm(10)
  Y = seq(11,20)
  Z = seq(21,30)
  
  F = array(0, dim=c( length(X),length(Y),length(Z) ) )
  for (i in 1:length(X))
    for (j in 1:length(Y))
      for (k in 1:length(Z))
        F[i,j,k] = X[i] * (Y[j] + Z[k])
  </code></pre>
  
  <p>Thanks.</p>