# How to sum products of vector elements in a set of vectors?

Let $a$ be a set of vectors such that $a = \{q^2, q^3, q^4\, ...\}$, where $q^k$ is a vector of length $2^k$, and of the form $q^k = \{q[1], q[2], ..., q[k]\}.$ I want to evaluate a sum like $\sum\limits_{k=2}^{N-1} \sum\limits_{h>k}^N \sum\limits_{i=1}^{n_k} q_{i}^k q_{i}^h$, where $n_k$ is the length of the vector $q^k$, and $N$ is the cardinality of $a$. This involves taking the product of components of vectors in seperate elements of the set. I made an attempt and got a nasty set of errors.

a = Array[q, 4]
b = Array[r, 8]
c = Array[s, 16]
z = {a, b, c}
OverlapV[v_] :=
Subscript[\[Lambda], overlap] Sum[With[{l = Length[v[[k]]]}, Sum[Sum[
v[[k]][[i]] v[[h]][[i]], {i, 1, l}, {h, i + 1, Length[v]}], {h,
i + 1, Length[v]}]], {k, 1, Length[[v[k]]] - 1}]


I managed to accomplish something similar for the term $\sum\limits_{k=2}^{N} \sum\limits_{i = 1}^{n_k - 1} \sum\limits_{j > i}^{n_k} q_{i}^k q_{j}^k$ after some help in a similar posted question with this code:

One[x_] := With[{l = Length[x]},
Sum[Sum[x[[i]] x[[j]], {i, 1, l}, {j, i + 1, l}], {i, 1, l - 1}]]
OneV[v_] :=
Subscript[\[Lambda], one] Sum[F[v[[k]]], {k, 1, Length[v]}]


which gives the output: This is what I am trying to output for the first term. I am new to Mathematica, so I apologize if the code is messy or not idiomatic.

• I don't understand your triple sum. Why don't you write down what you expect to get when a, b, c are smaller lists, e.g. of length 2, 3, 4. – corey979 Sep 25 '16 at 22:19
• Sorry Xavier, had the wrong sum written for the second term. Corrected. – Neil Philip Sep 25 '16 at 22:22
• One is wrong, unless you really want to sum twice over i; but the outer sum, where you have {i, 1, l - 1}, sums over an already summed expression with no iterator left, so it basically mulitplies it by l-1... – corey979 Sep 25 '16 at 22:31
• Also, I'm concerned about what you mean by $\sum_{j>i}^n a_j a_i$: that's a summation over only one index $j$ which must be greater than $i$, so e.g. when $i=4$, $j=5,6,\ldots,n-1,n$. You can then sum this over $i$. So I'm not sure whether the ansers to your previous question really answer your needs. – corey979 Sep 25 '16 at 22:48
• Considering your z: you want to sum over k from 2 to $N−1=3-1=2$, so basically you're just setting k=2. Next, over $h>k=2$ up to $N=3$, so in fact $h=3$. So effectively you're summing over $i$ from 1 to $n_k=n_2=$Length@b$=8$. The output is r[1] s[1] + r[2] s[2] + r[3] s[3] + r[4] s[4] + r[5] s[5] + r[6] s[6] + r[7] s[7] + r[8] s[8]. – corey979 Sep 25 '16 at 22:53

You may use Indexed and Sum.

With

a = Indexed[q, #] & /@ Range[1, #] & /@ Range[1, 4]


Then

Sum[a[[k, i]]*a[[h, i]],
{k, 2, Length[a] - 1},
{h, k + 1, Length[a]},
{i, 1, Length[a[[k]]]}]


or when entered with Esc+sumt+Esc

IMHO, the above bit of code demonstrates one of the best features of Mathematica: Your code can be written in mathematical notation.

Hope this helps.

I'm not sure if I understood what you wanted correctly, but it seems you wanted something like this:

a = Array[q, 4];
b = Array[r, 8];
c = Array[s, 16];
z = {a, b, c};

Total[Dot @@@ (PadRight[#, {2, Min[Length /@ #]}] & /@ Subsets[z, {2}])]
q[1] r[1] + q[2] r[2] + q[3] r[3] + q[4] r[4] + q[1] s[1] + r[1] s[1] + q[2] s[2] +
r[2] s[2] + q[3] s[3] + r[3] s[3] + q[4] s[4] + r[4] s[4] + r[5] s[5] + r[6] s[6] +
r[7] s[7] + r[8] s[8]


As I understood it, you want all possible pairs of your vectors in $a$ (thus, Subsets[]), take the dot product of the members of each pair (with truncation as needed, hence PadRight[]), and then sum that all up.