title: construct distance matrix in R, but from multiple input matrices
body: <p>There are some R functions to construct distance matrices by inputing a matrix/data frame (<code>x</code>) and specifying a distance measure (e.g. <code>Euclidean</code>), such as the <code>dist</code> function in <code>stats</code> R package (default). The <code>proxy</code> R package has a <code>dist</code> function (yes, the same name) that extends the <code>stats:dist</code>: it has the argument <code>method</code> from which users can pass a function, a registry entry, or a mnemonic string referencing the proximity measure. This is very convenient if users have their own distance measure programmed as a function. For example (from help document in <code>proxy</code>):</p>
  
  <pre><code>## input matrix
  x &lt;- matrix(rnorm(16), ncol = 4)
  ## custom distance function
  f &lt;- function(x, y) sum(x * y)
  dist(x, f)
  </code></pre>
  
  <p>The resultant distance matrix indicates that (for instance) the distance between row 1 and row 2 of <code>x</code> is 2.32, which can be manually calculated as <code>sum(x[1,]*x[2,])</code>. Note that the function <code>f</code> takes two arguments <code>x</code> and <code>y</code>, which are essentially two rows of the input matrix <code>x</code> in the <code>proxy:dist</code> function. In other words, the distance calculation <strong>relies entirely on the input matrix <code>x</code> alone</strong>.</p>
  
  <p><strong>Here is my question</strong>: I also want to calculate a distance matrix for the input matrix <code>x</code> (i.e. rows are observations and I want to get the pairwise distance between rows of <code>x</code>). However, the function I use to calculate the distance <strong>does NOT rely solely on the input matrix <code>x</code> but actually on some matrices derived from <code>x</code></strong>. I store the necessary matrices in a list called <code>prep_matrices</code>, which consists of three matrices: <code>A,B,C</code> (I made up these for reproducible results):</p>
  
  <pre><code>set.seed(111)
  A = matrix(rnorm(9), nr=3)
  set.seed(222)
  B = matrix(rnorm(9), nr=3)
  set.seed(333)
  C = matrix(rnorm(9), nr=3)
  </code></pre>
  
  <p>Obviously the input matrix <code>x</code> is 3-by-3 and <code>prep_matrices$A, prep_matrices$B, prep_matrices$C</code> will give the derived matrices from <code>x</code>. Now assume that the distance between two rows of <code>x</code> is calculated as (for instance, row 1 and row 2):</p>
  
  <pre><code>m1 = diag(A[1, ])
  m2 = diag(A[2, ])
  b1 = B[1, ]
  b2 = B[2, ]
  c1 = C[1, ]
  c2 = C[2, ]
  distance = mean(m1 %*% ( (diag(b1)-diag(b2)) %*% (diag(c1)-diag(c2)) %*% m2))
  </code></pre>
  
  <p>This example is for illustrations only, but I hope you'll get the idea of how the distance is calculated. I realize, then, that it might be impossible to pass a list (<code>prep_matrices</code>) to some R functions and get the distance directly, as there are more extra calculations involved and most importantly, the distance is not based on the input matrix but instead on many derived matrices...</p>
  
  <p>Is there a way to efficiently code in R to get a distance matrix in this case? Or we could possibly modify existing R functions? Thanks a lot!</p>