# 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, q, ..., 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. Sep 25, 2016 at 22:19
• Sorry Xavier, had the wrong sum written for the second term. Corrected. Sep 25, 2016 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... Sep 25, 2016 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. Sep 25, 2016 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 s + r s + r s + r s + r s + r s + r s + r s. Sep 25, 2016 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 r + q r + q r + q r + q s + r s + q s +
r s + q s + r s + q s + r s + r s + r s +
r s + r s


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.