I'm stuck with the following problem:
Suppose that I have three pairs of sequences:
{a_1, ..., a_N}, {A_1, ..., A_T}, {b_1, ..., b_N}, {B_1, ..., B_T}, {c_1, ..., c_N}, {C_1, ..., C_T}
.
My aim is to perform the following action (without looping!):
for (i in 1:N) {
for (j in 1:N) {
for (k in 1:N) {
ret[i,j,k] <- \sum_{t=1}^T (a_i - A_t) * (b_j - B_t) * (c_k - C_t)
}}}
The reason why I don't want to loop is that there might be more pairs of sequences than simply those three. And I want to structure the code as "efficiently and flexible" as possible.
If we only have two pairs of sequences, then the problem is quite easy since it reduces to simple matrix multiplication (an (N x T)
matrix for (a_i - A_t)
and an (N x T)
matrix for (b_i - B_t)
where you multiply the first with the transpose of the second).
But once you have more than two pairs of sequences, I'm not sure whether it can be done without loops since the dimensionality of output
depends on the number of pairs of sequences...
------------------------------------------ Related problem (Nov. 8th, 2013) ------------------------------------------
I got the first part successfully implemented thanks to @-mrip. But how would the code have to be altered if I want the following:
for (i in 1:N) {
for (j in 1:N) {
for (k in 1:N) {
ret[i,j,k] <- \sum_{t=1}^T foo(a_i, a_i - A_t) * foo(b_j, b_j - B_t) * foo(c_k, c_k - C_t)
}}}
Where foo(a, a-A)
is some common bivariate function. Is there a "general" solution, or do you need more information about the structure of foo(a, a-A)
?
I tried it using the straightforward solution and simply implement the loops. Of course this is neither flexible (since I have to restrict myself in advance to the possible number of pairs/dimensions) nor fast (since both a
and A
can be large - although it might be the case that a
is simply a scalar and A
some set of observations).
I know that I might be demanding. But I' m totally stuck on this problem since quite some time... Hence any help is extremely welcome.