Matrix computing of $a(i,j)a(j,i)$ I have a square, semi-positive matrix $A$ and I want to compute the sum of the products $a(i,j)a(j,i)$ for every $i$ and $j$. Is there any easy way to perform this computation that does not involve "for"?
Thanks
 A: You could use the trace formula:
$\mathrm{Tr}(AA)=\sum_i[AA]_{ii}=\sum_i\sum_j[A]_{ij}[A]_{ji}$
A: Let $A = (a_{ij})$, then $A^T = (a_{ji})$.
Take the elementwise product $\circ$ of $A$ and $A^T$:
$$A \circ A^T = (a_{ij}) \circ (a_{ji}) = ( a_{ij} a_{ji})$$
Thus, the element at position $k,l$ in $A \circ A^T$ will be $a_{kl}a_{lk}$.
In Mathematica, elementwise product is the standard product. In Matlab, it is .* and in Maple it is *~.
Then just sum your elements. The summing can be done if you vectorize your matrices:
$$vec(A)^Tvec(A^T) =
\begin{pmatrix} a_{11} & a_{21} &  a_{31} & \dots \end{pmatrix}
\begin{pmatrix} a_{11} \\ a_{12} \\ a_{13} \\ \vdots \end{pmatrix}
= a_{11}a_{11} + a_{21}a_{12} + a_{31}a_{13} + \dots
$$
Since you say your matrix is positive semi-definite (if that is what you mean by semi-positive?), I assume it is symmetric (at least if it is a matrix over the reals), so $A = A^T$.
But then we can go further, since if $A$ is symmetric, then $a_{ij} = a_{ji}$, so you just need elementwise exponentiation with 2 of the matrix, then sum the elements.
